-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompiler.cpp
More file actions
739 lines (681 loc) · 20 KB
/
Copy pathCompiler.cpp
File metadata and controls
739 lines (681 loc) · 20 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
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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
#include <iostream>
#include "Lexer.hpp"
#include <fstream>
#include "Fuhaobiao.hpp"
#include <stack>
#include "Siyuanshi.hpp"
#include "Global.hpp"
#include "Actions.hpp"
#include "Youhua.hpp"
// 递归下降子程序分析法
// #define RELEASE
// #define OUTDETAIL
#define FILE_DIR "./test_code/"
#define FILE_NAME "test4.hcc"
#define OUT_DIR "./output/"
#define DECRIPTION "-----------------------------------\n Happy C Compiler 1.0 \n-----------------------------------\n HCC 全体开发人员欢迎您的使用 \n-----------------------------------"
Lexer lexer;
Token* nowToken;
using namespace std;
void error(string tip) {
throw string("语法分析-" + tip);
}
int cur;
void next_w() {
nowToken = lexer.next();
#ifdef OUTDETAIL
cout << "下一token " << nowToken->val << " " << int(nowToken->type) << endl;
#endif
cur = lexer.getCur() - 1;
}
void Biaodashi();
void Yuju();
void Bianliangshengming();
void OG_analyse();
// 变量赋值
void Bianliangfuzhi() {
if (nowToken->type != Token::TokenType::IDENT)
error("贝贝,变量赋值要写标识符哦");
action1(nowToken);
next_w();
if (nowToken->val != "=")
error("贝贝,变量赋值要写等于号哦");
next_w();
Biaodashi();
action5(nowToken);
}
// for循环语句
void Forxunhuanyuju() {
action16(nowToken);
if (nowToken->val != "(")
error("贝贝,for循环语句要写左括号哦");
next_w();
Bianliangfuzhi();
if (nowToken->val != ";")
error("贝贝,for循环语句要写分号哦");
action17(nowToken);
next_w();
Biaodashi();
action18(nowToken);
if (nowToken->val != ";")
error("贝贝,for循环语句要写分号哦");
next_w();
Bianliangfuzhi();
if (nowToken->val != ")")
error("贝贝,for循环语句要写右括号哦");
next_w();
action19(nowToken);
Yuju();
action20(nowToken);
}
// 循环语句
void Xunhuanyuju() {
action10(nowToken);
if (nowToken->val != "(")
error("贝贝,循环语句,要写左括号哦");
next_w();
Biaodashi();
if (nowToken->val != ")")
error("贝贝,循环语句,要写右括号哦");
action11(nowToken);
next_w();
Yuju();
action12(nowToken);
}
// 返回语句
void Fanhuiyuju() {
Biaodashi();
if (nowToken->val != ";")
error("贝贝,返回语句,要写分号哦");
next_w();
action13(nowToken);
}
// 条件else语句
void Tiaojianelseyuju() {
if (nowToken->val == "else") {
action15(nowToken);
next_w();
Yuju();
}
}
// 条件语句
void Tiaojianyuju() {
if (nowToken->val != "(")
error("贝贝,if后面要写左括号哦");
next_w();
Biaodashi();
if (nowToken->val != ")")
error("贝贝,别忘记写右括号哦");
action6(nowToken);
next_w();
Yuju();
Tiaojianelseyuju();
action7(nowToken);
}
// 赋值语句
void Fuzhiyuju() {
Biaodashi();
action5(nowToken);
if (nowToken->val != ";")
error("贝贝,你忘记写分号了哦");
next_w();
}
// 调用参数
void Diaoyongcanshu() {
if (nowToken->type == Token::TokenType::CONST_STR) {
action14(nowToken);
next_w();
}
else {
OG_analyse();
action9(nowToken);
}
}
// 函数调用参数组
void Hanshudiaoyongcanshuzu() {
if (nowToken->val == ")")
return;
Diaoyongcanshu();
while (nowToken->val == ",") {
next_w();
Diaoyongcanshu();
}
}
// 函数调用语句
void Hanshudiaoyongyuju() {
Hanshudiaoyongcanshuzu();
action8(nowToken);
if (nowToken->val != ")")
error("贝贝,函数调用要写右括号哦");
next_w();
if (nowToken->val != ";")
error("贝贝,还要记得写分号哦");
next_w();
}
// 函数调用
void Hanshudiaoyong() {
Hanshudiaoyongcanshuzu();
action8(nowToken);
if (nowToken->val != ")")
error("贝贝,函数调用要写右括号哦");
next_w();
}
void Yujukuai() {
action26(nowToken);
// 当遇到 } 说明是语句块的结尾
while (nowToken->val != "}")
{
Yuju();
}
action27(nowToken);
}
// 数组变量声明语句
void Shuzubianliangfuzhiyuju() {
if (nowToken->val != ";")
error("贝贝,数组变量声明语句需要以分号结尾");
action24(nowToken);
next_w();
}
// 普通变量声明语句
void Putongbianliangfuzhiyuju() {
if (nowToken->val == "=") {
next_w();
Biaodashi();
action23(nowToken);
if (nowToken->val != ";")
error("贝贝,不要忘记写分号哦");
next_w();
}
else if (nowToken->val == ";") {
action4(nowToken);
next_w();
}
else {
error("贝贝,为什么会有一个 " + nowToken->val);
}
}
// 变量定义语句2
void Bianliangdiyiyujuer() {
// 数组型变量
if (nowToken->val == "[") {
next_w();
if (nowToken->type != Token::TokenType::CONST_NUM)
error("贝贝,这里我想要一个常数");
action1(nowToken);
next_w();
if (nowToken->val != "]")
error("贝贝,这里我想要一个右方括号");
next_w();
Shuzubianliangfuzhiyuju();
}
else {
Putongbianliangfuzhiyuju();
}
}
// 变量定义语句
void Bianliangdingyiyuju() {
if (nowToken->type != Token::TokenType::IDENT)
error("贝贝,变量定义语句不可以不写标识符哦");
action1(nowToken);
next_w();
Bianliangdiyiyujuer();
}
// 数组项赋值语句
void Shuzuxiangfuzhiyuju() {
// if (nowToken->type != Token::TokenType::CONST_NUM)
// error("贝贝,数组项赋值,要输入整数哦");
// action1(nowToken);
Biaodashi();
action30(nowToken);
if (nowToken->val != "]")
error("贝贝,数组项赋值,要输入右方括号哦");
next_w();
if (nowToken->val != "=")
error("贝贝,数组项赋值,要输入等于号哦");
next_w();
Biaodashi();
action25(nowToken);
if (nowToken->val != ";")
error("贝贝,数组项赋值语句末尾,要输入分号哦");
next_w();
}
// 语句
void Yuju() {
// 变量定义语句
if (nowToken->val == "int") {
#ifdef OUTDETAIL
cout << "变量定义语句" << endl;
#endif
action1(nowToken);
next_w();
Bianliangdingyiyuju();
}
else if (nowToken->val == "float") {
#ifdef OUTDETAIL
cout << "变量定义语句" << endl;
#endif
action1(nowToken);
next_w();
Bianliangdingyiyuju();
}
// 赋值语句或函数调用语句
else if (nowToken->type == Token::TokenType::IDENT) {
tokens.push(*nowToken);
next_w();
// 函数调用语句
if (nowToken->val == "(") {
#ifdef OUTDETAIL
cout << "函数调用语句" << endl;
#endif
next_w();
Hanshudiaoyongyuju();
}
// 赋值语句
else if (nowToken->val == "=") {
#ifdef OUTDETAIL
cout << "赋值语句" << endl;
#endif
next_w();
Fuzhiyuju();
}
// 数组项赋值语句
else if (nowToken->val == "[") {
#ifdef OUTDETAIL
cout << "数组项赋值语句" << endl;
#endif
next_w();
Shuzuxiangfuzhiyuju();
}
}
// 条件语句
else if (nowToken->val == "if") {
#ifdef OUTDETAIL
cout << "条件语句" << endl;
#endif
next_w();
Tiaojianyuju();
}
// 循环语句
else if (nowToken->val == "while") {
#ifdef OUTDETAIL
cout << "循环语句" << endl;
#endif
next_w();
Xunhuanyuju();
}
// for循环语句
else if (nowToken->val == "for") {
#ifdef OUTDETAIL
cout << "for循环语句" << endl;
#endif
next_w();
Forxunhuanyuju();
}
// 返回语句
else if (nowToken->val == "return") {
#ifdef OUTDETAIL
cout << "返回语句" << endl;
#endif
next_w();
Fanhuiyuju();
}
// 跳出语句
else if (nowToken->val == "break") {
#ifdef OUTDETAIL
cout << "跳出语句" << endl;
#endif
next_w();
action21(nowToken);
if (nowToken->val != ";")
error("贝贝,break语句后面要写分号哦");
next_w();
}
// 跳过语句
else if (nowToken->val == "continue") {
#ifdef OUTDETAIL
cout << "跳过语句" << endl;
#endif
next_w();
action22(nowToken);
if (nowToken->val != ";")
error("贝贝,continue语句后面要写分号哦");
next_w();
}
// 语句块
else if (nowToken->val == "{") {
next_w();
Yujukuai();
if (nowToken->val != "}")
error("贝贝,块结束要加上对应的} 哦");
next_w();
}
else {
error("贝贝,没有以" + nowToken->val + "开头的语句哦");
}
}
// 类型
void Leixing() {
action1(nowToken);
if (nowToken->val == "int") {
next_w();
}
else if (nowToken->val == "float") {
next_w();
}
else if (nowToken->val == "char") {
next_w();
}
else
error("贝贝," + nowToken->val + "是什么意思嘛~");
}
// 变量声明(仅声明,不含初始化)
void Bianliangshengming() {
Leixing();
if (nowToken->type != Token::TokenType::IDENT)
error("贝贝,变量声明不可以不写标识符哦");
action1(nowToken);
next_w();
}
// 函数定义参数组
void Hanshudingyicanshuzu() {
if (nowToken->val == ")")
return;
Bianliangshengming();
action3(nowToken);
while (nowToken->val == ",") {
next_w();
Bianliangshengming();
action3(nowToken);
}
}
// 函数定义或声明
void Hanshudingyihuoshengming() {
// cout << "hs" << endl;
action2(nowToken);
// cout << "1" << endl;
Hanshudingyicanshuzu();
// cout << "2" << endl;
if (nowToken->val != ")")
error("贝贝,你忘写) 了");
next_w();
// cout << "3" << endl;
if (nowToken->val == ";") {
action29(nowToken);
next_w();
return;
}
action28(nowToken);
if (nowToken->val != "{")
error("我想要一个{ 球球了");
next_w();
Yujukuai();
if (nowToken->val != "}")
error("贝贝,你忘写} 了");
next_w();
qualters.add("endfunc");
}
// 外部内容
void Waibuneirong() {
Leixing();
if (nowToken->type != Token::TokenType::IDENT)
error("贝贝,记得要写标识符哦");
action1(nowToken);
next_w();
if (nowToken->val == "(") {
next_w();
Hanshudingyihuoshengming();
}
else {
Bianliangdiyiyujuer();
}
}
// 程序
void Chengxu() {
while (nowToken->type != Token::TokenType::END)
Waibuneirong();
}
// 表达式
void Biaodashi() {
OG_analyse();
}
// 算符优先关系表
char orderBetween(string o1s, string o2s) {
char o1 = o1s[0];
char o2 = o2s[0];
// ... ia o1 ib o2 ...
if (o1 == '#' && o2 == '#')return '=';
if (o1 == '#' && o2 == ')')return 'e';
else if (o1 == '#')return '<';
else if (o2 == '#')return '>';
if (o1 == '(' && o2 == ')')return '=';
else if (o2 == '(')return '<';
else if (o2 == ')')return '>';
else if (o1 == '(')return '<';
if (o1 == '*' || o1 == '/')return '>';
else if (o2 == '*' || o2 == '/')return '<';
else return '>';
// cout << "比较" << o1s << " " << o2s << endl;
return 'e';
}
int tid = 0;
stack<string> optr; //运算符栈
set<string> VT = { "+", "-", "*", "/", "(", ")","=","==","!=",">","<",">=","<=", "#" }; // 终极符表
int tempid = 0;
// 算符优先分析
void OG_analyse() {
#ifdef OUTDETAIL
cout << "进入算符优先分析" << endl;
#endif
optr.push("#");
int endFlag = 0;
// next_w();
while (!optr.empty()) {
string cur_opt = nowToken->val;
if (endFlag) cur_opt = "#";
// 为终极符,比较算符优先级
if (VT.count(cur_opt) == 1) {
// cout << "terminal " << now_w << endl;
// pair<string, string> key = make_pair(optr.top(), get_token(now_w));
// if (OGMAP.count(key) == 1) {
char order;
if ((order = orderBetween(optr.top(), cur_opt)) != 'e') {
// 栈内优先级高,规约
if (order == '>') {
#ifdef OUTDETAIL
cout << "规约" << endl;
#endif
if (opnd.size() < 2) error("运算数栈 < 2, 天,怎么会遇到这个: " + nowToken->val);
Token val2 = opnd.top(); opnd.pop();
Token val1 = opnd.top(); opnd.pop();
SymbLPointer sid1 = symbL.findToken(&val1, layer);
SymbLPointer sid2 = symbL.findToken(&val2, layer);
SymbLPointer sid3 = symbL.addVariable("t" + to_string(tid++), ValType::INT, layer);
Token result(Token::TokenType::IDENT, symbL.get(sid3).ident, 0);
opnd.push(result);
qualters.add(optr.top(), sid1, sid2, sid3);
if (optr.size() < 1) error("运算符栈 < 1, 天,怎么会遇到这个: " + nowToken->val);
optr.pop();
}
else if (order == '=') {
// cout << "弹出" << endl;
if (optr.size() < 1) error("天,怎么会遇到这个: " + nowToken->val);
optr.pop();
if (endFlag == 0)next_w();
}
// 栈内优先级低,移进
else if (order == '<') {
#ifdef OUTDETAIL
cout << "移进算符" << nowToken->val << endl;
#endif
optr.push(nowToken->val);
next_w();
}
}
else if (endFlag == 0) {
// 遇到非表达式内容,完成表达式的计算并退出算符优先分析
endFlag = 1;
}
else {
error("天,怎么会遇到这个: " + string(string("") + optr.top()) + " : " + nowToken->val);
}
}
else if (nowToken->type == Token::TokenType::IDENT || nowToken->type == Token::TokenType::CONST_NUM || nowToken->type == Token::TokenType::CONST_FLOAT) {
#ifdef OUTDETAIL
cout << "token type " << int(nowToken->type) << endl;
#endif
// 是函数标识符
if (nowToken->type == Token::TokenType::IDENT && symbL.findFunc(nowToken->val) != -1) {
action1(nowToken);
next_w();
if (nowToken->val != "(")
error("贝贝,函数标识符后面要写左括号哦");
next_w();
Hanshudiaoyong();
opnd.push(Token(Token::TokenType::IDENT, string("gtret") + to_string(tempid), 0));
int sid = symbL.addVariable(string("gtret") + to_string(tempid), ValType::INT, layer);
qualters.add("gtret", sid);
tempid++;
}
// 是数组标识符
else if (nowToken->type == Token::TokenType::IDENT && symbL.findArray(nowToken->val) != -1) {
action1(nowToken);
int sid = symbL.findArray(nowToken->val);
next_w();
if (nowToken->val != "[")
error("贝贝,数组标识符后面要写 \"[\" 哦");
next_w();
Biaodashi();
if (nowToken->val != "]")
error("贝贝,记得写 \"]\" 哦");
next_w();
string ident = tokens.top().val; tokens.pop();
SymbLPointer posid = symbL.findToken(&(opnd.top()), layer); opnd.pop();
opnd.push(Token(Token::TokenType::IDENT, symbL.get(sid).ident, posid));
}
// 是其他标识符
else if (nowToken->type == Token::TokenType::IDENT) {
opnd.push(*nowToken);
#ifdef OUTDETAIL
cout << "运算数入栈:" << nowToken->val << endl;
#endif
next_w();
}
// 是常量
else {
SymbLPointer sid3 = symbL.addConst("t" + to_string(tid++), ValType::INT, nowToken->val, layer);
Token result(Token::TokenType::IDENT, symbL.get(sid3).ident, 0);
opnd.push(result);
#ifdef OUTDETAIL
cout << "运算数入栈:" << nowToken->val << endl;
#endif
next_w();
}
}
else {
endFlag = 1;
}
}
#ifdef OUTDETAIL
cout << "退出算符优先分析" << endl;
#endif
}
// 主控程序入口
void LL1_recursive() {
next_w();
Chengxu();
if (nowToken->type == Token::TokenType::END) {}
else
error("已退出,多余的符号: " + nowToken->val);
}
#include <chrono>
#include "Macroproce.hpp"
using namespace std;
using namespace chrono;
char temp[1000000];
int main() {
cout << DECRIPTION << endl;
string filename = FILE_NAME;
int auto_nasm = 1;
int auto_link = 1;
int auto_run = 1;
ifstream is(string(FILE_DIR) + FILE_NAME);
#ifdef RELEASE
cout << "请输入hcc文件名:";
cin >> filename;
is.open(filename.c_str());
#endif
string code = "";
// string temp;
while (is.getline(temp, 1000000)) {
code += temp;
code += "\n";
}
code = macro(code); // 进行宏处理
// cout << code << endl;
cur = -1;
lexer.loadCode(code);
try {
LL1_recursive();
// 更新符号表活动记录指针
for (int i = 0; i < symbL.size(); ++i) {
if (symbL.get(i).identType == IdentType::FUNCTION) {
symbL.getDisplay(symbL.get(i).ident);
}
}
ofstream os("orgin.fq", ios::binary);
qualters.display(os);
os.close();
qualters = youhua(qualters);
os.open("greater.fq", ios::binary);
qualters.display(os);
os.close();
os.open("symbl.table", ios::binary);
symbL.display(os);
os.close();
os.open("constl.table", ios::binary);
constL.display(os);
os.close();
os.open("arrayl.table", ios::binary);
arrayL.display(os);
os.close();
os.open("funcl.table", ios::binary);
funcL.display(os);
os.close();
qualters.makeASM(OUT_DIR + filename + ".asm");
cout << "---asm生成完毕" << endl;
if (auto_nasm) {
cout << "---编译asm" << endl;
system(((const char*)(string("nasm.exe -f win64 " + string(OUT_DIR) + filename + ".asm").c_str())));
cout << "---asm编译完毕" << endl;
if (auto_link) {
cout << "---生成可执行文件" << endl;
system(string("gcc " + string(OUT_DIR) + filename + ".obj -o " + string(OUT_DIR) + filename + ".exe").c_str());
cout << "---可执行文件已生成" << endl;
if (auto_run) {
cout << "---开始运行" << endl;
auto start = system_clock::now();
system(string(string("cd ") + OUT_DIR + "&&\"" + filename + ".exe" + "\"").c_str());
cout << "---结束运行 ";
auto end = system_clock::now();
auto duration = duration_cast<microseconds>(end - start);
cout << "用时"
<< double(duration.count()) * microseconds::period::num / microseconds::period::den
<< "秒" << endl;
}
}
}
}
catch (string e) {
cout << "error: " << e << endl;
cout << "行:" << lexer.getLine() << " 列:" << lexer.getCol() << endl;
int new_cur;
string line = lexer.getLine(new_cur);
cout << line << endl;
for (int i = 0; i < line.size() + 1; ++i) {
if (i >= new_cur - nowToken->val.size() && i < new_cur) cout << "~";
else cout << " ";
}
cout << endl;
}
return 0;
}