[antlr-interest] why are these productions different

Benoit Fouletier benblo+ANTLR at gmail.com
Wed Jun 24 12:16:53 PDT 2009


Ah, good to see someone struggling as I am ;) !
It seems to me that what you wrote is functionaly different than the
original, you're missing the repetitions:

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

... but even that wouldn't work, you can repeat plus operations, minus
operations... or a mix of the 2! so in the end I don't think the rule can be
rewritten at all.

I don't know why you get errors though.

           Ben


On Wed, Jun 24, 2009 at 8:41 PM, John Tijoe <john.tijoe at googlemail.com>wrote:

> Hello,
> I was playing with antlrworks and tried to understand the SimpleCalc demo.
> And tried to write the expr and the multexpr in a more readable way (at
> least to me). But my expr and multexpr seem to be different. Can someone
> tell me why they are different I always get the error:
> SimpleCalc.g:31:10: [fatal] rule multExpr has non-LL(*) decision due to
> recursive rule invocations reachable from alts 1,2.  Resolve by
> left-factoring or using syntactic predicates or using backtrack=true option.
> What Am I missing??
>
> except from it:
>
> WORKING --> expr    : multExpr ((PLUS | MINUS ) multExpr)*;
>
> NOT WORKING --> expr    : multExpr PLUS multExpr
>     | multExpr MINUS multExpr
>     | multExpr
>     ;
>
> NOT WORKING --> multExpr : atom ((MULT | DIV) atom )*;
>
> NOT WORKING --> multExpr : atom MULT atom
>     | atom DIV atom
>     atom
>     ;
>
> Best regard John
>
>
> PS:
> Whole grammar
> grammar SimpleCalc;
>
> tokens {
> PLUS = '+' ;
> MINUS = '-' ;
> MULT = '*' ;
> DIV = '/' ;
> RPAREN = ')' ;
> LPAREN = '(' ;
> ASSIGN = '=' ;
> }
> /*----------------
> * PARSER RULES
> *----------------*/
> prog    : stat+ EOF;
>
> stat    : expr NEWLINE
>     | ID ASSIGN expr NEWLINE
>     | NEWLINE; //Do nothing
>
> expr    : multExpr ((PLUS | MINUS ) multExpr)*;
>
> /*expr    : multExpr PLUS multExpr
>     | multExpr MINUS multExpr
>     | multExpr
>     ;
> */
>
> //multExpr : atom ((MULT | DIV) atom )*;
>
> multExpr : atom MULT atom
>     | atom DIV atom
>     atom
>     ;
>
> atom : INT
>     | ID
>     | LPAREN expr RPAREN;
> /*----------------
> * LEXER RULES
> *----------------*/
> ID : ('a'..'z'|'A'..'Z')+;
> INT : '0'..'9'+;
> NEWLINE : '\r'?'\n';
> WS : (' '|'\t'|'\n'|'\r')+;
>
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe:
> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090624/5e0f4d4f/attachment.html 


More information about the antlr-interest mailing list