[antlr-interest] Clarification on Attribute Reference in Actions

Randall R Schulz rschulz at sonic.net
Wed Jul 4 14:21:59 PDT 2007


Hi,

I just debugged a problem in my parser. I had rule in a tree parser that 
looked roughly like this:

term
[ returns Term t ]
   :   ^( ComplexTerm term termSequence )
	{
	    ...
	    Function function = asFunction($term.t)
	    ...
	}
   ;


What I found happening with this rule was that asFunction() was always 
being called with null.

When I changed the rule like this:

term
[ returns Term t ]
   :   ^( ComplexTerm funcTerm = term termSequence )
	{
	    ...
	    Function function = asFunction($funcTerm.t)
	    ...
	}
   ;


everything works. Looking at the generated Java code, I saw that the 
argument to asFunction() in the incorrectly functioning rule was 
just "t", which is the locally declared variable that holds the rule's 
result / return value. In the second, working form of the rule, the 
argument to asFunction() is "funcTerm" (a local variable declared to 
hold the result of the recursive call to the "term" rule, of course).


So was ANTLR mistaken by the recursive use of the "term" rule, or was I 
mistaken in expecting the reference to $term.t to refer to the most 
closely enclosing (recursive) right-hand-side use of the "term" rule?


Randall Schulz


More information about the antlr-interest mailing list