summaryrefslogtreecommitdiffhomepage
path: root/bnf.cpp
blob: 1efb4597f1aae36414d22538cff13aa88e904d69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "bnf.h"

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) {
  for (const auto& list : to) {
   for (const auto& element : list) {
    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;
}