renamings

This commit is contained in:
ChUrl
2020-12-13 15:01:55 +01:00
parent e56e1abc46
commit ed99c71d44
22 changed files with 215 additions and 213 deletions

View File

@ -1,7 +1,7 @@
import lexer.StupsLexer;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.Lexer;
import parser.LL1Parser;
import parser.Parser;
import parser.grammar.Grammar;
import java.io.IOException;
@ -50,7 +50,7 @@ public final class StupsCompiler {
return;
}
LL1Parser parser = LL1Parser.fromGrammar(grammar);
Parser parser = Parser.fromGrammar(grammar);
parser.parse(lexer.getAllTokens(), lexer.getVocabulary());

View File

@ -4,13 +4,13 @@ import parser.ast.AST;
import static util.Logger.log;
public class MyParseException extends RuntimeException {
public class ParseException extends RuntimeException {
public MyParseException(String message) {
public ParseException(String message) {
super(message);
}
public MyParseException(String message, AST ast) {
public ParseException(String message, AST ast) {
super(message);
log("\nAST at last state:\n" + ast);

View File

@ -3,9 +3,9 @@ package parser;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.Vocabulary;
import parser.ast.AST;
import parser.ast.Node;
import parser.ast.ASTNode;
import parser.grammar.Grammar;
import parser.grammar.LL1GrammarAnalyzer;
import parser.grammar.GrammarAnalyzer;
import java.io.IOException;
import java.nio.file.Path;
@ -15,27 +15,27 @@ import java.util.List;
import static util.Logger.log;
public class LL1Parser {
public class Parser {
private final LL1ParsingTable parsetable;
private final ParsingTable parsetable;
public LL1Parser(LL1ParsingTable parsetable) {
public Parser(ParsingTable parsetable) {
this.parsetable = parsetable;
}
public static LL1Parser fromGrammar(Path path) throws IOException {
return LL1Parser.fromGrammar(Grammar.fromFile(path));
public static Parser fromGrammar(Path path) throws IOException {
return Parser.fromGrammar(Grammar.fromFile(path));
}
public static LL1Parser fromGrammar(Grammar grammar) {
LL1GrammarAnalyzer analyzer = new LL1GrammarAnalyzer(grammar);
return new LL1Parser(analyzer.getTable());
public static Parser fromGrammar(Grammar grammar) {
GrammarAnalyzer analyzer = new GrammarAnalyzer(grammar);
return new Parser(analyzer.getTable());
}
public AST parse(List<? extends Token> token, Vocabulary voc) {
Node root = new Node(this.parsetable.getStartSymbol());
ASTNode root = new ASTNode(this.parsetable.getStartSymbol());
AST tree = new AST(root);
Deque<Node> stack = new ArrayDeque<>();
Deque<ASTNode> stack = new ArrayDeque<>();
stack.push(root);
int inputPosition = 0;
@ -75,24 +75,24 @@ public class LL1Parser {
System.out.println("Syntaxfehler.");
throw new MyParseException("Invalid terminal on stack: " + top, tree);
throw new ParseException("Invalid terminal on stack: " + top, tree);
} else if (prod == null) {
// Wenn es für das aktuelle Terminal und das Nichtterminal auf dem Stack keine Regel gibt
System.out.println("Syntaxfehler.");
throw new MyParseException("No prod. for nonterminal " + top + ", terminal " + currentTokenSym, tree);
throw new ParseException("No prod. for nonterminal " + top + ", terminal " + currentTokenSym, tree);
} else {
// Wenn das Nichtterminal auf dem Stack durch (s)eine Produktion ersetzt werden kann
// Hier wird auch der AST aufgebaut
log("Used: " + top + " -> " + prod);
Node pop = stack.pop();
ASTNode pop = stack.pop();
final String[] split = prod.split(" ");
for (int i = split.length - 1; i >= 0; i--) {
Node node = new Node(split[i]);
ASTNode ASTNode = new ASTNode(split[i]);
if (inputPosition + i < token.size()) {
// Die Schleife geht in der Eingabe weiter
@ -100,12 +100,12 @@ public class LL1Parser {
// Die Token mit semantischem Inhalt auswählen
if ("IDENTIFIER".equals(split[i]) || split[i].endsWith("_LIT")) {
node.setValue(currentTok.getText());
ASTNode.setValue(currentTok.getText());
}
}
stack.push(node);
pop.addChild(node);
stack.push(ASTNode);
pop.addChild(ASTNode);
}
}
}

View File

@ -1,7 +1,7 @@
package parser;
import parser.grammar.Grammar;
import parser.grammar.LL1GrammarAnalyzer;
import parser.grammar.GrammarAnalyzer;
import java.util.AbstractMap.SimpleEntry;
import java.util.Formatter;
@ -12,18 +12,18 @@ import java.util.Map.Entry;
import java.util.Set;
import java.util.stream.Collectors;
public class LL1ParsingTable {
public class ParsingTable {
private final Grammar grammar;
private final Map<Entry<String, String>, String> parsetable;
public LL1ParsingTable(Grammar grammar, Map<Entry<String, String>, String> parsetable) {
public ParsingTable(Grammar grammar, Map<Entry<String, String>, String> parsetable) {
this.grammar = grammar;
this.parsetable = parsetable;
}
public static LL1ParsingTable fromGrammar(Grammar grammar) {
LL1GrammarAnalyzer analyzer = new LL1GrammarAnalyzer(grammar);
public static ParsingTable fromGrammar(Grammar grammar) {
GrammarAnalyzer analyzer = new GrammarAnalyzer(grammar);
return analyzer.getTable();
}

View File

@ -6,13 +6,13 @@ import java.util.Objects;
public class AST {
private final Node root;
private final ASTNode root;
public AST(Node root) {
public AST(ASTNode root) {
this.root = root;
}
public Node getRoot() {
public ASTNode getRoot() {
return this.root;
}
@ -22,7 +22,7 @@ public class AST {
public void preprocess(Grammar grammar) {
ASTCompacter.clean(this, grammar);
ExpressionBalancer.balance(this);
ASTBalancer.balance(this);
}
@Override

View File

@ -6,9 +6,9 @@ import java.util.Collections;
import static util.Logger.log;
public final class ExpressionBalancer {
public final class ASTBalancer {
private ExpressionBalancer() {}
private ASTBalancer() {}
public static void balance(AST tree) {
flip(tree);
@ -25,8 +25,8 @@ public final class ExpressionBalancer {
flip(tree.getRoot());
}
private static void flip(Node root) {
for (Node child : root.getChildren()) {
private static void flip(ASTNode root) {
for (ASTNode child : root.getChildren()) {
flip(child);
}
@ -41,12 +41,12 @@ public final class ExpressionBalancer {
}
// Es wird solange rotiert bis die letzte "Rotation" durchgeführt wurde
private static void leftPrecedence(Node root) {
for (Node child : root.getChildren()) {
private static void leftPrecedence(ASTNode root) {
for (ASTNode child : root.getChildren()) {
leftPrecedence(child);
}
Node expr = getExpr(root);
ASTNode expr = getExpr(root);
if (expr == null || root.getChildren().size() != 2 || !root.getValue().isEmpty()) {
return;
@ -60,9 +60,9 @@ public final class ExpressionBalancer {
}
// Die Letzte Rotation ist keine richtige Rotation, dort wird false zurückgegeben
private static boolean specialLeftRotate(Node root) {
Node left = root.getChildren().get(0);
Node right = root.getChildren().get(1);
private static boolean specialLeftRotate(ASTNode root) {
ASTNode left = root.getChildren().get(0);
ASTNode right = root.getChildren().get(1);
// Verhindert Wurzel mit nur einem EXPR-Child (nach oben "hängende" Wurzel)
if (endOfExpr(right)) {
@ -72,7 +72,7 @@ public final class ExpressionBalancer {
return false; // Braucht keine weitere Rotation
}
Node insertLeft = new Node(root.getName());
ASTNode insertLeft = new ASTNode(root.getName());
insertLeft.setValue(right.getValue()); // Operation wird linksvererbt
insertLeft.setChildren(left, right.getChildren().get(0));
@ -82,8 +82,8 @@ public final class ExpressionBalancer {
return true;
}
private static Node getExpr(Node root) {
for (Node child : root.getChildren()) {
private static ASTNode getExpr(ASTNode root) {
for (ASTNode child : root.getChildren()) {
if (child.getName().equals("EXPR")) {
return child;
}
@ -92,7 +92,7 @@ public final class ExpressionBalancer {
return null;
}
private static boolean endOfExpr(Node root) {
private static boolean endOfExpr(ASTNode root) {
return root.getChildren().size() == 1;
}
@ -108,8 +108,8 @@ public final class ExpressionBalancer {
operatorPrecedence(tree.getRoot());
}
public static void operatorPrecedence(Node root) {
for (Node child : root.getChildren()) {
public static void operatorPrecedence(ASTNode root) {
for (ASTNode child : root.getChildren()) {
operatorPrecedence(child);
}
@ -118,8 +118,8 @@ public final class ExpressionBalancer {
}
}
private static boolean preceding(Node root) {
Node op = getExpr(root);
private static boolean preceding(ASTNode root) {
ASTNode op = getExpr(root);
if (op == null || !root.getName().equals("EXPR") || root.getValue().isEmpty()) {
return false;
@ -134,11 +134,11 @@ public final class ExpressionBalancer {
|| (logHigh.contains(root.getValue()) && logLow.contains(op.getValue()));
}
private static void simpleRightRotate(Node root) {
Node left = root.getChildren().get(0);
Node right = root.getChildren().get(1);
private static void simpleRightRotate(ASTNode root) {
ASTNode left = root.getChildren().get(0);
ASTNode right = root.getChildren().get(1);
Node insertRight = new Node(root.getName());
ASTNode insertRight = new ASTNode(root.getName());
insertRight.setValue(root.getValue());
insertRight.setChildren(left.getChildren().get(1), right);

View File

@ -53,11 +53,11 @@ public final class ASTCompacter {
return compacted;
}
private static int compact(Node root, Grammar grammar) {
private static int compact(ASTNode root, Grammar grammar) {
int compacted = 0;
Collection<Node> toRemove = new HashSet<>();
Collection<ASTNode> toRemove = new HashSet<>();
for (Node child : root.getChildren()) {
for (ASTNode child : root.getChildren()) {
if (grammar.hasCompact(root.getName())
&& root.getChildren().size() == 1) {
@ -91,11 +91,11 @@ public final class ASTCompacter {
return removed;
}
private static int removeNullable(Node root, Grammar grammar) {
private static int removeNullable(ASTNode root, Grammar grammar) {
int removed = 0;
Collection<Node> toRemove = new HashSet<>();
Collection<ASTNode> toRemove = new HashSet<>();
for (Node child : root.getChildren()) {
for (ASTNode child : root.getChildren()) {
if (grammar.hasNullable(child.getName())
&& child.getValue().isEmpty()
&& !child.hasChildren()) {
@ -125,11 +125,11 @@ public final class ASTCompacter {
return removed;
}
private static int removeEpsilon(Node root, Grammar grammar) {
private static int removeEpsilon(ASTNode root, Grammar grammar) {
int removed = 0;
Collection<Node> toRemove = new HashSet<>();
Collection<ASTNode> toRemove = new HashSet<>();
for (Node child : root.getChildren()) {
for (ASTNode child : root.getChildren()) {
if (child.getName().equals(grammar.getEpsilonSymbol()) && !child.hasChildren()) {
log("Removing " + root.getName() + " -> " + child.getName());
toRemove.add(child);
@ -155,13 +155,13 @@ public final class ASTCompacter {
return removed;
}
private static int removeRedundant(Node root) {
private static int removeRedundant(ASTNode root) {
int removed = 0;
Collection<Node> toRemove = new HashSet<>();
Collection<ASTNode> toRemove = new HashSet<>();
Collection<String> removable = Arrays.asList("IF", "ELSE", "WHILE", "ASSIGN");
for (Node child : root.getChildren()) {
for (ASTNode child : root.getChildren()) {
if (removable.contains(child.getName()) && !child.hasChildren()) {
log("Removing " + root.getName() + " -> " + child.getName());
toRemove.add(child);
@ -182,7 +182,7 @@ public final class ASTCompacter {
log("-".repeat(100));
}
private static void renameEXPR(Node root) {
private static void renameEXPR(ASTNode root) {
String newName = switch (root.getName()) {
case "EXPR_2", "EXPR_F" -> "EXPR";
default -> root.getName();
@ -194,7 +194,7 @@ public final class ASTCompacter {
root.setName(newName);
for (Node child : root.getChildren()) {
for (ASTNode child : root.getChildren()) {
renameEXPR(child);
}
}
@ -206,12 +206,12 @@ public final class ASTCompacter {
log("-".repeat(100));
}
private static void moveOperatorToEXPR(Node root) {
for (Node child : root.getChildren()) {
private static void moveOperatorToEXPR(ASTNode root) {
for (ASTNode child : root.getChildren()) {
moveOperatorToEXPR(child);
}
Node op = getOp(root);
ASTNode op = getOp(root);
if (op == null || !"EXPR".equals(root.getName())) {
return;
@ -222,9 +222,9 @@ public final class ASTCompacter {
root.getChildren().remove(op);
}
private static Node getOp(Node root) {
for (Node child : root.getChildren()) {
Node op = switch (child.getName()) {
private static ASTNode getOp(ASTNode root) {
for (ASTNode child : root.getChildren()) {
ASTNode op = switch (child.getName()) {
case "ADD", "SUB", "MUL", "DIV", "MOD" -> child;
case "NOT", "AND", "OR" -> child;
case "LESS", "LESS_EQUAL", "GREATER", "GREATER_EQUAL", "EQUAL", "NOT_EQUAL" -> child;
@ -245,12 +245,12 @@ public final class ASTCompacter {
log("-".repeat(100));
}
private static void moveIdentifierToASSIGNMENT(Node root) {
for (Node child : root.getChildren()) {
private static void moveIdentifierToASSIGNMENT(ASTNode root) {
for (ASTNode child : root.getChildren()) {
moveIdentifierToASSIGNMENT(child);
}
Node id = getId(root);
ASTNode id = getId(root);
if (id == null || !"ASSIGNMENT".equals(root.getName())) {
return;
@ -261,8 +261,8 @@ public final class ASTCompacter {
root.getChildren().remove(id);
}
private static Node getId(Node root) {
for (Node child : root.getChildren()) {
private static ASTNode getId(ASTNode root) {
for (ASTNode child : root.getChildren()) {
if (child.getName().equals("IDENTIFIER")) {
return child;
}

View File

@ -6,26 +6,26 @@ import java.util.Iterator;
import java.util.List;
import java.util.Objects;
public class Node {
public class ASTNode {
private String name;
private String value;
private List<Node> children = new ArrayList<>();
private List<ASTNode> children = new ArrayList<>();
public Node(String name) {
public ASTNode(String name) {
this.name = name;
this.value = "";
}
public void addChild(Node node) {
this.children.add(node);
public void addChild(ASTNode ASTNode) {
this.children.add(ASTNode);
}
public boolean hasChildren() {
return !this.children.isEmpty();
}
public List<Node> getChildren() {
public List<ASTNode> getChildren() {
return this.children;
}
@ -54,6 +54,19 @@ public class Node {
return buffer.toString();
}
public void setChildren(List<ASTNode> children) {
this.children = children;
}
public void setChildren(ASTNode... children) {
this.children = new ArrayList<>();
this.children.addAll(Arrays.asList(children));
}
public String getValue() {
return this.value;
}
private void print(StringBuilder buffer, String prefix, String childrenPrefix) {
buffer.append(prefix);
buffer.append(this.name);
@ -63,8 +76,8 @@ public class Node {
}
buffer.append('\n');
for (Iterator<Node> it = this.children.listIterator(); it.hasNext(); ) {
Node next = it.next();
for (Iterator<ASTNode> it = this.children.listIterator(); it.hasNext(); ) {
ASTNode next = it.next();
if (it.hasNext()) {
next.print(buffer, childrenPrefix + "├── ", childrenPrefix + "");
} else {
@ -75,32 +88,19 @@ public class Node {
@Override
public boolean equals(Object obj) {
if (obj instanceof Node) {
return this.name.equals(((Node) obj).name)
&& this.value.equals(((Node) obj).value)
&& this.children.equals(((Node) obj).children);
if (obj instanceof ASTNode) {
return this.name.equals(((ASTNode) obj).name)
&& this.value.equals(((ASTNode) obj).value)
&& this.children.equals(((ASTNode) obj).children);
}
return false;
}
public String getValue() {
return this.value;
}
public void setChildren(List<Node> children) {
this.children = children;
}
public void setChildren(Node... children) {
this.children = new ArrayList<>();
this.children.addAll(Arrays.asList(children));
}
public long size() {
int s = 0;
for (Node child : this.children) {
for (ASTNode child : this.children) {
s += child.size();
}

View File

@ -1,6 +0,0 @@
package parser.grammar;
public enum Actions {
COMPACT,
NULLABLE
}

View File

@ -9,8 +9,8 @@ import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import static parser.grammar.Actions.COMPACT;
import static parser.grammar.Actions.NULLABLE;
import static parser.grammar.GrammarActions.COMPACT;
import static parser.grammar.GrammarActions.NULLABLE;
import static util.Logger.log;
public class Grammar {
@ -96,7 +96,7 @@ public class Grammar {
.collect(Collectors.toList());
// Check for action validity
List<String> enumActions = Arrays.stream(Actions.values())
List<String> enumActions = Arrays.stream(GrammarActions.values())
.map(action -> action.toString().toLowerCase())
.collect(Collectors.toList());
for (String flag : flagList) {

View File

@ -0,0 +1,8 @@
package parser.grammar;
public enum GrammarActions {
COMPACT, // Ersetzt Node mit Child, wenn es nur ein Child gibt
NULLABLE, // Entfernt Node, wenn dieser keinen Inhalt hat
MOVE, // TODO: Setzt den Child-Namen als Parent-Value und löscht das Child
RENAME // TODO: Führt eine Umbenennung durch
}

View File

@ -1,6 +1,6 @@
package parser.grammar;
import parser.LL1ParsingTable;
import parser.ParsingTable;
import java.util.AbstractMap;
import java.util.Arrays;
@ -16,16 +16,16 @@ import static util.Logger.log;
import static util.Logger.logIfTrue;
import static util.Logger.logNullable;
public class LL1GrammarAnalyzer {
public class GrammarAnalyzer {
private final Grammar grammar;
private final Map<String, Set<String>> first;
private final Map<String, Set<String>> follow;
private final LL1ParsingTable table;
private final ParsingTable table;
public LL1GrammarAnalyzer(Grammar grammar) {
public GrammarAnalyzer(Grammar grammar) {
this.grammar = grammar;
log("Analyzing Grammar:\n");
@ -214,7 +214,7 @@ public class LL1GrammarAnalyzer {
return followOut;
}
private LL1ParsingTable initParseTable() {
private ParsingTable initParseTable() {
Map<Map.Entry<String, String>, String> tableOut = new HashMap<>();
log("Parsetable Aufstellen:");
@ -265,7 +265,7 @@ public class LL1GrammarAnalyzer {
}
}
final LL1ParsingTable table = new LL1ParsingTable(this.grammar, tableOut);
final ParsingTable table = new ParsingTable(this.grammar, tableOut);
log("\n" + table);
log("-".repeat(100) + "\n");
@ -347,7 +347,7 @@ public class LL1GrammarAnalyzer {
return this.follow;
}
public LL1ParsingTable getTable() {
public ParsingTable getTable() {
return this.table;
}
}

View File

@ -1,7 +1,7 @@
package typechecker;
import parser.ast.AST;
import parser.ast.Node;
import parser.ast.ASTNode;
import java.util.HashMap;
import java.util.Map;
@ -48,14 +48,14 @@ public class SymbolTable {
return new SymbolTable(tableOut);
}
private static void scanTree(Node root, Map<String, String> table) {
for (Node child : root.getChildren()) {
private static void scanTree(ASTNode root, Map<String, String> table) {
for (ASTNode child : root.getChildren()) {
scanTree(child, table);
}
if ("DECLARATION".equals(root.getName())) {
Node left = root.getChildren().get(0);
Node right = root.getChildren().get(1);
ASTNode left = root.getChildren().get(0);
ASTNode right = root.getChildren().get(1);
log("Adding Entry " + right.getValue() + " -> " + left.getName());
String oldEntry = table.put(right.getValue(), left.getName());

View File

@ -0,0 +1,8 @@
package typechecker;
public class TypeMismatchException extends RuntimeException {
public TypeMismatchException(String message) {
super(message);
}
}

View File

@ -1,8 +0,0 @@
package typechecker;
public class WrongTypeException extends RuntimeException {
public WrongTypeException(String message) {
super(message);
}
}

View File

@ -5,8 +5,8 @@ import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.Lexer;
import org.junit.jupiter.api.Test;
import parser.ast.AST;
import parser.ast.ASTBalancer;
import parser.ast.ASTCompacter;
import parser.ast.ExpressionBalancer;
import parser.grammar.Grammar;
import java.io.IOException;
@ -34,7 +34,7 @@ class Demo {
void demoClean() throws URISyntaxException, IOException {
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
Grammar grammar = Grammar.fromFile(path);
LL1Parser parser = LL1Parser.fromGrammar(grammar);
Parser parser = Parser.fromGrammar(grammar);
Lexer lex = this.initLexer("General.stups");
AST tree = parser.parse(lex.getAllTokens(), lex.getVocabulary());
@ -46,15 +46,15 @@ class Demo {
void demoLeftPrecedence() throws URISyntaxException, IOException {
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
Grammar grammar = Grammar.fromFile(path);
LL1Parser parser = LL1Parser.fromGrammar(grammar);
Parser parser = Parser.fromGrammar(grammar);
Lexer lex = this.initLexer("General.stups");
AST tree = parser.parse(lex.getAllTokens(), lex.getVocabulary());
ASTCompacter.clean(tree, grammar);
ExpressionBalancer.flip(tree);
ASTBalancer.flip(tree);
System.out.println("Before left-precedence:\n" + tree);
ExpressionBalancer.leftPrecedence(tree);
ASTBalancer.leftPrecedence(tree);
System.out.println("After left-precedence:\n" + tree);
}
@ -62,16 +62,16 @@ class Demo {
void demoOperatorPrecedence() throws URISyntaxException, IOException {
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
Grammar grammar = Grammar.fromFile(path);
LL1Parser parser = LL1Parser.fromGrammar(grammar);
Parser parser = Parser.fromGrammar(grammar);
Lexer lex = this.initLexer("General.stups");
AST tree = parser.parse(lex.getAllTokens(), lex.getVocabulary());
ASTCompacter.clean(tree, grammar);
ExpressionBalancer.flip(tree);
ExpressionBalancer.leftPrecedence(tree);
ASTBalancer.flip(tree);
ASTBalancer.leftPrecedence(tree);
System.out.println("Before operator-precedence:\n" + tree);
ExpressionBalancer.operatorPrecedence(tree);
ASTBalancer.operatorPrecedence(tree);
System.out.println("After operator-precedence:\n" + tree);
}
}

View File

@ -31,7 +31,7 @@ class LexerParserGrammarTest {
@Test
void testEmptyFile() throws URISyntaxException, IOException {
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
LL1Parser parser = LL1Parser.fromGrammar(path);
Parser parser = Parser.fromGrammar(path);
Lexer lex = this.initLexer("EmptyFile.stups");
@ -41,7 +41,7 @@ class LexerParserGrammarTest {
@Test
void testEmptyMain() throws URISyntaxException, IOException {
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
LL1Parser parser = LL1Parser.fromGrammar(path);
Parser parser = Parser.fromGrammar(path);
Lexer lex = this.initLexer("EmptyMain.stups");
@ -51,7 +51,7 @@ class LexerParserGrammarTest {
@Test
void testGeneralComment() throws URISyntaxException, IOException {
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
LL1Parser parser = LL1Parser.fromGrammar(path);
Parser parser = Parser.fromGrammar(path);
Lexer lex = this.initLexer("GeneralComment.stups");
@ -61,7 +61,7 @@ class LexerParserGrammarTest {
@Test
void tesMultiDecl() throws URISyntaxException, IOException {
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
LL1Parser parser = LL1Parser.fromGrammar(path);
Parser parser = Parser.fromGrammar(path);
Lexer lex = this.initLexer("MultipleDeclarations.stups");
@ -71,7 +71,7 @@ class LexerParserGrammarTest {
@Test
void testDeclarationAssignment() throws URISyntaxException, IOException {
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
LL1Parser parser = LL1Parser.fromGrammar(path);
Parser parser = Parser.fromGrammar(path);
Lexer lex = this.initLexer("DeclarationAssignment.stups");
@ -81,7 +81,7 @@ class LexerParserGrammarTest {
@Test
void testExpr() throws URISyntaxException, IOException {
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
LL1Parser parser = LL1Parser.fromGrammar(path);
Parser parser = Parser.fromGrammar(path);
Lexer lex = this.initLexer("Expr.stups");
@ -91,7 +91,7 @@ class LexerParserGrammarTest {
@Test
void testGeneralWhile() throws URISyntaxException, IOException {
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
LL1Parser parser = LL1Parser.fromGrammar(path);
Parser parser = Parser.fromGrammar(path);
Lexer lex = this.initLexer("GeneralWhile.stups");
@ -101,7 +101,7 @@ class LexerParserGrammarTest {
@Test
void testGeneralIfElse() throws URISyntaxException, IOException {
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
LL1Parser parser = LL1Parser.fromGrammar(path);
Parser parser = Parser.fromGrammar(path);
Lexer lex = this.initLexer("GeneralIfElse.stups");

View File

@ -4,22 +4,22 @@ import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
class ExpressionBalancerTest {
class ASTBalancerTest {
//EXPR
// EXPR: SUB
//| INTEGER_LIT: 2
// INTEGER_LIT: 1
private static AST tree1() {
AST tree = new AST(new Node("EXPR"));
AST tree = new AST(new ASTNode("EXPR"));
Node right = new Node("INTEGER_LIT");
ASTNode right = new ASTNode("INTEGER_LIT");
right.setValue("1");
Node left = new Node("EXPR");
ASTNode left = new ASTNode("EXPR");
left.setValue("SUB");
Node lleft = new Node("INTEGER_LIT");
ASTNode lleft = new ASTNode("INTEGER_LIT");
lleft.setValue("2");
left.setChildren(lleft);
@ -29,21 +29,21 @@ class ExpressionBalancerTest {
}
private static AST tree2() {
AST tree = new AST(new Node("EXPR"));
AST tree = new AST(new ASTNode("EXPR"));
Node right = new Node("INTEGER_LIT");
ASTNode right = new ASTNode("INTEGER_LIT");
right.setValue("1");
Node left = new Node("EXPR");
ASTNode left = new ASTNode("EXPR");
left.setValue("SUB");
Node lleft = new Node("EXPR");
ASTNode lleft = new ASTNode("EXPR");
lleft.setValue("SUB");
Node lright = new Node("INTEGER_LIT");
ASTNode lright = new ASTNode("INTEGER_LIT");
lright.setValue("2");
Node llleft = new Node("INTEGER_LIT");
ASTNode llleft = new ASTNode("INTEGER_LIT");
llleft.setValue("3");
lleft.setChildren(llleft);
@ -55,21 +55,21 @@ class ExpressionBalancerTest {
}
private static AST tree3() {
AST tree = new AST(new Node("EXPR"));
AST tree = new AST(new ASTNode("EXPR"));
Node right = new Node("INTEGER_LIT");
ASTNode right = new ASTNode("INTEGER_LIT");
right.setValue("1");
Node left = new Node("EXPR");
ASTNode left = new ASTNode("EXPR");
left.setValue("SUB");
Node lleft = new Node("EXPR");
ASTNode lleft = new ASTNode("EXPR");
lleft.setValue("MUL");
Node lright = new Node("INTEGER_LIT");
ASTNode lright = new ASTNode("INTEGER_LIT");
lright.setValue("2");
Node llleft = new Node("INTEGER_LIT");
ASTNode llleft = new ASTNode("INTEGER_LIT");
llleft.setValue("3");
lleft.setChildren(llleft);
@ -85,7 +85,7 @@ class ExpressionBalancerTest {
AST tree = tree1();
System.out.println("Before:\n" + tree);
ExpressionBalancer.flip(tree);
ASTBalancer.flip(tree);
System.out.println("After:\n" + tree);
assertThat(tree.getRoot().getChildren().get(0).getName()).isEqualTo("INTEGER_LIT");
@ -97,8 +97,8 @@ class ExpressionBalancerTest {
AST tree = tree1();
System.out.println("Before:\n" + tree);
ExpressionBalancer.flip(tree);
ExpressionBalancer.flip(tree);
ASTBalancer.flip(tree);
ASTBalancer.flip(tree);
System.out.println("After:\n" + tree);
assertThat(tree).isEqualTo(tree1());
@ -109,7 +109,7 @@ class ExpressionBalancerTest {
AST tree = tree2();
System.out.println("Before:\n" + tree);
ExpressionBalancer.flip(tree);
ASTBalancer.flip(tree);
System.out.println("After:\n" + tree);
assertThat(tree.getRoot().getChildren().get(0).getName()).isEqualTo("INTEGER_LIT");
@ -123,8 +123,8 @@ class ExpressionBalancerTest {
AST tree = tree2();
System.out.println("Before:\n" + tree);
ExpressionBalancer.flip(tree);
ExpressionBalancer.flip(tree);
ASTBalancer.flip(tree);
ASTBalancer.flip(tree);
System.out.println("After:\n" + tree);
assertThat(tree).isEqualTo(tree2());
@ -133,10 +133,10 @@ class ExpressionBalancerTest {
@Test
void testTree1LeftPrecedence() {
AST tree = tree1();
ExpressionBalancer.flip(tree);
ASTBalancer.flip(tree);
System.out.println("Before:\n" + tree);
ExpressionBalancer.leftPrecedence(tree);
ASTBalancer.leftPrecedence(tree);
System.out.println("After:\n" + tree);
assertThat(tree.size()).isEqualTo(3);
@ -146,10 +146,10 @@ class ExpressionBalancerTest {
@Test
void testTree2LeftPrecedence() {
AST tree = tree2();
ExpressionBalancer.flip(tree);
ASTBalancer.flip(tree);
System.out.println("Before:\n" + tree);
ExpressionBalancer.leftPrecedence(tree);
ASTBalancer.leftPrecedence(tree);
System.out.println("After:\n" + tree);
assertThat(tree.size()).isEqualTo(5);
@ -159,15 +159,15 @@ class ExpressionBalancerTest {
@Test
void testTree2OperatorPrecedence() {
AST tree = tree2();
ExpressionBalancer.flip(tree);
ExpressionBalancer.leftPrecedence(tree);
ASTBalancer.flip(tree);
ASTBalancer.leftPrecedence(tree);
System.out.println("Before:\n" + tree);
AST tree1 = tree2();
ExpressionBalancer.flip(tree1);
ExpressionBalancer.leftPrecedence(tree1);
ASTBalancer.flip(tree1);
ASTBalancer.leftPrecedence(tree1);
ExpressionBalancer.operatorPrecedence(tree);
ASTBalancer.operatorPrecedence(tree);
System.out.println("After:\n" + tree);
assertThat(tree).isEqualTo(tree1);
@ -176,13 +176,13 @@ class ExpressionBalancerTest {
@Test
void testTree3OperatorPrecedence() {
AST tree = tree3();
ExpressionBalancer.flip(tree);
ExpressionBalancer.leftPrecedence(tree);
ASTBalancer.flip(tree);
ASTBalancer.leftPrecedence(tree);
System.out.println("Before:\n" + tree);
assertThat(tree.getRoot().getValue()).isEqualTo("MUL");
ExpressionBalancer.operatorPrecedence(tree);
ASTBalancer.operatorPrecedence(tree);
System.out.println("After:\n" + tree);
assertThat(tree.size()).isEqualTo(5);

View File

@ -4,7 +4,7 @@ import lexer.StupsLexer;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.Lexer;
import org.junit.jupiter.api.Test;
import parser.LL1Parser;
import parser.Parser;
import parser.grammar.Grammar;
import java.io.IOException;
@ -34,7 +34,7 @@ class ASTCompacterTest {
void testRemoveEpsilon() throws URISyntaxException, IOException {
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
Grammar grammar = Grammar.fromFile(path);
LL1Parser parser = LL1Parser.fromGrammar(grammar);
Parser parser = Parser.fromGrammar(grammar);
Lexer lex = this.initLexer("GeneralOperator.stups");
AST tree = parser.parse(lex.getAllTokens(), lex.getVocabulary());
@ -47,7 +47,7 @@ class ASTCompacterTest {
void testCompact() throws URISyntaxException, IOException {
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
Grammar grammar = Grammar.fromFile(path);
LL1Parser parser = LL1Parser.fromGrammar(grammar);
Parser parser = Parser.fromGrammar(grammar);
Lexer lex = this.initLexer("GeneralOperator.stups");
AST tree = parser.parse(lex.getAllTokens(), lex.getVocabulary());
@ -60,7 +60,7 @@ class ASTCompacterTest {
void testRemoveNullable() throws URISyntaxException, IOException {
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
Grammar grammar = Grammar.fromFile(path);
LL1Parser parser = LL1Parser.fromGrammar(grammar);
Parser parser = Parser.fromGrammar(grammar);
Lexer lex = this.initLexer("GeneralOperator.stups");
AST tree = parser.parse(lex.getAllTokens(), lex.getVocabulary());
@ -74,7 +74,7 @@ class ASTCompacterTest {
void testClean() throws URISyntaxException, IOException {
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
Grammar grammar = Grammar.fromFile(path);
LL1Parser parser = LL1Parser.fromGrammar(grammar);
Parser parser = Parser.fromGrammar(grammar);
Lexer lex = this.initLexer("GeneralOperator.stups");
AST tree = parser.parse(lex.getAllTokens(), lex.getVocabulary());

View File

@ -8,7 +8,7 @@ class ASTTest {
@Test
void testOneNode() {
Node root = new Node("Wurzel");
ASTNode root = new ASTNode("Wurzel");
AST tree = new AST(root);
System.out.println(tree);
@ -18,9 +18,9 @@ class ASTTest {
@Test
void testThreeNodesBinary() {
Node root = new Node("Wurzel");
Node childA = new Node("A");
Node childB = new Node("B");
ASTNode root = new ASTNode("Wurzel");
ASTNode childA = new ASTNode("A");
ASTNode childB = new ASTNode("B");
root.addChild(childA);
root.addChild(childB);
@ -33,9 +33,9 @@ class ASTTest {
@Test
void testThreeNodesLinear() {
Node root = new Node("Wurzel");
Node childA = new Node("A");
Node childB = new Node("B");
ASTNode root = new ASTNode("Wurzel");
ASTNode childA = new ASTNode("A");
ASTNode childB = new ASTNode("B");
root.addChild(childA);
childA.addChild(childB);

View File

@ -2,7 +2,7 @@ package parser.grammar;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import parser.LL1ParsingTable;
import parser.ParsingTable;
import java.util.Arrays;
import java.util.HashSet;
@ -10,7 +10,7 @@ import java.util.Set;
import static org.assertj.core.api.Assertions.assertThat;
class LL1GrammarAnalyzerTest {
class GrammarAnalyzerTest {
private static Grammar grammar0;
private static Grammar grammar1;
@ -108,7 +108,7 @@ class LL1GrammarAnalyzerTest {
@Test
void testFirstGrammar0() {
LL1GrammarAnalyzer analyzer = new LL1GrammarAnalyzer(grammar0);
GrammarAnalyzer analyzer = new GrammarAnalyzer(grammar0);
assertThat(analyzer.getFirst().get("S")).containsOnly("i", "a");
assertThat(analyzer.getFirst().get("E")).containsOnly("b");
@ -116,7 +116,7 @@ class LL1GrammarAnalyzerTest {
@Test
void testFirstGrammar1() {
LL1GrammarAnalyzer analyzer = new LL1GrammarAnalyzer(grammar1);
GrammarAnalyzer analyzer = new GrammarAnalyzer(grammar1);
assertThat(analyzer.getFirst().get("E")).containsOnly("id", "(");
assertThat(analyzer.getFirst().get("E2")).containsOnly("+", grammar1.getEpsilonSymbol());
@ -127,7 +127,7 @@ class LL1GrammarAnalyzerTest {
@Test
void testFirstGrammar2() {
LL1GrammarAnalyzer analyzer = new LL1GrammarAnalyzer(grammar2);
GrammarAnalyzer analyzer = new GrammarAnalyzer(grammar2);
assertThat(analyzer.getFirst().get("X")).containsOnly("c", "a", grammar2.getEpsilonSymbol());
assertThat(analyzer.getFirst().get("Y")).containsOnly("c", grammar2.getEpsilonSymbol());
@ -136,7 +136,7 @@ class LL1GrammarAnalyzerTest {
@Test
void testFollowGrammar0() {
LL1GrammarAnalyzer analyzer = new LL1GrammarAnalyzer(grammar0);
GrammarAnalyzer analyzer = new GrammarAnalyzer(grammar0);
assertThat(analyzer.getFollow().get("S")).containsOnly("$");
assertThat(analyzer.getFollow().get("E")).containsOnly("t");
@ -144,7 +144,7 @@ class LL1GrammarAnalyzerTest {
@Test
void testFollowGrammar1() {
LL1GrammarAnalyzer analyzer = new LL1GrammarAnalyzer(grammar1);
GrammarAnalyzer analyzer = new GrammarAnalyzer(grammar1);
assertThat(analyzer.getFollow().get("E")).containsOnly(")", "$");
assertThat(analyzer.getFollow().get("E2")).containsOnly(")", "$");
@ -155,7 +155,7 @@ class LL1GrammarAnalyzerTest {
@Test
void testFollowGrammar2() {
LL1GrammarAnalyzer analyzer = new LL1GrammarAnalyzer(grammar2);
GrammarAnalyzer analyzer = new GrammarAnalyzer(grammar2);
assertThat(analyzer.getFollow().get("X")).containsOnly("a", "c", "d");
assertThat(analyzer.getFollow().get("Y")).containsOnly("a", "c", "d");
@ -164,8 +164,8 @@ class LL1GrammarAnalyzerTest {
@Test
void testTableGrammar1() {
LL1GrammarAnalyzer analyzer = new LL1GrammarAnalyzer(grammar1);
LL1ParsingTable table = analyzer.getTable();
GrammarAnalyzer analyzer = new GrammarAnalyzer(grammar1);
ParsingTable table = analyzer.getTable();
assertThat(table.get("E", "id")).isEqualTo("T E2");
assertThat(table.get("E", "(")).isEqualTo("T E2");

View File

@ -4,7 +4,7 @@ import lexer.StupsLexer;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.Lexer;
import org.junit.jupiter.api.Test;
import parser.LL1Parser;
import parser.Parser;
import parser.ast.AST;
import parser.grammar.Grammar;
import typechecker.SymbolAlreadyDefinedException;
@ -38,7 +38,7 @@ class SymbolTableTest {
void testSingleSymbol() throws URISyntaxException, IOException {
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
Grammar grammar = Grammar.fromFile(path);
LL1Parser parser = LL1Parser.fromGrammar(grammar);
Parser parser = Parser.fromGrammar(grammar);
Lexer lex = this.initLexer("SingleSymbol.stups");
AST tree = parser.parse(lex.getAllTokens(), lex.getVocabulary());
@ -54,7 +54,7 @@ class SymbolTableTest {
void testMultipleSymbol() throws URISyntaxException, IOException {
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
Grammar grammar = Grammar.fromFile(path);
LL1Parser parser = LL1Parser.fromGrammar(grammar);
Parser parser = Parser.fromGrammar(grammar);
Lexer lex = this.initLexer("MultipleSymbol.stups");
AST tree = parser.parse(lex.getAllTokens(), lex.getVocabulary());
@ -75,7 +75,7 @@ class SymbolTableTest {
void testExistingSymbol() throws URISyntaxException, IOException {
Path path = Paths.get(this.getClass().getClassLoader().getResource("exampleGrammars/Grammar.grammar").toURI());
Grammar grammar = Grammar.fromFile(path);
LL1Parser parser = LL1Parser.fromGrammar(grammar);
Parser parser = Parser.fromGrammar(grammar);
Lexer lex = this.initLexer("ExistingSymbol.stups");
AST tree = parser.parse(lex.getAllTokens(), lex.getVocabulary());