[antlr-interest] A Simple Question on Channels

Andreas Stefik stefika at gmail.com
Sat Apr 4 10:03:02 PDT 2009


Indhu,

Thanks, this is very helpful. One more very related question, if you'll
indulge me. The grammar you posted works great, and I think I'm close to
understanding what's going on here. You define @members, which dumps a
custom function into the generated parser. Inside, you get the tokens from
any channel that is not the default, starting from the index of the token in
the parser rule.

However, in my grammar, t cannot be set to a token, because I need that
newline to be processed after an expression, which can have any arbitrary
number of tokens in it, so my code (modifying your grammar), would need to
look something like this:

r              :               t=expression { newLineBeforeNextToken(
$t.getTokenIndex() ) }?=>/*nothing*/
                ;

expression    :    equality
    ;

equality:    add (('==' | '!=') add)*
    ;

add    :    multiply (('+' | '-') multiply)*
    ;

multiply:    atom (('*' | '/') atom)*
    ;

atom     : ID
    | '(' expression ')'
    ;

I know that first rule isn't right, but I need to have ANTLR determine (if
possible), the index of the last token in the expression, and have that
value be passed to your function. How do I modify the above to accommodate
for having an arbitrary rule in place of where you had ID?

Thanks again, this was very helpful, and gets me started,

Andreas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090404/d6071da3/attachment.html 


More information about the antlr-interest mailing list