[antlr-interest] Backtracking vs Lookahead

Andreas Meyer andreas.meyer at smartshift.de
Fri Feb 27 04:31:52 PST 2009


Hello again!

I have to be more precise here. Backtracking is not what I want, as it 
recognizes the string "AAB" as "any,any,some", whereas I want it to be 
recognized as "any,some". I understand that I could write a semantic 
predicate like :

   rule     : ( {input.LA(1) != B && (input.LA(1) != A || input.LA(2) != 
B)}? any )+ some;

which works very nice. However, I have (literally) hundreds of places in 
my grammar that would need this workaround (each with a different 
"lookahead language"), so I would like to ask if there is a more formal 
way to do it. If not, how hard would it be to extend ANTLR with such a 
mechanism? If you could provide me with some pointers/hints, I would be 
happy to implement this myself.

Best Regards,
Andreas Meyer

Andreas Meyer schrieb:
> Dear ANTLR users! (developers?)
>
> I have a very basic problem: supposed I have two rules, one which 
> basically includes any token, and another one that contains only a 
> subset of them, say:
>
> any : A | B | C | D ;
> some
>    : A B
>    | B
>    ;
>
> Now, I want to combine those two rules:
>
> rule     : ( options{ greedy=false;} : any )+ some      ;
>
> .. such that the inner loop should consume any's, as long as LA(1)!=B 
> && (LA(1)!=A||LA(2)!=B). This should be easily recognizable by the 
> parser (in my opinion). However, ANTLR 3 (3.1.2) says:
>
> warning: Decision can match input such as "A B" using multiple 
> alternatives: 1, 2
>
> Note that when I change the rule "some" to:
>
> some : A | B;
>
> such that only a lookahead of one character is enough, everything 
> works as expected: "any" is consumed until a character is found from 
> the difference "any-some".
>
> Now, I thought "ok, use backtracking". Enabling it globally seems to 
> work. But, if I try to enable it only locally, it does not seem to 
> have any effect. Actually, I tried to enable it for any rule in my 
> grammar - without any effect! (I include the grammar in question as 
> attachement)
>



More information about the antlr-interest mailing list