[antlr-interest] What does " The following alternatives can never be matched" mean

Gavin Lambert antlr at mirality.co.nz
Tue Dec 1 20:15:14 PST 2009


At 16:35 2/12/2009, qinglangee wrote:
 >
 >prog:   (command)+ ;
 >command	: (function |var)+;
 >function : (Letter)+',';
 >var : (Letter)+;
 >Letter  :	'A'..'Z';
 >WS	:	( '\t'|' '|'\r'|'\n'|'\u000C' )+ 	{ $channel = HIDDEN; } ;
[...]
 >[11:14:04] warning(200): cub.g:4:25: Decision can match input 
such
 >as "Letter" using multiple alternatives: 1, 2, 3
 >As a result, alternative(s) 3,2 were disabled for that input
 >[11:14:04] error(201): cub.g:4:25: The following alternatives 
can
 >never be matched: 2

The problem is that both the function and var rules have a common 
left prefix (Letter+); the only distinguishing feature is whether 
it's followed by a comma or not.  So when you're saying that 
"command" can consist of either "function" or "var", it's not 
possible to define static lookahead to disambiguate that case.

You can either force explicit lookahead (this doesn't always 
work):
   command : ( (function) => function
             | var
             )+
           ;

Or refactor the rules to eliminate the ambiguity:
   command : ( Letter+ ','? )+ ;

Note however that since you're hiding whitespace in the lexer, the 
input "HELLO" and "HE L      LO" will be treated as 
identical.  This may or may not be what you want.



More information about the antlr-interest mailing list