[antlr-interest] The following alternatives can never be matched

Kevin J. Cummings cummings at kjchome.homeip.net
Mon Dec 6 09:31:21 PST 2010


On 12/06/2010 12:20 PM, Markus Lottmann wrote:
> Hi,
> 
> i am quite new to antlr and dont understand whats wrong with the 
> following grammar:
> 
> grammar test2;
> options{
> backtrack=false;
> }
> octal_literal
>      :    '0' octal_digit*;
> 
> octal_digit
>      :    '0'..'7';
> 
> The error is:
> 
> [18:17:06] warning(200): test2.g:6:8: Decision can match input such as 
> "EOF" using multiple alternatives: 1, 2
> As a result, alternative(s) 2 were disabled for that input
> [18:17:06] error(201): test2.g:6:8: The following alternatives can never 
> be matched: 2

To begin with, octal_literal and octal_digit are parser rules and not
token rules?  This means that (assuming you discard whitespace) that the
strings "0 1 2 3" and "0123" are both valid octal_literals.

Did you mean to write:

octal_literal : '0' octal_digit+ ;

Is the character '0' an octal_digit or the beginning of an
octal_literal?  To me, this is ambiguous as to which you want.

OCTAL_LITERAL : '0' OCTAL_DIGIT+
              ;

fragment
OCTAL_DIGIT   : '0' .. '7'
              ;

Might be closer to what you intend here.

> Thanks so far.
> Greetings,
> ML

-- 
Kevin J. Cummings
kjchome at rcn.com
cummings at kjchome.homeip.net
cummings at kjc386.framingham.ma.us
Registered Linux User #1232 (http://counter.li.org)


More information about the antlr-interest mailing list