Commit 65c09a21 authored by Alexandru Dura's avatar Alexandru Dura
Browse files

Track grammatical categories by name

parent 92cbc29c
......@@ -9,6 +9,7 @@ public class Grammar {
private HashMap<Integer, Category> int2cat = new HashMap<>();
private HashMap<Category, ArrayList<Rule>> grammarRules = new HashMap<>();
private HashMap<EarleyRule, Rule> earleyRule2GrammarRule = new HashMap<>();
private HashMap<String, Category> name2cat = new HashMap<>();
private int nonTermIndex = 1;
private int termIndex = -1;
......@@ -30,6 +31,13 @@ public class Grammar {
if (cat2int.containsKey(t))
return;
Category existingCat = name2cat.get(t.getName());
if (existingCat == null) {
name2cat.put(t.getName(), t);
} else if (existingCat != t) {
throw new EarleyException("Two categories with the same name in the grammar.");
}
if (t.isTerminal()) {
cat2int.put(t, termIndex);
int2cat.put(termIndex, t);
......@@ -94,6 +102,13 @@ public class Grammar {
return c;
}
public Category getCategory(String name) {
Category c = name2cat.get(name);
if (c == null)
throw new EarleyException("Category '" + name + "' not present in the grammar");
return c;
}
public int getInternalSymbol(Category c) {
Integer i = cat2int.get(c);
if (i == null)
......
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