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

Alexey Demakov demakov at ispras.ru
Mon Mar 28 06:05:58 PST 2005


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*)

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++)
and tries to match "\r\n". If it is successful, antlr set flag (synPredMatched16)
indicating that predicate successfully matched
and (in any case) leaves guess mode (with rewind to position before '\r').
After that antlr checks if predicate flag is true and
match "\r\n".

You need to specify lookahead depth to generate this code.

Regards,
Alexey

-----
Alexey Demakov
TreeDL: Tree Description Language: http://treedl.sourceforge.net
RedVerst Group: http://www.unitesk.com




More information about the antlr-interest mailing list