[antlr-interest] Why doesn't this work?

Tim Gleason tgleason at gmail.com
Sat Jun 23 19:56:14 PDT 2007


I'm sorry for bothering you guys.  I've been translating a grammar
from a book into ANTLR and I can't seem to get passed a problem with
expressions and parenthesis.  At the bottom of included a subset of
the grammar that demonstrates the problem.

For some reason the parser gets confused when it sees '('.  It doesn't
seem to know if it is grouping an expression or the parameters of a
function call.  But I don't /think/ the grammar is ambiguous.

The simplest case that fails is the input to the 'statement' rule:

foo();

It will fail to parse at rule 'expression'.  But if I remove the line
marked with an asterisk, it parses fine.  I don't understand how one
affects the other.

Any help would be greatly appreciated!

Thanks,
tim


statement
	:	expression ';';

expression
	:	functionCall
	|	(functionCall ('*'|'/')) => functionCall ('*'|'/') expression
	;


functionCall
	:       basicExpression ('(' expressionList? ')')?
	;	
	
expressionList
	:	expression (',' expression)*
	;	
	
basicExpression
	:	ID
	|	constant
	|	'(' expression ')'        //  ****  Remove this line
	;	

constant:	'true' | 'false' | INT;

ID 	:	('A'..'Z'|'a'..'z')+;
INT 	:	('0'..'9')+;
WS	:	('\r'|'\n'|'\t'|' ') {skip();};


More information about the antlr-interest mailing list