[antlr-interest] ANTLR 2.7.7 TreeParser transformation question
Kevin J. Cummings
cummings at kjchome.homeip.net
Thu Apr 19 00:13:17 PDT 2007
Never mind! My bad! I forgot to put (back?) the buildAST=true; option.
(I *hate* it when I do that!) Things are working *much* better now.
Kevin J. Cummings wrote:
> Ric Klaren wrote:
>> 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?
>
> Using ## generates:
>
> aexpr_AST = RefGenevaAST(currentAST.root);
>
> before the actions for the rule, but neither aexpr_AST nor currentAST
> are declared inside the code blocks in which they are used, and G++
> complains about this.
>
> It doesn't seem to matter if I take the ! off the production rule or not.
>
>> Cheers,
>>
>> Ric
>>
>
>
--
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