[antlr-interest] why are these productions different

Gavin Lambert antlr at mirality.co.nz
Wed Jun 24 12:51:15 PDT 2009


At 06:41 25/06/2009, John Tijoe wrote:
>WORKING --> expr    : multExpr ((PLUS | MINUS ) multExpr)*;
>
>NOT WORKING --> expr    : multExpr PLUS multExpr
>     | multExpr MINUS multExpr
>     | multExpr
>     ;

ANTLR is an LL parser; you must always keep the left-side of a 
rule (or rule fragment) unique to avoid ambiguity.  This is why 
the first works but the second doesn't.  (Also, the two aren't 
equivalant.)  To rewrite it properly, you'd need either this:

expr : multExpr
        ( PLUS expr
        | MINUS expr
        )?
      ;

Or this:

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

(The second is usually a better pattern to follow; the first can 
sometimes get you into trouble.)

>NOT WORKING --> multExpr : atom ((MULT | DIV) atom )*;
>
>NOT WORKING --> multExpr : atom MULT atom
>     | atom DIV atom
>     atom
>     ;
[...]
>atom : INT
>     | ID
>     | LPAREN expr RPAREN;

The second of these won't work for the same reasons.  The first 
ought to work fine, though (even with the reference back to expr, 
despite what Johannes said).



More information about the antlr-interest mailing list