[Tutor] Python Control Flow Graph

Brian C. Lum bclum at cs.ucsd.edu
Tue Mar 21 10:23:26 CET 2006


Dear Python Tutors,

I have been looking for a good way to convert python code into a control 
flow graph.

I know of Python functions that will convert an expression into an 
abstract syntax tree (i.e. ast = parser.expr('(x+5)*5') then t = 
ast.totuple() then t), but I am not sure how to obtain a CFG.

I've gone through the compiler and it has code that converts the AST into 
a CFG (described here: 
http://www.python.org/doc/peps/pep-0339/#ast-to-cfg-to-bytecode). 
Basically, PyAST_Compile() in Python/compile.c coverts the AST to a CFG 
and outputs final bytecode from the CFG by calling two functions: 
PySymtable_Build() in Python/symtable.c and compiler_mod() in 
Python/compile.c.  PySymtable_Build() will build a symtable and 
compiler_mod() will create the CFG.

I can try to hack with the compiler, but I was wondering if anyone knew of 
any tools already out there or any easier way.

Thanks,
Brian



More information about the Tutor mailing list