[antlr-interest] Is ANTLR Hallucinating?

Wincent Colaiuta win at wincent.com
Mon Jun 25 23:12:35 PDT 2007


El 25/6/2007, a las 21:13, Randall R Schulz escribió:

> On Monday 25 June 2007 12:04, Wincent Colaiuta wrote:
>> El 25/6/2007, a las 20:33, Randall R Schulz escribió:
>>> Hi,
>>>
>>> I have this production in an ANTLR grammar:
>>>
>>> clifFile
>>>
>>>   : clText * EOF
>>>
>>>   ;
>>>
>>>
>>> It elicits this error from ANTLR:
>>>
>>> warning(200): CLIF.g:1371:4: Decision can match input such as
>>> "{EOF, Open}" using multiple alternatives: 1, 2
>>> As a result, alternative(s) 2 were disabled for that input
>>> warning(201): CLIF.g:1371:4: The following alternatives are
>>> unreachable: 2
>>>
>>>
>>> Why is it talking about alternatives in a rule that has none?
>>
>> Try looking at the syntax diagram in ANTLRWorks so that you can see
>> the two alternative paths.
>
> Of course, there are not two paths. Just the loop back over the closed
> clText production followed by the EOF token.

If you look in ANTLRWorks you'll see that there are indeed two paths,  
the first marked in green, and the other red. In order to be non- 
ambiguous there must be only a single, deterministic path.

Here is a basic demo grammar that shows a similar warning:

   grammar Simple;

   a : b* EOF;
   b : A+;
   A : 'a';

Given input like "aaaaaaaa" there are multiple ways to match it,  
despite the lack of explicit "alternatives" in the grammar; for  
example, rule "a" could match a series of short "b" tokens (each  
consisting of a single "a"), or a single long "b" token ("aaaaaaaa").  
ANTLR automatically resolves the ambiguity by disabling one of the  
alternatives, in this case favoring the greedy match.

Cheers,
Wincent



More information about the antlr-interest mailing list