From d27099285803cc724f79af1ed8fce2745ea44b43 Mon Sep 17 00:00:00 2001 From: Maximilian Stahl Date: Thu, 2 May 2024 15:22:29 +0200 Subject: [PATCH] =?UTF-8?q?Ausf=C3=BChrung=20Parser?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/parser/ASTBuilder.java | 20 ++++++++++++++ src/main/java/parser/ASTNode.java | 8 ++++++ .../java/parser/ClassDeclarationNode.java | 11 ++++++++ src/main/java/parser/Main.java | 27 +++++++++++++++++++ src/main/java/parser/ProgramNode.java | 15 +++++++++++ src/main/java/parser/Test.txt | 1 + 6 files changed, 82 insertions(+) create mode 100644 src/main/java/parser/ASTBuilder.java create mode 100644 src/main/java/parser/ASTNode.java create mode 100644 src/main/java/parser/ClassDeclarationNode.java create mode 100644 src/main/java/parser/Main.java create mode 100644 src/main/java/parser/ProgramNode.java create mode 100644 src/main/java/parser/Test.txt diff --git a/src/main/java/parser/ASTBuilder.java b/src/main/java/parser/ASTBuilder.java new file mode 100644 index 0000000..ccf3ba7 --- /dev/null +++ b/src/main/java/parser/ASTBuilder.java @@ -0,0 +1,20 @@ +package parser; + +import parser.generated.SimpleJavaBaseVisitor; +import parser.generated.SimpleJavaParser; + +public class ASTBuilder extends SimpleJavaBaseVisitor { + @Override + public ASTNode visitProgram(SimpleJavaParser.ProgramContext ctx) { + ProgramNode program = new ProgramNode(); + for (SimpleJavaParser.ClassDeclarationContext classDeclCtx : ctx.classDeclaration()) { + program.addClass((ClassDeclarationNode) visit(classDeclCtx)); + } + return program; + } + + @Override + public ASTNode visitClassDeclaration(SimpleJavaParser.ClassDeclarationContext ctx) { + return new ClassDeclarationNode(ctx.IDENTIFIER().getText()); + } +} diff --git a/src/main/java/parser/ASTNode.java b/src/main/java/parser/ASTNode.java new file mode 100644 index 0000000..fad3ac0 --- /dev/null +++ b/src/main/java/parser/ASTNode.java @@ -0,0 +1,8 @@ +package parser; + +import java.util.ArrayList; +import java.util.List; + +public abstract class ASTNode { } + + diff --git a/src/main/java/parser/ClassDeclarationNode.java b/src/main/java/parser/ClassDeclarationNode.java new file mode 100644 index 0000000..e6798fa --- /dev/null +++ b/src/main/java/parser/ClassDeclarationNode.java @@ -0,0 +1,11 @@ +package parser; + +import parser.ASTNode; + +public class ClassDeclarationNode extends ASTNode { + public String identifier; + + public ClassDeclarationNode(String identifier) { + this.identifier = identifier; + } +} diff --git a/src/main/java/parser/Main.java b/src/main/java/parser/Main.java new file mode 100644 index 0000000..ca8554f --- /dev/null +++ b/src/main/java/parser/Main.java @@ -0,0 +1,27 @@ +package parser; + +import org.antlr.v4.runtime.CharStream; +import org.antlr.v4.runtime.CharStreams; +import org.antlr.v4.runtime.CommonTokenStream; +import org.antlr.v4.runtime.tree.ParseTree; +import parser.generated.SimpleJavaLexer; +import parser.generated.SimpleJavaParser; + +public class Main { + public static void main(String[] args) throws Exception { + // Assuming args[0] contains the path to the input file + CharStream codeCharStream = CharStreams.fromString("class Test { }"); + SimpleJavaLexer lexer = new SimpleJavaLexer(codeCharStream); + CommonTokenStream tokens = new CommonTokenStream(lexer); + SimpleJavaParser parser = new SimpleJavaParser(tokens); + + ParseTree tree = parser.program(); // parse the input + + ASTBuilder builder = new ASTBuilder(); + ProgramNode ast = (ProgramNode) builder.visit(tree); // build the AST + + // Optionally print or process the AST + System.out.println("Parsed " + ast.classes.size() + " classes."); + System.out.println(ast.classes.get(0).identifier); + } +} \ No newline at end of file diff --git a/src/main/java/parser/ProgramNode.java b/src/main/java/parser/ProgramNode.java new file mode 100644 index 0000000..2b54468 --- /dev/null +++ b/src/main/java/parser/ProgramNode.java @@ -0,0 +1,15 @@ +package parser; + +import parser.ASTNode; +import parser.ClassDeclarationNode; + +import java.util.ArrayList; +import java.util.List; + +public class ProgramNode extends ASTNode { + public List classes = new ArrayList<>(); + + public void addClass(ClassDeclarationNode classNode) { + classes.add(classNode); + } +} diff --git a/src/main/java/parser/Test.txt b/src/main/java/parser/Test.txt new file mode 100644 index 0000000..5a0c977 --- /dev/null +++ b/src/main/java/parser/Test.txt @@ -0,0 +1 @@ +class Test { } \ No newline at end of file