[antlr-interest] How to best resolve ambiguity?
Martin Traverso
mtraverso at gmail.com
Fri Dec 30 18:40:11 PST 2005
Ok, I did some testing, and antlr seems to be doing the natural thing even
without explicitly setting k=1. What threw me off was that when I compiled
the grammar I got the following errors:
test.g:6:7: Decision can match input such as "'+'" using multiple
alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input
test.g:7:7: Decision can match input such as "'*'" using multiple
alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input
But looking at the generated code it doesn't seem like any alt was disabled.
If I change the (..)* to (..)?, antlr does seem to disable the alt by
converting the (..)? to (..). But then, setting k=1 as suggested will have
no effect whatsoever.
On a related note, any idea why antlr complains with "2:16: The following
alternatives are unreachable: 2" for the following grammar? A bug, maybe?
grammar T;
a : 'x' ('+' a)? ;
BTW, this is with the latest version in perforce.
Martin
On 12/30/05, Terence Parr <parrt at cs.usfca.edu> wrote:
>
>
> On Dec 30, 2005, at 5:06 PM, Martin Traverso wrote:
>
> > Howdy,
> >
> > What is the best way to resolve the ambiguities caused by 'D' a in
> > rule c in the following grammar?
> >
> > grammar T;
> > a: b ('+' b)*;
> > b: c ('*' c)*;
> > c: 'D' a
> > | 'x';
> >
> Which rule has the ambiguity? All the (...)* loops, right? That
> unterminated 'a' in 'c' will cause trouble...a true ambiguity I
> think. That said, set k=1 in the (...)* subrules and it will
> greedily and happily do the "natural" thing I think.
>
> Ter
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20051230/ab8b7a18/attachment-0001.html
More information about the antlr-interest
mailing list