summaryrefslogtreecommitdiffhomepage
path: root/bnf.cpp
diff options
context:
space:
mode:
authorRoland Reichwein <mail@reichwein.it>2020-02-16 16:33:19 +0100
committerRoland Reichwein <mail@reichwein.it>2020-02-16 16:33:19 +0100
commit2f4118526972f7f3d5147342bc65909fcc82b6c7 (patch)
tree3514b840da299d7203ce1bbcbf81d36be1108260 /bnf.cpp
parent0aaa6440a23e1dedf4a907fa46f979ea9d248e99 (diff)
clang++-9, integrate cppbnf
Diffstat (limited to 'bnf.cpp')
-rw-r--r--bnf.cpp31
1 files changed, 30 insertions, 1 deletions
diff --git a/bnf.cpp b/bnf.cpp
index 1efb459..f240f29 100644
--- a/bnf.cpp
+++ b/bnf.cpp
@@ -1,6 +1,7 @@
#include "bnf.h"
-std::map<std::string, std::set<std::string>> Reverse(BNF bnf){
+std::map<std::string, std::set<std::string>> Reverse(BNF bnf)
+{
std::map<std::string, std::set<std::string>> result;
for (const auto& [from, to] : bnf) {
@@ -18,4 +19,32 @@ std::map<std::string, std::set<std::string>> Reverse(BNF bnf){
return result;
}
+BNF SubBNF(const BNF& bnf, const std::string& top)
+{
+ BNF result;
+
+ std::vector<std::string> todo{top};
+
+ while (!todo.empty()) {
+ std::string current{todo.back()};
+ todo.pop_back();
+
+ auto it = bnf.find(current);
+ if (it != bnf.end()) {
+ // add current value
+ result[it->first] = it->second;
+
+ // add sub-tree values if not present yet, but available in original bnf
+ for (auto& variant: it->second) {
+ for (auto& child: variant) {
+ if (result.find(child) == result.end() && bnf.find(child) != bnf.end()) {
+ todo.push_back(child);
+ }
+ }
+ }
+ }
+ }
+
+ return result;
+}