fix grammar parsing bug
This commit is contained in:
@ -1,5 +1,7 @@
|
|||||||
package parser.grammar;
|
package parser.grammar;
|
||||||
|
|
||||||
|
import util.tools.Logger;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
@ -31,7 +33,9 @@ public class Grammar {
|
|||||||
public static Grammar fromFile(Path path) throws IOException {
|
public static Grammar fromFile(Path path) throws IOException {
|
||||||
List<String> lines = Files.readAllLines(path);
|
List<String> lines = Files.readAllLines(path);
|
||||||
|
|
||||||
lines = lines.stream().filter(line -> !(line.isBlank() || line.startsWith("//")))
|
lines = lines.stream()
|
||||||
|
.map(String::trim)
|
||||||
|
.filter(line -> !(line.isBlank() || line.startsWith("//")))
|
||||||
.collect(Collectors.toUnmodifiableList());
|
.collect(Collectors.toUnmodifiableList());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -41,8 +45,12 @@ public class Grammar {
|
|||||||
Set<String> nonterminals = new HashSet<>();
|
Set<String> nonterminals = new HashSet<>();
|
||||||
|
|
||||||
Set<GrammarRule> rules = new HashSet<>();
|
Set<GrammarRule> rules = new HashSet<>();
|
||||||
|
|
||||||
|
Logger.log("Parsing Grammar from File:");
|
||||||
for (String line : lines) {
|
for (String line : lines) {
|
||||||
|
|
||||||
|
Logger.log("Parsed: " + line);
|
||||||
|
|
||||||
if (line.startsWith("START:")) {
|
if (line.startsWith("START:")) {
|
||||||
|
|
||||||
startSymbol = line.split(" ")[1];
|
startSymbol = line.split(" ")[1];
|
||||||
@ -73,6 +81,7 @@ public class Grammar {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Logger.log("-".repeat(100));
|
||||||
|
|
||||||
return new Grammar(terminals, nonterminals,
|
return new Grammar(terminals, nonterminals,
|
||||||
startSymbol, epsilonSymbol,
|
startSymbol, epsilonSymbol,
|
||||||
|
|||||||
Reference in New Issue
Block a user