[antlr-interest] why are these productions different

Johannes Luber JALuber at gmx.de
Wed Jun 24 12:10:17 PDT 2009


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

You're missing that ANTLR is a LL-Parser. LL-Parser <http://en.wikipedia.org/wiki/LL_parser> work by calling the rules directly (transformed into the correct source code). Thus your changes would result in an infinite loop (atom includes a call to expr, I think).

Johannes
> 
> 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')+;

-- 
GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT!
Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01


More information about the antlr-interest mailing list