[antlr-interest] How to fix the ambiguous grammar

Terence Parr parrt at cs.usfca.edu
Mon Jun 4 21:57:24 PDT 2012


It sounds like you have backtracking turned on and that whichever rule calls 'a' has B following the reference to 'a'. When it can't figure out what to do with an ambiguity it picks the 1st match.
Ter
On Jun 4, 2012, at 9:49 PM, Jie Li wrote:

> Hi all,
> 
> I'm new to antlr and struggling to fix the grammar like below:
> 
> a:   A
>    | A B ;
> 
> But this grammar doesn't support "A B" as it complains it expects the end
> character after "A". If I switch the order of this grammar, e.g. "A B"
> comes before "A", then it doesn't support "A" as it complains it expects
> "B" after "A".
> 
> Then I tried:
> 
> a:  A
>   | A {input.LT(1).getText().equals("B")}? B;
> 
> It does work for both rules now. But I wonder if there is some more elegant
> way?
> 
> Thanks,
> Jie Li
> 
> 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