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

rmsanjivp <rmsanjivp at yahoo.com> rmsanjivp at yahoo.com
Thu Jan 30 22:52:56 PST 2003


All the grammars say that the primary expressin could be like

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

where expr is the top level rule for expression. I am using the same 
for my small grammar. Does that mean LL(k) parser can't handle 
paranthesized expression and doesn't throw exception for input 
(1+2))?? Is it a bug in antlr?

Thanks,
Sanjiv


--- In antlr-interest at yahoogroups.com, "Ney, Richard" 
<richard.ney at a...> wrote:
> 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 y...> [mailto:rmsanjivp at y...] 
> 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