From dbcf40ea84e2bcf595be0e063ddc2686b50cefd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krau=C3=9F=2C=20Josefine?= Date: Thu, 9 May 2024 15:12:08 +0200 Subject: [PATCH] antr gen package expilcitly added and some printlines in main --- Source/.idea/libraries/asm_9_7.xml | 1 + Source/.idea/misc.xml | 2 +- Source/Compiler.java | 40 ++++++++++++++++++++++++++++++ Source/gen/DecafBaseListener.java | 2 +- Source/gen/DecafBaseVisitor.java | 2 +- Source/gen/DecafLexer.java | 2 +- Source/gen/DecafListener.java | 2 +- Source/gen/DecafParser.java | 2 +- Source/gen/DecafVisitor.java | 2 +- 9 files changed, 48 insertions(+), 7 deletions(-) diff --git a/Source/.idea/libraries/asm_9_7.xml b/Source/.idea/libraries/asm_9_7.xml index 1d812dc..2233f59 100644 --- a/Source/.idea/libraries/asm_9_7.xml +++ b/Source/.idea/libraries/asm_9_7.xml @@ -2,6 +2,7 @@ + diff --git a/Source/.idea/misc.xml b/Source/.idea/misc.xml index e0844bc..ceb7609 100644 --- a/Source/.idea/misc.xml +++ b/Source/.idea/misc.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/Source/Compiler.java b/Source/Compiler.java index 8ba724a..d993d97 100644 --- a/Source/Compiler.java +++ b/Source/Compiler.java @@ -2,7 +2,17 @@ import abstractSyntaxTree.Class.FieldDecl; import abstractSyntaxTree.Class.MethodDecl; import abstractSyntaxTree.Class.RefType; import abstractSyntaxTree.Program; +import gen.DecafLexer; +import gen.DecafParser; +import org.antlr.v4.runtime.CharStream; +import org.antlr.v4.runtime.CharStreams; +import org.antlr.v4.runtime.CommonTokenStream; +import org.antlr.v4.runtime.Token; +import org.antlr.v4.runtime.tree.ParseTree; +import java.io.CharArrayReader; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; @@ -11,6 +21,36 @@ public class Compiler { public static void main(String[] args) throws Exception{ // get file +// String filePath = "EmptyClass.java"; +// String content = new String(Files.readAllBytes(Paths.get(filePath))); +// CharArrayReader charStream = new CharArrayReader(content.toCharArray()); + + CharStream codeCharStream = CharStreams.fromString("class Example { }"); + DecafLexer lexer = new DecafLexer(codeCharStream); + CommonTokenStream tokens = new CommonTokenStream(lexer); + + System.out.println("--- print tokens ---"); + tokens.fill(); + + List tokenList = tokens.getTokens(); + + StringBuilder stringBuilder = new StringBuilder(); + for (Token token : tokenList) { + stringBuilder.append(token.toString()).append(" "); + } + String readableTokens = stringBuilder.toString().trim(); + + System.out.println(readableTokens); + + + DecafParser parser = new DecafParser(tokens); + + ParseTree tree = parser.program(); + + System.out.println("--- print tree ---"); + System.out.println(tree); + + Program abstractSyntaxTree = new Program(new ArrayList<>()); List emptyFieldDecl = new ArrayList<>(); diff --git a/Source/gen/DecafBaseListener.java b/Source/gen/DecafBaseListener.java index 0821290..59f157f 100644 --- a/Source/gen/DecafBaseListener.java +++ b/Source/gen/DecafBaseListener.java @@ -1,4 +1,4 @@ -// Generated from C:/Users/dh10krj/OneDrive - Durr Group/Dokumente/S4/Compilerbau/projekt/NichtHaskell/Source/Decaf.g4 by ANTLR 4.13.1 +package gen;// Generated from C:/Users/dh10krj/OneDrive - Durr Group/Dokumente/S4/Compilerbau/projekt/NichtHaskell/Source/Decaf.g4 by ANTLR 4.13.1 import org.antlr.v4.runtime.ParserRuleContext; import org.antlr.v4.runtime.tree.ErrorNode; diff --git a/Source/gen/DecafBaseVisitor.java b/Source/gen/DecafBaseVisitor.java index b996318..9bd240e 100644 --- a/Source/gen/DecafBaseVisitor.java +++ b/Source/gen/DecafBaseVisitor.java @@ -1,4 +1,4 @@ -// Generated from C:/Users/dh10krj/OneDrive - Durr Group/Dokumente/S4/Compilerbau/projekt/NichtHaskell/Source/Decaf.g4 by ANTLR 4.13.1 +package gen;// Generated from C:/Users/dh10krj/OneDrive - Durr Group/Dokumente/S4/Compilerbau/projekt/NichtHaskell/Source/Decaf.g4 by ANTLR 4.13.1 import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor; /** diff --git a/Source/gen/DecafLexer.java b/Source/gen/DecafLexer.java index 71d203e..1448a7c 100644 --- a/Source/gen/DecafLexer.java +++ b/Source/gen/DecafLexer.java @@ -1,4 +1,4 @@ -// Generated from C:/Users/dh10krj/OneDrive - Durr Group/Dokumente/S4/Compilerbau/projekt/NichtHaskell/Source/Decaf.g4 by ANTLR 4.13.1 +package gen;// Generated from C:/Users/dh10krj/OneDrive - Durr Group/Dokumente/S4/Compilerbau/projekt/NichtHaskell/Source/Decaf.g4 by ANTLR 4.13.1 import org.antlr.v4.runtime.Lexer; import org.antlr.v4.runtime.CharStream; import org.antlr.v4.runtime.Token; diff --git a/Source/gen/DecafListener.java b/Source/gen/DecafListener.java index dac8933..4e2702f 100644 --- a/Source/gen/DecafListener.java +++ b/Source/gen/DecafListener.java @@ -1,4 +1,4 @@ -// Generated from C:/Users/dh10krj/OneDrive - Durr Group/Dokumente/S4/Compilerbau/projekt/NichtHaskell/Source/Decaf.g4 by ANTLR 4.13.1 +package gen;// Generated from C:/Users/dh10krj/OneDrive - Durr Group/Dokumente/S4/Compilerbau/projekt/NichtHaskell/Source/Decaf.g4 by ANTLR 4.13.1 import org.antlr.v4.runtime.tree.ParseTreeListener; /** diff --git a/Source/gen/DecafParser.java b/Source/gen/DecafParser.java index f9227d4..f46dc11 100644 --- a/Source/gen/DecafParser.java +++ b/Source/gen/DecafParser.java @@ -1,4 +1,4 @@ -// Generated from C:/Users/dh10krj/OneDrive - Durr Group/Dokumente/S4/Compilerbau/projekt/NichtHaskell/Source/Decaf.g4 by ANTLR 4.13.1 +package gen;// Generated from C:/Users/dh10krj/OneDrive - Durr Group/Dokumente/S4/Compilerbau/projekt/NichtHaskell/Source/Decaf.g4 by ANTLR 4.13.1 import org.antlr.v4.runtime.atn.*; import org.antlr.v4.runtime.dfa.DFA; import org.antlr.v4.runtime.*; diff --git a/Source/gen/DecafVisitor.java b/Source/gen/DecafVisitor.java index 68ee110..e9a524c 100644 --- a/Source/gen/DecafVisitor.java +++ b/Source/gen/DecafVisitor.java @@ -1,4 +1,4 @@ -// Generated from C:/Users/dh10krj/OneDrive - Durr Group/Dokumente/S4/Compilerbau/projekt/NichtHaskell/Source/Decaf.g4 by ANTLR 4.13.1 +package gen;// Generated from C:/Users/dh10krj/OneDrive - Durr Group/Dokumente/S4/Compilerbau/projekt/NichtHaskell/Source/Decaf.g4 by ANTLR 4.13.1 import org.antlr.v4.runtime.tree.ParseTreeVisitor; /**