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

Hrvoje Nezic hrvoje.nezic at envox-lab.hr
Fri Jan 31 02:39:35 PST 2003


----- Original Message -----
From: <rmsanjivp at yahoo.com>
To: <antlr-interest at yahoogroups.com>
Sent: Friday, January 31, 2003 7:52 AM
Subject: [antlr-interest] Re: How to write parenthesized expression gramma
r??


> 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))??

Generally, LL(k) parsers can certainly handle paranthesized
expressions.

> Is it a bug in antlr?
>

In my view this is certainly a bug.

Your program works as expected if you write topLevel like this:

topLevel :  expr EOF ;

I think that ANTLR-generated parsers should implicitly
include an EOF at the end of the starting rule.

The problem with input
    1+2)
is that 1+2 is a valid expression and that RPAREN is among the tokens
that can follow pow_expr. So, the parser matches 1+2 and doesn't
check if the current token is EOF.

The same effect can be seen in case of the input:
    1 + 2 )  1 + 3 + 5 + 6 * 7 / 6 * (3 -1 )
Here the parser also matches 1+2. RPAREN is also among
the tokens that can follow pow_expr, so the parser
happily ends and ignores the rest of input.

Regards,
Hrvoje Nezic

> Thanks,
> Sanjiv



 

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



More information about the antlr-interest mailing list