[antlr-interest] unary minus
Johannes Luber
JALuber at gmx.de
Fri Mar 6 07:39:11 PST 2009
> Hi,
>
> I've just turned to ANTLR and everything looks very nice. Except, I
> have some problems with my implementation of unary minus. Can anyone
> help me with the following errors?
I think the problem is with the rules
arithmeticExpr
: sumExpr ( addSubOp^ sumExpr )*
;
factorExpr
: (MINUS^)? atom
;
With the input "5 - 3" ANTLR doesn't know if it should match factorExpr with a MINUS or use addSubOp instead. Looking at my C# grammar, it uses the following (simplified) rule
unary_expression
: primary_expression
| PLUS unary_expression
| MINUS unary_expression
;
Looking at primary_expression I'd say it is the equivalent of the atom rule. So try the following change:
factorExpr
: atom
: MINUS^ factorExpr
;
Johannes
>
> [16:11:03] error(211): McAntlrExpression.g:61:32: [fatal] rule
> arithmeticExpr 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.
> [16:11:03] warning(200): McAntlrExpression.g:61:32: Decision can match
> input such as "MINUS INTEGER" using multiple alternatives: 1, 2
> As a result, alternative(s) 2 were disabled for that input
>
> Here is my grammar:
>
> grammar McAntlrExpression;
> options {
> output=AST;
> ASTLabelType=CommonTree;
> // backtrack=true;
> // memoize=true;
> }
> prog: ( orExpr { System.out.println($orExpr.tree.toStringTree()); } )+
> ;
>
> orExpr
> : andExpr ( OR^ andExpr )*
> ;
>
> andExpr
> : equalityExpr ( AND^ equalityExpr )*
> ;
>
> equalityExpr
> : notExpr ( equalityOp notExpr )*
> ;
>
> notExpr
> : NOT^ boolExpr
> | boolExpr
> ;
>
> boolExpr
> : BOOLEAN
> | relationalExpr
> ;
>
> relationalExpr
> : arithmeticExpr ( relationalOp^ | equalityOp^ arithmeticExpr )
> ;
>
> arithmeticExpr
> : sumExpr ( addSubOp^ sumExpr )*
> ;
>
> sumExpr
> : factorExpr ( mulDivOp^ factorExpr )*
> ;
>
> factorExpr
> : (MINUS^)? atom
> ;
>
> relationalOp
> : LT
> | GT
> | LE
> | GE
> ;
>
> equalityOp
> : EQ
> | NEQ
> ;
>
> addSubOp
> : PLUS
> | MINUS
> ;
>
> mulDivOp
> : MULT
> | DIV ;
>
> atom
> : INTEGER
> | LP! arithmeticExpr RP!
> ;
>
> LT : '<';
> GT : '>';
> LE : '<=';
> GE : '>=';
> EQ : '=';
> NEQ : '!=';
> PLUS: '+';
> MINUS
> : '-';
> MULT: '*';
> DIV : '/';
> LP : '(';
> RP : ')';
> NOT : 'not';
> AND : 'and';
> OR : 'or';
> INTEGER
> : '0'..'9'+ ;
> BOOLEAN : 'true' | 'false';
> CONSTANT : ('a'..'z'|'A'..'Z')+ ;
> NEWLINE:'\r'? '\n' ;
> WS : (' '|'\t')+ {skip();} ;
>
> -- AH
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe:
> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
--
Computer Bild Tarifsieger! GMX FreeDSL - Telefonanschluss + DSL
für nur 17,95 Euro/mtl.!* http://dsl.gmx.de/?ac=OM.AD.PD003K11308T4569a
More information about the antlr-interest
mailing list