我使用 PycParser 为 C 函数生成抽象语法树,但我仍在尝试弄清楚如何将解析树转换为字符串:
from pycparser import c_parser
src = '''
int hello(a, b){
return a + b;
}
'''
ast = c_parser.CParser().parse(src)
aString = ast.show()
print(aString) #This prints "None" instead of printing the parse tree
是否可以从已生成的解析树中生成一个字符串(或字符串数组)?
这是打印的抽象语法树,但我不知道如何将其转换为字符串:
FileAST:
FuncDef:
Decl: hello, [], [], []
FuncDecl:
ParamList:
ID: a
ID: b
TypeDecl: hello, []
IdentifierType: ['int']
Compound:
Return:
BinaryOp: +
ID: a
ID: b