[antlr-interest] Perplexed about template option

Pete Siemsen siemsen at UCAR.EDU
Fri Dec 14 11:08:21 PST 2007


I think the main reason to use templates is to allow you to easily  
change your output language.  Suppose you are parsing input language  
A and producing either output language B or output language C.   
Templates provide a great mechanism for this.  But suppose A, B and C  
have major elements that appear in a different order.  I solve this  
by saving up Java variables as I parse and then calling a single  
template from one rule.

Dunno if this helps.  I'm learning bigtime, and I confess I don't  
really know how to use the implicit string templates returned by  
individual rules.

-- Pete



On Dec 13, 2007, at 1:37 PM, ima_band at comcast.net wrote:

> Hi,
>
> To my sheer delight I am generating tons of code with ANTLR without  
> having to deal with the more complex AST trees. But since I'm using  
> the template option all of my rules return an implicit string  
> template, right? Trouble is, unless I'm mistaken, this ties you  
> into having template output that mimics the hierarchy (i.e.  
> nesting) of your input data. When I created a DataObject wrapper it  
> worked fine but my Bean implementation needed field name and type  
> data in two separate parts of the file. At first I imagined being  
> able to invoke 2 different templates from my columnDef rule and  
> pass them up and into my outermost template as two separate  
> parameters. As far as I can tell that is not possible. Instead I  
> created an aggregate FieldDef class in the @members area and added  
> them to a fieldDefList as columns where recognized down in the  
> columnDef rule then passed the list of aggregates to the outermost  
> template and leveraged StringTemplates var ious i terator features.
>
> I was forced to abandon what I thought was the stated intent of the  
> template option (bubbling up STs) and piece together a bunch of  
> Java code instead. Don't get me wrong, applying templates to multi- 
> valued attributes is pretty cool and gets me where I want to be.  
> But if you could emit multiple STs for the same rule then you could  
> simply pass them to the outermost template and stick them where  
> they need to be! No muss. No fuss. No unneeded class definitions.
>
> Thoughts?
>
> Jeff
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20071214/e61c2094/attachment.html 


More information about the antlr-interest mailing list