[antlr-interest] resolving ambiguity

Stephen Siegel siegel at udel.edu
Mon May 21 16:12:17 PDT 2012


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?




More information about the antlr-interest mailing list