[antlr-interest] Implementing if statements in a tree parsing Interpreter
Matthew M. Burke
mmburke at gwu.edu
Wed May 6 08:27:18 PDT 2009
My next question:
I am building an interpreter based on the ideas in
http://www.antlr.org/wiki/display/ANTLR3/Simple+tree-based+interpeter.
In particular, I am taking the first approach of starting a new tree
parser for each function invocation.
Now I need to implement if statements (well, actually I have "smaller?",
"notsmaller?", "same?".... but once I get one figured out I can
generalize...) and I'm not quite sure how to do it. I read through the
recent thread on this list "Skip subtrees in tree grammar" since it
touches on this subject, but I don't see how to adapt what was discussed
in that thread to my situation.
My parser builds trees as follows:
statement
: ...
| SAME val val NL+ block --> ^(SAME_CMD val val block)
| ...
;
(where NL is a newline, val is essentially a standard expression rule,
block is a list of statements)
Now in the tree parser, I have
statement
: ...
| ^(SAME_CMD a=val b=val .) {
int aval = $a.value;
int bval = $b.value;
if (a == b) {
// help ?!?
}
}
| ...
;
If I had ^(SAME_CMD a=val b=val block), then block is automatically
evaluated before the action is run. So it seems to me that I need to
skip the block in the matching (as I have above) and then (at the spot
marked 'help') get hold of the root of the block tree, create a new
evaluator and run it. But I haven't been able to figure out how to do
that.
One possibility I thought of was in the parser, each time I come to a
block I could create a function with a unique name to wrap the block,
but that seems like a kludge.
Any suggestions?
Thanks,
Matt
More information about the antlr-interest
mailing list