summaryrefslogtreecommitdiffhomepage
path: root/bnf.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'bnf.cpp')
-rw-r--r--bnf.cpp20
1 files changed, 20 insertions, 0 deletions
diff --git a/bnf.cpp b/bnf.cpp
index f240f29..7290962 100644
--- a/bnf.cpp
+++ b/bnf.cpp
@@ -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;