Fixed bug in extractDefs & extractUses
This commit is contained in:
10
run.sh
10
run.sh
@@ -1,5 +1,11 @@
|
||||
#!/bin/bash
|
||||
|
||||
if [ "$1" = "clean" ]; then
|
||||
rm -rf bin
|
||||
shift
|
||||
fi
|
||||
|
||||
|
||||
if [ "$#" -lt 1 ]; then
|
||||
echo "Usage: ./run.sh <file> [line]"
|
||||
echo "Example: ./run.sh examples/Example1.java 3"
|
||||
@@ -19,9 +25,9 @@ if [ ! -d "bin/pdg" ]; then
|
||||
CP="lib/antlr-4.13.2-complete.jar"
|
||||
javac -d bin -cp "$CP" src/org/lsmr/cfg/*.java || exit 1
|
||||
javac -d bin -cp "$CP:bin" src/pdg/PDG.java || exit 1
|
||||
javac -d bin -cp "$CP:bin" src/CFGBuilder.java src/PDGTool.java || exit 1
|
||||
javac -d bin -cp "$CP:bin" src/CFGBuilder.java src/CIA.java || exit 1
|
||||
echo "Build complete!"
|
||||
fi
|
||||
|
||||
# Run
|
||||
java -cp "bin:lib/antlr-4.13.2-complete.jar" PDGTool "$@"
|
||||
java -cp "bin:lib/antlr-4.13.2-complete.jar" CIA "$@"
|
||||
|
||||
@@ -77,4 +77,5 @@ public class CFGBuilder {
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -43,6 +43,7 @@ public class CIA {
|
||||
|
||||
// Step 3: Show results
|
||||
System.out.println("\n=== Available Lines ===");
|
||||
|
||||
List<Integer> lines = cfg.getAllLineNumbers();
|
||||
System.out.println("Lines with statements: " + lines);
|
||||
|
||||
|
||||
@@ -2,6 +2,8 @@ package pdg;
|
||||
|
||||
import org.lsmr.cfg.*;
|
||||
import java.util.*;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Simple Program Dependence Graph (PDG)
|
||||
@@ -66,10 +68,11 @@ public class PDG {
|
||||
defs.put(label, extractDefs(label));
|
||||
uses.put(label, extractUses(label));
|
||||
}
|
||||
System.out.println(defs.toString());
|
||||
System.out.println(uses.toString());
|
||||
|
||||
// 2. Compute reaching definitions
|
||||
Map<String, Map<String, Set<String>>> reaching = computeReachingDefinitions(defs);
|
||||
// TODO: check what happens whena node has multiple variable definitions
|
||||
|
||||
// 3. Build data dependencies
|
||||
for (Node node : cfg.nodes()) {
|
||||
@@ -94,24 +97,31 @@ public class PDG {
|
||||
*/
|
||||
private Set<String> extractDefs(String statement) {
|
||||
Set<String> vars = new HashSet<>();
|
||||
|
||||
// Skip special nodes
|
||||
if (statement.startsWith("*"))
|
||||
return vars;
|
||||
|
||||
if (statement.contains("=") && !statement.contains("==")) {
|
||||
String[] parts = statement.split("=");
|
||||
if (parts.length > 0) {
|
||||
String lhs = parts[0].trim();
|
||||
String[] tokens = lhs.split("\\s+");
|
||||
if (tokens.length > 0) {
|
||||
String varName = tokens[tokens.length - 1];
|
||||
varName = varName.replaceAll("[\\[\\].();,]", "");
|
||||
if (!varName.isEmpty() && Character.isJavaIdentifierStart(varName.charAt(0))) {
|
||||
vars.add(varName);
|
||||
}
|
||||
// Debug proper variabel naem selection:
|
||||
// System.out.printf("Statement: %s", statement);
|
||||
// print anything before "="
|
||||
// System.out.println(parts[0]);
|
||||
|
||||
// retrieve teh first "word" before equals
|
||||
Pattern p = Pattern.compile("(\\w+)\\s*$");
|
||||
Matcher m = p.matcher(parts[0]);
|
||||
String varLabel = "-1";
|
||||
|
||||
if (m.find()) {
|
||||
varLabel = m.group(1);
|
||||
}
|
||||
if ((!varLabel.isEmpty() || varLabel == "-1") && Character.isJavaIdentifierStart(varLabel.charAt(0))) {
|
||||
// print variable to be assigned
|
||||
// System.out.println(varLabel);
|
||||
vars.add(varLabel);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (statement.contains("for") && statement.contains("(")) {
|
||||
@@ -143,20 +153,25 @@ public class PDG {
|
||||
*/
|
||||
private Set<String> extractUses(String statement) {
|
||||
Set<String> vars = new HashSet<>();
|
||||
System.out.printf("Uses Statement: %s \n", statement);
|
||||
// make sure we only work on the rhs
|
||||
int eq = statement.indexOf('=');
|
||||
if (eq != -1) {
|
||||
statement = statement.substring(eq + 1);
|
||||
}
|
||||
// Split on any non-identifier character (keeps letters/digits/_ together)
|
||||
String[] tokens = statement.split("[^a-zA-Z0-9_]+");
|
||||
|
||||
if (statement.startsWith("*"))
|
||||
return vars;
|
||||
String[] tokens = statement.split("[\\s+\\-*/=<>!&|(){}\\[\\];,.]");
|
||||
for (String token : tokens) {
|
||||
token = token.trim();
|
||||
if (!token.isEmpty() &&
|
||||
Character.isJavaIdentifierStart(token.charAt(0)) &&
|
||||
!isKeyword(token) &&
|
||||
!token.matches("\\d+")) {
|
||||
if (!token.isEmpty()
|
||||
&& Character.isJavaIdentifierStart(token.charAt(0))
|
||||
&& !isKeyword(token)
|
||||
&& !token.matches("\\d+")) {
|
||||
System.out.println("Token to add:");
|
||||
System.out.println(token);
|
||||
vars.add(token);
|
||||
}
|
||||
}
|
||||
vars.removeAll(extractDefs(statement));
|
||||
|
||||
return vars;
|
||||
}
|
||||
@@ -169,7 +184,8 @@ public class PDG {
|
||||
"if", "else", "while", "for", "do", "return", "break", "continue",
|
||||
"int", "double", "float", "char", "boolean", "void", "String",
|
||||
"new", "null", "true", "false", "class", "public", "private",
|
||||
"static", "final", "this", "super", "try", "catch", "throw", "throws"));
|
||||
"static", "final", "this", "super", "try", "catch", "throw", "throws", "EXIT", "block", "ENTRY",
|
||||
"THROWN"));
|
||||
return keywords.contains(word);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,27 +1,28 @@
|
||||
public class Example1 {
|
||||
public void test() {
|
||||
int x = 5;
|
||||
int y = x + 10;
|
||||
int z = y * 2;
|
||||
int result = z + x;
|
||||
}
|
||||
public void test1(int a) {
|
||||
int x = 5;
|
||||
int y = 0;
|
||||
// public void test() {
|
||||
// int x = 5;
|
||||
// int y = x + 10;
|
||||
// int z = y * 2;
|
||||
// int result = z + x;
|
||||
// }
|
||||
// public void test1(int a) {
|
||||
// int x = 5;
|
||||
// int y = 0;
|
||||
|
||||
if (a > 10) {
|
||||
y = x + a;
|
||||
} else {
|
||||
y = x - a;
|
||||
}
|
||||
// if (a > 10) {
|
||||
// y = x + a;
|
||||
// } else {
|
||||
// y = x - a;
|
||||
// }
|
||||
|
||||
int result = y * 2;
|
||||
}
|
||||
// int result = y * 2;
|
||||
// }
|
||||
public void test2() {
|
||||
int sum = 0;
|
||||
int i = 0;
|
||||
|
||||
while (i < 10) {
|
||||
i = 2;
|
||||
sum = sum + i;
|
||||
i = i + 1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user