Skip to content

Commit

Permalink
Set up visitor (#3)
Browse files Browse the repository at this point in the history
Co-authored-by: geno <[email protected]>
  • Loading branch information
boozec and gabrielegenovese authored Jun 13, 2024
1 parent 1c87619 commit 8e7089a
Show file tree
Hide file tree
Showing 43 changed files with 2,188 additions and 41 deletions.
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ PARSER_DIR = src/parser
BIN_DIR = out
MAIN_CLASS = com.clp.project.Main
PARSEALL_CLASS = com.clp.project.ParseAll
SOURCES = $(wildcard $(SRC_DIR)/*.java)
SOURCES = $(wildcard $(SRC_DIR)/*.java $(SRC_DIR)/*/*.java $(SRC_DIR)/*/*/*.java)
GRAMMARS = $(PARSER_DIR)/Python3Lexer.g4 $(PARSER_DIR)/Python3Parser.g4
ANTLR_OUTPUT = $(PARSER_DIR)/*.java
DATE = $(shell date +%Y%m%d-%H%M%S)
Expand All @@ -18,6 +18,9 @@ all: $(SOURCES) $(ANTLR_OUTPUT)
$(ANTLR_OUTPUT): $(GRAMMARS)
java -jar lib/antlr-4.13.1-complete.jar $^

build:
$(JAVAC) $(JAVAC_FLAGS) $(SOURCES)

run:
java -cp $(ANTLR_COMPLETE):$(BIN_DIR) $(MAIN_CLASS) $(ARGS)

Expand All @@ -30,4 +33,4 @@ clean:
release: clean
zip -r ../python3-miniparser-$(DATE).zip .

.PHONY: all run clean release runall
.PHONY: all build run clean release runall
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@ Project for the AA 2023/2024 **Complementi di programmazione**'s course of the M

```shell
make
make run
make runall
```

For archlinux users, add the following env variable to visualize the window: `export _JAVA_AWT_WM_NONREPARENTING=1`
11 changes: 8 additions & 3 deletions progs/test.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
def find_first_duplicate(nums):
import math
from re import f4, r3

from abs import *


def find_first_duplicate(nums, x):
num_set = set()
no_duplicate = -1

for i in range(len(nums)):

if nums[i] in num_set:
return nums[i]
else:
num_set.add(nums[i])

return no_duplicate
return no_duplicate
3 changes: 3 additions & 0 deletions progs/test2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
x = 1
if y == 1:
print("a")
87 changes: 59 additions & 28 deletions src/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,79 @@
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Objects;
import javax.swing.*;
import org.antlr.v4.gui.TreeViewer;
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.*;

import com.clp.project.ast.*;
import com.clp.project.ast.nodes.*;
import com.clp.project.parser.*;
import com.clp.project.semanticanalysis.*;

public class Main {
public static void main(String[] args) {
for (File file : Objects.requireNonNull(new File("./progs/").listFiles())) {
try {
String fileStr = file.getPath();
// FIXME: use the fileStr above
fileStr = "./progs/test.py";
System.out.println(fileStr);

System.out.println(readFile(fileStr));
public static void main(String[] args) {

CharStream cs = CharStreams.fromFileName(fileStr);
Python3Lexer lexer = new Python3Lexer(cs);
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
Python3Parser parser = new Python3Parser(tokenStream);
Python3Parser.RootContext tree = parser.root();
// String treeStr = tree.toString();
// System.out.println(treeStr);
// Visualize the parse tree
JFrame frame = new JFrame("Parse Tree");
JPanel panel = new JPanel();
TreeViewer viewer = new TreeViewer(Arrays.asList(parser.getRuleNames()), tree);
viewer.setScale(1.5); // Zoom factor
panel.add(viewer);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);
frame.setVisible(true);
break;
} catch (Exception e) {
e.printStackTrace();
// for (File file : Objects.requireNonNull(new File("./progs/").listFiles())) {
try {
// String fileStr = file.getPath();
// FIXME: use the fileStr above
String fileStr = "./progs/test.py";
System.out.println(fileStr);
System.out.println(readFile(fileStr));
CharStream cs = CharStreams.fromFileName(fileStr);
Python3Lexer lexer = new Python3Lexer(cs);
CommonTokenStream tokens = new CommonTokenStream(lexer);
Python3Parser parser = new Python3Parser(tokens);
Python3Parser.RootContext tree = parser.root();
// DEBUG
// {
// tokens.fill();
// for (Token token : tokens.getTokens()) {
// System.out.println(token.toString());
// }
//
// System.out.println("Tree: " + tree);
// }
JFrame frame = new JFrame("Parse Tree");
JPanel panel = new JPanel();
TreeViewer viewer = new TreeViewer(Arrays.asList(parser.getRuleNames()),
tree);
viewer.setScale(1.5); // Zoom factor
panel.add(viewer);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);
frame.setVisible(true);
if (tree == null) {
System.err.println("The tree is null.");
return;
}
if (parser.getNumberOfSyntaxErrors() > 0) {
System.err.println("Error on program parsing.");
return;
}
Python3VisitorImpl visitor = new Python3VisitorImpl();
SymbolTable ST = new SymbolTable();
Node ast = visitor.visit(tree);
ArrayList<SemanticError> errors = ast.checkSemantics(ST, 0);
if (errors.size() > 0) {
System.out.println("You had: " + errors.size() + " errors:");
for (SemanticError e : errors) {
System.out.println("\t" + e);
}
} else {
System.out.println("Visualizing AST...");
System.out.println(ast.toPrint(""));
}
} catch (Exception e) {
e.printStackTrace();
}
// }
}

private static String readFile(String filePath) throws IOException {
Expand Down
Loading

0 comments on commit 8e7089a

Please sign in to comment.