[antlr-interest] Parsing (a+b)+(c+d)

Szczepan Hołyszewski rulatir at wp.pl
Sun Aug 17 12:15:35 PDT 2008


Tom Edwards wrote:

> I am trying to parse the following simple expression:
>
> (a+b)+(c+d)
>
> With the following grammar:

[snip]

> expr    :       IDENTIFIER exprD
>
>        |       '(' expr ')';
>
> exprD   :       OP (exprD|IDENTIFIER);

There is no way this grammar can ever parse (a+b)+(c+d) because it specifies 
that OP must always _follow_ IDENTIFIER. This is the case because:

	- exprD must follow IDENTIFIER, by the first rule and the fact that exprD
	  doesn't occur anywhere else

	- exprD must start with OP and OP doesn't occur anywhere else

Your input however contains ')+', which is OP following something other than 
IDENTIFIER.

Try this:

expr  :  term (OP^ term)*  ;

term  :  IDENTIFIER  |  '('! expr ')'!  ;

Szczepan Holyszewski



More information about the antlr-interest mailing list