-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lexical.cpp
290 lines (278 loc) · 5.47 KB
/
Lexical.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
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
#include "Lexical.h"
string token_to_string(Token t) {
const char* LexicalTypeStr[] = {
"ENDFILE",
"ERROR",
"IF", // if
"ELSE", // else
"INT", // int
"RETURN", // return
"VOID", // void
"WHILE", // while
"ID", // identifier,标识符
"NUM", // 常数
"LBRACE", // {
"RBRACE", // }
"GTE", // >=
"LTE", // <=
"NEQ", // !=
"EQ", // ==
"ASSIGN", // =
"LT", // <
"GT", // >
"PLUS", // +
"MINUS", // -
"MULT", // *
"DIV", // /
"LPAREN", // (
"RPAREN", // )
"SEMI", // ;
"COMMA", // ,
"LCOMMENT", // //
"PCOMMENT", // /* */
"LBRACKET", // [
"RBRACKET" // ]
};
string res;
res += LexicalTypeStr[t.first];
res += " " + t.second;
return res;
}
Lexical::Lexical(const char* path) {
lineCount = 0;
openFile(path);
}
Lexical::~Lexical() {
if (src.is_open()) {
src.close();
}
}
void Lexical::openFile(const char* path) {
src.open(path, ios::in);
if (!src.is_open()) {
cerr << "file " << path << " open error" << endl;
exit(-1);
}
}
//获取下一个字符,忽略空白符
char Lexical::getNextChar() {
char c;
while (src >> c) {
if (c == ' '||c=='\t') {
continue;
}
else if (c == '\n') {
lineCount++;
return '\n';
}
break;
}
if (src.eof())
return 0;
else
return c;
}
Token Lexical::getNextToken() {
char c = getNextChar();
switch (c) {
case '\n':
return Token(NEXTLINE, "\n");
case '(':
return Token(LPAREN, "(");
break;
case ')':
return Token(RPAREN, ")");
break;
case '{':
return Token(LBRACE, "{");
break;
case '}':
return Token(RBRACE, "}");
break;
case '#':
return Token(ENDFILE, "#");
break;
case '+':
return Token(PLUS, "+");
break;
case '-':
return Token(MINUS, "-");
break;
case '*':
return Token(MULT, "*");
break;
case ',':
return Token(COMMA, ",");
break;
case ';':
return Token(SEMI, ";");
break;
case '=':
if (src.peek() == '=') {
src.get();
return Token(EQ, "==");
}
else {
return Token(ASSIGN, "=");
}
break;
case '>':
if (src.peek() == '=') {
src.get();
return Token(GTE, ">=");
}
else {
return Token(GT, ">");
}
break;
case '<':
if (src.peek() == '=') {
src.get();
return Token(LTE, "<=");
}
else {
return Token(LT, "<");
}
break;
case '!':
if (src.peek() == '=') {
src.get();
return Token(NEQ, "!=");
}
else {
return Token(ERROR, string("词法分析第")+to_string(lineCount)+string("行:未识别的符号!"));
}
break;
case '[':
return Token(LBRACKET,"[");
break;
case ']':
return Token(RBRACKET,"]");
break;
case '/':
//行注释
if (src.peek() == '/') {
char buf[1024];
src.getline(buf, 1024);
return Token(LCOMMENT, string("/")+buf);
}
//段注释
else if (src.peek() == '*') {
src.get();
string buf = "/*";
while (src >> c) {
buf += c;
if (c == '*') {
src >> c;
buf += c;
if (c == '/') {
return Token(PCOMMENT, buf);
break;
}
}
}
//读到最后都没找到*/,因不满足while循环条件退出
if (src.eof()) {
return Token(ERROR, string("词法分析第")+to_string(lineCount)+string("行:段注释没有匹配的*/"));
}
}
//除法
else {
return Token(DIV, "/");
}
break;
default:
if (isdigit(c)) {
string buf;
buf.push_back(c);
while (c=src.peek()) {
if (isdigit(c)) {
src >> c;
buf += c;
}
else {
break;
}
}
return Token(NUM, buf);
}
else if (isalpha(c)) {
string buf;
buf.push_back(c);
while (c = src.peek()) {
if (isdigit(c)||isalpha(c)) {
src >> c;
buf += c;
}
else {
break;
}
}
if (buf == "int") {
return Token(INT, "int");
}
else if (buf == "void") {
return Token(VOID, "void");
}
else if (buf == "if") {
return Token(IF, "if");
}
else if (buf == "else") {
return Token(ELSE, "else");
}
else if (buf == "while") {
return Token(WHILE, "while");
}
else if (buf == "return") {
return Token(RETURN, "return");
}
else {
return Token(ID, buf);
}
}
else {
return Token(ERROR, string("词法分析第") + to_string(lineCount) + string("行:未识别的符号") + c);
}
}
return Token(ERROR, "UNKOWN ERROR");
}
// Lexical::result存放解析后的Token列表
void Lexical::analyse() {
while (1) {
Token t = getNextToken();
result.push_back(t);
if (t.first == ERROR) {
outputError(t.second);
}
else if (t.first == ENDFILE) {
break;
}
}
}
void Lexical::outputToStream(ostream&out) {
if (result.back().first == ERROR) {
out << token_to_string(result.back())<<endl;
}
else {
list<Token>::iterator iter;
for (iter = result.begin(); iter != result.end(); iter++) {
out << token_to_string(*iter) << endl;
}
}
}
void Lexical::outputToScreen() {
outputToStream(cout);
}
void Lexical::outputToFile(const char *fileName) {
ofstream fout;
fout.open(fileName, ios::out);
if (!fout.is_open()) {
cerr << "file " << fileName << " open error" << endl;
return;
}
outputToStream(fout);
fout.close();
}
list<Token>&Lexical::getResult() {
return result;
}