[antlr-interest] predicates and backtracking

Terence Parr parrt at cs.usfca.edu
Tue May 20 17:31:11 PDT 2008


3.1Should fix this; it was not stopping at actions when looking for  
predicates. Try the latest builds.
Ter
On May 20, 2008, at 5:26 PM, Ymo wrote:

> 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 ?
>



More information about the antlr-interest mailing list