[antlr-interest] Implementing if statements in a tree parsing Interpreter

Martijn Reuvers martijn.reuvers at gmail.com
Wed May 6 11:07:59 PDT 2009


Hello Matt,

You have a different approach then what I did. Maybe it helps you if I
tell you how I do it?

What I do is I parse the the source file using tree grammar completely
but do not execute any real actions, except registering some symbols,
checking a few types etc. Then I reparse from a certain point (in my
case a main function) and from there I actually execute actions.

I do not use a new parser but use the same parser I already have, in
my case I remember the start of my main function using input.index()
and when I need to reparse I push it on the nodestream with:
nodeStream.push(index), from there a second parsing 'round' is started
but then I execute real actions. I do use the BufferedTreeNodeStream
for it to be able push/pop (thus reparse) instead of the
CommonNodeStream. Hence the solution with boolean stack in my case
works as in the second parse I can check that and decide to actually
do something (execute the contents of the if statement) or not. But
see the other thread for details, the second solution Gavin suggested
does not work for me so far. Perhaps if you remember the index of your
block you can do the same and push it in your if on success, not sure
though.

Martijn


More information about the antlr-interest mailing list