[antlr-interest] best method of outputting multiple templates from AST?
Terence Parr
parrt at cs.usfca.edu
Mon Oct 16 17:35:10 PDT 2006
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