refactor action parsing
This commit is contained in:
@ -5,9 +5,7 @@ import parser.Actions;
|
|||||||
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;
|
||||||
import java.util.AbstractMap.SimpleEntry;
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
@ -16,6 +14,7 @@ import java.util.Map;
|
|||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import static parser.Actions.COMPACT;
|
||||||
import static util.tools.Logger.log;
|
import static util.tools.Logger.log;
|
||||||
|
|
||||||
public class Grammar {
|
public class Grammar {
|
||||||
@ -25,29 +24,19 @@ public class Grammar {
|
|||||||
private final String startSymbol;
|
private final String startSymbol;
|
||||||
private final String epsilonSymbol;
|
private final String epsilonSymbol;
|
||||||
|
|
||||||
private final Map<Map.Entry<String, String>, Set<String>> actions;
|
// Actions
|
||||||
|
private final Map<String, Set<String>> compact;
|
||||||
|
|
||||||
private final Set<GrammarRule> rules;
|
private final Set<GrammarRule> rules;
|
||||||
|
|
||||||
public Grammar(Set<String> terminals, Set<String> nonterminals,
|
public Grammar(Set<String> terminals, Set<String> nonterminals,
|
||||||
String startSymbol, String epsilonSymbol,
|
String startSymbol, String epsilonSymbol,
|
||||||
Set<GrammarRule> rules) {
|
Map<String, Set<String>> compact, Set<GrammarRule> rules) {
|
||||||
this.terminals = terminals;
|
this.terminals = terminals;
|
||||||
this.nonterminals = nonterminals;
|
this.nonterminals = nonterminals;
|
||||||
this.startSymbol = startSymbol;
|
this.startSymbol = startSymbol;
|
||||||
this.epsilonSymbol = epsilonSymbol;
|
this.epsilonSymbol = epsilonSymbol;
|
||||||
this.actions = null;
|
this.compact = compact;
|
||||||
this.rules = rules;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Grammar(Set<String> terminals, Set<String> nonterminals,
|
|
||||||
String startSymbol, String epsilonSymbol,
|
|
||||||
Map<Map.Entry<String, String>, Set<String>> actions, Set<GrammarRule> rules) {
|
|
||||||
this.terminals = terminals;
|
|
||||||
this.nonterminals = nonterminals;
|
|
||||||
this.startSymbol = startSymbol;
|
|
||||||
this.epsilonSymbol = epsilonSymbol;
|
|
||||||
this.actions = actions;
|
|
||||||
this.rules = rules;
|
this.rules = rules;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -65,7 +54,7 @@ public class Grammar {
|
|||||||
Set<String> terminals = new HashSet<>();
|
Set<String> terminals = new HashSet<>();
|
||||||
Set<String> nonterminals = new HashSet<>();
|
Set<String> nonterminals = new HashSet<>();
|
||||||
|
|
||||||
Map<Map.Entry<String, String>, Set<String>> actions = new HashMap<>();
|
Map<String, Set<String>> compact = new HashMap<>();
|
||||||
Set<GrammarRule> rules = new HashSet<>();
|
Set<GrammarRule> rules = new HashSet<>();
|
||||||
|
|
||||||
log("Parsing Grammar from File:");
|
log("Parsing Grammar from File:");
|
||||||
@ -116,20 +105,20 @@ public class Grammar {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// "S[C R]" wird zu "S"
|
// "S[C R]" wird zu "S"
|
||||||
leftside = leftside.substring(0, open);
|
leftside = leftside.substring(0, open).trim();
|
||||||
}
|
}
|
||||||
|
|
||||||
// "E T2 | epsilon" wird zu prods[0] = "E T2" und prods[1] = "epsilon"
|
// "E T2 | epsilon" wird zu prods[0] = "E T2" und prods[1] = "epsilon"
|
||||||
String[] prods = rightside.split("\\|");
|
String[] prods = rightside.split("\\|");
|
||||||
|
|
||||||
|
compact.put(leftside, new HashSet<>());
|
||||||
for (String prod : prods) {
|
for (String prod : prods) {
|
||||||
GrammarRule rule = new GrammarRule(leftside, prod.split(" "));
|
GrammarRule rule = new GrammarRule(leftside, prod.split(" "));
|
||||||
rules.add(rule);
|
rules.add(rule);
|
||||||
|
|
||||||
actions.put(new SimpleEntry<>(leftside, prod), new HashSet<>());
|
if (flagList.contains(COMPACT.toString().toLowerCase())) {
|
||||||
actions.get(new SimpleEntry<>(leftside, prod)).addAll(flagList);
|
compact.get(leftside).add(prod.trim());
|
||||||
if (!flagList.isEmpty()) {
|
log("Registered compact: " + leftside + " -> " + prod.trim());
|
||||||
log("Registered actions: " + leftside + " -> " + prod + ": " + flagList);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -139,15 +128,15 @@ public class Grammar {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
log("\n" + actions);
|
log("\n" + compact);
|
||||||
log("-".repeat(100));
|
log("-".repeat(100));
|
||||||
|
|
||||||
return new Grammar(terminals, nonterminals,
|
return new Grammar(terminals, nonterminals,
|
||||||
startSymbol, epsilonSymbol,
|
startSymbol, epsilonSymbol,
|
||||||
rules);
|
compact, rules);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
System.out.println("Die Grammatik kann nicht gelesen werden!");
|
log("Die Grammatik kann nicht gelesen werden!");
|
||||||
System.out.println(path);
|
log(path.toString());
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -187,15 +176,7 @@ public class Grammar {
|
|||||||
.collect(Collectors.toUnmodifiableSet());
|
.collect(Collectors.toUnmodifiableSet());
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<String> getActions(String leftside, String rightside) {
|
public boolean hasCompact(String leftside, String rightside) {
|
||||||
return this.actions == null ? Collections.emptySet() : this.actions.get(new SimpleEntry<>(leftside, rightside));
|
return this.compact != null && this.compact.get(leftside).contains(rightside);
|
||||||
}
|
|
||||||
|
|
||||||
public Set<String> getAllActions(String rightside) {
|
|
||||||
return this.actions.entrySet().stream()
|
|
||||||
.filter(entry -> entry.getKey().getValue().equals(rightside))
|
|
||||||
.map(Map.Entry::getValue)
|
|
||||||
.flatMap(Collection::stream)
|
|
||||||
.collect(Collectors.toUnmodifiableSet());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user