1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
#include "bnf.h"
std::unordered_map<std::string, std::unordered_set<std::string>> Reverse(const BNF& bnf)
{
std::unordered_map<std::string, std::unordered_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::unordered_set{from});
}
}
}
return result;
}
std::unordered_map<std::string, std::unordered_set<std::string>> reverseFirst(const BNF& bnf)
{
std::unordered_map<std::string, std::unordered_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::unordered_set{from});
}
}
}
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;
}
namespace {
bool isHeadRecursive(const std::vector<std::string>& list, const std::string& symbol) {
if (list.size() > 0 && list[0] == symbol)
return true;
return false;
}
bool isHeadRecursive(const std::vector<std::vector<std::string>>& lists, const std::string& symbol) {
for (const auto& list: lists) {
if (isHeadRecursive(list, symbol))
return true;
}
return false;
}
} // anonymous namespace
// Change head recursion to tail recursion
BNF removeHeadRecursion(const BNF& bnf)
{
std::unordered_map<std::string, std::vector<std::vector<std::string>>> result;
for (const auto& [from, to]: bnf) {
if (isHeadRecursive(to, from)) { // modify rule by adding additional one
std::string from_ext = from + "-ext";
if (bnf.find(from_ext) != bnf.end())
throw std::runtime_error("ICE: Symbol "s + from_ext + " already exists in original BNF");
std::vector<std::vector<std::string>> to_new;
std::vector<std::vector<std::string>> to_ext{{}}; // starts with empty list as first element
for (const auto& list: to) {
if (isHeadRecursive(list, from)) {
std::vector<std::string> list_new{list.begin() + 1, list.end()};
list_new.push_back(from_ext);
to_ext.push_back(list_new);
} else {
std::vector<std::string> list_new{list.begin(), list.end()};
list_new.push_back(from_ext);
to_new.push_back(list_new);
}
}
result[from] = to_new;
result[from_ext] = to_ext;
} else { // just add
result[from] = to;
}
}
return result;
}
bool isTerminal(const BNF& bnf, const std::string& symbol)
{
return bnf.find(symbol) == bnf.end();
}
std::unordered_set<std::string> getTerminals(const BNF& bnf)
{
std::unordered_set<std::string> result;
for (const auto& [from, to] : bnf) {
for (const auto& list : to) {
for (const auto& element : list) {
if (isTerminal(bnf, element))
result.insert(element);
}
}
}
return result;
}
std::unordered_set<std::pair<std::string, size_t>, PairHash> getEmptyPositions(const BNF& bnf)
{
std::unordered_set<std::pair<std::string, size_t>, PairHash> result;
for (const auto& [from, to] : bnf) {
for (size_t i = 0; i < to.size(); i++) {
const auto& list{to[i]};
if (list.size() == 0)
result.insert({from, i});
}
}
return result;
}
std::unordered_map<std::string, std::unordered_set<std::pair<std::string, index_t>, PairHash>> reversePosFirst(const BNF& bnf)
{
std::unordered_map<std::string, std::unordered_set<std::pair<std::string, index_t>, PairHash>> result;
for (const auto& [from, to] : bnf) {
for (size_t i = 0; i < to.size(); i++) {
const auto& list{to[i]};
if (list.size() > 0) {
const auto& element{list[0]};
auto it{result.find(element)};
if (it != result.end()) // already present
it->second.insert({from, i});
else // new element
result.emplace(element, std::unordered_set<std::pair<std::string, index_t>, PairHash>{{from, i}});
}
}
}
return result;
}
|