[antlr-interest] keeping derived attributes after initial parse

Ben Liblit liblit at cs.wisc.edu
Tue Nov 24 13:55:22 PST 2009

My AST contains the basic syntax of the language I am parsing.  I would 
like to augment that with some derived information.  For example, I have 
struct-like aggregates where each field knows its size, and I would like 
the overall aggregate to know its overall size (summed across all fields).

This derived information is very easy to express using "returns" 
attributes.  For example:

	aggregate returns [int size]
	  : '{' (field { $size += $field.size })+ '}'
	  -> ... ;

	field returns [int size]
	  : ... { $size = ... }
	  -> ... ;

However, these returned attributes seem too transient for my use.  The 
size of the aggregate will be visible to the rule that invoked the 
aggregate rule, but I need to keep this information around long after 
initial parsing is done.  In particular, I would like to refer to the 
aggregate's already-computed size in later tree grammar passes that emit 
string templates.  For example:

	tree grammar ... ;

	aggregate: ... -> aggregate(size={????}, fields={$fields}) ;

I cannot find any way to access the computed size attribute in order to 
fill in that "????" spot.  It seems that the returned attributes are 
quite transient, no longer accessible once the initial parse is over.

I considered stashing the computed attribute values in the text of extra 
(imaginary) tokens, but that requires converting all such values to 
strings, which would be painful in many cases.  Where / how should I 
keep this extra information around?  Should I be creating my own 
CommonTree subclass with extra fields to store this stuff?  Or is there 
some more standard way of carrying this extra information around?

-- Ben

More information about the antlr-interest mailing list