[antlr-interest] Manual tree construction in 2.7.6 (buggy?)
F Reig
fermin.reig at gmail.com
Thu Mar 30 13:48:36 PST 2006
On 3/28/06, Micheal J <open.zone at virgin.net> wrote:
> > I construct the tree by hand like this:
> >
> > decl_default!
> > : d:DEFAULT i:IDENT COMMA e:exp SEMI
> >
> > {## = #(#d, #([IF,":IF"],
> > #([EQ_EQ,"=="],#i,#([NIL,"NIL"])),
> > #([STMTS,"STMTS"],
> > #([EXPR,"EXPR"],
> > #([BECOMES,"BECOMES"],#i,#e))
> > )
> > )
> > );
> > }
> > ;
> >
> > When I dump the tree (or visualize it with antlr.astframe) I see this:
> >
> > ( :DEFAULT ( :IF ( == x NIL 1) ( STMTS ( EXPR ( BECOMES x NIL 1 )))))
> >
> > That is, there is a spurious "1" in the test, and a spurious
> > "NIL" in the assignment. Yet, the tree constructor looks OK
> > to me. Am I doing something wrong
>
> Yes. You are trying to add the single IDENT node to two different AST
> [sub-]trees.
>
> You need to copy the node if you wish to use it multiple times. Use
> dupTree() for that and be sure to set the first-child and next-sibling links
> appropriately.
>
> Cheers,
>
> Micheal
This seems to work:
{ AST i2 = astFactory.dupTree(#i);
## = #(#d, #([IF,":IF"],
#([EQ_EQ,"=="],#i,#([NIL,"NIL"])),
#([STMTS,"STMTS"],
#([EXPR,"EXPR"],
#([BECOMES,"BECOMES"], i2 ,#e)))));
And it seems that I don't need to set any links by hand. Thanks Micheal
I would suggest that a similar example be included in the manual.
Fermin
More information about the antlr-interest
mailing list