[antlr-interest] ANTLR 3.0 template files

Terence Parr parrt at cs.usfca.edu
Mon Jun 13 12:20:17 PDT 2005


Guys,

I'm wondering if it's a bad time for you folks to be trying to build  
C/C++/C# targets... To do the tree stuff, I'm changing a lot of stuff.

I was hoping to avoid this, but I may need to do a good bit of  
refactoring of the templates themselves.  Ric you may remember from  
the ANTLR2004 workshop that I'm thinking of a "template composition"  
thing to encapsulate different concerns.  So we'd have a basic code  
generator template file and then a group of templates that get  
applied to alter that for debugging then a set of templates to alter  
even that for tree construction.  At this point there are a million  
IF statements in the templates depending on debug etc... and *lots*  
of duplication (as I build the trees now) in order to avoid even more  
IFs.

I may need to add a concept to StringTemplate like aspects or  
composition.  Essentially I need to have a simple template like

tokenRef(label,token) ::= <<
<if(label)>
<label> = input.LT(1);
<endif>
match(token);
 >>

and then in another file (almost like a subclass) create the  
differences needed for trees.  But, how does the tree template stuff  
know where to stick stuff (front and back is easy, but what about in  
the middle)?  I figured we could leave holes:

tokenRef(label,token) ::= <<
<if(label)>
<label> = input.LT(1);
<else>
<treeLabel>
<endif>
match(token);
<treeBuild>
 >>

then have

tokenRef.treeLabel ::= "Token tk = input.LT(1);"

tokenRef.treeBuild ::= "Tree t = adaptor.create(tk);"

in another file.  The funny thing is that we could *almost* use  
subclassing for this.

tokenRef(label,token) ::= <<
super.tokenRef(treeLabel="...", treeBuild="...")
 >>

But the arguments can be many lines long and you lose the sense that  
it's a super group template instantiation.

For some reason, I'm gravitating towards the "composition" idea.

I hate having to leave holes in the main tokenRef even as that will  
get more and more complicated as I add debugging and all that.  I  
need a way to specify locations in a template after-the-fact--w/o  
having to actually alter the original template.  I read some aspect- 
oriented programming stuff but it seems pretty complicated how they  
specify where to add function calls etc...  Hmm... anybody have some  
suggestions?

Ter
--
CS Professor & Grad Director, University of San Francisco
Creator, ANTLR Parser Generator, http://www.antlr.org
Cofounder, http://www.jguru.com



More information about the antlr-interest mailing list