[antlr-interest] How to write parenthesized expression gramma r??

Ney, Richard richard.ney at aspect.com
Thu Jan 30 12:21:29 PST 2003


I recommend looking at the Java grammar since it contains a real nice set of
rules for expression evaluation and there is the tree grammar to go with it.

-Richard


-----Original Message-----
From: rmsanjivp <rmsanjivp at yahoo.com> [mailto:rmsanjivp at yahoo.com] 
Sent: Thursday, January 30, 2003 11:53 AM
To: antlr-interest at yahoogroups.com
Subject: [antlr-interest] How to write parenthesized expression grammar??


Hi for the following grammar:

topLevel : expr;

expr : mult_expr ( (PLUS | MINUS) mult_expr)*;

mult_expr : pow_expr ((MULT | DIV) pow_expr)*;

pow_expr: primary_expr ( POWER pow_expr)?;

primary_expr: ID | (LPAREN! expr RPAREN!) | NUM
;



If I give input as (1+2)) then it is taken as a legal input and ast 
gives nodes for 1, 2, and +. This is wrong as there is unmatched 
right paranthesis. The problem is happening because of RPAREN being a 
valid look ahead in method pow_expr() in the generated file after 
method primary_expr().

What grammar should I write for paranthesized expression That can 
support (1+2)*(3+4 - (4*5))+4


Please help.

Thanks,
Sanjiv


 

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


 

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



More information about the antlr-interest mailing list