[antlr-interest] How to reference a CommonTree in an action
Costa Basil
costa_basil at yahoo.ca
Tue Sep 4 14:35:39 PDT 2012
Iin the following rule (expr), how can I get a reference to the ^(FUNCTION_CALL $id (exprList)?) node?
options {
language=CSharp3;
output = AST;
ASTLabelType=FormulaAstNode;
}
...
expr
@init {
}
: (ID '(') =>
id=ID '(' (exprList)? ')' -> ^(FUNCTION_CALL $id (exprList)?) /* Here I would like to have an action that references the FUNCTION_CALL node and that sets some custom properties. Something like this: {$fc.tree.SomeProperty = true;} where SomeProperty is a member of FormulaAstNode and $fc points to ^(FUNCTION_CALL ...) */
| ID
| INT
| '(' expr ')' -> expr
;
Writing: fc=^(FUNCTION_CALL $id (exprList)?) doesn't seem to work.
I searched the existing code that gets distributed with antlr-3.4 and the closed I found is this (from antlrv3.g):
ebnf
@init {
IToken firstToken = input.LT(1);
}
@after {
$ebnf.tree.Token.Line = firstToken.Line;
$ebnf.tree.Token.CharPositionInLine = firstToken.CharPositionInLine;
}
: block
( op='?' -> ^(OPTIONAL[op] block)
| op='*' -> ^(CLOSURE[op] block)
| op='+' -> ^(POSITIVE_CLOSURE[op] block)
| '=>' // syntactic predicate
-> {gtype==COMBINED_GRAMMAR &&
Char.IsUpper($rule::name[0])}?
// if lexer rule in combined, leave as pred for lexer
^(SYNPRED["=>"] block)
// in real antlr tool, text for SYN_SEMPRED is predname
-> SYN_SEMPRED
| -> block
)
;
The node created is referenced via $<ruleName>.tree. I think this would ultimately work, but I was curious if there is another way?
Thank you,
Costa
More information about the antlr-interest
mailing list