From 5681e3e5f2a5ae39c32322f7f0e2a25833c65123 Mon Sep 17 00:00:00 2001 From: Purplumbi Date: Wed, 8 May 2024 11:43:04 +0200 Subject: [PATCH] AST --- src/main/java/{parser => ast}/ASTNode.java | 2 +- src/main/java/ast/AccessModifier.java | 5 - src/main/java/ast/ClassDeclaration.java | 7 - src/main/java/ast/ClassNode.java | 24 ++ src/main/java/ast/ConstructorNode.java | 7 + src/main/java/ast/FieldNode.java | 12 + src/main/java/ast/Identifier.java | 13 -- src/main/java/ast/MemberNode.java | 4 + src/main/java/ast/MethodNode.java | 11 + src/main/java/ast/ProgramNode.java | 12 + src/main/java/ast/Type.java | 7 - src/main/java/ast/TypeNode.java | 5 + src/main/java/bytecode/ByteCodeGenerator.java | 3 +- src/main/java/parser/ASTBuilder.java | 20 +- .../java/parser/ClassDeclarationNode.java | 11 - src/main/java/parser/Main.java | 3 +- src/main/java/parser/ProgramNode.java | 15 -- src/main/java/parser/SimpleJava.g4 | 52 ++++- .../java/parser/generated/SimpleJava.interp | 23 -- .../java/parser/generated/SimpleJava.tokens | 8 - .../generated/SimpleJavaBaseListener.java | 64 ----- .../generated/SimpleJavaBaseVisitor.java | 29 --- .../parser/generated/SimpleJavaLexer.interp | 32 --- .../parser/generated/SimpleJavaLexer.java | 140 ----------- .../parser/generated/SimpleJavaLexer.tokens | 8 - .../parser/generated/SimpleJavaListener.java | 30 --- .../parser/generated/SimpleJavaParser.java | 221 ------------------ .../parser/generated/SimpleJavaVisitor.java | 25 -- 28 files changed, 146 insertions(+), 647 deletions(-) rename src/main/java/{parser => ast}/ASTNode.java (84%) delete mode 100644 src/main/java/ast/AccessModifier.java delete mode 100644 src/main/java/ast/ClassDeclaration.java create mode 100644 src/main/java/ast/ClassNode.java create mode 100644 src/main/java/ast/ConstructorNode.java create mode 100644 src/main/java/ast/FieldNode.java delete mode 100644 src/main/java/ast/Identifier.java create mode 100644 src/main/java/ast/MemberNode.java create mode 100644 src/main/java/ast/MethodNode.java create mode 100644 src/main/java/ast/ProgramNode.java delete mode 100644 src/main/java/ast/Type.java create mode 100644 src/main/java/ast/TypeNode.java delete mode 100644 src/main/java/parser/ClassDeclarationNode.java delete mode 100644 src/main/java/parser/ProgramNode.java delete mode 100644 src/main/java/parser/generated/SimpleJava.interp delete mode 100644 src/main/java/parser/generated/SimpleJava.tokens delete mode 100644 src/main/java/parser/generated/SimpleJavaBaseListener.java delete mode 100644 src/main/java/parser/generated/SimpleJavaBaseVisitor.java delete mode 100644 src/main/java/parser/generated/SimpleJavaLexer.interp delete mode 100644 src/main/java/parser/generated/SimpleJavaLexer.java delete mode 100644 src/main/java/parser/generated/SimpleJavaLexer.tokens delete mode 100644 src/main/java/parser/generated/SimpleJavaListener.java delete mode 100644 src/main/java/parser/generated/SimpleJavaParser.java delete mode 100644 src/main/java/parser/generated/SimpleJavaVisitor.java diff --git a/src/main/java/parser/ASTNode.java b/src/main/java/ast/ASTNode.java similarity index 84% rename from src/main/java/parser/ASTNode.java rename to src/main/java/ast/ASTNode.java index fad3ac0..7836a79 100644 --- a/src/main/java/parser/ASTNode.java +++ b/src/main/java/ast/ASTNode.java @@ -1,4 +1,4 @@ -package parser; +package ast; import java.util.ArrayList; import java.util.List; diff --git a/src/main/java/ast/AccessModifier.java b/src/main/java/ast/AccessModifier.java deleted file mode 100644 index 87a76de..0000000 --- a/src/main/java/ast/AccessModifier.java +++ /dev/null @@ -1,5 +0,0 @@ -package ast; - -public enum AccessModifier { - Public,Private -} diff --git a/src/main/java/ast/ClassDeclaration.java b/src/main/java/ast/ClassDeclaration.java deleted file mode 100644 index abf46c9..0000000 --- a/src/main/java/ast/ClassDeclaration.java +++ /dev/null @@ -1,7 +0,0 @@ -package ast; - -public abstract class ClassDeclaration { - public AccessModifier accessModifier; - - -} diff --git a/src/main/java/ast/ClassNode.java b/src/main/java/ast/ClassNode.java new file mode 100644 index 0000000..d9e2f04 --- /dev/null +++ b/src/main/java/ast/ClassNode.java @@ -0,0 +1,24 @@ +package ast; + +import java.util.ArrayList; +import java.util.List; + +public class ClassNode extends ASTNode{ + public String name; + public List members = new ArrayList<>(); + public boolean hasConstructor = false; + + public void addMember(MemberNode member) { + if (member instanceof ConstructorNode) { + this.hasConstructor = true; + } + members.add(member); + } + + public void ensureConstructor(){ + if(!hasConstructor) { + ConstructorNode constructor = new ConstructorNode("public", name); + members.add(0,constructor); + } + } +} diff --git a/src/main/java/ast/ConstructorNode.java b/src/main/java/ast/ConstructorNode.java new file mode 100644 index 0000000..21c73ea --- /dev/null +++ b/src/main/java/ast/ConstructorNode.java @@ -0,0 +1,7 @@ +package ast; + +public class ConstructorNode extends MethodNode{ + public ConstructorNode(String visibility, String name) { + super(visibility, name); + } +} diff --git a/src/main/java/ast/FieldNode.java b/src/main/java/ast/FieldNode.java new file mode 100644 index 0000000..3bf6d2b --- /dev/null +++ b/src/main/java/ast/FieldNode.java @@ -0,0 +1,12 @@ +package ast; + +public class FieldNode { + String type; + String name; + + public FieldNode(String type, String name){ + this.type = type; + this.name = name; + } + +} diff --git a/src/main/java/ast/Identifier.java b/src/main/java/ast/Identifier.java deleted file mode 100644 index fd15728..0000000 --- a/src/main/java/ast/Identifier.java +++ /dev/null @@ -1,13 +0,0 @@ -package ast; - -public class Identifier { - private String name; - - public Identifier(String name) { - this.name = name; - } - - public String getName() { - return this.name; - } -} diff --git a/src/main/java/ast/MemberNode.java b/src/main/java/ast/MemberNode.java new file mode 100644 index 0000000..c1086cd --- /dev/null +++ b/src/main/java/ast/MemberNode.java @@ -0,0 +1,4 @@ +package ast; + +public class MemberNode extends ASTNode{ +} diff --git a/src/main/java/ast/MethodNode.java b/src/main/java/ast/MethodNode.java new file mode 100644 index 0000000..b9a8ab4 --- /dev/null +++ b/src/main/java/ast/MethodNode.java @@ -0,0 +1,11 @@ +package ast; + +public class MethodNode extends MemberNode{ + public String visibility; + public String name; + + public MethodNode(String visibility, String name){ + this.visibility = visibility; + this.name = name; + } +} diff --git a/src/main/java/ast/ProgramNode.java b/src/main/java/ast/ProgramNode.java new file mode 100644 index 0000000..d22af33 --- /dev/null +++ b/src/main/java/ast/ProgramNode.java @@ -0,0 +1,12 @@ +package ast; + +import java.util.ArrayList; +import java.util.List; + +public class ProgramNode extends ASTNode { + public List classes = new ArrayList<>(); + + public void addClass(ClassNode classNode) { + classes.add(classNode); + } +} diff --git a/src/main/java/ast/Type.java b/src/main/java/ast/Type.java deleted file mode 100644 index c412781..0000000 --- a/src/main/java/ast/Type.java +++ /dev/null @@ -1,7 +0,0 @@ -package ast; - -public interface Type { - -} - - diff --git a/src/main/java/ast/TypeNode.java b/src/main/java/ast/TypeNode.java new file mode 100644 index 0000000..1f657df --- /dev/null +++ b/src/main/java/ast/TypeNode.java @@ -0,0 +1,5 @@ +package ast; + +public class TypeNode { + String type; +} diff --git a/src/main/java/bytecode/ByteCodeGenerator.java b/src/main/java/bytecode/ByteCodeGenerator.java index 8b07bbb..876ec63 100644 --- a/src/main/java/bytecode/ByteCodeGenerator.java +++ b/src/main/java/bytecode/ByteCodeGenerator.java @@ -4,8 +4,7 @@ import java.io.FileOutputStream; import java.io.IOException; import org.objectweb.asm.ClassWriter; import org.objectweb.asm.Opcodes; -import parser.ClassDeclarationNode; -import parser.ProgramNode; +import ast.ProgramNode; public class ByteCodeGenerator { diff --git a/src/main/java/parser/ASTBuilder.java b/src/main/java/parser/ASTBuilder.java index ccf3ba7..fe0bdeb 100644 --- a/src/main/java/parser/ASTBuilder.java +++ b/src/main/java/parser/ASTBuilder.java @@ -1,5 +1,6 @@ package parser; +import ast.*; import parser.generated.SimpleJavaBaseVisitor; import parser.generated.SimpleJavaParser; @@ -8,13 +9,28 @@ public class ASTBuilder extends SimpleJavaBaseVisitor { public ASTNode visitProgram(SimpleJavaParser.ProgramContext ctx) { ProgramNode program = new ProgramNode(); for (SimpleJavaParser.ClassDeclarationContext classDeclCtx : ctx.classDeclaration()) { - program.addClass((ClassDeclarationNode) visit(classDeclCtx)); + program.addClass((ClassNode) visit(classDeclCtx)); } return program; } @Override public ASTNode visitClassDeclaration(SimpleJavaParser.ClassDeclarationContext ctx) { - return new ClassDeclarationNode(ctx.IDENTIFIER().getText()); + ClassNode classNode = new ClassNode(); + classNode.name = ctx.IDENTIFIER().getText(); + for (SimpleJavaParser.MemberDeclarationContext member : ctx.memberDeclaration()) { + classNode.addMember((MemberNode) visit(member)); + } + classNode.ensureConstructor(); // Check and add default constructor if needed + return classNode; } + + @Override + public ASTNode visitFieldDeclaration(SimpleJavaParser.FieldDeclarationContext ctx) { + TypeNode type = (TypeNode) visit(ctx.type()); + String identifier = ctx.IDENTIFIER().getText(); + return new FieldNode(type, identifier); + } + + } diff --git a/src/main/java/parser/ClassDeclarationNode.java b/src/main/java/parser/ClassDeclarationNode.java deleted file mode 100644 index e6798fa..0000000 --- a/src/main/java/parser/ClassDeclarationNode.java +++ /dev/null @@ -1,11 +0,0 @@ -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 index 64a1c8b..c587771 100644 --- a/src/main/java/parser/Main.java +++ b/src/main/java/parser/Main.java @@ -1,11 +1,10 @@ package parser; +import ast.ProgramNode; 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 { diff --git a/src/main/java/parser/ProgramNode.java b/src/main/java/parser/ProgramNode.java deleted file mode 100644 index 2b54468..0000000 --- a/src/main/java/parser/ProgramNode.java +++ /dev/null @@ -1,15 +0,0 @@ -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/SimpleJava.g4 b/src/main/java/parser/SimpleJava.g4 index b0779c7..122da44 100644 --- a/src/main/java/parser/SimpleJava.g4 +++ b/src/main/java/parser/SimpleJava.g4 @@ -2,8 +2,56 @@ grammar SimpleJava; program : classDeclaration+; -classDeclaration : 'class' IDENTIFIER '{' '}'; +classDeclaration : 'class' IDENTIFIER '{' memberDeclaration* '}'; -IDENTIFIER : [a-zA-Z][a-zA-Z0-9_]*; +memberDeclaration : fieldDeclaration | methodDeclaration; + +fieldDeclaration : type IDENTIFIER ';'; + +methodDeclaration : 'public' 'static' type IDENTIFIER '(' parameterList? ')' '{' statement* '}' ; + +parameterList : parameter (',' parameter)* ; +parameter : type IDENTIFIER ; + +type : 'int' | 'boolean' | 'char' ; + +statement + : variableDeclarationStatement + | assignmentStatement + | ifStatement + | whileStatement + | returnStatement + | block + ; + +variableDeclarationStatement : type IDENTIFIER ('=' expression)? ';' ; + +assignmentStatement : IDENTIFIER '=' expression ';' ; + +ifStatement : 'if' '(' expression ')' statement ('else' statement)? ; + +whileStatement : 'while' '(' expression ')' statement ; + +returnStatement : 'return' (expression)? ';' ; + +block : '{' statement* '}' ; + +expression + : expression ('&&' | '||') expression + | expression ('==' | '!=' | '<' | '<=' | '>' | '>=') expression + | expression ('+' | '-' | '*' | '/' | '%') expression + | '-' expression + | '!' expression + | '(' expression ')' + | literal + | IDENTIFIER + ; + +literal : INTEGERLITERAL | booleanLiteral | charLiteral ; + +INTEGERLITERAL : [0-9]+ ; +booleanLiteral : 'true' | 'false' ; +charLiteral : '\'' . '\'' ; +IDENTIFIER : [a-zA-Z][a-zA-Z0-9_]* ; WS : [ \t\r\n]+ -> skip; \ No newline at end of file diff --git a/src/main/java/parser/generated/SimpleJava.interp b/src/main/java/parser/generated/SimpleJava.interp deleted file mode 100644 index 5754367..0000000 --- a/src/main/java/parser/generated/SimpleJava.interp +++ /dev/null @@ -1,23 +0,0 @@ -token literal names: -null -'class' -'{' -'}' -null -null - -token symbolic names: -null -null -null -null -IDENTIFIER -WS - -rule names: -program -classDeclaration - - -atn: -[4, 1, 5, 15, 2, 0, 7, 0, 2, 1, 7, 1, 1, 0, 4, 0, 6, 8, 0, 11, 0, 12, 0, 7, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 2, 0, 2, 0, 0, 13, 0, 5, 1, 0, 0, 0, 2, 9, 1, 0, 0, 0, 4, 6, 3, 2, 1, 0, 5, 4, 1, 0, 0, 0, 6, 7, 1, 0, 0, 0, 7, 5, 1, 0, 0, 0, 7, 8, 1, 0, 0, 0, 8, 1, 1, 0, 0, 0, 9, 10, 5, 1, 0, 0, 10, 11, 5, 4, 0, 0, 11, 12, 5, 2, 0, 0, 12, 13, 5, 3, 0, 0, 13, 3, 1, 0, 0, 0, 1, 7] \ No newline at end of file diff --git a/src/main/java/parser/generated/SimpleJava.tokens b/src/main/java/parser/generated/SimpleJava.tokens deleted file mode 100644 index d009d79..0000000 --- a/src/main/java/parser/generated/SimpleJava.tokens +++ /dev/null @@ -1,8 +0,0 @@ -T__0=1 -T__1=2 -T__2=3 -IDENTIFIER=4 -WS=5 -'class'=1 -'{'=2 -'}'=3 diff --git a/src/main/java/parser/generated/SimpleJavaBaseListener.java b/src/main/java/parser/generated/SimpleJavaBaseListener.java deleted file mode 100644 index 357529f..0000000 --- a/src/main/java/parser/generated/SimpleJavaBaseListener.java +++ /dev/null @@ -1,64 +0,0 @@ -// Generated from C:/Users/Johannes Ehlert/Documents/Git/JavaCompiler/src/main/java/parser/SimpleJava.g4 by ANTLR 4.13.1 -package parser.generated; - -import org.antlr.v4.runtime.ParserRuleContext; -import org.antlr.v4.runtime.tree.ErrorNode; -import org.antlr.v4.runtime.tree.TerminalNode; - -/** - * This class provides an empty implementation of {@link SimpleJavaListener}, - * which can be extended to create a listener which only needs to handle a subset - * of the available methods. - */ -@SuppressWarnings("CheckReturnValue") -public class SimpleJavaBaseListener implements SimpleJavaListener { - /** - * {@inheritDoc} - * - *

The default implementation does nothing.

- */ - @Override public void enterProgram(SimpleJavaParser.ProgramContext ctx) { } - /** - * {@inheritDoc} - * - *

The default implementation does nothing.

- */ - @Override public void exitProgram(SimpleJavaParser.ProgramContext ctx) { } - /** - * {@inheritDoc} - * - *

The default implementation does nothing.

- */ - @Override public void enterClassDeclaration(SimpleJavaParser.ClassDeclarationContext ctx) { } - /** - * {@inheritDoc} - * - *

The default implementation does nothing.

- */ - @Override public void exitClassDeclaration(SimpleJavaParser.ClassDeclarationContext ctx) { } - - /** - * {@inheritDoc} - * - *

The default implementation does nothing.

- */ - @Override public void enterEveryRule(ParserRuleContext ctx) { } - /** - * {@inheritDoc} - * - *

The default implementation does nothing.

- */ - @Override public void exitEveryRule(ParserRuleContext ctx) { } - /** - * {@inheritDoc} - * - *

The default implementation does nothing.

- */ - @Override public void visitTerminal(TerminalNode node) { } - /** - * {@inheritDoc} - * - *

The default implementation does nothing.

- */ - @Override public void visitErrorNode(ErrorNode node) { } -} \ No newline at end of file diff --git a/src/main/java/parser/generated/SimpleJavaBaseVisitor.java b/src/main/java/parser/generated/SimpleJavaBaseVisitor.java deleted file mode 100644 index 3f44819..0000000 --- a/src/main/java/parser/generated/SimpleJavaBaseVisitor.java +++ /dev/null @@ -1,29 +0,0 @@ -// Generated from C:/Users/Johannes Ehlert/Documents/Git/JavaCompiler/src/main/java/parser/SimpleJava.g4 by ANTLR 4.13.1 -package parser.generated; -import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor; - -/** - * This class provides an empty implementation of {@link SimpleJavaVisitor}, - * which can be extended to create a visitor which only needs to handle a subset - * of the available methods. - * - * @param The return type of the visit operation. Use {@link Void} for - * operations with no return type. - */ -@SuppressWarnings("CheckReturnValue") -public class SimpleJavaBaseVisitor extends AbstractParseTreeVisitor implements SimpleJavaVisitor { - /** - * {@inheritDoc} - * - *

The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

- */ - @Override public T visitProgram(SimpleJavaParser.ProgramContext ctx) { return visitChildren(ctx); } - /** - * {@inheritDoc} - * - *

The default implementation returns the result of calling - * {@link #visitChildren} on {@code ctx}.

- */ - @Override public T visitClassDeclaration(SimpleJavaParser.ClassDeclarationContext ctx) { return visitChildren(ctx); } -} \ No newline at end of file diff --git a/src/main/java/parser/generated/SimpleJavaLexer.interp b/src/main/java/parser/generated/SimpleJavaLexer.interp deleted file mode 100644 index 7e306ab..0000000 --- a/src/main/java/parser/generated/SimpleJavaLexer.interp +++ /dev/null @@ -1,32 +0,0 @@ -token literal names: -null -'class' -'{' -'}' -null -null - -token symbolic names: -null -null -null -null -IDENTIFIER -WS - -rule names: -T__0 -T__1 -T__2 -IDENTIFIER -WS - -channel names: -DEFAULT_TOKEN_CHANNEL -HIDDEN - -mode names: -DEFAULT_MODE - -atn: -[4, 0, 5, 35, 6, -1, 2, 0, 7, 0, 2, 1, 7, 1, 2, 2, 7, 2, 2, 3, 7, 3, 2, 4, 7, 4, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 2, 1, 2, 1, 3, 1, 3, 5, 3, 24, 8, 3, 10, 3, 12, 3, 27, 9, 3, 1, 4, 4, 4, 30, 8, 4, 11, 4, 12, 4, 31, 1, 4, 1, 4, 0, 0, 5, 1, 1, 3, 2, 5, 3, 7, 4, 9, 5, 1, 0, 3, 2, 0, 65, 90, 97, 122, 4, 0, 48, 57, 65, 90, 95, 95, 97, 122, 3, 0, 9, 10, 13, 13, 32, 32, 36, 0, 1, 1, 0, 0, 0, 0, 3, 1, 0, 0, 0, 0, 5, 1, 0, 0, 0, 0, 7, 1, 0, 0, 0, 0, 9, 1, 0, 0, 0, 1, 11, 1, 0, 0, 0, 3, 17, 1, 0, 0, 0, 5, 19, 1, 0, 0, 0, 7, 21, 1, 0, 0, 0, 9, 29, 1, 0, 0, 0, 11, 12, 5, 99, 0, 0, 12, 13, 5, 108, 0, 0, 13, 14, 5, 97, 0, 0, 14, 15, 5, 115, 0, 0, 15, 16, 5, 115, 0, 0, 16, 2, 1, 0, 0, 0, 17, 18, 5, 123, 0, 0, 18, 4, 1, 0, 0, 0, 19, 20, 5, 125, 0, 0, 20, 6, 1, 0, 0, 0, 21, 25, 7, 0, 0, 0, 22, 24, 7, 1, 0, 0, 23, 22, 1, 0, 0, 0, 24, 27, 1, 0, 0, 0, 25, 23, 1, 0, 0, 0, 25, 26, 1, 0, 0, 0, 26, 8, 1, 0, 0, 0, 27, 25, 1, 0, 0, 0, 28, 30, 7, 2, 0, 0, 29, 28, 1, 0, 0, 0, 30, 31, 1, 0, 0, 0, 31, 29, 1, 0, 0, 0, 31, 32, 1, 0, 0, 0, 32, 33, 1, 0, 0, 0, 33, 34, 6, 4, 0, 0, 34, 10, 1, 0, 0, 0, 3, 0, 25, 31, 1, 6, 0, 0] \ No newline at end of file diff --git a/src/main/java/parser/generated/SimpleJavaLexer.java b/src/main/java/parser/generated/SimpleJavaLexer.java deleted file mode 100644 index 37dda97..0000000 --- a/src/main/java/parser/generated/SimpleJavaLexer.java +++ /dev/null @@ -1,140 +0,0 @@ -// Generated from C:/Users/Johannes Ehlert/Documents/Git/JavaCompiler/src/main/java/parser/SimpleJava.g4 by ANTLR 4.13.1 -package parser.generated; -import org.antlr.v4.runtime.Lexer; -import org.antlr.v4.runtime.CharStream; -import org.antlr.v4.runtime.Token; -import org.antlr.v4.runtime.TokenStream; -import org.antlr.v4.runtime.*; -import org.antlr.v4.runtime.atn.*; -import org.antlr.v4.runtime.dfa.DFA; -import org.antlr.v4.runtime.misc.*; - -@SuppressWarnings({"all", "warnings", "unchecked", "unused", "cast", "CheckReturnValue", "this-escape"}) -public class SimpleJavaLexer extends Lexer { - static { RuntimeMetaData.checkVersion("4.13.1", RuntimeMetaData.VERSION); } - - protected static final DFA[] _decisionToDFA; - protected static final PredictionContextCache _sharedContextCache = - new PredictionContextCache(); - public static final int - T__0=1, T__1=2, T__2=3, IDENTIFIER=4, WS=5; - public static String[] channelNames = { - "DEFAULT_TOKEN_CHANNEL", "HIDDEN" - }; - - public static String[] modeNames = { - "DEFAULT_MODE" - }; - - private static String[] makeRuleNames() { - return new String[] { - "T__0", "T__1", "T__2", "IDENTIFIER", "WS" - }; - } - public static final String[] ruleNames = makeRuleNames(); - - private static String[] makeLiteralNames() { - return new String[] { - null, "'class'", "'{'", "'}'" - }; - } - private static final String[] _LITERAL_NAMES = makeLiteralNames(); - private static String[] makeSymbolicNames() { - return new String[] { - null, null, null, null, "IDENTIFIER", "WS" - }; - } - private static final String[] _SYMBOLIC_NAMES = makeSymbolicNames(); - public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES); - - /** - * @deprecated Use {@link #VOCABULARY} instead. - */ - @Deprecated - public static final String[] tokenNames; - static { - tokenNames = new String[_SYMBOLIC_NAMES.length]; - for (int i = 0; i < tokenNames.length; i++) { - tokenNames[i] = VOCABULARY.getLiteralName(i); - if (tokenNames[i] == null) { - tokenNames[i] = VOCABULARY.getSymbolicName(i); - } - - if (tokenNames[i] == null) { - tokenNames[i] = ""; - } - } - } - - @Override - @Deprecated - public String[] getTokenNames() { - return tokenNames; - } - - @Override - - public Vocabulary getVocabulary() { - return VOCABULARY; - } - - - public SimpleJavaLexer(CharStream input) { - super(input); - _interp = new LexerATNSimulator(this,_ATN,_decisionToDFA,_sharedContextCache); - } - - @Override - public String getGrammarFileName() { return "SimpleJava.g4"; } - - @Override - public String[] getRuleNames() { return ruleNames; } - - @Override - public String getSerializedATN() { return _serializedATN; } - - @Override - public String[] getChannelNames() { return channelNames; } - - @Override - public String[] getModeNames() { return modeNames; } - - @Override - public ATN getATN() { return _ATN; } - - public static final String _serializedATN = - "\u0004\u0000\u0005#\u0006\uffff\uffff\u0002\u0000\u0007\u0000\u0002\u0001"+ - "\u0007\u0001\u0002\u0002\u0007\u0002\u0002\u0003\u0007\u0003\u0002\u0004"+ - "\u0007\u0004\u0001\u0000\u0001\u0000\u0001\u0000\u0001\u0000\u0001\u0000"+ - "\u0001\u0000\u0001\u0001\u0001\u0001\u0001\u0002\u0001\u0002\u0001\u0003"+ - "\u0001\u0003\u0005\u0003\u0018\b\u0003\n\u0003\f\u0003\u001b\t\u0003\u0001"+ - "\u0004\u0004\u0004\u001e\b\u0004\u000b\u0004\f\u0004\u001f\u0001\u0004"+ - "\u0001\u0004\u0000\u0000\u0005\u0001\u0001\u0003\u0002\u0005\u0003\u0007"+ - "\u0004\t\u0005\u0001\u0000\u0003\u0002\u0000AZaz\u0004\u000009AZ__az\u0003"+ - "\u0000\t\n\r\r $\u0000\u0001\u0001\u0000\u0000\u0000\u0000\u0003\u0001"+ - "\u0000\u0000\u0000\u0000\u0005\u0001\u0000\u0000\u0000\u0000\u0007\u0001"+ - "\u0000\u0000\u0000\u0000\t\u0001\u0000\u0000\u0000\u0001\u000b\u0001\u0000"+ - "\u0000\u0000\u0003\u0011\u0001\u0000\u0000\u0000\u0005\u0013\u0001\u0000"+ - "\u0000\u0000\u0007\u0015\u0001\u0000\u0000\u0000\t\u001d\u0001\u0000\u0000"+ - "\u0000\u000b\f\u0005c\u0000\u0000\f\r\u0005l\u0000\u0000\r\u000e\u0005"+ - "a\u0000\u0000\u000e\u000f\u0005s\u0000\u0000\u000f\u0010\u0005s\u0000"+ - "\u0000\u0010\u0002\u0001\u0000\u0000\u0000\u0011\u0012\u0005{\u0000\u0000"+ - "\u0012\u0004\u0001\u0000\u0000\u0000\u0013\u0014\u0005}\u0000\u0000\u0014"+ - "\u0006\u0001\u0000\u0000\u0000\u0015\u0019\u0007\u0000\u0000\u0000\u0016"+ - "\u0018\u0007\u0001\u0000\u0000\u0017\u0016\u0001\u0000\u0000\u0000\u0018"+ - "\u001b\u0001\u0000\u0000\u0000\u0019\u0017\u0001\u0000\u0000\u0000\u0019"+ - "\u001a\u0001\u0000\u0000\u0000\u001a\b\u0001\u0000\u0000\u0000\u001b\u0019"+ - "\u0001\u0000\u0000\u0000\u001c\u001e\u0007\u0002\u0000\u0000\u001d\u001c"+ - "\u0001\u0000\u0000\u0000\u001e\u001f\u0001\u0000\u0000\u0000\u001f\u001d"+ - "\u0001\u0000\u0000\u0000\u001f \u0001\u0000\u0000\u0000 !\u0001\u0000"+ - "\u0000\u0000!\"\u0006\u0004\u0000\u0000\"\n\u0001\u0000\u0000\u0000\u0003"+ - "\u0000\u0019\u001f\u0001\u0006\u0000\u0000"; - public static final ATN _ATN = - new ATNDeserializer().deserialize(_serializedATN.toCharArray()); - static { - _decisionToDFA = new DFA[_ATN.getNumberOfDecisions()]; - for (int i = 0; i < _ATN.getNumberOfDecisions(); i++) { - _decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i); - } - } -} \ No newline at end of file diff --git a/src/main/java/parser/generated/SimpleJavaLexer.tokens b/src/main/java/parser/generated/SimpleJavaLexer.tokens deleted file mode 100644 index d009d79..0000000 --- a/src/main/java/parser/generated/SimpleJavaLexer.tokens +++ /dev/null @@ -1,8 +0,0 @@ -T__0=1 -T__1=2 -T__2=3 -IDENTIFIER=4 -WS=5 -'class'=1 -'{'=2 -'}'=3 diff --git a/src/main/java/parser/generated/SimpleJavaListener.java b/src/main/java/parser/generated/SimpleJavaListener.java deleted file mode 100644 index bfacf32..0000000 --- a/src/main/java/parser/generated/SimpleJavaListener.java +++ /dev/null @@ -1,30 +0,0 @@ -// Generated from C:/Users/Johannes Ehlert/Documents/Git/JavaCompiler/src/main/java/parser/SimpleJava.g4 by ANTLR 4.13.1 -package parser.generated; -import org.antlr.v4.runtime.tree.ParseTreeListener; - -/** - * This interface defines a complete listener for a parse tree produced by - * {@link SimpleJavaParser}. - */ -public interface SimpleJavaListener extends ParseTreeListener { - /** - * Enter a parse tree produced by {@link SimpleJavaParser#program}. - * @param ctx the parse tree - */ - void enterProgram(SimpleJavaParser.ProgramContext ctx); - /** - * Exit a parse tree produced by {@link SimpleJavaParser#program}. - * @param ctx the parse tree - */ - void exitProgram(SimpleJavaParser.ProgramContext ctx); - /** - * Enter a parse tree produced by {@link SimpleJavaParser#classDeclaration}. - * @param ctx the parse tree - */ - void enterClassDeclaration(SimpleJavaParser.ClassDeclarationContext ctx); - /** - * Exit a parse tree produced by {@link SimpleJavaParser#classDeclaration}. - * @param ctx the parse tree - */ - void exitClassDeclaration(SimpleJavaParser.ClassDeclarationContext ctx); -} \ No newline at end of file diff --git a/src/main/java/parser/generated/SimpleJavaParser.java b/src/main/java/parser/generated/SimpleJavaParser.java deleted file mode 100644 index 174e385..0000000 --- a/src/main/java/parser/generated/SimpleJavaParser.java +++ /dev/null @@ -1,221 +0,0 @@ -// Generated from C:/Users/Johannes Ehlert/Documents/Git/JavaCompiler/src/main/java/parser/SimpleJava.g4 by ANTLR 4.13.1 -package parser.generated; -import org.antlr.v4.runtime.atn.*; -import org.antlr.v4.runtime.dfa.DFA; -import org.antlr.v4.runtime.*; -import org.antlr.v4.runtime.misc.*; -import org.antlr.v4.runtime.tree.*; -import java.util.List; -import java.util.Iterator; -import java.util.ArrayList; - -@SuppressWarnings({"all", "warnings", "unchecked", "unused", "cast", "CheckReturnValue"}) -public class SimpleJavaParser extends Parser { - static { RuntimeMetaData.checkVersion("4.13.1", RuntimeMetaData.VERSION); } - - protected static final DFA[] _decisionToDFA; - protected static final PredictionContextCache _sharedContextCache = - new PredictionContextCache(); - public static final int - T__0=1, T__1=2, T__2=3, IDENTIFIER=4, WS=5; - public static final int - RULE_program = 0, RULE_classDeclaration = 1; - private static String[] makeRuleNames() { - return new String[] { - "program", "classDeclaration" - }; - } - public static final String[] ruleNames = makeRuleNames(); - - private static String[] makeLiteralNames() { - return new String[] { - null, "'class'", "'{'", "'}'" - }; - } - private static final String[] _LITERAL_NAMES = makeLiteralNames(); - private static String[] makeSymbolicNames() { - return new String[] { - null, null, null, null, "IDENTIFIER", "WS" - }; - } - private static final String[] _SYMBOLIC_NAMES = makeSymbolicNames(); - public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES); - - /** - * @deprecated Use {@link #VOCABULARY} instead. - */ - @Deprecated - public static final String[] tokenNames; - static { - tokenNames = new String[_SYMBOLIC_NAMES.length]; - for (int i = 0; i < tokenNames.length; i++) { - tokenNames[i] = VOCABULARY.getLiteralName(i); - if (tokenNames[i] == null) { - tokenNames[i] = VOCABULARY.getSymbolicName(i); - } - - if (tokenNames[i] == null) { - tokenNames[i] = ""; - } - } - } - - @Override - @Deprecated - public String[] getTokenNames() { - return tokenNames; - } - - @Override - - public Vocabulary getVocabulary() { - return VOCABULARY; - } - - @Override - public String getGrammarFileName() { return "SimpleJava.g4"; } - - @Override - public String[] getRuleNames() { return ruleNames; } - - @Override - public String getSerializedATN() { return _serializedATN; } - - @Override - public ATN getATN() { return _ATN; } - - public SimpleJavaParser(TokenStream input) { - super(input); - _interp = new ParserATNSimulator(this,_ATN,_decisionToDFA,_sharedContextCache); - } - - @SuppressWarnings("CheckReturnValue") - public static class ProgramContext extends ParserRuleContext { - public List classDeclaration() { - return getRuleContexts(ClassDeclarationContext.class); - } - public ClassDeclarationContext classDeclaration(int i) { - return getRuleContext(ClassDeclarationContext.class,i); - } - public ProgramContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_program; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SimpleJavaListener ) ((SimpleJavaListener)listener).enterProgram(this); - } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SimpleJavaListener ) ((SimpleJavaListener)listener).exitProgram(this); - } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SimpleJavaVisitor ) return ((SimpleJavaVisitor)visitor).visitProgram(this); - else return visitor.visitChildren(this); - } - } - - public final ProgramContext program() throws RecognitionException { - ProgramContext _localctx = new ProgramContext(_ctx, getState()); - enterRule(_localctx, 0, RULE_program); - int _la; - try { - enterOuterAlt(_localctx, 1); - { - setState(5); - _errHandler.sync(this); - _la = _input.LA(1); - do { - { - { - setState(4); - classDeclaration(); - } - } - setState(7); - _errHandler.sync(this); - _la = _input.LA(1); - } while ( _la==T__0 ); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - @SuppressWarnings("CheckReturnValue") - public static class ClassDeclarationContext extends ParserRuleContext { - public TerminalNode IDENTIFIER() { return getToken(SimpleJavaParser.IDENTIFIER, 0); } - public ClassDeclarationContext(ParserRuleContext parent, int invokingState) { - super(parent, invokingState); - } - @Override public int getRuleIndex() { return RULE_classDeclaration; } - @Override - public void enterRule(ParseTreeListener listener) { - if ( listener instanceof SimpleJavaListener ) ((SimpleJavaListener)listener).enterClassDeclaration(this); - } - @Override - public void exitRule(ParseTreeListener listener) { - if ( listener instanceof SimpleJavaListener ) ((SimpleJavaListener)listener).exitClassDeclaration(this); - } - @Override - public T accept(ParseTreeVisitor visitor) { - if ( visitor instanceof SimpleJavaVisitor ) return ((SimpleJavaVisitor)visitor).visitClassDeclaration(this); - else return visitor.visitChildren(this); - } - } - - public final ClassDeclarationContext classDeclaration() throws RecognitionException { - ClassDeclarationContext _localctx = new ClassDeclarationContext(_ctx, getState()); - enterRule(_localctx, 2, RULE_classDeclaration); - try { - enterOuterAlt(_localctx, 1); - { - setState(9); - match(T__0); - setState(10); - match(IDENTIFIER); - setState(11); - match(T__1); - setState(12); - match(T__2); - } - } - catch (RecognitionException re) { - _localctx.exception = re; - _errHandler.reportError(this, re); - _errHandler.recover(this, re); - } - finally { - exitRule(); - } - return _localctx; - } - - public static final String _serializedATN = - "\u0004\u0001\u0005\u000f\u0002\u0000\u0007\u0000\u0002\u0001\u0007\u0001"+ - "\u0001\u0000\u0004\u0000\u0006\b\u0000\u000b\u0000\f\u0000\u0007\u0001"+ - "\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0000"+ - "\u0000\u0002\u0000\u0002\u0000\u0000\r\u0000\u0005\u0001\u0000\u0000\u0000"+ - "\u0002\t\u0001\u0000\u0000\u0000\u0004\u0006\u0003\u0002\u0001\u0000\u0005"+ - "\u0004\u0001\u0000\u0000\u0000\u0006\u0007\u0001\u0000\u0000\u0000\u0007"+ - "\u0005\u0001\u0000\u0000\u0000\u0007\b\u0001\u0000\u0000\u0000\b\u0001"+ - "\u0001\u0000\u0000\u0000\t\n\u0005\u0001\u0000\u0000\n\u000b\u0005\u0004"+ - "\u0000\u0000\u000b\f\u0005\u0002\u0000\u0000\f\r\u0005\u0003\u0000\u0000"+ - "\r\u0003\u0001\u0000\u0000\u0000\u0001\u0007"; - public static final ATN _ATN = - new ATNDeserializer().deserialize(_serializedATN.toCharArray()); - static { - _decisionToDFA = new DFA[_ATN.getNumberOfDecisions()]; - for (int i = 0; i < _ATN.getNumberOfDecisions(); i++) { - _decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i); - } - } -} \ No newline at end of file diff --git a/src/main/java/parser/generated/SimpleJavaVisitor.java b/src/main/java/parser/generated/SimpleJavaVisitor.java deleted file mode 100644 index c00434b..0000000 --- a/src/main/java/parser/generated/SimpleJavaVisitor.java +++ /dev/null @@ -1,25 +0,0 @@ -// Generated from C:/Users/Johannes Ehlert/Documents/Git/JavaCompiler/src/main/java/parser/SimpleJava.g4 by ANTLR 4.13.1 -package parser.generated; -import org.antlr.v4.runtime.tree.ParseTreeVisitor; - -/** - * This interface defines a complete generic visitor for a parse tree produced - * by {@link SimpleJavaParser}. - * - * @param The return type of the visit operation. Use {@link Void} for - * operations with no return type. - */ -public interface SimpleJavaVisitor extends ParseTreeVisitor { - /** - * Visit a parse tree produced by {@link SimpleJavaParser#program}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitProgram(SimpleJavaParser.ProgramContext ctx); - /** - * Visit a parse tree produced by {@link SimpleJavaParser#classDeclaration}. - * @param ctx the parse tree - * @return the visitor result - */ - T visitClassDeclaration(SimpleJavaParser.ClassDeclarationContext ctx); -} \ No newline at end of file