2017-04-07 6 views
0

私は比較的新しいpythonとpycparserです。私は既にhttps://github.com/eliben/pycparserのc-to-c.pyファイルを使用して、cファイルをASTに解析しました。私は現在、ASTを使用してCFGを作成しようとしていますが、文字列として.show()の情報を格納できません。どのように私はこの.show()情報を保存しようとすると、私はtest=ast.children()[0][1].show()を使用しようとしましたが、testを印刷しようとすると "none"と表示されます。それで、それを保存する別の方法がありますか?または、.show()情報を読み取るために使用できる別のメソッドがあります。ありがとうございました。あなたはその説明文字列から見ることができるようにAST子供情報(Python)を保存する方法

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) 

答えて

0

は、showは、それが表現を印刷する先のbufパラメータを取ります。デフォルトではsys.stdoutですが、自分で渡すことができます。

StringIOを柔軟に使用できるようにすると、出力を文字列にすることができます。

関連する問題