-
Notifications
You must be signed in to change notification settings - Fork 4
/
cfg-to-gnf.cpp
262 lines (237 loc) · 7.34 KB
/
cfg-to-gnf.cpp
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#include "header.h"
#include <vector>
#include <string>
using std::string;
using std::set;
using std::to_string;
using std::vector;
using std::pair;
/* function to remove null or lambda */
void eliminateNull() {
set<char> eliminate;
int flag = 0;
for (auto i: nonTerminal) {
if (rule[i].find("$") != rule[i].end()) {
flag = 1;
if (rule[i].size() == 1) {
eliminate.insert(i);
for (auto j: nonTerminal) {
//replace every nonterminal except the nullable nonterminal
if (j != i) {
set<string> temp;
for (auto k: rule[j]) {
//strcmp(var1, "dev") == 0
if (k.size() == 1 && k == to_string(i))
k = "$";
if (k.size() > 1 && k.find(i) != string::npos) {
size_t ps = k.find(i);
k.erase(ps, 1);
}
temp.insert(k);
}
rule[j] = temp;
}
}
} else {
rule[i].erase("$");
for (auto j: nonTerminal) {
set <string> temp;
string s;
for (auto k: rule[j]) {
if (k.size() > 1 && k.find(i) != string::npos) {
s = k;
size_t ps = k.find(i);
k.erase(ps, 1);
}
temp.insert(k);
if (!s.empty()) {
temp.insert(s);
s = "";
}
}
rule[j] = temp;
}
}
}
}
for (auto e: eliminate) {
nonTerminal.erase(e);
rule.erase(e);
}
if (flag == 1)
eliminateNull();
}
/* function to remove unit production */
void unitProductions() {
int flag;
do {
flag = 0;
for (auto i: nonTerminal) {
set <string> temp;
for (auto j: rule[i]) {
if (j != to_string(i) && nonTerminal.find(j[0]) != nonTerminal.end()) {
flag = 1;
for (const auto& k: rule[j[0]])
temp.insert(k);
} else
temp.insert(j);
}
rule[i] = temp;
}
} while (flag);
}
/* function to remove useless production */
void uselessProductions() {
set<char> n1;
for (auto n: nonTerminal)
for (auto p: rule[n]) {
int i = 0;
while (i < p.size() && nonTerminal.find(p[i]) == nonTerminal.end())
i++;
if (i == p.size()) {
n1.insert(n);
break;
}
}
int ok;
do {
ok = 0;
for (auto n: nonTerminal)
if (n1.find(n) == n1.end()) {
for (auto p: rule[n]) {
int i = 0;
while (i < p.size() && nonTerminal.find(p[i]) == nonTerminal.end() || n1.find(p[i]) != n1.end())
i++;
if (i == p.size()) {
n1.insert(n);
ok = 1;
break;
}
}
}
} while (ok);
//Find symbol that are accessible
vector<char> N2;
set<char> n2;
n2.insert('S');
N2.push_back('S');
int i = 0;
while (i < N2.size()) {
char n = N2[i];
for (const auto& p: rule[n]) {
for (char j : p)
if (nonTerminal.find(j) != nonTerminal.end() && n2.find(j) == n2.end()) {
n2.insert(j);
N2.push_back(j);
}
}
i++;
}
//Find common element in n1
auto it = n1.begin();
while (it != n1.end()) {
if (n2.find(*it) == n2.end()) {
nonTerminal.erase(*it);
rule.erase(*it);
it = n1.erase(it);
} else {
n2.erase(*it);
it++;
}
}
for (auto s: n2) {
nonTerminal.erase(s);
rule.erase(s);
}
//Delete production that contains non-terminal that are not in terminal set
for (auto n: nonTerminal) {
set <string> Pn;
for (auto p: rule[n]) {
int j = 0;
while (j < p.size()) {
if (p[j] != '$' && terminal.find(p[j]) == terminal.end() && nonTerminal.find(p[j]) == nonTerminal.end())
break;
j++;
}
if (j == p.size())
Pn.insert(p);
}
rule[n] = Pn;
}
}
/* function that makes all production have all non-terminals or 1 terminal
for example: S->abBB (A->a, B->b) will become S->ABBB */
void allNonTerminal() {
set <pair<char, char>> newAdd;
for (auto n: nonTerminal) {
set <string> Pn;
for (auto p: rule[n]) {
//if string length >= 2 and first letter p[0] is terminal
if (p.size() >= 2) {
for (char & i : p) {
if (terminal.find(i) != terminal.end()) {
int ok = 0;
char c = 'A';
for (auto s: newAdd) {
if (s.second == p[i]) {
c = s.first;
ok = 1;
break;
}
}
for (auto r: nonTerminal) {
string sc = string(1, p[i]);
if (rule[r].size() == 1 && rule[r].find(sc) != rule[r].end()) {
c = r;
ok = 1;
break;
}
}
if (ok == 1) {
i = c;
} else {
c++;
while (nonTerminal.find(c) != nonTerminal.end())
c++;
string sc = string(1, c);
rule[c].insert(string(1, i));
newAdd.insert({c, i});
i = c;
nonTerminal.insert(c);
}
}
}
}
Pn.insert(p);
}
rule[n] = Pn;
}
}
/* function that makes all-nonterminal-production have one terminal at the begining
for ex. S -> BBBB (B->b) will become S -> bBBB
*/
void oneBeginTerminal() {
for (auto n: nonTerminal) {
set <string> temp;
for (auto s: rule[n]) {
if (s.size() >= 2) {
char c = 'a';
for (auto p: rule[s[0]]) {
c = p[0];
}
s[0] = c;
}
temp.insert(s);
}
rule[n] = temp;
}
uselessProductions();
}
/* function to convert to GNF */
void toGNF() {
eliminateNull();
unitProductions();
uselessProductions();
allNonTerminal();
oneBeginTerminal();
}