Is it possible to do a postorder traversal on an instance of ast.NodeVisitor in Python just by manipulating the ast.NodeVisitor.generic_visit()? I did this:
class ExpParser(ast.NodeVisitor):
def generic_visit(self, node):
for x in ast.iter_child_nodes(node):
ast.NodeVisitor.generic_visit(self, x)
ast.NodeVisitor.generic_visit(self, node)
def visit_BinOp(self, node):
print type(node.op).__name__
def visit_Name(self, node):
print node.id
if __name__ == '__main__':
node = ast.parse("T1+T2*T3")
v = ExpParser()
v.visit(node)
this gave me:
T1
T2
T3
Mult
Add
I want it to give me:
T2
T3
Mult
T1
Add
How can I do it? please I'm stuck.