[antlr-interest] Re: Use of "(" and ")"

jduquevieira jpdv at medialog.pt
Tue Apr 6 10:37:25 PDT 2004


--- In antlr-interest at yahoogroups.com, "Mark" <markl at g...> wrote:
> > Now, I want to add "(" and ")" to change the priority.
> > Can anyone help me or point out a place where I can see an 
exemple?
> 
> A tiny example:
> 
> class ExpressionParser extends Parser;
>     options { buildAST = true; }
> 
> expression: level1expr ;
> level1expr: level2expr ( OP_1^ level2expr )* ;
> level2expr: level3expr ( OP_2^ level3expr )* ;
> level3expr: level4expr ( OP_3^ level4expr )* ;
> level4expr: value ;
> value:
>       NUMBER
>     | LPAREN! expression RPAREN!
>     ;
> 
> class ExpressionLexer extends Lexer;
> 
> OP_1: '+' | '-' ;
> OP_2: '*' | '/' ;
> OP_3: '^' ;
> NUMBER: ( DIGIT )+ ;
> protected DIGIT: '0'..'9' ;
> LPAREN: '(' ;
> RPAREN: ')' ;
> 
> WS: ( ' ' | '\t' | '\f' )+
>     { $setType(Token::SKIP); }
>     ;
> 
> NL: ( '\r' ( '\n' )? | '\n' )
>     { newline(); $setType(Token::SKIP); }
>     ;
> 
> This is a complete example that builds a correct AST.  The only 
tricks with the 
> parenthesized expression are the exclamation points at the ends of 
LPAREN and RPAREN 
> in the value rule.  These keep the parenthesis tokens out of the 
tree.
> 
> Always amazes me how easy Antlr makes this kind of thing.
> 
> - Mark



Thanks a lot Mark.
This does the trick.

Best regards.




 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
     http://groups.yahoo.com/group/antlr-interest/

<*> To unsubscribe from this group, send an email to:
     antlr-interest-unsubscribe at yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
     http://docs.yahoo.com/info/terms/
 



More information about the antlr-interest mailing list