我在python中编写了递归函数来遍历Eclipse中由JDT编译器生成的AST 我将在AST树上遍历此图像: This image 我得到左边的Tree并编写了此函数:
ast ='MethodDeclaration SimpleType SimpleName String SingleVariableDeclaration SimpleType SimpleName id SimpleName Integer Block ExpressionStatement MethodInvocation SimpleName Log SimpleName debug SimpleName Extracting_method_with_ID:{} SimpleName id ReturnStatement MethodInvocation SimpleName request SimpleName remove SimpleName id SimpleName extractFor'
def traverseTree(token,i):
if i == len(token)-1:
return ''
else:
i += 1
return '('+token[0]+traverseTree(token[1:],i)+')'
print traverseTree(astlist,0)
结果: (MethodDeclaration(SimpleType(SimpleName(String(SingleVariableDeclaration(SimpleType(SimpleName(id(SimpleName(Integer(Block(ExpressionStatement(MethodInvocation(MethodInvocation(SimpleName(Log)))))
与正确的图片不同, 我感到困惑,我无法处理用户定义的任何字符串或标识符,并将其放在该图像This image
上的正确部分中的正确位置