Abstract Syntax Tree conundrum

  • Thread starter Lonnie Princehouse
  • Start date
L

Lonnie Princehouse

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, '']]
 
A

Andrew Dalke

Lonnie Princehouse:
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,

Does this help?
Traceback (most recent call last):
File "<pyshell#20>", line 1, in -toplevel-
a
NameError: name 'a' is not definedModule(None, Stmt([Assign([AssName('a', 'OP_ASSIGN')], Add((Const(3),
Const(4))))]))
The compiler module is somewhat cumbersome (eg, the need to force
a 'filename' attribute above) and woefully underdocumented, but much
better to use than the results of the parser structure.
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).

That one I don't know about. decompyle seems like your best bet.

Andrew
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top