summaryrefslogtreecommitdiffhomepage
path: root/bnf.cpp
diff options
context:
space:
mode:
authorRoland Reichwein <mail@reichwein.it>2020-03-01 22:34:35 +0100
committerRoland Reichwein <mail@reichwein.it>2020-03-01 22:34:35 +0100
commit6fcfe0a5cf8f53658e50e346076768eec229e695 (patch)
tree69d9ce555c3a671e2d3c0dfe46834d6fcb183753 /bnf.cpp
parent10c2b7f9b6676dafd62d0eeda507b5ee5c6db216 (diff)
Vector invalidation fix
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;