Is There a Way to Skip a Terminal Wherever it Appears?

May 31, 2012 at 1:45 PM

I'm trying to parse something like this chunk:

god_names = {
            GOD_THE_SPIRITS GOD_THE_ANCESTORS
        }

 

But the problem is that there could be a comment on any of those lines, and while I can white the grammar like this:

GodName -> 'god_names'! ASSIGN! OPEN! (COMMENT?)! ID+ (COMMENT?)! CLOSE! (COMMENT?)!;

It looks ugly and makes it harder to read. Is there a way to tell the parser to ignore any instance of the COMMENT terminal where ever it appears?

Coordinator
May 31, 2012 at 1:59 PM

Sure, in your previous example, modify the SEPARATOR terminal definition as follow:

SEPARATOR -> (WHITE_SPACE | COMMENT)+;

The 'Separator' option in the grammar tells the lexer which token is used as a token separator (usually white space). Separator tokens are always droped by the lexer and never passed to the parser. Using Separator = "SEPARATOR"; in your case, we modify the SEPARATOR definition to also match comments so that they will also be droped.

May 31, 2012 at 2:33 PM
Edited May 31, 2012 at 2:33 PM

Of course, that's rather obvious, isn't it? I feel like a fool, now. Thanks.