-
Notifications
You must be signed in to change notification settings - Fork 0
/
word_parsing.h
346 lines (327 loc) · 9.2 KB
/
word_parsing.h
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
//
// Created by 23207 on 2020/9/20.
//
#ifndef COMPILER_WORD_PARSING_H
#define COMPILER_WORD_PARSING_H
#include <iostream>
#include <fstream>
#include <memory.h>
#include <list>
#include "symbol.h"
#include "global.h"
#include "global_error.h"
using namespace std;
list<symbol> symbols;
list<symbol>::const_iterator list_it;
static list<symbol>::const_iterator temp_it1;
list<symbol>::const_iterator temp_it;
list<symbol>::const_iterator loc_it;
list<global_error> global_errors;
void g_error(int line, char c) {
global_errors.emplace_back(line, c);
}
void output_errors() {
int line = -1;
for (auto err_it = global_errors.cbegin();err_it != global_errors.cend();err_it++) {
global_error err = *err_it;
if (err.get_line() != line) {
err.output();
line = err.get_line();
}
}
}
void g_error() {
// type not captured
return;
}
bool is_alpha(char c) {
return (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || (c == '_'));
}
bool is_suitable_for_string(char c) {
if (c == 32 || c == 33 || (c >= 35 && c <= 126)) return true;
return false;
}
bool is_suitable_for_char(char c) {
if (c == '+' || c == '*' || c == '-' || c == '/' || is_alpha(c) || ('0' <= c && c <= '9')) return true;
else return false;
}
string tolower(string cc) {
string ret = cc;
for (char & i : ret) {
if ('A' <= i && i <= 'Z') {
i = i + 32;
}
}
return ret;
}
void parse_symbols()
{
fstream f;
f.open("testfile.txt");
char c = f.get();
int line_num = 1;
int col_num = 0;
while (true) {
while (c == ' ' || c == '\t' || c == '\v' || c == '\f' || c == '\r') { c = f.get(); }
if (c == EOF) { break; }
if (c == '\n') {
line_num++;
col_num = 0;
c = f.get();
continue;
}
// 字符
if (c == '\'') {
string cc;
// TODO exception
bool printed = false;
while ((c = f.get()) != '\'') {
if (!printed && !is_suitable_for_char(c)) {
g_error(line_num, 'a');
printed = true;
}
col_num++;
cc.append(1, c);
}
col_num++;
c = f.get();
symbols.emplace_back(2, cc[0], cc, line_num, col_num);
}
// 字符串
else if (c == '"') {
string cc;
// TODO exception
bool printed = false;
while ((c = f.get()) != '"') {
if (!is_suitable_for_string(c) && !printed) {
g_error(line_num, 'a');
printed = true;
}
if (c == '\\') {
cc.append(2, c);
col_num++;
}
else {
col_num++;
cc.append(1, c);
}
}
if (cc.length() == 0) g_error(line_num, 'a');
col_num++;
c = f.get();
symbols.emplace_back(3, cc[0], cc, line_num, col_num);
}
// 标识符及保留字
else if (is_alpha(c)) {
string cc;
do {
cc.append(1, c);
col_num++;
c = f.get();
} while (('0' <= c && c <= '9') || is_alpha(c));
int k;
for (k = 0;k < rwn;k++) {
string ret = tolower(cc);
if (ret == reserved_words[k]) break;
}
if (k < rwn) {
symbols.emplace_back(reserved2id[k], 0, cc, line_num, col_num);
} else {
symbols.emplace_back(0, 0, cc, line_num, col_num);
}
}
// 整数常量
else if ('0' <= c && c <= '9') {
int k = 0;
int num = 0;
string cc;
do {
cc.append(1, c);
num = 10 * num + (c - '0');
k = k + 1;
col_num++;
c = f.get();
} while (c >= '0' && c <= '9');
symbols.emplace_back(1, num, cc, line_num, col_num);
}
// +
else if (c == '+') {
string cc("+");
col_num++;
c = f.get();
symbols.emplace_back(19, 0, cc, line_num, col_num);
}
// -
else if (c == '-') {
string cc("-");
col_num++;
c = f.get();
symbols.emplace_back(20, 0, cc, line_num, col_num);
}
// *
else if (c == '*') {
string cc("*");
col_num++;
c = f.get();
symbols.emplace_back(21, 0, cc, line_num, col_num);
}
// /
else if (c == '/') {
string cc("/");
col_num++;
c = f.get();
symbols.emplace_back(22, 0, cc, line_num, col_num);
}
// 小于等于、小于
else if (c == '<') {
col_num++;
c = f.get();
if (c == '=') {
col_num++;
c = f.get();
string cc("<=");
symbols.emplace_back(24, 0, cc, line_num, col_num);
} else {
string cc("<");
symbols.emplace_back(23, 0, cc, line_num, col_num);
}
}
// 大于等于、大于
else if (c == '>') {
col_num++;
c = f.get();
if (c == '=') {
col_num++;
c = f.get();
string cc(">=");
symbols.emplace_back(26, 0, cc, line_num, col_num);
} else {
string cc(">");
symbols.emplace_back(25, 0, cc, line_num, col_num);
}
}
// 等于、赋值
else if (c == '=') {
col_num++;
c = f.get();
if (c == '=') {
col_num++;
c = f.get();
string cc("==");
symbols.emplace_back(27, 0, cc, line_num, col_num);
} else {
string cc("=");
symbols.emplace_back(30, 0, cc, line_num, col_num);
}
}
// 不等于
else if (c == '!') {
col_num++;
c = f.get();
if (c == '=') {
col_num++;
c = f.get();
string cc("!=");
symbols.emplace_back(28, 0, cc, line_num, col_num);
}
// TODO exception
}
// 冒号
else if (c == ':') {
string cc(":");
col_num++;
c = f.get();
symbols.emplace_back(29, 0, cc, line_num, col_num);
}
// 分号
else if (c == ';') {
string cc(";");
col_num++;
c = f.get();
symbols.emplace_back(31, 0, cc, line_num, col_num);
}
// 逗号
else if (c == ',') {
string cc(",");
col_num++;
c = f.get();
symbols.emplace_back(32, 0, cc, line_num, col_num);
}
// 各种括号
else if (c == '(') {
string cc("(");
col_num++;
c = f.get();
symbols.emplace_back(33, 0, cc, line_num, col_num);
}
else if (c == ')') {
string cc(")");
col_num++;
c = f.get();
symbols.emplace_back(34, 0, cc, line_num, col_num);
}
else if (c == '[') {
string cc("[");
col_num++;
c = f.get();
symbols.emplace_back(35, 0, cc, line_num, col_num);
}
else if (c == ']') {
string cc("]");
col_num++;
c = f.get();
symbols.emplace_back(36, 0, cc, line_num, col_num);
}
else if (c == '{') {
string cc("{");
col_num++;
c = f.get();
symbols.emplace_back(37, 0, cc, line_num, col_num);
}
else if (c == '}') {
string cc("}");
col_num++;
c = f.get();
symbols.emplace_back(38, 0, cc, line_num, col_num);
}
// TODO exceptions
else {
g_error(line_num, 'a');
col_num++;
c = f.get();
}
}
list_it = symbols.cbegin();
}
symbol getsym() {
symbol ans = *list_it;
if (t3) {
if (list_it != symbols.cbegin()) {
fout << symbol_type[(*temp_it1).get_type()] << " ";
fout << (*temp_it1).get_source() << endl;
}
}
temp_it = temp_it1;
temp_it1 = list_it;
list_it++;
return ans;
}
symbol get_loc_sym() {
symbol ans = *loc_it;
loc_it++;
return ans;
}
void output_symbols_to_file() {
for (auto it = symbols.cbegin(); it != symbols.end(); it++) {
fout << symbol_type[it->get_type()] << " ";
fout << it->get_source() << endl;
}
}
void output_symbols_to_console() {
while (list_it != symbols.end()) {
symbol temp = getsym();
cout << symbol_type[temp.get_type()] << " ";
cout << temp.get_source() << endl;
}
}
# endif