Tracking Control Structures in Java for Static Code Analysis (Available Expressions)

66 Views Asked by At

I am writing a program in Java that does Available expression Analysis on a given Java Program. I have managed to extract Generate and Kill set. To keep it simple, I am going with a constraint that will only analyze programs that declare ONE VARIABLE IN ONE LINE (The regex expression works for it with declaration and initialization but, the logic for redefinition of variables is still in progress. This is the reason why it won't correctly detect the kill set).

The problem I am facing right now is that how would I track the loops? conditional statements? all the control structures? like While, for, and if-else statements.

Can anyone please help? what to use for this? how to keep track of the paths? Below are the code files.

Any assistance you can provide would be greatly appreciated. Thank you!

ReadFile.java

   // Import the File class
import java.io.File; 
 
// Import this class to handle errors
import java.io.FileNotFoundException;
import java.util.ArrayList;
// Import the Scanner class to read text files
import java.util.Scanner; 
import java.util.Stack;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ReadFile 
{
    public static void main(String[] args) {
        try {
        // Creating an object of the file for reading the stmt
        File myObj = new File("./Files/Test.txt");  
    
        ArrayList<State> stmts = new ArrayList<State>();

        String gen="{ }";
        String kill="{ }";
        Scanner myReader = new Scanner(myObj);
        int i=0;
        while (myReader.hasNextLine()) 
        {
            String stmt = myReader.nextLine();
            boolean def=true;

            if(stmt.contains("class") || stmt.contains("{") || stmt.contains("}") || stmt.contains("args[]") || stmt.startsWith("//"))
            {
                continue;
            }
             
                    
            Pattern p = Pattern.compile("^(int|long|float|double)\\s(([\\_\\w\\d])+)");
            
            Matcher m = p.matcher(stmt.trim());


            while(m.find())
            {
                kill = m.group(3);
            }

            if(stmt.contains("="))
            {
                String[] exp = stmt.split("=");
                
                
                for(int l=0; l<exp.length;l++)
                {
                    
                    if(exp[l].contains("+") || exp[l].contains("-") || exp[l].contains("*") || exp[l].contains("/"))
                    {
                        gen = exp[l];       
                    }
                }
                
            stmts.add(new State(new ArrayList<String>(), i, false, kill,gen,stmt));
            gen = "{ }";
            kill = "{ }";
            i++;
        }
        myReader.close();
        
        for (int j = 0; j < stmts.size(); j++)
        {
            System.out.println("Line Number " + stmts.get(j).lineNo);
            System.out.println("Statement " + stmts.get(j).statement);
            System.out.println("Generated " + stmts.get(j).genSet);
            System.out.println("Kill " + stmts.get(j).killSet);

        }


        
        } catch (FileNotFoundException e) {
        System.out.println("An error occurred.");
        e.printStackTrace();
        }
        
    }

}

State.java

import java.util.ArrayList;

public class State 
{
    //Elements
    public ArrayList<String> variables = new ArrayList<String>();
    public int lineNo;
    public boolean isDef;
    public String killSet;
    public String genSet;
    public String statement;
    
    //Constructor
    public State(ArrayList<String> al, int ln, boolean def, String kill, String gen, String stmt) 
    {
        for(int i=0;i<al.size();i++)
        {
            variables.add(al.get(i));
        }
        lineNo = ln;
        isDef = def;
        killSet = kill;
        genSet = gen;
        statement = stmt;
    }
    
}
0

There are 0 best solutions below