-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexical.h
447 lines (375 loc) · 13.8 KB
/
lexical.h
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
#ifndef __LEXICAL_H__
#define __LEXICAL_H__
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include "util.h"
// 词法分析器
class Lexical {
private:
struct ID {
std::set<long long> tokens;
ID(std::set<long long> ids) {
tokens = ids;
}
~ID() {
}
bool operator<(const ID& other) const {
return tokens < other.tokens;
}
};
class Node {
public:
ID id;
std::multimap<char, Node*> edges;
bool end;
int type;
Node(bool end, int type) : id({NODE_COUNT++}), end(end), type(type) {
}
Node(ID id, bool end, int type) : id(id), end(end), type(type) {
}
~Node() {
}
static long long NODE_COUNT;
};
class NFA {
public:
Node* start;
Node* end;
NFA(Node* start, Node* end) : start(start), end(end) {
}
NFA(char c, int type) : start(nullptr), end(nullptr) {
start = new Node(false, type);
end = new Node(true, type);
start->edges.insert({c, end});
}
~NFA() {
}
public:
static NFA* rgexToNFA(const std::string& rgex, int type) {
auto tokens = Rgex::toRPN(rgex);
for (auto t : tokens)
if (!t.op && t.c != '@')
charSet.insert(t.c);
std::stack<NFA*> nfaStack;
// 表达式运算
for (int i = 0; i < tokens.size(); ++i) {
if (tokens[i].op) {
if (tokens[i].c == '|') {
NFA* a = nfaStack.top();
nfaStack.pop();
NFA* b = nfaStack.top();
nfaStack.pop();
nfaStack.push(NFA::nfaOr(a, b));
delete a, b;
} else if (tokens[i].c == '.') {
NFA* b = nfaStack.top();
nfaStack.pop();
NFA* a = nfaStack.top();
nfaStack.pop();
nfaStack.push(NFA::nfaDot(a, b));
delete a, b;
} else if (tokens[i].c == '*') {
NFA* a = nfaStack.top();
nfaStack.pop();
nfaStack.push(NFA::nfaStar(a));
delete a;
} else {
}
} else {
nfaStack.push(new NFA(tokens[i].c, type)); // 为单个字符构建NFA,rgex: a ===> S->a
}
}
return nfaStack.top();
}
static NFA* merge(NFA* a, NFA* b) {
Node* start = new Node(false, a->start->type | b->start->type);
start->edges.insert({'@', a->start});
start->edges.insert({'@', b->start});
return new NFA(start, nullptr);
}
private:
static NFA* nfaOr(NFA* a, NFA* b) {
a->end->end = false;
b->end->end = false;
// 1. 新建一个初始状态和一个终止状态
Node* s1 = new Node(false, a->start->type | b->start->type);
Node* s2 = new Node(true, a->end->type | b->end->type);
// 2. 将s1用空边指向 a.start, b.start
s1->edges.insert({'@', a->start});
s1->edges.insert({'@', b->start});
// 3. 将 a.end, b.end 用空边指向s2
a->end->edges.insert({'@', s2});
b->end->edges.insert({'@', s2});
return new NFA(s1, s2);
}
static NFA* nfaStar(NFA* a) {
a->end->end = false;
// 1. 新建一个初始状态s1和一个终止状态s2
Node* s1 = new Node(false, a->end->type);
Node* s2 = new Node(true, a->end->type);
// 2. 让s1用空边指向a.start,s2
s1->edges.insert({'@', a->start});
s1->edges.insert({'@', s2});
// 3. 让a.end用空边指向s2, a.start
a->end->edges.insert({'@', s2});
a->end->edges.insert({'@', a->start});
return new NFA(s1, s2);
}
static NFA* nfaDot(NFA* a, NFA* b) {
// 1. 用空边将a的终结状态和b的初始状态连接
a->end->end = false;
a->end->edges.insert({'@', b->start});
return new NFA(a->start, b->end);
}
public:
static std::set<char> charSet; // 非终结符集
};
class DFA {
public:
Node* start = nullptr;
std::set<Node*> ends;
public:
~DFA() {
}
private:
DFA(Node* start) : start(start) {
}
public:
static DFA* NFAtoDFA(NFA* nfa) {
std::queue<std::set<Node*>> workList;
std::set<std::set<Node*>> visited;
std::map<ID, Node*> dfaNodes;
std::set<Node*> startSet = epsilonClosure({nfa->start});
ID startId = createStateId(startSet);
Node* startNode = new Node(startId, containsFinalState(startSet), calueType(startSet));
dfaNodes.insert({startId, startNode});
workList.push(startSet);
while (!workList.empty()) {
std::set<Node*> currentSet = workList.front();
workList.pop();
if (visited.find(currentSet) != visited.end()) continue;
visited.insert(currentSet);
for (char c : NFA::charSet) {
// 1. 去除空边,构建子集
std::set<Node*> nextSet = epsilonClosure(move(currentSet, c));
if (nextSet.empty()) continue;
if (visited.find(nextSet) == visited.end())
workList.push(nextSet);
ID fromId = createStateId(currentSet);
ID toId = createStateId(nextSet);
Node* from = dfaNodes[fromId];
Node* to = nullptr;
if (dfaNodes.find(toId) == dfaNodes.end()) {
to = new Node(toId, containsFinalState(nextSet), calueType(nextSet));
dfaNodes.insert({toId, to});
} else {
to = dfaNodes[toId];
}
// 2. 添加一条DFA的边(from->to)
from->edges.insert({c, to});
}
}
DFA* dfa = new DFA(startNode);
simplify(dfa);
return dfa;
}
private:
static void simplify(DFA* dfa) {
std::set<Node*> terminationSet;
std::set<Node*> nonTerminalSet;
std::set<Node*> visited;
std::stack<Node*> stack;
stack.push(dfa->start);
while (!stack.empty()) {
Node* n = stack.top();
stack.pop();
visited.insert(n);
if (n->end)
terminationSet.insert(n);
else
nonTerminalSet.insert(n);
for (auto e : n->edges)
if (visited.find(e.second) == visited.end())
stack.push(e.second);
}
std::queue<std::set<Node*>> workList;
workList.push(terminationSet);
workList.push(nonTerminalSet);
bool done = false;
while (!done) {
done = true;
int n = workList.size();
while (n-- > 0) {
auto set = workList.front();
auto newSet = std::set<Node*>();
Node* first = *(set.begin());
for (auto b : set)
if (!equivalent(first, b, workList))
newSet.insert(b);
if (newSet.size() > 0) {
for (auto n : newSet)
set.erase(n);
workList.push(newSet);
done = false;
}
workList.pop();
workList.push(set);
}
}
// 根据集合重构dfa
}
// 状态a和状态b是否等价
static bool equivalent(Node* a, Node* b, std::queue<std::set<Node*>>& simplifiedSet) {
// a和b的每条边都指向同一个集合里的元素
for (auto c : NFA::charSet) {
Node* aNext = nullptr;
Node* bNext = nullptr;
auto it = a->edges.find(c);
if (it != a->edges.end())
aNext = (*it).second;
it = b->edges.find(c);
if (it != b->edges.end())
bNext = (*it).second;
int n = simplifiedSet.size();
while (n > 0) {
auto set = simplifiedSet.front();
simplifiedSet.pop();
bool c = set.find(aNext) == set.end();
bool d = set.find(bNext) == set.end();
if (c ^ d)
return false;
n--;
simplifiedSet.push(set);
}
}
return true;
}
static std::set<Node*> epsilonClosure(const std::set<Node*>& inputSet) {
std::set<Node*> resultSet;
std::stack<Node*> stack;
for (Node* node : inputSet)
stack.push(node);
while (!stack.empty()) {
Node* current = stack.top();
stack.pop();
resultSet.insert(current);
auto range = current->edges.equal_range('@');
for (auto it = range.first; it != range.second; it++) {
Node* n = (*it).second;
if (resultSet.find(n) == resultSet.end())
stack.push(n);
}
}
return resultSet;
}
static bool containsFinalState(const std::set<Node*>& stateSet) {
for (Node* node : stateSet)
if (node->end) return true;
return false;
}
static std::set<Node*> move(const std::set<Node*>& inputSet, char input) {
std::set<Node*> resultSet;
for (auto node : inputSet) {
auto range = node->edges.equal_range(input);
for (auto it = range.first; it != range.second; ++it)
resultSet.insert((*it).second);
}
return resultSet;
}
static ID createStateId(const std::set<Node*>& stateSet) {
std::set<long long> ids;
for (auto node : stateSet)
for (int token : node->id.tokens)
ids.insert(token);
return ID(ids);
}
static int calueType(std::set<Node*> stateSet) {
int type = 0;
for (auto n : stateSet)
type |= n->type;
return type;
}
};
private:
DFA* dfa = nullptr;
NFA* nfa = nullptr;
public:
Lexical(std::vector<std::pair<std::string, int>> rgexList) : dfa(nullptr) {
if (rgexList.size() == 0)
exit(1);
nfa = NFA::rgexToNFA(rgexList[0].first, 1 << rgexList[0].second);
if (rgexList[0].second >= 32 || rgexList[0].second < 0)
exit(1);
for (size_t i = 1; i < rgexList.size(); ++i) {
if (rgexList[i].second >= 32 || rgexList[i].second < 0)
exit(1);
NFA* tmp = NFA::rgexToNFA(rgexList[i].first, 1 << rgexList[i].second); // type = 1 * 2^ t ==> t = log2(type)
NFA* merged = NFA::merge(nfa, tmp);
delete nfa;
delete tmp;
nfa = merged;
}
dfa = DFA::NFAtoDFA(nfa);
}
~Lexical() {
if (dfa != nullptr) {
std::stack<Node*> stack;
std::set<Node*> visited;
stack.push(dfa->start);
while (!stack.empty()) {
Node* n = stack.top();
stack.pop();
visited.insert(n);
for (auto e : n->edges)
if (visited.find(e.second) != visited.end())
stack.push(e.second);
delete n;
}
delete dfa;
}
if (nfa != nullptr) {
std::stack<Node*> stack;
std::set<Node*> visited;
stack.push(nfa->start);
while (!stack.empty()) {
Node* n = stack.top();
stack.pop();
visited.insert(n);
for (auto e : n->edges)
if (visited.find(e.second) != visited.end())
stack.push(e.second);
delete n;
}
delete nfa;
}
}
std::vector<std::pair<int, std::string>> scan(const std::string& code) {
std::vector<std::pair<int, std::string>> tokens;
size_t pos = 0;
while (pos < code.size()) {
Node* n = dfa->start;
size_t startPos = pos;
int type = 0;
for (size_t i = pos; i < code.size(); ++i) {
char c = code[i];
auto it = n->edges.find(c);
if (it == n->edges.end()) {
break;
} else if ((*it).second->end) {
pos = i;
type = (*it).second->type;
}
n = (*it).second;
}
tokens.push_back({log(type & -type) / log(2), code.substr(startPos, ++pos - startPos)});
}
return tokens;
}
};
long long Lexical::Node::NODE_COUNT = 0;
std::set<char> Lexical::NFA::charSet; // 非终结符集
#endif // __LEXICAL_H__