[antlr-interest] Retaining comments

Blake Meike bmeike at speakeasy.net
Wed Feb 27 09:06:33 PST 2008


I am writing a fairly simple translator using v3, and having some  
trouble handling comments.

In the first versions of the translator, I completely ignored  
comments, treating them as whitespace.  This works very, very nicely,  
using either skip() or the HIDDEN channel.

I'm adding a new feature that will copy comment from the source to the  
source to the destination language.  The ANTLR book suggests that this  
is not only possible, but, using channels, an ANTRL feature.  I wasn't  
able to discern, though, exactly how to do it.

I changed my grammar so that I could attach an action to the lexer's  
recognition of a comment token.  This has almost the desired effect:  
the action is called for every comment in the source.  Unfortunately,  
every call to the lexer action is called before any call to any parser  
action, as if the entire source file were converted to tokens, before  
any token is fed to the parser.

Is there a way to keep the, very convenient lexical handling of  
comments, and still, to have the action associated with them called in  
context?

Thanks
  Blake Meike


More information about the antlr-interest mailing list