[antlr-interest] unary minus

Anders Hessellund anders.hessellund at gmail.com
Sat Mar 7 13:16:48 PST 2009


Thanks,

I found your guide on removing global backtracking
(http://www.antlr.org/wiki/display/ANTLR3/How+to+remove+global+backtracking+from+your+grammar).
After looking at your grammar and playing around with my own, I found
that the problem was caused by another rule.

The problem arises because a paranthesis can either contain an orExpr
or an arithmeticExpr. I will probably have do some sort of magic with
predicates?

-- AH

grammar McAntlrExpression;
options {
	output=AST;
	ASTLabelType=CommonTree;
    //backtrack=true;
    //memoize=true;
}

expr:	orExpr	{ System.out.println($orExpr.tree.toStringTree()); } EOF
	;

orExpr
	:	andExpr ( OR^ andExpr )*
	; 	

andExpr
	:	equalityExpr ( AND^ equalityExpr )*
	;

equalityExpr
	:	notExpr ( equalityOp^ notExpr )*
	;	
	
notExpr
	:	(NOT*)^ boolExpr
	;

boolExpr
	:	arithmeticExpr ( relationalOp^ | equalityOp^ ) arithmeticExpr
	|	LP! orExpr RP!
	|	BOOLEAN
	;

arithmeticExpr
	:	sumExpr ( addSubOp^ sumExpr )*
	;
	
sumExpr
	:	unaryExpr ( mulDivOp^ unaryExpr )*
	;

unaryExpr
	:	(MINUS*)^ atom
	;

atom
	:	LP! arithmeticExpr RP!
	| 	INTEGER
	;

relationalOp
    :	LT
    |	GT
    |	LE
    |	GE
    ;

equalityOp
	:	EQ
	|	NEQ
	;

addSubOp
	:	PLUS
	|	MINUS
	;

mulDivOp
	:	MULT
	|	DIV	
	;

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();} ;


More information about the antlr-interest mailing list