How to write the Visitor Pattern for Abstract Syntax Tree in Python?
Posted
by bodacydo
on Stack Overflow
See other posts from Stack Overflow
or by bodacydo
Published on 2010-03-26T18:14:07Z
Indexed on
2010/03/26
18:23 UTC
Read the original article
Hit count: 551
My collegue suggested me to write a visitor pattern to navigate the AST. Can anyone tell me more how would I start writing it?
As far as I understand, each Node in AST would have visit()
method (?) that would somehow get called (from where?). That about concludes my understanding.
To simplify everything, suppose I have nodes Root
, Expression
, Number
, Op
and the tree looks like this:
Root
|
Op(+)
/ \
/ \
Number(5) \
Op(*)
/ \
/ \
/ \
Number(2) Number(444)
Can anyone think of how the visitor pattern would visit this tree to produce output:
5 + 2 * 444
Thanks, Boda Cydo.
© Stack Overflow or respective owner