CFG remove unused rules
This commit is contained in:
parent
e6d9439ba9
commit
b205438601
3 changed files with 20 additions and 0 deletions
|
|
@ -157,6 +157,7 @@ namespace tp {
|
|||
[[nodiscard]] Node* find(const TypeArg data) const {
|
||||
Node* found = mFirst;
|
||||
for (alni i = 0; data != found->data; i++) {
|
||||
if (i == length()) return nullptr;
|
||||
if (!found->next) {
|
||||
return nullptr;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -128,11 +128,22 @@ bool CfGrammar::compile() {
|
|||
}
|
||||
}
|
||||
|
||||
List<String> remove;
|
||||
for (auto nonTerminal : mNonTerminals) {
|
||||
if (!nonTerminal->val.references.size() && nonTerminal->key != startTerminal) {
|
||||
printf("Non-terminal '%s' is defined but not used\n", nonTerminal->key.read());
|
||||
remove.pushBack(nonTerminal->key);
|
||||
}
|
||||
}
|
||||
|
||||
for (auto rem : remove) {
|
||||
auto nonTerminal = &mNonTerminals.get(rem.data());
|
||||
for (auto referencing : nonTerminal->referencing) {
|
||||
referencing->val->references.remove(nonTerminal->rules.first()->data->id);
|
||||
}
|
||||
for (auto rule : nonTerminal->rules) {
|
||||
rules.removeNode(rules.find(*rule.data()));
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
|
@ -12,10 +12,18 @@ namespace tp {
|
|||
String id;
|
||||
bool isTerminal = false;
|
||||
bool isEpsilon = false;
|
||||
|
||||
bool operator==(const Arg& in) const {
|
||||
return (id == in.id) && (isEpsilon == in.isEpsilon) && (isTerminal == in.isTerminal);
|
||||
}
|
||||
};
|
||||
|
||||
String id;
|
||||
List<Arg> args;
|
||||
|
||||
bool operator==(const Rule& in) const {
|
||||
return (id == in.id) && (args == in.args);
|
||||
}
|
||||
};
|
||||
|
||||
struct Sentence {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue