[antlr-interest] "Comments" token from source to the target language

Mateus Baur da Silva mateus.baur at gmail.com
Tue Nov 13 02:44:46 PST 2007


Hi Ter,

I apologize if the questions are stupid and if I'm taking to long to figure
out this stuff. However, I'm a beginner on ANTLR and languages/parsers
stuff.

The problem you mentioned in the other message ("...You have must have a
COMMENT? subrule after every single token in case there is a comment on the
input stream...") is exactly what I'm trying to avoid.

I still don't understand how I can do that. What else I need to do besides
including the $channel=HIDDEN in the COMMENTS token? What do I need to to on
the parser side? Could you show me a real sample, a rule having this
implemented?

Cheers,
Mateus

On Nov 12, 2007 11:19 PM, Terence Parr <parrt at cs.usfca.edu> wrote:

> Hi. Sure.  Each token as an index into token buffer via
> t.getTokenIndex().  Then ask for the token at index -1, -2, etc...
> You can ask for it's channel number too.  Just scan :)
>
> Ter
> On Nov 12, 2007, at 4:13 PM, Mateus Baur da Silva wrote:
>
> > Hi Ter,
> >
> > I understand that parser will ignore the tokens if I set the token
> > to be sent to the parser thru the hidden channel ($channel=HIDDEN;).
> >
> > By reading your message (and your book), I know I can check the
> > hidden channel for the comments token inside my actions. However, I
> > don't know how to do that. Is there some sample implementing this
> > behavior?
> >
> > If not, could you (or someone else) let me know how I should
> > implement that inside my actions?
> >
> > Thanks and Regards,
> > Mateus
> >
> >
> > On Nov 12, 2007 8:03 PM, Terence Parr < parrt at cs.usfca.edu> wrote:
> >
> > On Nov 12, 2007, at 11:38 AM, Mateus Baur da Silva wrote:
> >
> > > Hi Guys,
> > >
> > > As I mentioned in some my other email, I doing a translator from a
> > > Pascal subset to java. Currently, I'm ignoring the "comments" by
> > > using skip() on the lexer rule that defines the "comments".
> > >
> > > However, I would like to translate the comments from Pascal to Java
> > > code as well. I was wondering if I could do that by using the
> > > HIDDEN_CHANNEL or some other feature to properly translate the
> > > comments. Does someone have any clue on how to do that?
> > >
> >
> > Yep, use the hidden token thing.  Your actions then ask for the
> > hidden tokens between real tokens.  Parser ignores them.
> >
> > Ter
> >
> >
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20071113/97850bdb/attachment.html 


More information about the antlr-interest mailing list