[antlr-interest] A Simple Question on Channels

Indhu Bharathi indhu.b at s7software.com
Sat Apr 4 21:16:53 PDT 2009


'expression' is rule 'r' wont return a Token since it is not a lexer rule. It will instead return 'ParserRuleReturnScope' since it is a parser rule. So '$expression.stop' or '$t.stop' will give you the last token of 'expr'. You can use it like shown below: 

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

or 

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

Cheers, Indhu 

----- Original Message ----- 
From: Andreas Stefik <stefika at gmail.com> 
To: Michael Bedward <michael.bedward at gmail.com>, antlr-interest at antlr.org 
Sent: Saturday, April 4, 2009 10:33:02 PM GMT+0530 Asia/Calcutta 
Subject: Re: [antlr-interest] A Simple Question on Channels 

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/20090405/1d2750db/attachment.html 


More information about the antlr-interest mailing list