make stuff final

This commit is contained in:
ChUrl
2020-12-15 16:58:52 +01:00
parent 1738771d1d
commit 4721f80f9c
7 changed files with 85 additions and 86 deletions

View File

@ -37,14 +37,14 @@ public final class StupsCompiler {
try {
// Relativer Pfad
Path programPath = Paths.get(System.getProperty("user.dir") + "/" + filename);
final Path programPath = Paths.get(System.getProperty("user.dir") + "/" + filename);
lexer = new StupsLexer(CharStreams.fromPath(programPath));
} catch (IOException e) {
try {
// Absoluter Pfad
Path programPath = Paths.get(filename);
final Path programPath = Paths.get(filename);
lexer = new StupsLexer(CharStreams.fromPath(programPath));
} catch (IOException ee) {
System.out.println("Das Programm konnte nicht gelesen werden.");
@ -52,18 +52,18 @@ public final class StupsCompiler {
}
}
Grammar grammar;
final Grammar grammar;
try {
Path grammarFile = Paths.get(System.getProperty("user.dir") + "/stups.grammar");
final Path grammarFile = Paths.get(System.getProperty("user.dir") + "/stups.grammar");
grammar = Grammar.fromFile(grammarFile);
} catch (IOException e) {
System.out.println("Die Grammatik konnte nicht geöffnet werden.");
return;
}
StupsParser stupsParser = StupsParser.fromGrammar(grammar);
final StupsParser stupsParser = StupsParser.fromGrammar(grammar);
AST tree = stupsParser.parse(lexer.getAllTokens(), lexer.getVocabulary());
final AST tree = stupsParser.parse(lexer.getAllTokens(), lexer.getVocabulary());
tree.postprocess(grammar);
TypeChecker.validate(tree);

View File

@ -29,14 +29,14 @@ public class StupsParser {
}
public static StupsParser fromGrammar(Grammar grammar) {
GrammarAnalyzer analyzer = new GrammarAnalyzer(grammar);
final GrammarAnalyzer analyzer = new GrammarAnalyzer(grammar);
return new StupsParser(analyzer.getTable());
}
public AST parse(List<? extends Token> token, Vocabulary voc) {
ASTNode root = new ASTNode(this.parsetable.getStartSymbol(), 0);
AST tree = new AST(root);
Deque<ASTNode> stack = new ArrayDeque<>();
final ASTNode root = new ASTNode(this.parsetable.getStartSymbol(), 0);
final AST tree = new AST(root);
final Deque<ASTNode> stack = new ArrayDeque<>();
stack.push(root);
int inputPosition = 0;
@ -92,16 +92,16 @@ public class StupsParser {
// Hier wird auch der AST aufgebaut
log("Used: " + top + " -> " + prod);
ASTNode pop = stack.pop();
final ASTNode pop = stack.pop();
final String[] split = prod.split(" ");
for (int i = split.length - 1; i >= 0; i--) {
ASTNode node = new ASTNode(split[i], currentLine);
final ASTNode node = new ASTNode(split[i], currentLine);
if (inputPosition + i < token.size()) {
// Die Schleife geht in der Eingabe weiter
Token currentTok = token.get(inputPosition + i);
final Token currentTok = token.get(inputPosition + i);
// Die Token mit semantischem Inhalt auswählen
if ("IDENTIFIER".equals(split[i]) || split[i].endsWith("_LIT")) {
@ -124,10 +124,10 @@ public class StupsParser {
}
private void printSourceLine(int line, List<? extends Token> token) {
Optional<String> srcLine = token.stream()
.filter(tok -> tok.getLine() == line)
.map(Token::getText)
.reduce((s1, s2) -> s1 + " " + s2);
final Optional<String> srcLine = token.stream()
.filter(tok -> tok.getLine() == line)
.map(Token::getText)
.reduce((s1, s2) -> s1 + " " + s2);
srcLine.ifPresent(s -> System.out.println(" :: " + s));
}

View File

@ -91,11 +91,11 @@ public class GrammarAnalyzer {
// ...and epsilon is in all of first(Y1) ... first(Yi-1).
// Because a != epsilon
Set<String> firstYiNoEps = firstOut.get(split[i]).stream()
.filter(sym -> !sym.equals(this.grammar.getEpsilonSymbol()))
.collect(Collectors.toSet());
final Set<String> firstYiNoEps = firstOut.get(split[i]).stream()
.filter(sym -> !sym.equals(this.grammar.getEpsilonSymbol()))
.collect(Collectors.toSet());
boolean changeNow = firstOut.get(leftside).addAll(firstYiNoEps);
final boolean changeNow = firstOut.get(leftside).addAll(firstYiNoEps);
change = change || changeNow;
logIfTrue(changeNow, "First: Added " + firstYiNoEps + " to " + leftside + " (All before are nullable)");
@ -104,7 +104,7 @@ public class GrammarAnalyzer {
if (i == split.length - 1 && allNullable.test(split)) {
// 2. (b) If epsilon is in first(Y1) ... first(Yk), then add epsilon to first(X).
boolean changeNow = firstOut.get(leftside).add(this.grammar.getEpsilonSymbol());
final boolean changeNow = firstOut.get(leftside).add(this.grammar.getEpsilonSymbol());
change = change || changeNow;
logIfTrue(changeNow, "First: Added " + this.grammar.getEpsilonSymbol() + " to " + leftside + " (All are nullable)");
@ -115,7 +115,7 @@ public class GrammarAnalyzer {
if (rightside.equals(this.grammar.getEpsilonSymbol())) {
// 3. If X -> epsilon is a production, then add epsilon to first(X).
boolean changeNow = firstOut.get(leftside).add(this.grammar.getEpsilonSymbol());
final boolean changeNow = firstOut.get(leftside).add(this.grammar.getEpsilonSymbol());
change = change || changeNow;
logIfTrue(changeNow, "First: Added " + this.grammar.getEpsilonSymbol() + " to " + leftside + " (X -> EPS exists)");
@ -177,7 +177,7 @@ public class GrammarAnalyzer {
.filter(sym -> !sym.equals(this.grammar.getEpsilonSymbol()))
.collect(Collectors.toSet());
boolean changeNow = followOut.get(split[i - 1]).addAll(firstXkNoEps);
final boolean changeNow = followOut.get(split[i - 1]).addAll(firstXkNoEps);
change = change || changeNow;
logIfTrue(changeNow, "Follow: Added " + firstXkNoEps + " to " + split[i - 1] + " (Dazwischen nullable)");
@ -190,7 +190,7 @@ public class GrammarAnalyzer {
if (this.allNullable(sub)) {
boolean changeNow = followOut.get(split[i - 1]).addAll(followOut.get(leftside));
final boolean changeNow = followOut.get(split[i - 1]).addAll(followOut.get(leftside));
change = change || changeNow;
logIfTrue(changeNow, "Follow: Added " + leftside + " to " + split[i - 1] + " (Dahinter nullable)");
@ -200,7 +200,7 @@ public class GrammarAnalyzer {
if (this.grammar.getNonterminals().contains(split[split.length - 1])) {
// 3. (a) If there is a production A -> aB, then everything in follow(A) is in follow(B).
boolean changeNow = followOut.get(split[split.length - 1]).addAll(followOut.get(leftside));
final boolean changeNow = followOut.get(split[split.length - 1]).addAll(followOut.get(leftside));
change = change || changeNow;
logIfTrue(changeNow, "Follow: Added " + followOut.get(leftside) + " to " + split[split.length - 1] + " (Ende der Regel)");
@ -217,7 +217,7 @@ public class GrammarAnalyzer {
}
private ParsingTable initParseTable() {
Map<Map.Entry<String, String>, String> tableOut = new HashMap<>();
final Map<Map.Entry<String, String>, String> tableOut = new HashMap<>();
log("Parsetable Aufstellen:");
@ -231,7 +231,7 @@ public class GrammarAnalyzer {
for (String sym : firstRightside) {
// 1. For each terminal t in first(a), add A -> a to table[A, t]
String prev = tableOut.put(new AbstractMap.SimpleEntry<>(leftside, sym), rightside);
final String prev = tableOut.put(new AbstractMap.SimpleEntry<>(leftside, sym), rightside);
log("Add " + rightside + " to cell (" + leftside + ", " + sym + ") (" + sym + " in first of " + rightside + ")");
logNullable("Overwritten " + prev + "!\n", prev);
@ -249,7 +249,7 @@ public class GrammarAnalyzer {
for (String sym : followLeftside) {
// ...for each terminal b in follow(A), add A -> a to table[A, b].
String prev = tableOut.put(new AbstractMap.SimpleEntry<>(leftside, sym), rightside);
final String prev = tableOut.put(new AbstractMap.SimpleEntry<>(leftside, sym), rightside);
log("Add " + rightside + " to cell (" + leftside + ", " + sym + ") (" + sym + " in follow of " + leftside + ")");
logNullable("Overwritten " + prev + "!\n", prev);
@ -258,7 +258,7 @@ public class GrammarAnalyzer {
if (followLeftside.contains("$")) {
// If epsilon is in first(a) and $ is in follow(A), add A -> a to table[A, $].
String prev = tableOut.put(new AbstractMap.SimpleEntry<>(leftside, "$"), rightside);
final String prev = tableOut.put(new AbstractMap.SimpleEntry<>(leftside, "$"), rightside);
log("Add " + rightside + " to cell (" + leftside + ", $) (epsilon in first of " + rightside + " and $ in follow of " + leftside + ")");
logNullable("Overwritten " + prev + "!\n", prev);
@ -305,7 +305,7 @@ public class GrammarAnalyzer {
if (this.allNullable(sub)) {
// X1 ... Xi-1 are nullable, so first(X1 ... Xn) contains first(Xi)
Set<String> firstXiNoEps;
final Set<String> firstXiNoEps;
if (split.length == 1 && split[0].equals(this.grammar.getEpsilonSymbol())) {
// Stream collect has to be evaluated, doesn't work on empty stream