Abstract Syntax Tree conundrum

Lonnie Princehouse fnord at u.washington.edu
Fri Jan 2 14:14:25 EST 2004


Hello,

It looks like the compiler and parser modules both implement Python
ASTs.  Is one of these preferred over the other, or do they play
different roles?  What I'm trying to do is this-

(1) Inspect a block of python source
(2) Execute selected statements from the source

The compiler's AST implementation seems better for (1), but I can't
for the life of me figure out how to compile it into a code object,
and there's not even an apparent way to convert it back into source (I
suppose I could write a function to do this, but it seems like there
should be a better way).
On the other hand, the parser's AST implementation can easily be
executed, but for inspection it's not so good--- there's the extra
hassle of translating all of the numeric symbols.

Decompyle might be useful, but it's tricky to use third party modules
at work...

Any suggestions are heartily appreciated =)  And Happy New Year.

-ljp




For reference,

compiler AST:
-------------
Module(None, Stmt([Function('function1', ['x', 'y', 'z'],
[Const(3.0)], 0, None,Stmt([Return(Mul((Mul((Name('x'), Name('y'])),
Function('sumall', ['args'], [], 4, None,
Stmt([Return(CallFunc(Name('reduce'), [Lambda(['a', 'b'], [], 0,
Add((Name('', Name('args')], None, None))]))]))

parser.st AST of the same code:
-------------------------------
[257, [264, [285, [259, [1, 'def'], [1, 'function1'], [260, [7, '('],
[261,[262, [1, 'x']], [12, ','], [262, [1, 'y']], [12, 'z']], [22,
'='], [292, [293, [294, [295, [297, [298, [299, [300, [301, [302,
[303, [304, [305, [2, '3.0']]]]]]]]]]]]]]],':'], [291, [4, ''], [5,
''], [264, [265, [266, [272, [275, [1, 'return'], [313, [292, [293,
[294, [295, [297, [298, [299, [ [303, [304, [305, [1, 'x']]]], [16,
'*'], [303, [304, [305, [1, 'y']]]], [16, '*'], [303, [304, [305, [1,
'z']]]]]]]]]]]]]] [6, '']]]]], [264, [285, [259, [1, 'def'], [1,
'sumall'], [260, [7, '('], [261, [16, '*'], [1, 'args']], [8, ')']],
[11, ':], [5, ''], [264, [265, [266, [272, [275, [1, 'return'], [313,
[292, [293, [294, [295, [297, [298, [299, [300, [301, [302, [ [1,
'reduce']], [308, [7, '('], [317, [318, [292, [307, [1, 'lambda'],
[261, [262, [1, 'a']], [12, ','], [262, [1, 'b']]], [293, [294, [295,
[297, [298, [299, [300, [301, [302, [303, [304, [305, [1, 'a']]]]],
[14, '+'], [302, [303, [304, [305, [1]]]]]], [12, ','], [318, [292,
[293, [294, [295, [297, [298, [299, [300, [301, [302, [303, [304,
[305, [1, 'args']]]]]]]]]]]]]]]]]]]]]]]]]]], [4, '']]], [6, '']]]]],
[4, ''], [0, '']]



More information about the Python-list mailing list