[antlr-interest] ANTLR 2.7.7 TreeParser transformation question
Kevin J. Cummings
cummings at kjchome.homeip.net
Fri Apr 13 18:33:08 PDT 2007
language = C++
I'm trying to do a tree transformation before I generate some code from
the resulting tree. So, I have the following set of rules in my tree
walker:
> bexpr : #( AND a1:bexpr a2:bexpr )
> | #( OR o1:bexpr o2:bexpr )
> | #( NOT n:bexpr )
> | #( LESSTHAN lt1:aexpr lt2:aexpr )
> | #( LE le1:aexpr le2:aexpr )
> | #( GT gt1:aexpr gt2:aexpr )
> | #( GE ge1:aexpr ge2:aexpr )
> | #( EQ eq1:aexpr eq2:aexpr )
> | #( NE ne1:aexpr ne2:aexpr )
> ;
>
> // Lets fold constant expressions out below
> // and emit the final code for aexpr when they get used above.
> aexpr :! #( PLUS p1:aexpr p2:aexpr )
> {
> // Anything plus 0 is Anything
> if (isZero(p1))
> {
> #aexpr = #p2;
> }
> else if (isZero(p2))
> {
> #aexpr = #p1;
> }
> else
> {
> #aexpr = #(PLUS, p1, p2);
> }
> }
> |! ( #( MINUS aexpr aexpr ) )=> #( minus:MINUS m1:aexpr m2:aexpr )
> |! #( mult:MULT mu1:aexpr mu2:aexpr )
> |! #( div:DIV d1:aexpr d2:aexpr )
> |! #( neg:MINUS n:aexpr )
> | NUM
> | STRING
> ;
What I'm trying to do is transform the tree passed into aexpr in those
cases when I can remove complexity. My isZero() method is correctly
recognizing those NUM trees which contain the number "0".
If I examine #aexpr before leaving this rule, I can see that:
(PLUS 2 0)
is correctly reduced to just
2
My problem is that the assignment to #aexpr appears in the .cpp code as
a reference to: aexpr_AST_in, but it is never used again after it gets
assigned!
The end of the case continues with assigning _retTree = _t and not my
new tree. The result of this is that when I examine my aexpr subtree in
the bexpr rule, it is the original (PLUS 2 0) tree! Not the transformed
one!
So what's the point of assigning it if it never gets returned?
Am I doing something wrong?
--
Kevin J. Cummings
kjchome at rcn.com
cummings at kjchome.homeip.net
cummings at kjc386.framingham.ma.us
Registered Linux User #1232 (http://counter.li.org)
More information about the antlr-interest
mailing list