-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.c
549 lines (540 loc) · 14.2 KB
/
main.c
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
#include <stdio.h>
#include <string.h>
#define TRUE 1
#define FALSE 0
#define KEY_NUM 14 // 关键字个数
#define KEY_LEN 20 // 关键字长度
#define ID_MAX 1000 // 标识符个数
#define ID 25 // 标识符
#define DECNUM 26 // 十进制无符号整数
#define HEXNUM 27 // 十六进制无符号整数
#define OCTNUM 28 // 八进制无符号整数
#define GT 29 // >
#define GE 30 // >=
#define LS 31 // <
#define LE 32 // <=
#define EQ 33 // ==
#define NE 34 // !=
#define ASSIGN 35 // =
#define NOT 36 // !
#define AND 37 // &
#define DAND 38 // &&
#define OR 39 // |
#define DOR 40 // ||
// 14个(1-14)
char key_set[][KEY_LEN] = {"for",
"while",
"do",
"switch",
"case",
"break",
"continue",
"if",
"else",
"char",
"int",
"float",
"return",
"main"};
// 10个(15-24)
char single_set[][KEY_LEN] = {"+",
"-",
"/",
"*",
"(",
")",
"{",
"}",
";",
","};
// 符号表的数据结构
struct ID_NAME{
char name[KEY_LEN];
char type[KEY_LEN];
char address[KEY_LEN];
};
struct ID_LIST{
struct ID_NAME names[ID_MAX];
int length;
};
char* INPUT_FILE = "/Users/DWL/Downloads/input_file.txt"; // 输入文件
char* OUTPUT_FILE = "/Users/DWL/Downloads/output_file.txt"; // 输出文件
char strBuffer[1026]; // 字符串缓冲区
char token[KEY_LEN];
int line_num = 0; // 需要统计的程序的行数,表示当前行号,用于定位错误位置
int state = 0; // 当前程序的状态
struct ID_LIST idList;
//从字符串中获取指针当前所指位置的字符
char getChar(char* str)
{
return *str;
}
// 判断是否是数字0-9
int isDigit09(char c)
{
if (c>='0' && c<='9')
{
return TRUE;
} else {
return FALSE;
}
}
// 判断是否是数字1-9
int isDigit19(char c)
{
if (c>='1' && c<='9')
{
return TRUE;
} else {
return FALSE;
}
}
// 判断是否是数字0-7
int isDigit07(char c)
{
if (c>='0' && c<='7')
{
return TRUE;
} else {
return FALSE;
}
}
// 判断是否是0-9或a-f
int isDigit09Letteraf(char c)
{
if ((c>='0' && c<='9') || (c>='a' && c<='f') )
{
return TRUE;
} else {
return FALSE;
}
}
// 判断是否是字母
int isLetter(char c)
{
if ((c>='a'&&c<='z') || (c>='A'&& c<='Z'))
{
return TRUE;
}
else
{
return FALSE;
}
}
//判断是否是标点符号
int isPunctuation(char c)
{
for(int i=0; i<10; i++)
{
if(c == single_set[i][0])
{
return (i+15);
}
}
return FALSE;
}
// 判断是否是关键字
int isKey(char* str)
{
for(int i=0; i<KEY_NUM; i++)
{
if(strcmp(str, key_set[i]) ==0)
{
return i+1;
}
}
return FALSE;
}
void error()
{
printf("the %dth line error\n",line_num);
}
// 将token存入符号表
void install_id(char id[])
{
struct ID_NAME tmp;
strcpy(tmp.name, id);
strcpy(tmp.type, "-1");
strcpy(tmp.address, "?");
int flag = 0;
if(idList.length)
{
for(int i=0; i<idList.length; i++)
{
if(strcmp(idList.names[i].name, tmp.name) == 0)
{
flag = 1;
break;
}
}
if(flag == 0)
{
idList.names[idList.length] = tmp;
idList.length++;
}
}
else
{
idList.names[0] = tmp;
idList.length++;
}
}
//对一行字符串的内容进行词法分析
void process_string(char* buf)
{
char C = getChar(buf); // 从but中读取一个字符
while(C!='\n' && C!='\0') // 不是换行符,不是结束符
{
switch(state)
{ //对当前状态进行分析
case 0: //处于代读取字符串状态,还没有进入任何自动机
if(isLetter(C) || C=='_' )
{
state = 1; //进入状态1
char tmp[2];
tmp[0] = C;
tmp[1] = '\0';
strcpy(token, tmp);
buf++;
C = getChar(buf);
}
else if(C == '0')
{
state = 2;
char tmp[2];
tmp[0] = C;
tmp[1] = '\0';
strcpy(token, tmp);
buf++;
C = getChar(buf);
}
else if(isDigit19(C))
{
state = 12;
char tmp[2];
tmp[0] = C;
tmp[1] = '\0';
strcpy(token, tmp);
buf++;
C = getChar(buf);
}
else if(isPunctuation(C))
{
state = 0;
printf("(%d, %c)\n", isPunctuation(C), C);
buf++;
C = getChar(buf);
}
else if(C == ' ') //空格
{
state = 0;
buf++;
C = getChar(buf);
}
else if(C == '<')
{
state = 3;
char tmp[2];
tmp[0] = C;
tmp[1] = '\0';
strcpy(token, tmp);
buf++;
C = getChar(buf);
}
else if(C=='>')
{
state = 4;
char tmp[2];
tmp[0] = C;
tmp[1] = '\0';
strcpy(token, tmp);
buf++;
C = getChar(buf);
}
else if(C == '=')
{
state = 5;
char tmp[2];
tmp[0] = C;
tmp[1] = '\0';
strcpy(token, tmp);
buf++;
C = getChar(buf);
}
else if(C == '!')
{
state = 6;
char tmp[2];
tmp[0] = C;
tmp[1] = '\0';
strcpy(token, tmp);
buf++;
C = getChar(buf);
}
else if(C == '&')
{
state = 7;
char tmp[2];
tmp[0] = C;
tmp[1] = '\0';
strcpy(token, tmp);
buf++;
C = getChar(buf);
}
else if(C == '|')
{
state = 8;
char tmp[2];
tmp[0] = C;
tmp[1] = '\0';
strcpy(token, tmp);
buf++;
C = getChar(buf);
}
else
{
error();
//跳过这个字符
buf++;
C = getChar(buf);
}
break;
case 1:
if(isLetter(C) || C=='_' || isDigit09(C))
{
state=1;
char tmp[2];
tmp[0] = C;
tmp[1] = '\0';
strcat(token, tmp);
buf++;
C = getChar(buf);
}
else
{ //到了分隔符
if(isKey(token))
{
printf("(%d, %s)\n", isKey(token), token);
}
else
{
install_id(token);
printf("(%d, %s)\n", ID, token);
}
state = 0;
}
break;
case 2:
if(isDigit07(C))
{
state = 25;
char tmp[2];
tmp[0] = C;
tmp[1] = '\0';
strcat(token, tmp);
buf++;
C = getChar(buf);
}
else if(C == 'x')
{
state = 26;
char tmp[2];
tmp[0] = C;
tmp[1] = '\0';
strcat(token, tmp);
buf++;
C = getChar(buf);
}
else
{
printf("(%d, %s)\n", DECNUM, token);
state = 0;
}
break;
case 3:
if(C == '=')
{
state = 0;
printf("(%d, <=)\n", LE);
buf++;
C = getChar(buf);
}
else
{
state = 0;
printf("(%d, <)\n", LS);
}
break;
case 4:
if(C == '=')
{
state = 0;
printf("(%d, >=)\n", GE);
buf++;
C = getChar(buf);
}
else
{
state = 0;
printf("(%d, >)\n", GT);
}
break;
case 5:
if(C == '=')
{
state = 0;
printf("(%d, ==)\n", EQ);
buf++;
C = getChar(buf);
}
else
{
state = 0;
printf("(%d, =)\n", ASSIGN);
}
break;
case 6:
if(C == '=')
{
state = 0;
printf("(%d, !=)\n", NE);
buf++;
C = getChar(buf);
}
else
{
printf("(%d, !)\n", NOT);
state = 0;
}
break;
case 7:
if(C == '&')
{
printf("(%d, &&)\n", DAND);
state = 0;
buf++;
C = getChar(buf);
}
else
{
printf("(%d, &)\n", AND);
state = 0;
}
break;
case 8:
if(C == '|')
{
printf("(%d, ||)\n", DOR);
state = 0;
buf++;
C = getChar(buf);
}
else
{
printf("(%d, |)\n", OR);
state = 0;
}
break;
case 12:
if ((isDigit09(C)))
{
state = 12;
char tmp[2];
tmp[0] = C;
tmp[1] = '\0';
strcat(token, tmp);
buf++;
C = getChar(buf);
}
else
{
printf("(%d, %s)\n", DECNUM, token);
state = 0;
}
break;
case 25:
if ((isDigit07(C)))
{
state = 25;
char tmp[2];
tmp[0] = C;
tmp[1] = '\0';
strcat(token, tmp);
buf++;
C = getChar(buf);
}
else
{
printf("(%d, %s)\n", OCTNUM, token);
state = 0;
}
break;
case 26:
if ((isDigit09Letteraf(C)))
{
state = 28;
char tmp[2];
tmp[0] = C;
tmp[1] = '\0';
strcat(token, tmp);
buf++;
C = getChar(buf);
}
else
{
error();
state = 0;
buf++;
C = getChar(buf);
}
break;
case 28:
if ((isDigit09Letteraf(C)))
{
state = 28;
char tmp[2];
tmp[0] = C;
tmp[1] = '\0';
strcat(token, tmp);
buf++;
C = getChar(buf);
}
else
{
printf("(%d, %s)\n", HEXNUM, token);
state = 0;
}
break;
default:
error();
state = 0;
buf++;
C = getChar(buf);
}
}
}
int main() {
FILE* file = fopen(INPUT_FILE,"rw"); //只读的方式打开要扫描的文件
freopen(OUTPUT_FILE,"w",stdout); //输出重定向
if(file == NULL)
{
printf("Error! opening file");
return 0;
}
printf("*****输出单词*****\n");
idList.length = 0;
//实现从文件中逐行读取
while(!feof(file))
{
line_num++;
fgets(strBuffer,1024,file);
process_string(strBuffer); //处理字符串
}
fclose(file);
// 输出符号表
printf("*****符号表*****\n");
for(int i=0; i<idList.length; i++){
printf("(%s, %s, %s)\n", idList.names[i].name, idList.names[i].type, idList.names[i].address);
}
return 0;
}