I've been looking at:
http://www.antlr.org/wiki/display/~admin/2008/04/11/Rewrite+rules
There is the section on grammatical context that says:
[classDef var]: // context is "field"
type ID -> blort
[... method var]: // context is "local"
type ID -> blech
which as I understand is a way for you to look at the past non-terminals that derived the current non-terminal. I've tried to use this in my grammar but I don't know the correct Antlr syntax for doing so. I've been trying to do something like this in my tree grammar:
enclosed returns [String s]
:
[andCond enclosed*]:
^(ENCLOSED andCond)
{$s = $andCond.s; }
|
[orCond enclosed*]:
^(ENCLOSED orCond)
{$s = $orCond.s; }
|
^(ENCLOSED enclosed) // return whatever is enclosed
{$s = $enclosed.s; }
|
^(ENCLOSED condition) // last resort, have to keep parenthesis
{$s = "(" + $condition.s + ")"; }
;
where enclosed is the nonterminal that matches AST nodes that represents an expression that's nested inside parenthesis. What I try to accomplish is the removal of the useless parentheses in the string produced.
For instance, if user puts in (((a==b)))
I expect to get the string a==b
after I run it through the parser and the tree walker.