如何存储AST子项信息(python)

如何存储AST子项信息(python),python,abstract-syntax-tree,pycparser,Python,Abstract Syntax Tree,Pycparser,我对python和pycparser比较陌生。我已经使用中的c-to-c.py文件将c文件解析为AST。我现在尝试使用AST生成CFG,但无法将信息作为字符串存储在.show()中。如何存储此.show()信息,我已尝试使用test=ast.children()[0][1].show()但是,当我尝试打印test时,它会显示“无”。那么,有没有另一种存储方式呢?或者有没有其他方法可以用来读取.show()信息。多谢各位 def show(self, buf=sys.stdout, offset=

我对python和pycparser比较陌生。我已经使用中的c-to-c.py文件将c文件解析为AST。我现在尝试使用AST生成CFG,但无法将信息作为字符串存储在.show()中。如何存储此.show()信息,我已尝试使用
test=ast.children()[0][1].show()
但是,当我尝试打印
test
时,它会显示“无”。那么,有没有另一种存储方式呢?或者有没有其他方法可以用来读取.show()信息。多谢各位

def show(self, buf=sys.stdout, offset=0, attrnames=False, nodenames=False, showcoord=False, _my_node_name=None):
        """ Pretty print the Node and all its attributes and
            children (recursively) to a buffer.

            buf:
                Open IO buffer into which the Node is printed.

            offset:
                Initial offset (amount of leading spaces)

            attrnames:
                True if you want to see the attribute names in
                name=value pairs. False to only see the values.

            nodenames:
                True if you want to see the actual node names
                within their parents.

            showcoord:
                Do you want the coordinates of each Node to be
                displayed.
        """
        lead = ' ' * offset
        if nodenames and _my_node_name is not None:
            buf.write(lead + self.__class__.__name__+ ' <' + _my_node_name + '>: ')
        else:
            buf.write(lead + self.__class__.__name__+ ': ')

        if self.attr_names:
            if attrnames:
                nvlist = [(n, getattr(self,n)) for n in self.attr_names]
                attrstr = ', '.join('%s=%s' % nv for nv in nvlist)
            else:
                vlist = [getattr(self, n) for n in self.attr_names]
                attrstr = ', '.join('%s' % v for v in vlist)
            buf.write(attrstr)

        if showcoord:
            buf.write(' (at %s)' % self.coord)
        buf.write('\n')

        for (child_name, child) in self.children():
            child.show(
                buf,
                offset=offset + 2,
                attrnames=attrnames,
                nodenames=nodenames,
                showcoord=showcoord,
                _my_node_name=child_name)
def show(self,buf=sys.stdout,offset=0,attrnames=False,nodenames=False,showcoord=False,_my_node_name=None):
“”“漂亮地打印节点及其所有属性和
子项(递归)到缓冲区。
缓冲器:
打开打印节点的IO缓冲区。
抵消:
初始偏移(前导空格的数量)
属性名称:
如果要在中查看属性名称,则为True
名称=值对。如果为False,则仅查看值。
节点名称:
如果要查看实际节点名称,则为True
在他们的父母里面。
showcoord:
是否希望每个节点的坐标都是
显示。
"""
超前=“”*偏移量
如果节点名称和_我的_节点名称不是无:
buf.write(lead+self.\uuuuuu class.\uuuuuu.\uuuuuu name.\uuuuuuuuu+':')
其他:
buf.write(lead+self.\uuuuuu class.\uuuuuu.\uuuuuu name.\uuuuuuuuu+':')
如果self.attr\u名称:
如果指定名称:
nvlist=[(n,getattr(self,n))表示self.attr_名称中的n]
attrstr=',join(“%s=%s”%nv用于nvlist中的nv)
其他:
vlist=[getattr(self,n)表示self.attr\u名称中的n]
attrstr=',.join(“%s”%v表示vlist中的v)
buf.write(属性)
如果是showcoord:
buf.write(‘(在%s)’%self.coord)
buf.write(“\n”)
对于self.children()中的(child_name,child):
儿童节目(
缓冲器,
偏移量=偏移量+2,
attrnames=attrnames,
节点名称=节点名称,
showcoord=showcoord,
_我的节点名称=子节点名称)

您可以从其文档字符串中看到,
show
接受一个
buf
参数,它将向该参数打印表示。默认情况下,它是
sys.stdout
,但您可以传递自己的

为了获得充分的灵活性,您可以使用它,它可以让您将输出捕获到字符串中