Commit 64498cc6 authored by Alexandru Dura's avatar Alexandru Dura
Browse files

Make the node labels accessible from outside the package

parent 7863c686
......@@ -8,6 +8,7 @@ public class Grammar {
private HashMap<Category, Integer> cat2int = new HashMap<>();
private HashMap<Integer, Category> int2cat = new HashMap<>();
private HashMap<Category, ArrayList<Rule>> grammarRules = new HashMap<>();
private HashMap<EarleyRule, Rule> earleyRule2GrammarRule = new HashMap<>();
private int nonTermIndex = 1;
private int termIndex = -1;
......@@ -65,7 +66,9 @@ public class Grammar {
for (int j = 0; j < body.length; ++j) {
body[j] = cat2int.get(b.getBody().get(j));
}
eBodies.add(new EarleyRule(i, body));
EarleyRule eRule = new EarleyRule(i, body);
earleyRule2GrammarRule.put(eRule, b);
eBodies.add(eRule);
}
rules.add(eBodies);
}
......@@ -97,4 +100,11 @@ public class Grammar {
throw new EarleyException("Category not present in the gramamr");
return i;
}
public Rule getRule(EarleyRule rule) {
Rule r = earleyRule2GrammarRule.get(rule);
if (r == null)
throw new EarleyException("Rule not preset in the grammar");
return r;
}
}
......@@ -20,49 +20,3 @@ public abstract class NodeLabel {
public abstract String prettyPrint(Grammar info);
}
class ItemLabel extends NodeLabel {
DottedRule item;
public ItemLabel(DottedRule item, int start, int end) {
super(start, end);
this.item = item;
}
@Override public boolean equals(Object other) {
if (!super.equals(other))
return false;
return (other instanceof ItemLabel) &&
item.equals(((ItemLabel)other).item);
}
@Override public int hashCode() {
return super.hashCode() * 31 + item.hashCode();
}
public String prettyPrint(Grammar info) {
return item.prettyPrint(info) + ", " + super.start + ", " + super.end;
}
}
class SymbolLabel extends NodeLabel {
int symbol;
public SymbolLabel(int symbol, int start, int end) {
super(start, end);
this.symbol = symbol;
}
@Override public boolean equals(Object other) {
if (!super.equals(other))
return false;
return (other instanceof SymbolLabel) &&
((SymbolLabel)other).symbol == symbol;
}
@Override public int hashCode() {
return super.hashCode() * 31 + symbol;
}
public String prettyPrint(Grammar info) {
return info.getCategory(symbol).toString() + ", " + super.start + ", " + super.end;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment