improve grammar parsing
This commit is contained in:
@ -35,33 +35,42 @@ public class Grammar {
|
|||||||
.collect(Collectors.toUnmodifiableList());
|
.collect(Collectors.toUnmodifiableList());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
String startSymbol = lines.get(0).split(" ")[1];
|
String startSymbol = "";
|
||||||
String epsilonSymbol = lines.get(1).split(" ")[1];
|
String epsilonSymbol = "";
|
||||||
|
Set<String> terminals = new HashSet<>();
|
||||||
String[] term = lines.get(2).split(" ");
|
Set<String> nonterminals = new HashSet<>();
|
||||||
term = Arrays.copyOfRange(term, 1, term.length);
|
|
||||||
Set<String> terminals = new HashSet<>(Arrays.asList(term));
|
|
||||||
|
|
||||||
String[] nterm = lines.get(3).split(" ");
|
|
||||||
nterm = Arrays.copyOfRange(nterm, 1, nterm.length);
|
|
||||||
Set<String> nonterminals = new HashSet<>(Arrays.asList(nterm));
|
|
||||||
|
|
||||||
Set<GrammarRule> rules = new HashSet<>();
|
Set<GrammarRule> rules = new HashSet<>();
|
||||||
for (int i = 4; i < lines.size(); i++) {
|
for (String line : lines) {
|
||||||
// "S -> E T2 | EPS" wird zu leftside = "S" und rightside = "E T2 | epsilon"
|
|
||||||
String[] split = lines.get(i)
|
|
||||||
.replaceAll("EPS", epsilonSymbol)
|
|
||||||
.split("->");
|
|
||||||
|
|
||||||
String leftside = split[0].trim();
|
if (line.startsWith("START:")) {
|
||||||
String rightside = split[1].trim();
|
|
||||||
|
|
||||||
// "E T2 | epsilon" wird zu prods[0] = "E T2" und prods[1] = "epsilon"
|
startSymbol = line.split(" ")[1];
|
||||||
String[] prods = rightside.split("\\|");
|
} else if (line.startsWith("EPS:")) {
|
||||||
|
|
||||||
for (String prod : prods) {
|
epsilonSymbol = line.split(" ")[1];
|
||||||
GrammarRule rule = new GrammarRule(leftside, prod.split(" "));
|
} else if (line.startsWith("TERM:")) {
|
||||||
rules.add(rule);
|
|
||||||
|
terminals.addAll(Arrays.stream(line.split(" ")).skip(1).collect(Collectors.toSet()));
|
||||||
|
} else if (line.startsWith("NTERM:")) {
|
||||||
|
|
||||||
|
nonterminals.addAll(Arrays.stream(line.split(" ")).skip(1).collect(Collectors.toSet()));
|
||||||
|
} else {
|
||||||
|
|
||||||
|
// "S -> E T2 | EPS" wird zu leftside = "S" und rightside = "E T2 | epsilon"
|
||||||
|
String[] split = line.replaceAll("EPS", epsilonSymbol)
|
||||||
|
.split("->");
|
||||||
|
|
||||||
|
String leftside = split[0].trim();
|
||||||
|
String rightside = split[1].trim();
|
||||||
|
|
||||||
|
// "E T2 | epsilon" wird zu prods[0] = "E T2" und prods[1] = "epsilon"
|
||||||
|
String[] prods = rightside.split("\\|");
|
||||||
|
|
||||||
|
for (String prod : prods) {
|
||||||
|
GrammarRule rule = new GrammarRule(leftside, prod.split(" "));
|
||||||
|
rules.add(rule);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user