CFG remove unused rules

This commit is contained in:
IlushaShurupov 2023-08-12 08:27:50 +03:00
parent e6d9439ba9
commit b205438601
3 changed files with 20 additions and 0 deletions

View file

@ -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;
}

View file

@ -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 {