[antlr-interest] resolving ambiguity

Jim Idle jimi at temporal-wave.com
Tue May 22 17:39:13 PDT 2012


You need:

((B)=> B)+

And even then you will sometimes just shift the warning elsewhere. This
does incur a single token predicate overhead of course, but in reality
that costs nothing. I usually find that such things as this mean that
grammar is not structured correctly, and though sometimes the language you
are parsing means that there is no way to structure it properly, there
quite often is.

Jim

-----Original Message-----
From: antlr-interest-bounces at antlr.org
[mailto:antlr-interest-bounces at antlr.org] On Behalf Of Terence Parr
Sent: Tuesday, May 22, 2012 11:43 PM
To: Stephen Siegel
Cc: ANTLR Interest
Subject: Re: [antlr-interest] resolving ambiguity

try (options {k=1;} : B)+
Ter
On May 21, 2012, at 4:12 PM, Stephen Siegel wrote:

> Thanks, Ter.  That's true, but the warnings are scary -- or at least
suggest I'm not doing something quite right -- and I want to make them go
away.  (This is an abstraction of a much larger grammar where I ran into
this problem.  In the larger grammar, there are a bunch of such warnings.)
Is there a way to tell ANTLR that I want that behavior, so don't warn me?
> -Steve
>
> On May 21, 2012, at 6:14 PM, Terence Parr wrote:
>
>> hi Steve, it will match greedily so that b_chain will match all B's in
a row.
>> Ter
>> On May 21, 2012, at 8:50 AM, Stephen Siegel wrote:
>>
>>> In the following grammar:
>>>
>>> -----------------------------------------------
>>> grammar g1;
>>> options { output=AST; }
>>> tokens { B_CHAIN; }
>>> r	:	(A | b_chain)*;
>>> b_chain	:	B+ -> ^(B_CHAIN B+);
>>> A 	:	'A';
>>> B	:	'B';
>>>
>>> // BB should yield ^(BCHAIN B B)
>>> -----------------------------------------------
>>>
>>> there is an ambiguity since input "BB" could be parsed as (1) two
"b_chain"s (each with one "B") or as (2) one b_chain (with two "B"s).  I
want to tell ANTLR to choose option (2).
>>>
>>> Right now, I get this warning:
>>>
>>> -------------------------------------
>>> [11:45:02] Checking Grammar g1.g...
>>> [11:45:02] warning(200): g1.g:5:11:
>>> Decision can match input such as "B" using multiple alternatives: 1, 2
>>>
>>> As a result, alternative(s) 2 were disabled for that input
>>> -------------------------------------
>>>
>>> Does anyone know how to resolve the ambiguity?
>
>


List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe:
http://www.antlr.org/mailman/options/antlr-interest/your-email-address


More information about the antlr-interest mailing list