[antlr-interest] Tree transformations
Bryan Ewbank
ewbank at gmail.com
Thu Jul 14 09:37:37 PDT 2005
You can do it with code, but it's perhaps better to use the ANTLR constructs:
refactor { RefAST ctree; }
: #( A ctree=brule erule[ctree] F ) ;
brule returning [ RefAST ctree ]
: #( b:B c:C d:D ) { ## = #( b d ); ctree = #c; } ;
crule : C ;
erule [ RefAST ctree ]
: #( e:E children ) { ## = #( e, ctree, #children ); } ;
This is not quite right - no access to compiler at the moment - but
it's close. Beware of dangling links in the intermediate tree (ctree)
that is passed around; the factory has a "dupTree" method that might
come in handy.
Sorry for being sketchy...
- Bryan
On 7/14/05, Tarun Khanna <tarunkhanna at gmail.com> wrote:
> I guess yes. You can do something like
>
> E.addChild(B.getFirstChild());
> and then set the child of B to NULL
> B.setFirstChild(null) or
> B.setFirstChild(B.getFirstChild().getNextSibling());
>
> All you ANTLR experts, please let me know if the above procedure is
> correct. I would appreciate if you can point out the flaw as I spent a
> couple of days in trying to understand the AST stuff in ANTLR and if the
> above is flawed then I need to spend some more time.
>
> Tarun
>
>
> On 7/14/05, Jeff Dahl <jddahl at micron.com> wrote:
> > When transforming a tree, is it possible to move a subtree to some
> > arbitrary node? For example, given the tree:
> >
> > A
> > |-- B
> > | |-- C
> > | | |-- attribute1
> > | | | `-- value 1
> > | | |-- attribute2
> > | | | `-- value 2
> > | | `-- attribute3
> > | | `-- value 3
> > | `-- D
> > | `-- attribute4
> > | `-- value 4
> > |-- E
> > | `-- attribute5
> > | `-- value 5
> > `-- F
> > `-- attribute6
> > `-- value 6
> >
> > is it possible to move subtree "C" to subtree "E" as illustrated below?
> >
> > A
> > |-- B
> > | `-- D
> > | `-- attribute4
> > | `-- value 4
> > |-- E
> > | |-- C
> > | | |-- attribute1
> > | | | `-- value 1
> > | | |-- attribute2
> > | | | `-- value 2
> > | | `-- attribute3
> > | | `-- value 3
> > | `-- attribute5
> > | `-- value 5
> > `-- F
> > `-- attribute6
> > `-- value 6
> >
> > Thanks,
> > Jeff
> >
> >
>
>
>
> --
> Tarun Khanna
More information about the antlr-interest
mailing list