abstract syntax tree - How to store AST Children information(python) -
i relatively new python , pycparser. have parsed c file ast using c-to-c.py file https://github.com/eliben/pycparser. trying make cfg using ast unable store information in .show() string. how go storing .show() information, have tried use test=ast.children()[0][1].show()
when try print test
out states "none". there way of storing it? or there method can use read through .show() information. thank you.
def show(self, buf=sys.stdout, offset=0, attrnames=false, nodenames=false, showcoord=false, _my_node_name=none): """ pretty print node , attributes , children (recursively) buffer. buf: open io buffer node printed. offset: initial offset (amount of leading spaces) attrnames: true if want see attribute names in name=value pairs. false see values. nodenames: true if want see actual node names within parents. showcoord: want coordinates of each node displayed. """ lead = ' ' * offset if nodenames , _my_node_name 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)) n in self.attr_names] attrstr = ', '.join('%s=%s' % nv nv in nvlist) else: vlist = [getattr(self, n) n in self.attr_names] attrstr = ', '.join('%s' % v v in vlist) buf.write(attrstr) if showcoord: buf.write(' (at %s)' % self.coord) buf.write('\n') (child_name, child) in self.children(): child.show( buf, offset=offset + 2, attrnames=attrnames, nodenames=nodenames, showcoord=showcoord, _my_node_name=child_name)
as can see documentation string, show
takes buf
parameter print representation. default it's sys.stdout
can pass own.
for full flexibility can use stringio - let grab output string.
Comments
Post a Comment