-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
130 lines (124 loc) · 3.33 KB
/
main.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
#include"main.h"
using namespace std;
bool constantSubstitutionSwitch=false;
bool inlineSwitch=false;
bool propagationSwitch=false;
bool dagMapSwitch=false;
bool deadCodeEliminateSwitch=false;
bool PeepHoleSwitch=false;
int main(int argc, char* argv[]) {
string inputFile = "testfile.txt";
string outputFile = "mips.txt";
string errorFile = "";
string debugFile = "";
string recursiveDescendInformationFile = "";
cout << "Final project of 'Complier Design' Course(Autumn2019),SCSE BUAA" << endl;
/*添加命令行参数支持与学校验收测试的支持*/
if (argc==1) {
//如果是学校验收的话无参数
cout << "HomeWork Acceptance Test Mode" << endl;
errorFile = "error.txt";
debugFile = "debug.txt";
//recursiveDescendInformationFile = "output.txt";
constantSubstitutionSwitch = true;
inlineSwitch = true;
propagationSwitch = true;
dagMapSwitch = true;
deadCodeEliminateSwitch = true;
PeepHoleSwitch = true;
}
else {
//使用了命令行参数
inputFile = argv[1];
for (int i = 2; i < argc; i++) {
string header = argv[i];
if (header == "-opt") {
constantSubstitutionSwitch = true;
inlineSwitch = true;
propagationSwitch = true;
dagMapSwitch = true;
deadCodeEliminateSwitch = true;
PeepHoleSwitch = true;
continue;
}
if (i + 1 >= argc) {
cout << "invalid parameter for operand " << header << endl;
exit(0);
}
string para = argv[i + 1];
if (header == "-o") {
outputFile = para;
}
else if (header == "-d") {
debugFile = para;
}
else if (header == "-rdi") {
recursiveDescendInformationFile = para;
}
else if (header == "-h") {
/*准备说明文档*/
cout << "usage: C0compiler.exe sourcefile " << endl;
cout << "[-o outputfile] " << endl;
cout << "[-d debugInformation]" << endl;
cout << "[-rdi recursiveDescendInformationFile]" << endl;
cout << "[-h] help document" << endl;
cout << "[-opt] turn on the optimization" << endl;
}
else {
cout << "invalid operand " << header << endl;
exit(0);
}
i++;
}
}
/*实例化各个组件===========================*/
//符号表
SymbolTable symbolTable;
MidCode::table = &symbolTable;
SubSymbolTable::table = &symbolTable;
//错误处理
FaultHandler faultHandler(errorFile);
faultHandler.debugOn();
//词法分析
LexicalAnalyzer lexicalAnalyzer(faultHandler);
lexicalAnalyzer.readAll(inputFile);
lexicalAnalyzer.getNextSym();
//目标代码生成
MipsTranslator mips(outputFile);
//编译器框架
MidCodeFramework frame(mips);
//语法分析
GrammarAnalyzer grammarAnalyzer(faultHandler,symbolTable,lexicalAnalyzer,
frame,recursiveDescendInformationFile);
if (recursiveDescendInformationFile != "") {
grammarAnalyzer.homeworkOn(true, true);
}
/*开始编译*/
fstream f;
if (debugFile != "") {
f.open(debugFile, ios_base::trunc | ios_base::out);
}
grammarAnalyzer.programme();
if (faultHandler.haveBug()) {
faultHandler.terminate();
}
else {
frame.optimize();
if (debugFile != "") {
f << "BEFORE BACKEND OPTIMIZATION" << endl;
f << frame;
f << endl << endl;
}
if (debugFile != "") {
f << "BEFORE BACKEND OPTIMIZATION" << endl;
frame.dumpNewMidCode(f);
f << endl << endl;
f << "SYMBOLTABLE" << endl;
f << symbolTable;
f << endl << endl;
}
frame.generateMips();
system("pause");
}
return 0;
}