-
Notifications
You must be signed in to change notification settings - Fork 1
/
grammar.cpp
135 lines (130 loc) · 4.21 KB
/
grammar.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
131
132
133
134
135
#include "grammar.h"
GrammarAnalysis::GrammarAnalysis() {
parser = new Parser();
operation = new OperationAnalysis(parser);
conditional = new ConditionalAnalyzer(parser, operation);
}
GrammarAnalysis::~GrammarAnalysis() {
delete parser;
delete operation;
delete conditional;
}
void GrammarAnalysis::program() {
int temp, q, ffc, Tchain;
string tempstring;
sym1 = symbolTable[symbolIndex++].code;
if (sym1 >= 27 && sym1 <= 29) {
sym1 = symbolTable[symbolIndex++].code;
while (sym1 != 24) {
//;
if (sym1 == 1) {
sym1 = symbolTable[symbolIndex++].code;
if (sym1 == 16)
//=
program();
else if (sym1 == 25)
//,
sym1 = symbolTable[symbolIndex++].code;
else if (sym1 == 24)
//;
break;
else
cout << "错误的变量定义" << endl;
} else
cout << "错误的变量定义" << endl;
}
} else if (sym1 == 1) {
tempstring = symbolTable[symbolIndex - 1].sign;
sym1 = symbolTable[symbolIndex++].code;
if (sym1 == 16) {
Schain = 0;
if (operation->analyse()) {
temp = parser->entry(tempstring);
if (Flag == 1) {
parser->gen("=", Temp, -1, signTable[temp].name);
} else {
parser->gen("=", 1000 + tempIndex - 1, -1, signTable[temp].name);
}
}
if (sym1 != 24)
cout << "赋值语句缺少;" << endl;
} else
cout << "赋值语句缺少=" << endl;
} else if (sym1 == 30) {
// if
sym1 = symbolTable[symbolIndex++].code;
if (sym1 == 17) {
ifFlag = true;
conditional->analyse();
if (sym1 == 18) {
parser->Backpatch(E_TC, NXQ);
ffc = E_FC;
program();
sym1 = symbolTable[symbolIndex++].code;
if (sym1 == 31) {
q = NXQ;
parser->gen("j", -1, -1, "0");
parser->Backpatch(ffc, NXQ);
E_FC = q;
Tchain = parser->Merg(Schain, q);
program();
Schain = parser->Merg(Tchain, Schain);
} else {
symbolIndex--;
Schain = parser->Merg(ffc, Schain);
}
} else
cout << "if语句缺少右括号" << endl;
} else
cout << "if语句缺少左括号" << endl;
} else if (sym1 == 32) {
// while
sym1 = symbolTable[symbolIndex++].code;
if (sym1 == 17) {
q = NXQ;
conditional->analyse();
if (sym1 == 18) {
whileFlag = true;
parser->Backpatch(E_TC, NXQ);
ffc = E_FC;
program();
parser->Backpatch(Schain, NXQ);
parser->gen("j", -1, -1, to_string(q));
Schain = ffc;
E_FC = ffc;
} else
cout << "while语句缺少右括号" << endl;
} else
cout << "while语句缺少左括号" << endl;
} else if (sym1 == 33) {
// do
q = NXQ;
program();
parser->Backpatch(Schain, NXQ);
sym1 = symbolTable[symbolIndex++].code;
if (sym1 == 32) {
sym1 = symbolTable[symbolIndex++].code;
if (sym1 == 17) {
conditional->analyse();
if (sym1 == 18) {
doFlag = true;
parser->Backpatch(E_TC, q);
Schain = E_FC;
} else
cout << "do-while语句缺少右括号" << endl;
} else
cout << "do-while语句缺少左括号" << endl;
}
}
}
void GrammarAnalysis::analyse() {
while (symbolIndex < symbolTableSize) {
program();
if (ifFlag || whileFlag || doFlag) {
parser->Backpatch(E_FC, NXQ);
ifFlag = false;
whileFlag = false;
doFlag = false;
}
}
}