[antlr-interest] tree transformation in Parser PLEASE
Bryan Ewbank
ewbank at gmail.com
Fri Sep 9 16:57:26 PDT 2005
Speaking just for me, I simply had to delete a weeks worth of mailing
list stuff (vacation)...
You have this:
#( = A #( = B #( = C 4 ) ) )
And you want to replace it with this:
#( = C 4 ) #( = B 4 ) #( = A 4 )
The hard part, from an ANTLR point of view, is knowing that you must
duplicate the node containing the constant.
Generally, in a following TreeParser, you can do this as shown below.
There may be errors (I didn't test this), but it should be close to
what you need. Note that I'm using C++ in my actions; if you're using
Java, your actions will be different.
// wrap resulting list of EQUALS nodes with a single root node
parent_rule
: xform_rule { ## = #( #[ROOT,"ROOT"], ## ); }
;
// flatten cascading EQUALS into a sequence of EQUALS nodes
xform_rule
returns [ RefAST r ]
: #( EQUALS id
( c:constant
// node at the bottom of the EQUALS cascade
{ r = #c; }
| r=x:xform_rule
{
// intermediate node; convert to flattened
node. the constant value
// is copied in this operation to avoid
destroying the tree...
## = #( #[EQUALS,"="], #id, COPY(r) );
##->setNextSibling(#x);
}
) // end of alternative
) // end of #EQUALS
;
More information about the antlr-interest
mailing list