[antlr-interest] ANTLR 2.7.7 TreeParser transformation question

Ric Klaren ric.klaren at gmail.com
Wed Apr 18 03:59:25 PDT 2007


Hi,

On 4/14/07, Kevin J. Cummings <cummings at kjchome.homeip.net> wrote:
> > 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!

Try using ## in stead of #aexpr. Does removing the ! from the first
alternative work as well?

Cheers,

Ric


More information about the antlr-interest mailing list