[antlr-interest] Whitespace is significant sometimes

Daniel Lidström daniel.lidstrom at gpsgate.com
Tue Sep 28 00:03:09 PDT 2010


From: "Daniel Lidström" <daniel.lidstrom at gpsgate.com>
[...]
> Is there a way to capture
> the part outside [] as-is,
> with whitespace exactly like written, and the part inside [] is relaxed
> where whitespace to go hidden
> channel?
>
> Daniel
>
> Here's my grammar, btw:
>
> parse
> : (IDENT | variable)+ EOF
> ;
>
> variable
> : '[' mult ']'
> ;
>
> // the multiplication is optional
> mult
> : IDENT ('*' INTEGER)?
> ;
>
> fragment DIGIT : '0'..'9' ;
> INTEGER : DIGIT+ ;
>
> fragment LETTER : 'a'..'z' | 'A'..'Z';
> IDENT : LETTER+ ;
>
> WS : ' '+ { $channel = HIDDEN; } ;

I will try to answer myself. After a good nights sleep I think I have found 
a way to solve
this problem, using actions. I am going to build an abstract syntax tree 
from this grammar.
Then I figured I can keep a context variable within my parser. Whenever I am 
parsing a
variable (rule above) I set the variableContext to true. Then I capture 
whitespace within
the WS rule but only when the variableContext is set to false. I haven't 
worked out the
details yet but I am positive it should work. Is there anyone who knows of 
an online
grammar example that does something like this?

Daniel 



More information about the antlr-interest mailing list