[antlr-interest] How to match a tree without executing the actions

Michael Bedward michael.bedward at gmail.com
Sun Apr 17 23:27:40 PDT 2011


Perhaps an easier approach is just to use two separate grammars ?  I
commonly do this: one grammar for validation and a second, with more
or less identical rules but additional actions, for rewriting, code
generation or whatever.

Michael


On 18 April 2011 16:13, Mu Qiao <qiaomuf at gmail.com> wrote:
> Thanks for answering. I think a global scope with a boolean flag can
> work, but it still needs to use if statement everywhere to control
> action execution. Currently I write a method:
> int count_nodes(pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE curr)
> {
>        int child_count = adaptor->getChildCount(adaptor, curr);
>        if(child_count == 0)
>        {
>                // Leaf node
>                return 1;
>        }
>        else
>        {
>                int result = 0;
>                // Count every child
>                for(int i = 0; i != child_count; ++i)
>                        result += count_nodes(adaptor,
> (pANTLR3_BASE_TREE)(adaptor->getChild(adaptor, curr, i)));
>                // Add itself, DOWN and UP
>                return result + 3;
>        }
> }
> This method will count the number of nodes in the function body. Then
> I'll call SEEK(INDEX() + count_nodes(ADAPTOR, LT(1)) - 1) to skip the
> function body. This works for me and I don't have to modify other
> rules. But I'm not sure if this is the best approach.
>
> On Mon, Apr 18, 2011 at 11:38 AM, Michael Bedward
> <michael.bedward at gmail.com> wrote:
>> Hello,
>>
>> I'm not sure if I understand what you are trying to do properly, but
>> using a global scope with a boolean flag would let you control action
>> execution without the need pass a parameter down the rule chain.
>>
>> Michael
>>
>> On 18 April 2011 00:19, Mu Qiao <qiaomuf at gmail.com> wrote:
>>> Hi,
>>>
>>> I have an AST and a subtree that represents for function definition.
>>> When I implement the tree walker, I don't want to executing the
>>> actions in the function body when I match a function definition. I
>>> know I can pass an argument down indicating whether to execute the
>>> actions or not. Is there any way that doesn't require any modification
>>> to other rules except the function definition rule?
>>>
>>> I tried to skip the whole function body AST but didn't success because
>>> it's hard for me to find the last node index of the body. If I can
>>> have that, I guess SEEK(index + 1) could work for me if I'm right.
>>>
>>> Could any one please give me a hand?
>>>
>>> --
>>> Best wishes,
>>> Mu Qiao
>>> GnuPG fingerprint: 92B1 B0C4 8D14 F8C4 EFA5  3ACC 30B3 0DE4 17B1 57E9
>>>
>>> List: http://www.antlr.org/mailman/listinfo/antlr-interest
>>> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>>>
>>
>
>
>
> --
> Best wishes,
> Mu Qiao
> GnuPG fingerprint: 92B1 B0C4 8D14 F8C4 EFA5  3ACC 30B3 0DE4 17B1 57E9
>


More information about the antlr-interest mailing list