ASTVisitor

Peter Maas peter.maas at mplusr.de
Thu Apr 10 08:58:36 EDT 2003


I am trying to employ ASTVisitor to convert a python expression to
postfix form. Following the documentation, I created my own visitor
class with methods called visit<class>, where <class> represents a
syntax node like And, Or, ...:

class cVisitor:
     def printNode(self, node, level):
         ....
     visitExpression = printNode
     visitOr = printNode
     visitAnd = printNode
     ...

When using the visitor

cv = cVisitor()
av = compiler.visitor.ASTVisitor()
print av.preorder(tree, cv)

only the Expression node was handled, i.e. the code above didn't
descend down the tree. According to the docs the return value
of visit... should decide wether to visit the node's subtree but
my code didn't bother.

The code of the ASTVisitor class (methods preorder, dispatch)
shows no indication of tree traversal which would explain my
failure.

Have I missed something or have I found a dark corner of python
with some cobwebs? :) I can do the recursion myself but I thought
this to be the responsibility of ASTVisitor. I would appreciate
hints to working example code. Until now I haven't found any on
the web.

Mit freundlichen Gruessen,

Peter Maas

-------------------------------------------------------------------
Peter Maas, M+R Infosysteme, D-52070 Aachen, Hubert-Wienen-Str. 24
Tel +49-241-93878-0 Fax +49-241-93878-20 eMail peter.maas at mplusr.de
-------------------------------------------------------------------





More information about the Python-list mailing list