[antlr-interest] If statement for Tree-based Interpreter: is it the correct way to do it?
Pavel Ganelin
ganelin at mail.com
Thu Aug 23 11:02:07 PDT 2007
Hi,
I found that in ANTLR v3 it is more difficult to skip the child for tree
walker compare with v2. Is it true or it is just learning curve?
Below I attached how I process if statement for a tree-based
interpreter. Please note that I had to add artificial token EOL while
building the tree in the parser so that I could position the cursor just
before Token.UP. I did not find a way to move the pointer to the last
node of the the given tree ($s in the example below). Start and Stop
indexes in the tree node point to the original token tree, not the
CommonTreeNodeStream.
Is it the way it is supposed to be implemented? If not what is the
correct approach
PS. I did read
http://www.antlr.org/wiki/display/ANTLR3/Simple+tree-based+interpeter.
It does not cover this question.
Thanks,
Pavel
==================================================== grammar
==========================
ifStatement
:
^(s=IF e=eval
/*
^(then=SLIST)
^(els=SLIST )
EOL // artificial token added by parser to help position
// stream after evaluation
*/
{
CommonTreeNodeStream stream = (CommonTreeNodeStream) input;
Tree then=$s.getChild(1);
Tree els =$s.getChild(2);
if ($e.value) {
input.seek(stream.getNodeIndex(then));
slist(queue);
}
else {
input.seek(stream.getNodeIndex(els));
slist(queue);
}
input.seek(stream.getNodeIndex($s.getChild(3)));
}
EOL)
;
// evaluates the expression
eval returns [bool value] :
^(EXPR
....
);
// process statements:
^(SLIST
....
)
;
More information about the antlr-interest
mailing list