0

I have been trying to figure out what is the correct approach for retrieving a control flow graph for a Python code, I'm looking for similar output as I got with a gcc output on a c program (I have used the gcc -fdump-tree-cfg).

I'll give a short example for clarifying my intentions, for example given the following simple c source code:

fib function in c

I have got the control flow graph which looks like this:

cfg generated from gcc

The cfg output consists the original c keywords and goto instructions, instead of loops (for, while etc..), which is great for my purpose.

So for the past few days I was looking into implementing a cfg generator for a python code, delved into AST module (read: Green Tree Snakes ) but it looks the AST parses each keyword in great detail but I would like to avoid it . I'm more interested in focusing on the flow of the original keywords taken from the original python script.

Does anybody has any suggestion, how to achieve it?

Thank you all!

JammingThebBits
  • 732
  • 11
  • 31
  • Why do you care if the AST has a lot of detail? I thought I provided you a scheme to do this: http://stackoverflow.com/a/39720615/120163 that given an AST produced the CFG. You can pretty much ignore most of the details of the CFG (e.g., what's in an expression) if it doesn't affect control flow . [I think expressions do, so you need that detail]. What was not adequate about that scheme? – Ira Baxter Oct 04 '16 at 04:09
  • ... if you attempt to extract the control flow from the "original keywords" (you mean text?) mostly what you will do is reinvent parsing badly. The AST is the result of parsing, done well. – Ira Baxter Oct 04 '16 at 04:12
  • Ira Baxter - eventually I have retrieved the relevant lines (according to lineno) from the text while traversing on the AST tree (I have over-ride ast.visit with a subclass) p.s I have read your scheme and this wiki (https://en.wikipedia.org/wiki/Basic_block), thank you. – JammingThebBits Oct 05 '16 at 14:58

0 Answers0