[antlr-interest] [C] Skip sub-tree nodes from AST?

Gonzague Reydet gonzague.reydet at gmail.com
Wed Jan 4 03:24:39 PST 2012


I worked around the wildcard issue by making my own function that walk the
subtree counting the UP & DOWN tokens and ignoring all other tokens.
I don't think it is a good practice to make it this way, but a least it
does the job...

I use the following tree grammar rule:

// ^(IF expression statement (ELSE statement)?)
if_
    @declarations { ANTLR3_MARKER thenIdx, elseIdx = 0; }
  : ^(IF expression
      {
        thenIdx = INDEX();
        ignoreSubTree(ctx);
        if (LA(1) == ELSE) {
            MATCHT(ELSE, NULL);
            elseIdx = INDEX();
            ignoreSubTree(ctx);
        }
      })
    { // My code that rewind to either then or else block }
  ;

and the ignoreSubTree() function is implemented as following:

static void ignoreSubTree(psshell_tree ctx)
    {
        ebd_sint32_t nestedS32 = 0;

        do {
            MATCHANYT();
            if  (HASEXCEPTION()) {
                return;
            }

            switch(LA(1)) {
                case DOWN:
                    nestedS32++;
                    break;
                case UP:
                    nestedS32--;
                    break;
                default:
                    break;
            }
        } while (nestedS32 > 0);
        MATCHANYT(); // Eat last UP token
    }



2012/1/3 Gonzague Reydet <gonzague.reydet at gmail.com>

> Hi Jim,
>
> I suppose by "RELEASE" macro you mean "REWIND"?
>
> Yes I tried to use these macros as following :
>  ^(IF expression { thenIdx = MARK(); } ^(THEN .*) { elseIdx = MARK(); }
> ^(ELSE .*) ENDIF)
>
> But the same problem occurs as described in my previous message, the
> generated tree parser does not handle properly the UP and DOWN tokens for
> the " .* " pattern.
> I don't see how to use the MARK macro without the " .* " pattern.
>
>
> 2012/1/3 Jim Idle <jimi at temporal-wave.com>
>
>> Did you look at the MARK and RELEASE macros?
>>
>> Jim
>>
>> > -----Original Message-----
>> > From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
>> > bounces at antlr.org] On Behalf Of Gonzague Reydet
>> > Sent: Tuesday, January 03, 2012 9:32 AM
>> > To: antlr-interest at antlr.org
>> > Subject: [antlr-interest] [C] Skip sub-tree nodes from AST?
>> >
>> > Hi all!
>> >
>> > It's been two days I am investigating on how to skip a subtree with the
>> > C target of ANTLR v3.4. I found many discussions on this subject on
>> > markmail (notably the following one:
>> > http://markmail.org/message/f5op6tdn4vfpw57v).
>> > Exactly like this discussion, my original purpose is to implement an
>> > if/then/else interpreter.
>> > But I can't find a valid solution to parse only either the "then" or
>> > the "else" statement skipping the other one and going to the end of the
>> > if statement after having handled it.
>> >
>> > The "if" rule of my parser grammar constructs the AST as following:*
>> > ^(IF expression ^(THEN statement) ^(ELSE statement?) ENDIF);  *
>> >
>> > I have tried two different ways:
>> > - First using the method proposed in the discussion :
>> > ^(IF expression {
>> >     pANTLR3_BASE_TREE n = $IF.getChild($IF, 3);
>> >     SEEK(n->savedIndex);
>> >     ...
>> > } )
>> > The problem here is the 'savedIndex' field is never set. Is their a way
>> > for this field to be set before parsing the tree?
>> >
>> > - Second using a "wildcard pattern" as following : ^(IF expression
>> > ^(THEN
>> > .*) ^(ELSE .*) ENDIF)
>> > The problem here is the generated tree parser does not handle DOWN
>> > tokens, so it results in parsing errors when trying to match the
>> > "wildcarded"
>> > subtree.
>> >
>> >
>> > I have found the Pie example
>> > (http://www.antlr.org/wiki/display/ANTLR3/Pie)
>> > that passes a "defer" parameter to disable execution in all subrules.
>> > Is this really the only solution for this issue with the C target?
>> > I would really prefer to use the approach proposed by Terrence for the
>> > Java target (http://markmail.org/message/7thj4um2bzhuvqpy) but this
>> > will lead to the same issue of my second solution.
>> >
>> > Is there any other solution for this?
>> >
>> > Regards,
>> > Gonzague
>> >
>> > List: http://www.antlr.org/mailman/listinfo/antlr-interest
>> > Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-
>> > email-address
>>
>> List: http://www.antlr.org/mailman/listinfo/antlr-interest
>> Unsubscribe:
>> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>>
>
>
>
> --
> Tél : +33 (0)6 21 02 90 48
>



-- 
Tél : +33 (0)6 21 02 90 48


More information about the antlr-interest mailing list