[antlr-interest] predicates and backtracking

Ymo ymo.mail at gmail.com
Tue May 20 17:26:28 PDT 2008


Hi folks.

I am trying to follow
http://www.antlr.org/wiki/display/ANTLR3/6.+Advanced+processing

However my predicate is getting hit *before* the below code in the statrtTag
gets executed:

 {$ElementScope::currentElementName = $GENERIC_ID.text; }

So my code ends up throwing NPE since currentElementName never gets
initialized.

I debuged the parser code and

 Here is what i have in my grammar :
 options {
   k=3;
   memoize=true;
   backtrack=true;
}

<snip>

nodeElement
scope ElementScope;
   : nodeElementStart (nodeBlock)* nodeElementEnd
   ;

nodeElementStart
   : TOK_ELEMENT
      attrBlock* TOK_RG {
      $ElementScope::currentElementName = $TOK_ELEMENT.text;
      //$ElementScope::currentElementName = tokElement.getText();
   };

nodeElementEnd
   :  { $ElementScope::currentElementName.equals(input.LT(1).getText()) }?
      TOK_ELEMENT_CLOSE TOK_RG {
   };
   catch [FailedPredicateException fpe] {
      String hdr = getErrorHeader(fpe);
      String msg = "end tag (" + input.LT(2).getText() +
                    ") does not match start tag (" +
                    $ElementScope::currentElementName +
                    ") currently open, closing it anyway";
      emitErrorMessage(hdr+" "+msg);
      consumeUntil(input, TOK_RG);
      input.consume();
   }

The predicate always seems to get executed before  the currentElementName is
initialized by the nodeElementStart.

The generated parser is :

            match(input,TOK_RG,FOLLOW_TOK_RG_in_nodeElementStart289); if
(failed) return ;
            if ( backtracking==0 ) {


((ElementScope_scope)ElementScope_stack.peek()).currentElementName =
TOK_ELEMENT1.getText();

//((ElementScope_scope)ElementScope_stack.peek()).currentElementName =
tokElement.getText();

            }


When debugging the if ( backtracking==0 ) resolves to false so effectively
currentElementName is not initialized !

Is there anything else i need to do in my grammar to make sure the predicate
gets executed after the currentElementName is initialized ?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080520/487b3802/attachment.html 


More information about the antlr-interest mailing list