其他分享
首页 > 其他分享> > ast模块使用哪种类型的树遍历?

ast模块使用哪种类型的树遍历?

作者:互联网

ast使用哪种类型的树遍历(特别是ast.NodeVisitor())?当我创建一个堆栈并将被遍历的每个节点推入堆栈时,结果似乎是“广度优先”的树遍历.这意味着顺序取决于树中的级别.

例如树看起来像

Module
  Assign
    Name
      Store
    Call
      Attribute
        Str
        Load

堆栈看起来像

[Module,Assign,Name,Call,Store,Attribute,Str,Load]

例如码

stack = []
class a(ast.NodeTransformer):
    def visit_Num(self,node):
        stack.append(node)
        ...
        return node

    ...                      #this is all the other visit_*() functions

    def visit_Str(self,node):
        stack.append(node)
        ...
        return node

if __name__ == "__main__":
    with open('some_file.py','r') as pt:
        tree = ast.parse(pt)
    new_tree = a()
    new_tree_edit = ast.fix_missing_locations(new_tree.visit(tree)) # I have tried with and without calling fix_missing_locations and got the same results.
    print stack

解决方法:

ast.walk()函数使树呼吸优先.查看ast.py source

def walk(node):
    """
    Recursively yield all descendant nodes in the tree starting at *node*
    (including *node* itself), in no specified order.  This is useful if you
    only want to modify nodes in place and don't care about the context.
    """
    from collections import deque
    todo = deque([node])
    while todo:
        node = todo.popleft()
        todo.extend(iter_child_nodes(node))
        yield node

新节点被推入队列,下一个被遍历的节点是队列的前端.

如果要进行深度优先遍历,请改用ast.NodeVisitor()的子类;它将使用递归在树上行走;除非定义了更多特定于节点的访问者方法,否则NodeVisitor.visit()调用NodeVisitor.generic_visit(),而NodeVisitor.generic_visit()再次为子节点调用NodeVisitor.visit():

class NodeVisitor(object):
    """
    A node visitor base class that walks the abstract syntax tree and calls a
    visitor function for every node found.  This function may return a value
    which is forwarded by the `visit` method.

    This class is meant to be subclassed, with the subclass adding visitor
    methods.

    Per default the visitor functions for the nodes are ``'visit_'`` +
    class name of the node.  So a `TryFinally` node visit function would
    be `visit_TryFinally`.  This behavior can be changed by overriding
    the `visit` method.  If no visitor function exists for a node
    (return value `None`) the `generic_visit` visitor is used instead.

    Don't use the `NodeVisitor` if you want to apply changes to nodes during
    traversing.  For this a special visitor exists (`NodeTransformer`) that
    allows modifications.
    """

    def visit(self, node):
        """Visit a node."""
        method = 'visit_' + node.__class__.__name__
        visitor = getattr(self, method, self.generic_visit)
        return visitor(node)

    def generic_visit(self, node):
        """Called if no explicit visitor function exists for a node."""
        for field, value in iter_fields(node):
            if isinstance(value, list):
                for item in value:
                    if isinstance(item, AST):
                        self.visit(item)
            elif isinstance(value, AST):
                self.visit(value)

如果您对NodeVisitor(或其派生版本NodeTransformer)进行子类化,请记住也要在您的特定visit_ *方法中调用super(YourClass,self).generic_visit(node)以继续遍历树.

标签:tree-traversal,abstract-syntax-tree,stack,python
来源: https://codeday.me/bug/20191121/2055641.html