[antlr-interest] How to interpret an IF with antlr 3.0
Terence Parr
parrt at cs.usfca.edu
Sat May 5 14:54:07 PDT 2007
On May 5, 2007, at 1:21 PM, ugol wrote:
> Hi all,
> I am back with the same question. 3 months ago I rolled back to
> javacc, which I know marginally better, but now I really want to move
> to antlr. Unfortunately, I can't figure out how to delay the
> processing of a node... (if this is the correct solution)
>
> If you don't want to read all this old thread, the question is:
>
> - How to translate this antlr v2 in antlr v3?
>
> ^(IF condition x:.) // x is NOT evaluated, merely captured
> { if ($condition.value) { stat(x); }
>
> tia,
Hi Ugol,
first, thanks for updating the wiki on @finally/finally.
ok, to your question. ANTLR v3 does allow '.' now to mean scan past
entire tree w/o parsing. From there you have to create another node
stream and invoke the rule. I'll come up with a short hand soon.
This is from the tree-based interpreter example:
call returns [int value]
@init {
Map argScope = new HashMap();
scopes.push(argScope); // push new arg scope
}
: ^(CALL ID expr)
{
CommonTreeNodeStream stream = (CommonTreeNodeStream)input;
CommonTree funcRoot = (CommonTree)functions.get($ID.text);
int addr = stream.getNodeIndex(funcRoot);
if ( addr>=0 ) {
CommonTree argNode = (CommonTree)funcRoot.getChild(1);
String argName = argNode.getText();
int exprIndex = addr + 4; // skip over FUNC DOWN ID ID to
expr
argScope.put(argName, new Integer($expr.value));
((CommonTreeNodeStream)input).push(exprIndex);
$value = expr(); // invoke expr and set CALL's value
((CommonTreeNodeStream)input).pop();
}
else {
System.err.println("no such function: "+$ID.text);
}
}
;
finally {
scopes.pop(); // remove arg scope
}
Ter
More information about the antlr-interest
mailing list