[antlr-interest] Unused rule trashes my grammar

Martin Becker becker at lpr.e-technik.tu-muenchen.de
Thu Mar 24 08:42:54 PDT 2011


Hi,

I don't understand what's happening here. I am generating an ANTLR
grammar with XText. Xtext introduces rules like shown below with
'someGeneratedRule1' and 'someGeneratedRule2', which I can't influence.
One of the generated rules ("someGeneratedRule2") isn't actually used,
but seems to provoke an ambiguity. How could that be possible?

When checking this (stripped down) grammar ANTLR yields 'error(202):
grammar_min2.g:14:14: the decision cannot distinguish between
alternative(s) 1,2 for input such as "RULE_INT '.' EOF EOF"' 
Obviously this has something to to with the EOF's Xtext introduces.

When I remove the rule "someGeneratedRule2" the problem disappears.

{{{
grammar grammar_min2;

someGeneratedRule1: rulemodule EOF;

rulemodule:
	'module' RULE_ID ruleCOLON
	ruleunaryexpr 
	('end' 'module'	|rulePERIOD)
;

// PROVOKES THE PROBLEM
someGeneratedRule2: ruleunaryexpr EOF;

ruleunaryexpr:
	RULE_INT
	|ruleDOUBLECONST
;

ruleEXPONENT:
	('e' |	'E') ('+'|'-')? RULE_INT
;

ruleDOUBLECONST:
	( RULE_INT '.' (RULE_INT)? (ruleEXPONENT)?)
	|         ('.'	RULE_INT   (ruleEXPONENT)?)
	|              (RULE_INT    ruleEXPONENT)
;

ruleCOLON:':';
rulePERIOD:'.';

// lexer

RULE_ID:('a' .. 'z' | 'A' .. 'Z' | '_' ) ( 'a' .. 'z' | 'A' .. 'Z' | '_'
| '0' .. '9' ) * ;
RULE_INT:('0' .. '9' ) + ; 
RULE_WS:(' ' | '\t' | '\r' | '\n')+;
RULE_ML_COMMENT:'%{' (options {greedy = false; } : . ) * '}%' ;
RULE_SL_COMMENT : '%' ~ ( ( '\n' | '\r' ) ) * ( '\r' ? '\n' ) ? ;
}}}

Additionally ANTLRWorks doesn't visualize the error, just writes it to
the console.

Any ideas?

Thanks,
Martin



More information about the antlr-interest mailing list