在Java中跟踪控制结构进行静态代码分析(可用表达式)

发布于 2025-01-21 12:17:46 字数 3523 浏览 1 评论 0原文

我正在Java中编写一个程序,该程序在给定的Java程序上进行可用的表达分析。我设法提取生成和杀死集。为了使它变得简单,我正在使用一个约束,该约束只会分析一行声明一个变量的程序(REGEX表达式可用于声明和初始化,但是,重新定义变量的逻辑仍在进行中。这是原因为什么它无法正确检测到杀戮集)。

我现在面临的问题是如何跟踪循环?有条件的陈述?所有控制结构?就像while,for for for和if-else语句。

有人可以帮忙吗?该用什么?如何跟踪路径?以下是代码文件。

您可以提供的任何帮助将不胜感激。谢谢你!

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;
    }
    
}

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;
    }
    
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文