[antlr-interest] Backtracking vs Lookahead

Andreas Meyer andreas.meyer at smartshift.de
Thu Feb 26 09:38:50 PST 2009


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)

What can I do? Thanks for any hints!
Andreas Meyer
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: GreedyTest.g
Url: http://www.antlr.org/pipermail/antlr-interest/attachments/20090226/672ed088/attachment.pl 


More information about the antlr-interest mailing list