[antlr-interest] unary minus

Anders Hessellund anders.hessellund at gmail.com
Fri Mar 6 07:15:32 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?

[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


More information about the antlr-interest mailing list