pythoncstaticanalysispycparser

Parsing variable dependency with Pycparser


I'm currently working on a project to find variable dependencies in a given function for I/O, and have chosen to work with Pycparser. My code right now is mostly just one visitor-class, and a few node-classes. What it does is it traverses the AST and creates nodes for each variable assignment, declaration, input, output and If-statements. These nodes have list-attributes where their dependencies are stored, for example:

a = b + c/2;

would create a node with name 'a', and connect it to the most recent node of 'b' and 'c'.

My question is from here: is there a general idea about how I would go connecting an output variable?

return b;

This code does not give us any info on what dependencies it has apart from maybe the latest instances of 'b'. However, all solutions I could come up with would also add previous nodes of 'b'. Example:

b = 3;
b = b + 4;

I would not want both of those 'b' nodes for my output dependency. But I would want multiple nodes if there are multiple paths taken from If-statements, but only the most recent one in each path. If you find the question vague, are there any methods or sources about finding variable dependencies you can recommend? In short, what this project should achieve is for output x (there could be multiple outputs) I want to be able to find out which inputs it relies on. This could in turn be multiple scenarios depending on other variables. Example of different dependencies:

int function(int a, int b, int c, int d, bool z){
    int x;
    if(z){
       x = a + b;
    }
    else{
        x = c + d;
    }

    return x;

Solution

  • I just found out that what I've been trying to achieve is impossible with the method I have been using. What the AST and Pycparser can provide me with is all variables used to determine the value of a variable.

    To proceed with my problem, you would have to look into creating a CFG (Control Flow Graph) aswell as Data Flow. This is however another method and result entirely.