[antlr-interest] Syntactic predicates require "k=x" ?

Terence Parr parrt at cs.usfca.edu
Mon Mar 28 10:35:33 PST 2005


On Mar 28, 2005, at 6:05 AM, Alexey Demakov wrote:

> Hi,
>
>> I was wondering:
>>
>> NEWLINE : ("\r\n") => "\r\n" | '\r' | '\n';
>>
>> Does that syntactic predicate ("\r\n") works if I don't specify k=2 
>> in options?
>> It seems that it doesn't (it only checks for '\r' which is k=1) but I
>> can't be sure... help me please...
>>
>> The FAQ says that I can have infinite lookahead using syntactic
>> predicates, yet I have to specify k ???? (*confused*)

That is correct.  No need to specify lookahead (it could be arbitrarily 
large; i.e., not fixed k).

> I see the following generated code:
>
>   boolean synPredMatched16 = false;
>   if (((LA(1)=='\r'))) {
>    int _m16 = mark();
>    synPredMatched16 = true;
>    inputState.guessing++;
>    try {
>     {
>     match("\r\n");
>     }
>    }
>    catch (RecognitionException pe) {
>     synPredMatched16 = false;
>    }
>    rewind(_m16);
>    inputState.guessing--;
>   }
>   if ( synPredMatched16 ) {
>    match("\r\n");
>   }
>
> if LA(1) is '\r', antlr enters guess mode (inputState.guessing++)

Just as an efficiency; it tries your fixed lookahead first to avoid 
unnecessary backtracking.  If the fixed lookahead is consistent it will 
attempt to match the predicate regardless of lookahead depth.

My guess is that you have another issue entirely ;)  What, I can't 
guess, but the predicate should definitely work :)

Ter
--
CS Professor & Grad Director, University of San Francisco
Creator, ANTLR Parser Generator, http://www.antlr.org
Cofounder, http://www.jguru.com





More information about the antlr-interest mailing list