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

Gonzague Reydet gonzague.reydet at gmail.com
Tue Jan 3 09:32:07 PST 2012


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


More information about the antlr-interest mailing list