[antlr-interest] resolving ambiguity

Bart Kiers bkiers at gmail.com
Tue May 22 11:05:21 PDT 2012


Hi Stephen,

Explicitly tell ANTLR to match B's greedily:

b_chain : (options{greedy=true;}: B)+ -> ^(B_CHAIN B+);


For input "BB", it will cause the tree ^(B_CHAIN B B) to be created, and
ANTLR will not produce warnings when generating the parser.

Regards,

Bart.


On Tue, May 22, 2012 at 6:04 PM, Stephen Siegel <siegel at udel.edu> wrote:

> No difference.
>
> On May 22, 2012, at 11:42 AM, Terence Parr wrote:
>
> > 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