-
Notifications
You must be signed in to change notification settings - Fork 0
/
Preprocess.hpp
581 lines (493 loc) · 12.3 KB
/
Preprocess.hpp
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
#ifndef MY_PREPROCESS_HPP
#define MY_PREPROCESS_HPP
/***********************************************************
From: Jiahuan Song,
School of Computer Science and Engineering,
Southeast University, Jiulonghu Campus,
Nanjing, China
************************************************************/
#include "Headers.h"
class Preprocessor
{
public:
Preprocessor()
{
cout << "Start preprocessing..." << endl;
change("./lex.l");
cout << "Finish preprocessing..." << endl;
};
Preprocessor(string FileName)
{
cout << "Start preprocessing..." << endl;
change(FileName);
cout << "Finish preprocessing..." << endl;
}
void getResult(vector<string> &result)
{
result.resize(re.size());
for (unsigned int i = 0; i < re.size(); i++)
result[i] = re[i];
}
private:
static vector<string> re;//用于保存规范化正规表达式的结果
//static vector<string> act;
static map<string, string> mapRe;//用于保存声明部分的正规定义(RE是已经进行过规范化的)
static fstream f1, f2;
void change(string FileName)
{
f1.open(FileName, ios::in);//打开.l文件
f2.open("./lex.yy.c", ios::out);//建立临时.c文件
string line;//缓存每一行的内容
getline(f1, line);
skipAndCopy(line);//拷贝%{ %}之间的部分
reProcess(line);//声明部分正规定义的处理
skipAndCopy(line);//拷贝%{ %}之间的部分
modeProcess(line);//转换规则部分的处理
copy(line);//辅助函数部分的直接拷贝
f1.close();
f2.close();
}
void skipAndCopy(string &line)//拷贝%{ %}之间的部分
{
//跳过空行
while (judgeNull(line))
getline(f1, line);
//拷贝%{ %}之间的部分
if (line == "%{")
{
getline(f1, line);
while (line != "%}")
{
f2 << line << "\n";
getline(f1, line);
}
getline(f1, line);
while (judgeNull(line))
getline(f1, line);
}
}
void reProcess(string &line)//声明部分正规定义的处理
{
string reName, reContent;
int count, nameStart, nameEnd, contentStart, contentEnd;
while (true)
{
if (line == "%{")
break;
else if (line == "%%")
break;
else if (judgeNull(line))
{
getline(f1, line);
continue;
}
else
{
count = 0;
while (true)
{
if (line[count] != ' ' && line[count] != '\t')
{
nameStart = count;
count++;
break;
}
count++;
}
while (true)
{
if (line[count] == ' ' || line[count] == '\t')
{
nameEnd = count-1;
count++;
break;
}
count++;
}
while (true)
{
if (line[count] != ' ' && line[count] != '\t')
{
contentStart = count;
count++;
break;
}
count++;
}
while (line[count] != '\0')
count++;
count--;
while (line[count] == ' ' || line[count] == '\t')
count--;
contentEnd = count;
reName = line.substr(nameStart, nameEnd - nameStart + 1);
reContent = line.substr(contentStart, contentEnd - contentStart + 1);
mapRe.insert(pair<string, string>(reName, process(reContent)));
}
getline(f1, line);
}
}
void modeProcess(string &line)//转换规则部分的处理
{
getline(f1, line);
string regu;
string action;
int reStart;
int reEnd;
int actionStart;
int actionEnd;
int count;
int flag;
int pid = 0;
f2 << "int act(int pid) {" << endl;
while (line != "%%")
{
if (judgeNull(line))
{
getline(f1, line);
continue;
}
else
{
count = 0;
while (true)
{
if (line[count] != ' ' && line[count] != '\t')
{
reStart = count;
count++;
break;
}
count++;
}
while (line[count] != '\0')
count++;
count--;
while(line[count] != '}')
count--;
actionEnd = count;
flag = 1;
while (flag != 0)
{
count--;
if (line[count] == '\'')
count -= 4;
else if (line[count] == '}')
flag++;
else if (line[count] == '{')
flag--;
}
actionStart = count;
count--;
while (line[count] == ' ' || line[count] == '\t')
count--;
reEnd = count;
regu = line.substr(reStart, reEnd - reStart + 1);
re.push_back(process(regu));
action = line.substr(actionStart, actionEnd - actionStart + 1);
//act.push_back(action);
if(pid > 0)
f2 << "else ";
f2 << "if (pid == " << pid++ << ") " << action << endl;
}
getline(f1, line);
}
f2 << "else if (pid == -1) { return -1; }" << endl;
f2 << "return 0;\n}" << endl;
}
void copy(string &line)//辅助函数部分的直接拷贝
{
while (!f1.eof())
{
getline(f1, line);
f2 << line << "\n";
}
}
bool judgeNull(const string &str)//判断字符串是否为空或只有空格和tab
{
for (int i = 0; str[i] != '\0'; i++)
if (str[i] != ' ' && str[i] != '\t')
return false;
return true;
}
string process(const string &str)//非规范RE到规范RE的转化
{
string resultStr = "";
string tempStr="";
unsigned int count = 0;
while (str[count] != '\0')
{
if (str[count] == '"')
{
tempStr = "(";
count++;
while (str[count] != '"')
{
if(str[count] == '('||str[count] == ')'||str[count] == '*'||str[count] == '|'||str[count] == '\\')
tempStr += '\\';
tempStr += str[count];
count++;
}
tempStr += ")";
resultStr += tempStr;
}
else if (str[count] == '[')
{
tempStr = "(";
count++;
if (str[count] == '^')
{
string chs="";
count++;
while (str[count] != ']')
{
if (str[count] == '\\')
count++;
chs += str[count];
count++;
}
int ch = 0;
int result;
while (ch <= 127)
{
result = chs.find((char)ch); //查找ch
if (result == -1 && ch != '$')
{
if(ch == '('||ch == ')'||ch == '*'||ch == '|'||ch == '\\')
tempStr += '\\';
tempStr = tempStr + (char)ch + '|';
}
/*if(ch == 255) break;*/
ch++;
}
tempStr = tempStr.substr(0, tempStr.length() - 1);
}
else
{
while (str[count] != ']')
{
if (str[count + 1] == '-' && ((isDigital(str[count]) && isDigital(str[count+2])) || (isLetter(str[count]) && isLetter(str[count + 2])) ) )
{
char ch = str[count];
while (ch <= str[count + 2])
{
tempStr = tempStr + ch + '|';
ch++;
}
count += 2;
}
else if(str[count] == '\\')
{
count++;
if(str[count]=='t')
tempStr = tempStr + '\t' + '|';
else if(str[count] == 'v')
tempStr = tempStr + '\v' + '|';
else if(str[count] == 'n')
tempStr = tempStr + '\n' + '|';
else if(str[count] == 'f')
tempStr = tempStr + '\f' + '|';
}
else
{
tempStr = tempStr + str[count] + '|';
}
count++;
}
tempStr = tempStr.substr(0, tempStr.length() - 1);
}
tempStr += ")";
resultStr += tempStr;
}
else if (str[count] == '{')
{
string subStr = "";
bool flag = false;
int pos = 0;
count++;
while (str[count] != '}')
{
if (str[count] == ',')
{
flag = true;
pos = count;
}
subStr += str[count];
count++;
}
if (isLetter(subStr[0]))
{
tempStr = "(" + mapRe.find(subStr)->second + ")";
resultStr += tempStr;
}
else if (isDigital(subStr[0]))
{
if (flag)
{
int lengthOfEnd = count - pos - 1;
int lengthOfStart = subStr.length() - lengthOfEnd - 1;
int numStart = stoi(subStr.substr(0, lengthOfStart));
int numEnd = stoi(subStr.substr(lengthOfStart + 1, lengthOfEnd));
resultStr = resultStr.substr(0, resultStr.length() - tempStr.length());
string temp = "";
for (int i = 0; i < numStart; i++)
{
temp = temp + tempStr;
}
string tempString = "(" + temp + ")";
for (int j = numStart+1; j <= numEnd; j++)
{
temp = temp + tempStr;
tempString = tempString + "|" + "(" + temp + ")";
}
tempStr = "(" + tempString + ")";
resultStr += tempStr;
}
else
{
int num = stoi(subStr);
resultStr = resultStr.substr(0, resultStr.length() - tempStr.length());
string temp = "";
for (int i = 0; i < num; i++)
{
temp = temp + tempStr;
}
tempStr = "(" + temp + ")";
resultStr += tempStr;
}
}
}
else if (str[count] == '(')
{
string subStr = "";
count++;
int flag = 1;
while (flag != 0)
{
if (str[count] == '[')
{
subStr += str[count];
count++;
while (str[count] != ']')
{
subStr += str[count];
count++;
}
subStr += str[count];
count++;
}
else if (str[count] == '\\')
{
subStr += str[count];
count++;
subStr += str[count];
count++;
}
else if (str[count] == '"')
{
subStr += str[count];
count++;
while (str[count] != '"')
{
subStr += str[count];
count++;
}
subStr += str[count];
count++;
}
else if(str[count] == '(')
{
subStr += str[count];
flag++;
count++;
}
else if (str[count] == ')')
{
flag--;
if (flag != 0)
{
subStr += str[count];
count++;
}
}
else
{
subStr += str[count];
count++;
}
}
tempStr = "(" + process(subStr) + ")";
resultStr += tempStr;
}
else if (str[count] == '\\')
{
count++;
if(str[count] == '('||str[count] == ')'||str[count] == '*'||str[count] == '|'||str[count] == '\\')
{
tempStr = '\\';
tempStr += str[count];
}
else
{
tempStr = str[count];
}
resultStr += tempStr;
}
else if (str[count] == '+')
{
resultStr = resultStr.substr(0, resultStr.length() - tempStr.length());
tempStr = "(" + tempStr + tempStr + "*" + ")";
resultStr += tempStr;
}
else if (str[count] == '.')
{
tempStr = "(";
int ch = 0;
while (ch <= 127)
{
if (ch != '$')
{
if(ch == '('||ch == ')'||ch == '*'||ch == '|'||ch == '\\')
tempStr += '\\';
tempStr = tempStr + (char)ch + '|';
}
/*if(ch == 255) break;*/
ch++;
}
tempStr = tempStr.substr(0, tempStr.length() - 1);
tempStr += ")";
resultStr += tempStr;
}
else if (str[count] == '?')
{
resultStr = resultStr.substr(0, resultStr.length() - tempStr.length());
tempStr = "(" + tempStr + "|" + "()" + ")";
resultStr += tempStr;
}
else
{
tempStr = str[count];
resultStr += tempStr;
}
count++;
}
return resultStr;
}
bool isDigital(const char ch)//判断是否为数字
{
if (ch >= '0'&&ch <= '9')
return true;
return false;
}
bool isLetter(const char ch)//判断是否为字母
{
if ((ch >= 'a'&&ch <= 'z') || (ch >= 'A'&&ch <= 'Z'))
return true;
return false;
}
};
vector<string> Preprocessor::re {};
map<string, string> Preprocessor::mapRe {};
fstream Preprocessor::f1;
fstream Preprocessor::f2;
#endif