[antlr-interest] Formula parser and optional brackets

Martin Probst mail at martin-probst.com
Mon Apr 17 01:38:06 PDT 2006


Hi,

I take your grammar starts with the "expr" rule. First off: "1 + 2 )"  
works because ANTLR does not implicitly check for the end of the  
file. I.e. a syntactically correct expression that is followed by  
garbage is fine with ANTLR by default, you've got to explicitly check  
for the magic EOF token in your start rule, e.g.

expr	: sumBaseExpr EOF;

Why did you add all that stuff to expr anyways? I think it shouldn't  
be there - the PLUS MINUS etc are parsed within the lower rules, no  
need to have them within "expr". Also, if your grammar starts with  
"expr", you require every expression to be encapsulated in  
parenthesis, is that really what you want?

> expr     :  (sumBaseExpr) => (LPAREN^ sumBaseExpr RPAREN! (PLUS^ |
> MINUS^ |MULT^ |DIV^) sumBaseExpr)
> 				|(complexExpr) => (LPAREN^ sumBaseExpr
> RPAREN!) | sumBaseExpr ;

I'd guess you rather need something like this:

expr: sumBaseExpr EOF;

This should work correctly for arithmetic expressions and for  
parenthesis.

Martin


More information about the antlr-interest mailing list