[antlr-interest] Custom AST Nodes in v3

Brian Cox coxbrian at msu.edu
Mon Jun 5 17:29:22 PDT 2006


On Monday 05 June 2006 04:29, Rebecca Chernoff wrote:
> I apologize if I've overlooked this.  I am trying to convert a v2 grammar
> to v3.  I have my tokens section defined with custom AST nodes.
>
> tokens {
>   RULE<AST=CustomRule>;
>   ...
> }

As others upthread have mentioned, that's not available in v3 yet; however, 
I'm currently thinking about a workaround.  It's based upon the 
org.antlr.runtime.tree.CommonTreeAdaptor which provides a 
rulePostProcessing(Object) method that can be overridden.  This method is 
called at the tail end of each rule.

Right now I'm using it to rearrange some of the subtrees my grammar generates 
in ways that I couldn't figure out the proper way to express within the 
grammar itself.  So far I'm still using a homogeneous tree based around the 
default Tree/CommonTree, but I'm planning on using the same basic strategy to 
replace subtress with heterogeneous nodes.

This approach does require that you set the parser's TreeAdptor to your custom 
subclass at some point via the parser's setTreeAdaptor() method before the 
parser starts doing its work.  I've accomplished this via an @init tag on the 
very first grammar rule as so:

	start
	@init {setTreeAdaptor(new EtlTreeAdapter(this));}
		: program ;

... where EtlTreeAdapter is my custom subclass.  As for the overridden method, 
here's an example from my code:

    public Object rulePostProcessing(Object root) {
        Tree r = (Tree)super.rulePostProcessing(root);
        if (r != null) r = convertTree(r);
        return r;
    }

.... where I call the super classes's implementation, make sure the tree node 
isn't null, then hand-off to a helper method within my subclass to do the 
actual legwork.  I then return the modified tree which the parser will then 
use as its own root node for the particular rule that called 
rulePostProcessing().  You could probably use the same process to intercept 
the tail-end of each rule, capture the tree node, then create a new 
heterogeneous node based upon the original node.

-- 
 * Brian Cox <coxbrian at msu.edu>


More information about the antlr-interest mailing list