[antlr-interest] best method of outputting multiple templatesfrom AST?

Hill, Robert rhill03 at eds.com
Mon Oct 16 23:27:46 PDT 2006


Thanks Ter!
	I'd ended up settling on that because after another hour of
reflecting about neater ways came to the conclusion that it was the only
choice, and it doesn't actually make much sense to try to return chunks
of code/templates from rules when they're not going to be in the order
the token came in. You hit the nail on the head - "decoupling the output
from the order of tree traversal", pretty much what I ended up with :) I
thought I may have been missing something, or some syntax (other than
returns [String Template blah]) on the rules to allow this. I was
thinking , just how much of the stuff I've done that isn't on the wiki
anywhere.... can I pre-order your book? LOL! 
And I've lost count of the number of times I've typed in 
myArraylist.size	instead of .size() <sigh> I miss those c#
properties! :(

Ok :) sweet, im back in business!

Ta very much!

Rob

--
Rob Hill
EDS - Hallamshire Business Park
F1E/045
Sheffield	
T:	+44 (0) 114 291 1928
M:	+44 (0) 791 732 1227
E:	rhill03 at eds.com

 

>-----Original Message-----
>From: antlr-interest-bounces at antlr.org 
>[mailto:antlr-interest-bounces at antlr.org] On Behalf Of Terence Parr
>Sent: 17 October 2006 01:35
>To: ANTLR Interest
>Subject: Re: [antlr-interest] best method of outputting 
>multiple templatesfrom AST?
>
>
>On Oct 16, 2006, at 3:47 PM, Robert Hill wrote:
>
>> 	
>> If have a fair few rules which must output bits of code in more than 
>> one place in the final output file, whats the best way to do this?
>> Eg, say im generating a kind of loop and I need to add a variable to 
>> the start of a function method but need to generate the code further 
>> down in the body.
>
>Hi Rob,
>
>I keep a ptr around to the template for the function and then 
>when I see a loop that needs a var I do
>
>functionST.setAttribute("implicitlyDefinedVarList", "foo");
>
>to add "foo" as a var for which I need a define.  That could 
>also be a template not just a string.
>
>> I guess what I'd like would be something like the following.
>>
>> Rule
>> :	^(blah1 blah2)
>> -> template1(id={blah}) , template2(pos={blah2)
>
>Think of reaching up and sticking the local var def where it 
>belongs instead of having to return multiple things.
>
>>
>> Also I'd like to accumulate a bunch of these templates In a single  
>> block
>> somewhere to keep them together, nomatter where I come across them  
>> in my
>> language, do I just declare a single template in java code 
>as a parser
>> member and fill it in from all the rules?
>
>Yep.  Imagine you want to track class refs so you can add imports.   
>The import "hole" is at the top of the output file but you can fill  
>from anywhere.  This is the key thing that decouples the order of  
>tree traversal with order of output.
>
>Ter
>


More information about the antlr-interest mailing list