diff options
Diffstat (limited to 'bnf.cpp')
-rw-r--r-- | bnf.cpp | 20 |
1 files changed, 20 insertions, 0 deletions
@@ -19,6 +19,26 @@ std::map<std::string, std::set<std::string>> Reverse(BNF bnf) return result; } +std::map<std::string, std::set<std::string>> reverseFirst(BNF bnf) +{ + std::map<std::string, std::set<std::string>> result; + + for (const auto& [from, to] : bnf) { + for (const auto& list : to) { + if (list.size() > 0) { + const auto& element{list[0]}; + auto i{result.find(element)}; + if (i != result.end()) // already present + i->second.insert(from); + else // new element + result.emplace(element, std::set{from}); + } + } + } + + return result; +} + BNF SubBNF(const BNF& bnf, const std::string& top) { BNF result; |