[antlr-interest] Lexer rules and unreachable alternatives (trying to understand lexer)

Johannes Luber jaluber at gmx.de
Thu Apr 19 03:15:27 PDT 2007


Wincent Colaiuta wrote:
> Given a lexer with a single rule:
> 
>     OTHER : .+ ;
> 
> Why would ANTLR issue this warning?
> 
>     warning(201): T.g:8:9: The following alternatives are unreachable: 1
> 
> The warning goes away if I instead write:
> 
>     OTHER : .* ;
> 
> Looking at the syntax diagram in ANTLRWorks I see something like this:
> 
>      /-------------------------\
>     v                           |
> OTHER -------> '\u0000'..'\uFFFE' ---===>
> 
> The final part of the rightmost arrow is highlighted in red.
> 
> If I write the rule like this, the warning goes away:
> 
>     OTHER : '\u0000'..'\uFFFE'+ ;
> 
> What am I misunderstanding here?
> 
> Cheers,
> Wincent

The problem is, that your first OTHER rule is ambiguous - it can match
everything, even the keywords, etc. defined in the other rules. With .*
the OTHER rule becomes optional. I suggest to either change your
grammar, so that it doesn't need the OTHER rule, or to use syntactic
predicates, which prevent that OTHER matches anything what another rule
could match.

Best regards,
Johannes Luber



More information about the antlr-interest mailing list