[antlr-interest] How to fix the ambiguous grammar

Jie Li jay23jack at gmail.com
Mon Jun 4 21:49:54 PDT 2012


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


More information about the antlr-interest mailing list