[antlr-interest] saving, duplicating and substituting tree nodes in the C API
Jim Idle
jimi at temporal-wave.com
Sun Sep 6 07:53:02 PDT 2009
David Minor wrote:
> I'm working on a language that allows assignment of functions to
> variables.
> Something like this.
> fun = execute(params);
>
> Later in the same scope if I want to do.
> another_fun = fun
>
> The most natural way to do this
> seems to me to save the tree from the first execute(params) in a
> symbol table
> and then, while generating the AST, when I see that "fun" is defined,
> to substitute a duplicate of it's tree for the assignment to another_fun.
That doesn't sound like the correct way to do things to be honest, the
AST is just the parsed elements and you probably need to do a lot more
with these than just store them? Linguistically, that looks very
confusing, you shoudl probably use a different operator than = :-)
>
> The trouble is the symbol table is in C world not Antlr. It looks
> like I could just duplicate the node save
> the pointer and then re-use it, but I don't see any examples of how to
> do this.
>
> Does anyone have an idea? Even a Java example would be helpful.
>
What are you trying to do/build? An interpreter? Basically, you should
do as little as possible in the parser other than the basics such as
build the symbol table if that is a a possibility (you know what all the
types are while you are parsing etc). Generally, you would not want to
duplicate the sub-tree for a symbol table but you can just reuse the
pointer anyway as it is returned by the rule that parses your function.
$rule.tree should do it I think.
Jim
More information about the antlr-interest
mailing list