[antlr-interest] Tree parser tree building bug and workaround

Ric Klaren ric.klaren at gmail.com
Wed Nov 29 13:03:15 PST 2006


Hi,

On 11/29/06, Monty Zukowski <monty at codetransform.com> wrote:
> Oh, actually my rule did call subrules instead of just terminals.
> Here's a simple example, just for looking at the generated code.
> functionBad generates code which overwrites the built AST.
> functionGood generates code which builds and preserves the AST
> properly.
>
> class Test extends TreeParser;
> options {
>         buildAST=true;
> }
>
> functionBad
>     :
>     #(f:FUNCTION
>               (functionBad)*
>               {##=#(#[FUNCTION],##);}
>              )
>     ;
>
> functionGood
>     :
>     #(f:FUNCTION
>               (functionGood)*
>              )
>               {##=#(#[FUNCTION],##);}
>     ;

         functionBad_AST = (AST)currentAST.root;
         functionBad_AST=(AST)astFactory.make( (new ASTArray(2)).add(astFactory$
         currentAST.root = functionBad_AST;
         currentAST.child = functionBad_AST!=null &&functionBad_AST.getFirstChi$
            functionBad_AST.getFirstChild() : functionBad_AST;
         currentAST.advanceChildToEnd();
.         currentAST = __currentAST2;
.         _t = __t2;
.         _t = _t.getNextSibling();
         functionBad_AST = (AST)currentAST.root;

vs:

.        currentAST = __currentAST6;
.         _t = __t6;
.         _t = _t.getNextSibling();
         functionGood_AST = (AST)currentAST.root;
         functionGood_AST=(AST)astFactory.make( (new ASTArray(2)).add(astFactor$
         currentAST.root = functionGood_AST;
         currentAST.child = functionGood_AST!=null &&functionGood_AST.getFirstC$
            functionGood_AST.getFirstChild() : functionGood_AST;
         currentAST.advanceChildToEnd();
         functionGood_AST = (AST)currentAST.root;

The bad AST example modifies the currentAST before it's set as a
result of the #( .. ) construct. Compare the position of the lines
with a '.' in front of them. I guess the fix in this case would be to
disallow assigning to ## (and probably to #rulename) inside #( .. ).

Cheers,

Ric


More information about the antlr-interest mailing list