1

I need to extract types and dependencies flow for python code.

E.g: for the following code -

x = 1 + 2

y = x

m = y.someFunc("123")

I want to say that:

x is a Number

y is dependent in x (hence it is a Number too)

m is dependent in y and in "123" (hence dependent in Number and String) and its type is y.someFunc return value which is unknown

I figured out that the best choice will probably be using the ast library to parse the code and then traverse it using the visitor pattern..

There is a tool \ implementation that does the same thing or something similar that i can adjust for my needs?

Thanks!

Community
  • 1
  • 1
meitalbs
  • 81
  • 5
  • You'll need a lot more than just an AST. You'll have to know which identifier is which, you'll need to know the control flow (y isn't the same as x, unless the assignment to x dominates the assignment to y with no other intervening assignments to x) and more generally you'll need to do some kind of object-based dataflow. That's a lot of machinery to have in place before you even start your analysis. – Ira Baxter Dec 03 '14 at 11:13
  • It should not be perfect, just cover most cases. I am working with snippets, not full programs, so maybe it may be little easier? – meitalbs Dec 04 '14 at 07:54
  • There are virtually no static analyses which are perfect; Turing machines are involved. So your answer will either be approximate or conservative. If your snippets are big enough to include even a few statements you have pretty much all the troubles of full programs: scopes, function calls, conditionals, loops, ... [You actually have Turing troubles with just "assignment", "addition", "compare", and "loop"). Your first question should be, "what degree of inaccuracy can you tolerate, and why?" – Ira Baxter Dec 04 '14 at 11:10
  • Consider this code snippet: begin; x=bar(); if foo() then x=baz(); end; with bar and baz returning different types (which means we have to already know what types *they* have, and we're talking Python here). Let's throw in the possibility that bar and baz may throw exceptions. What's the type of x at the end? – Ira Baxter Dec 04 '14 at 12:31

0 Answers0