Fixed bug in extractDefs & extractUses

This commit is contained in:
Nicolas Amaya
2025-12-15 18:06:48 -07:00
parent c40bdeb411
commit 046634109c
5 changed files with 65 additions and 40 deletions

10
run.sh
View File

@@ -1,5 +1,11 @@
#!/bin/bash #!/bin/bash
if [ "$1" = "clean" ]; then
rm -rf bin
shift
fi
if [ "$#" -lt 1 ]; then if [ "$#" -lt 1 ]; then
echo "Usage: ./run.sh <file> [line]" echo "Usage: ./run.sh <file> [line]"
echo "Example: ./run.sh examples/Example1.java 3" 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" 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" 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/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!" echo "Build complete!"
fi fi
# Run # Run
java -cp "bin:lib/antlr-4.13.2-complete.jar" PDGTool "$@" java -cp "bin:lib/antlr-4.13.2-complete.jar" CIA "$@"

View File

@@ -77,4 +77,5 @@ public class CFGBuilder {
} }
System.out.println(); System.out.println();
} }
} }

View File

@@ -43,6 +43,7 @@ public class CIA {
// Step 3: Show results // Step 3: Show results
System.out.println("\n=== Available Lines ==="); System.out.println("\n=== Available Lines ===");
List<Integer> lines = cfg.getAllLineNumbers(); List<Integer> lines = cfg.getAllLineNumbers();
System.out.println("Lines with statements: " + lines); System.out.println("Lines with statements: " + lines);

View File

@@ -2,6 +2,8 @@ package pdg;
import org.lsmr.cfg.*; import org.lsmr.cfg.*;
import java.util.*; import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/** /**
* Simple Program Dependence Graph (PDG) * Simple Program Dependence Graph (PDG)
@@ -66,10 +68,11 @@ public class PDG {
defs.put(label, extractDefs(label)); defs.put(label, extractDefs(label));
uses.put(label, extractUses(label)); uses.put(label, extractUses(label));
} }
System.out.println(defs.toString());
System.out.println(uses.toString());
// 2. Compute reaching definitions // 2. Compute reaching definitions
Map<String, Map<String, Set<String>>> reaching = computeReachingDefinitions(defs); Map<String, Map<String, Set<String>>> reaching = computeReachingDefinitions(defs);
// TODO: check what happens whena node has multiple variable definitions
// 3. Build data dependencies // 3. Build data dependencies
for (Node node : cfg.nodes()) { for (Node node : cfg.nodes()) {
@@ -94,24 +97,31 @@ public class PDG {
*/ */
private Set<String> extractDefs(String statement) { private Set<String> extractDefs(String statement) {
Set<String> vars = new HashSet<>(); Set<String> vars = new HashSet<>();
// Skip special nodes // Skip special nodes
if (statement.startsWith("*")) if (statement.startsWith("*"))
return vars; return vars;
if (statement.contains("=") && !statement.contains("==")) { if (statement.contains("=") && !statement.contains("==")) {
String[] parts = statement.split("="); String[] parts = statement.split("=");
if (parts.length > 0) { // Debug proper variabel naem selection:
String lhs = parts[0].trim(); // System.out.printf("Statement: %s", statement);
String[] tokens = lhs.split("\\s+"); // print anything before "="
if (tokens.length > 0) { // System.out.println(parts[0]);
String varName = tokens[tokens.length - 1];
varName = varName.replaceAll("[\\[\\].();,]", ""); // retrieve teh first "word" before equals
if (!varName.isEmpty() && Character.isJavaIdentifierStart(varName.charAt(0))) { Pattern p = Pattern.compile("(\\w+)\\s*$");
vars.add(varName); 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("(")) { if (statement.contains("for") && statement.contains("(")) {
@@ -143,20 +153,25 @@ public class PDG {
*/ */
private Set<String> extractUses(String statement) { private Set<String> extractUses(String statement) {
Set<String> vars = new HashSet<>(); 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) { for (String token : tokens) {
token = token.trim(); if (!token.isEmpty()
if (!token.isEmpty() && && Character.isJavaIdentifierStart(token.charAt(0))
Character.isJavaIdentifierStart(token.charAt(0)) && && !isKeyword(token)
!isKeyword(token) && && !token.matches("\\d+")) {
!token.matches("\\d+")) { System.out.println("Token to add:");
System.out.println(token);
vars.add(token); vars.add(token);
} }
} }
vars.removeAll(extractDefs(statement));
return vars; return vars;
} }
@@ -169,7 +184,8 @@ public class PDG {
"if", "else", "while", "for", "do", "return", "break", "continue", "if", "else", "while", "for", "do", "return", "break", "continue",
"int", "double", "float", "char", "boolean", "void", "String", "int", "double", "float", "char", "boolean", "void", "String",
"new", "null", "true", "false", "class", "public", "private", "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); return keywords.contains(word);
} }

View File

@@ -1,27 +1,28 @@
public class Example1 { public class Example1 {
public void test() { // public void test() {
int x = 5; // int x = 5;
int y = x + 10; // int y = x + 10;
int z = y * 2; // int z = y * 2;
int result = z + x; // int result = z + x;
} // }
public void test1(int a) { // public void test1(int a) {
int x = 5; // int x = 5;
int y = 0; // int y = 0;
if (a > 10) { // if (a > 10) {
y = x + a; // y = x + a;
} else { // } else {
y = x - a; // y = x - a;
} // }
int result = y * 2; // int result = y * 2;
} // }
public void test2() { public void test2() {
int sum = 0; int sum = 0;
int i = 0; int i = 0;
while (i < 10) { while (i < 10) {
i = 2;
sum = sum + i; sum = sum + i;
i = i + 1; i = i + 1;
} }