[antlr-interest] Fwd: Re: Trying to keep whitespace in an AST

Johannes Luber jaluber at gmx.de
Fri Feb 8 10:04:26 PST 2008


Artem Portnoy schrieb:
> Johannes,
> 
> I have no idea how to do the same thing in Antlr v3. I couldn't find 
> much information or examples. This is the only thing I found and I 
> couldn't make much sense of it 
> http://www.antlr.org/wiki/pages/viewpage.action?pageId=1057. It just 
> sounds too vague for me with what little understanding of antlr I've 
> got. In my case, I do not care about whitespace. All I need is the 
> ability to access the comments.

Looking at this, one would set in the comments rule "{channel = 
COMMENT_CHANNEL;}", with COMMENT_CHANNEL defined like 50. Then you need 
to access from your CommonTree node in the tree grammar getStartIndex() 
and getStopIndex(). Then you scan in the TokenStream via a for-loop from 
start index until stop index via get(i), if the returned token has the 
channel value COMMENT_CHANNEL. Output those.

That should principally work; start and stop index may vary on your 
needs though.

Johannes

P.S.: 
<http://www.antlr.org/wiki/display/ANTLR3/encoding+off-channel+tokens+in+the+tree> 
looks at first as the right match, but is missing all the required 
information.


More information about the antlr-interest mailing list