[Compiler-sig] AST observations
Eric C. Newton
ecn@metaslash.com
Thu, 18 Apr 2002 07:14:08 -0400
Yes, I did a lot of this, too.
I gave up when I wanted to re-use something like a "find all Name
Nodes" visitor, and then look up the parents later.
I can certainly keep a reverse map. I'm already keeping a reverse map
from symbol to node, too.
On Thu, Apr 18, 2002 at 09:37:52AM +0000, Finn Bock wrote:
> [Eric C. Newton]
>
> >I gave up on trying to use recursion to figure out a Node's parents in
> >an AST tree. Very often I need to know the parents of the Node I'm
> >looking at, and using recursion to hold this information was becoming
> >cumbersome.
>
> If you only need the ancestry of the current node in the visitXXXX
> methods, then the open_level(), close_level() visitor methods that I
> suggested previously should work nicely:
>
> class MyVisitor(ASTVisitor):
> def __init__(self):
> self.ancestry = []
>
> def open_level(self, node):
> self.ancestry.append(node)
>
> def close_level(self, node):
> self.ancestry.pop()
>
> def visitFunctionDef(self, node):
> print "parent is", self.ancestry[-2]
>
>
> regards,
> finn