-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
439 lines (413 loc) · 13.4 KB
/
Copy pathmain.cpp
File metadata and controls
439 lines (413 loc) · 13.4 KB
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
#include "State.h"
#include <string>
#include <iostream>
#include <vector>
#include <deque>
#include <map>
#include <set>
#include <sstream>
#define DEBUG (cout << "[DEBUG] ")
using std::vector;
using std::string;
using std::cin;
using std::cout;
using std::endl;
using std::map;
using std::stringstream;
using std::set;
using std::deque;
// Simple regular expression: only consists of
// The literal character: a-z, A-Z, 0-9
// Parenthesis: () no capture function, only used to sepcify the
// calculate sequnce
// Quantity character: *, ?, +
// Alternation character: |
// The regular expressions are consists of operans and operators, the
// literal characters are the baisc operands, while all the other
// characters are operators.
// Concating lietral characters consists of unit
// First the program will sacn the regualr expression and figure out
// all the units
// Then according to the units and operators we make a NFA
// Finally we let the test string go through the NFA to see if there
// is a match
// global label counter
int label_count = 0;
// Utilitiy functions
static void printStateChain(State *s);
static void buildPlainChain(vector<State *> &cache, string &result,
map<string, State *>&sumbol_map, string *sh, int &label_count);
static void buildQuantityChain(vector<string> &cat_labels,
map<string, State*> &symbol_map, string &result,
set<string> labels);
static string getFirstLabel(const string &result);
// Main work functions
State *LevelOneParse(string ®exp, map<string, State *>&symbol_map);
State *LevelTwoParse(string ®exp, map<string, State *>&symbol_map);
State *LevelThreeParse(string ®exp, map<string, State *>&symbol_map);
State *parseRegExp(string ®exp);
bool walk(string &search, State *start);
int main() {
string regexp;
cout << "input your regular expression: ";
cin >> regexp;
State *start = parseRegExp(regexp);
// Walk Part
cout << "input your search string: ";
string search;
cin >> search;
cout << "Your search string is: " << search << endl;
cout << (walk(search, start) ? "match" : "no match") << endl;
return 0;
}
void printStateChain(State *s) {
if (!s) {
return;
}
s->print();
if (s->m_out1)
printStateChain(s->m_out1);
if (s->m_out2)
printStateChain(s->m_out2);
}
void buildPlainChain(vector<State *> &cache, string &result,
map<string, State *>&symbol_map, string *sh, int &label_count) {
cache.clear();
for (auto j : *sh) {
State *pState = new State();
pState->m_need = j;
cache.push_back(pState);
}
if (cache.size() > 1) {
for (std::size_t s = 1; s < cache.size(); ++s) {
cache[s - 1]->m_out1 = cache[s];
}
}
stringstream ss;
ss << "@" << "1_" << label_count << "@";
result.append(ss.str());
symbol_map.insert(std::make_pair(ss.str(), cache.front()));
ss.str("");
++label_count;
}
State *LevelOneParse(string ®exp, map<string, State *>&symbol_map) {
string *sh = nullptr;
string result;
vector<State *> cache;
for (std::size_t i = 0; i < regexp.size(); ++i) {
switch (regexp[i]) {
case '*':
case '+':
case '?':
case '|': {
if (!sh) {
result.push_back(regexp[i]);
break;
}
buildPlainChain(cache, result, symbol_map, sh, label_count);
delete sh;
sh = nullptr;
result.push_back(regexp[i]);
break;
}
default:
if (!sh) {
sh = new string();
}
sh->push_back(regexp[i]);
break;
}
}
if (sh) {
buildPlainChain(cache, result, symbol_map, sh, label_count);
delete sh;
sh = nullptr;
}
// DEBUG << "result: " << result << endl;
// for (auto i : symbol_map) {
// DEBUG << "label " << i.first << endl;
// printStateChain(i.second);
// }
regexp = result;
return symbol_map.begin()->second;
}
State *LevelTwoParse(string ®exp, map<string, State *>&symbol_map) {
// replace *, ?, + and concate the corresponding state with the
// NFA generated in level one parse
DEBUG << "level two regexp: " << regexp << endl;
bool in_label = false;
set<string> labels;
vector<State *> cache;
string label;
string result;
for (std::size_t i = 0; i < regexp.size(); ++i) {
result.push_back(regexp[i]);
if (regexp[i] == '@' && !in_label) {
in_label = true;
label.clear();
}
if (regexp[i] == '@' && in_label && !label.empty()) {
label.push_back(regexp[i]);
in_label = false;
labels.insert(label);
}
if (in_label) {
label.push_back(regexp[i]);
}
switch (regexp[i]) {
default: {
// DEBUG << "scan plain character: " << regexp[i] << endl;
break;
}
case '?': {
result.pop_back();
QuestionState *qs = new QuestionState();
qs->m_out1 = symbol_map.at(label);
stringstream ss;
ss << "@2_" << label_count << "@";
++label_count;
string new_label = ss.str();
symbol_map.erase(label);
symbol_map.insert(std::make_pair(new_label, qs));
labels.erase(label);
labels.insert(new_label);
size_t old_label_pos = result.find(label);
if (old_label_pos == std::string::npos) {
DEBUG << "Fatal Error: Can't find label: " << label
<< "in result: " << result << endl;
DEBUG << "Program Terminated!\n";
exit(0);
}
result.replace(old_label_pos, label.length(), new_label);
break;
}
case '+': {
result.pop_back();
PlusState *ps = new PlusState();
State *s1 = symbol_map.at(label);
State *s2 = s1;
ps->m_out1 = s1;
while (s2->m_out1) {
s2 = s2->m_out1;
}
s2->m_out1 = ps;
stringstream ss;
ss << "@2_" << label_count << "@";
++label_count;
string new_label = ss.str();
symbol_map.erase(label);
symbol_map.insert(std::make_pair(new_label, s1));
size_t old_label_pos = result.find(label);
if (old_label_pos == std::string::npos) {
DEBUG << "Fatal Error: Can't find label: " << label
<< "in result: " << result << endl;
DEBUG << "Program Terminated!\n";
exit(0);
}
result.replace(old_label_pos, label.length(), new_label);
break;
}
case '*': {
result.pop_back();
AsteriskState *as = new AsteriskState();
State *s1 = symbol_map.at(label);
State *s2 = s1;
as->m_out1 = s1;
while (s2->m_out1) {
s2 = s2->m_out1;
}
s2->m_out1 = as;
stringstream ss;
ss << "@2_" << label_count << "@";
++label_count;
string new_label = ss.str();
symbol_map.erase(label);
symbol_map.insert(std::make_pair(new_label, as));
size_t old_label_pos = result.find(label);
if (old_label_pos == std::string::npos) {
DEBUG << "Fatal Error: Can't find label: " << label
<< "in result: " << result << endl;
DEBUG << "Program Terminated!\n";
exit(0);
}
result.replace(old_label_pos, label.length(), new_label);
break;
}
case '|': {
break;
}
}
}
DEBUG << "After level 2 parse the result is " << result << endl;
// parse it again and concatenate any adjacent labels
regexp = result;
result.clear();
in_label = false;
vector<string> cat_labels;
for (std::size_t i = 0; i < regexp.size(); ++i) {
result.push_back(regexp[i]);
if (regexp[i] == '@' && !in_label) {
in_label = true;
label.clear();
}
if (regexp[i] == '@' && in_label && !label.empty()) {
label.push_back(regexp[i]);
in_label = false;
labels.insert(label);
cat_labels.push_back(label);
}
if (in_label) {
label.push_back(regexp[i]);
}
switch (regexp[i]) {
default: {
break;
}
case '|': {
if (cat_labels.size() < 2) {
cat_labels.clear();
break;
}
buildQuantityChain(cat_labels, symbol_map, result, labels);
break;
}
}
}
if (cat_labels.size() > 1) {
buildQuantityChain(cat_labels, symbol_map, result, labels);
}
DEBUG << "After level 2 reparse result is " << result << endl;
State *start = symbol_map.at(getFirstLabel(result));
regexp = result;
return start;
}
State *LevelThreeParse(string ®exp, map<string, State *>&symbol_map) {
// replace | and reorgnize the NFA generated in the first two
// level parse
vector<State *> alternation_states;
DEBUG << "Before level 3 parse, the regexp is " << regexp << endl;
if (symbol_map.size() == 1) {
return symbol_map.begin()->second;
} else {
AlternationState *first_als = new AlternationState();
auto it = symbol_map.begin();
first_als->m_out1 = it->second;
++it;
first_als->m_out2 = it->second;
alternation_states.push_back(first_als);
++it;
while (it != symbol_map.end()) {
AlternationState *other_als = new AlternationState();
other_als->m_out2 = it->second;
alternation_states.push_back(other_als);
++it;
}
for (std::size_t i = alternation_states.size() - 1; i != 0; --i) {
State *s1 = alternation_states[i];
State *s2 = alternation_states[i - 1];
s1->m_out1 = s2;
}
// printStateChain(alternation_states.back());
return alternation_states.back();
}
}
State *parseRegExp(string ®exp) {
State *start = nullptr;
map<string, State *> symbol_map;
start = LevelOneParse(regexp, symbol_map);
start = LevelTwoParse(regexp, symbol_map);
start = LevelThreeParse(regexp, symbol_map);
return start;
}
/* When the string is empty and the regular expression is like ab*
* this match fucntion will return false. This is not true. But I
* don't fix it now. */
bool walk(string &search, State *start) {
for (auto it = search.begin(), end = search.end();
it != end; ++it) {
if (start->check(it, end)) {
return true;
}
}
return false;
}
void buildQuantityChain(vector<string> &cat_labels, map<string, State*> &symbol_map,
string &result, set<string> labels) {
State *head = nullptr;
for (std::size_t i = 0; i < cat_labels.size() - 1; ++i) {
State *s1 = symbol_map.at(cat_labels[i]);
State *s2 = symbol_map.at(cat_labels[i + 1]);
if (i == 0) {
head = s1;
}
if (QuestionState *qs = dynamic_cast<QuestionState *>(s1)) {
qs->m_out2 = s2;
while (s1->m_out1) {
s1 = s1->m_out1;
}
s1->m_out1 = s2;
} else if (AsteriskState *as = dynamic_cast<AsteriskState *>(s1)) {
as->m_out2 = s2;
} else {
State *s3 = s1;
while (s3->m_out1 && s3->m_out1 != s1) {
s3 = s3->m_out1;
}
if (s3->m_out1 == s1) {
// s3 is plus state
s3->m_out2 = s2;
} else {
s3->m_out1 = s2;
}
}
}
stringstream ss;
ss << "@2_" << label_count << "@";
++label_count;
string new_label = ss.str();
string old_label = cat_labels.front();
std::size_t old_pos = result.find(old_label);
if (old_pos == std::string::npos) {
DEBUG << "Fatal Error: Can't find label: " << old_label
<< "in result: " << result << endl;
DEBUG << "Program Terminated!\n";
exit(0);
}
result.replace(old_pos, old_label.length(), new_label);
symbol_map.erase(old_label);
symbol_map.insert(std::make_pair(new_label, head));
labels.erase(old_label);
labels.insert(new_label);
for (std::size_t i = 1; i < cat_labels.size(); ++i) {
old_label = cat_labels[i];
old_pos = result.find(old_label);
if (old_pos == std::string::npos) {
DEBUG << "Fatal Error: Can't find label: " << old_label
<< "in result: " << result << endl;
DEBUG << "Program Terminated!\n";
exit(0);
}
result.replace(old_pos, old_label.length(), "");
symbol_map.erase(old_label);
labels.erase(old_label);
}
cat_labels.clear();
}
string getFirstLabel(const string &result) {
string label;
bool in_label = false;
for (std::size_t i = 0; i < result.size(); ++i) {
if (result[i] == '@' && !in_label) {
in_label = true;
}
if (result[i] == '@' && in_label && !label.empty()) {
label.push_back(result[i]);
in_label = false;
break;
}
if (in_label) {
label.push_back(result[i]);
}
}
return label;
}