/
语法分析.cpp
634 lines (525 loc) · 11.9 KB
/
语法分析.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
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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
#include "data.h"
extern char* err_msg[];
extern vector<comtab> comtabs; //变量表
extern vector<funtab> funtabs; //函数表
extern vector<Quadruple> quadruples;//四元式序列表
extern char token[];
extern bool enter(char name[]);
extern bool isexist(char name[]);
extern bool isFuncExist(char name[]);
extern int FindTypeByName(char tname[]);
extern int FindIdByName(char funname[]);
extern char* GetNameByID(int index);//通过变量id找到名字
extern int GetIdByName(char varname[]);//通过变量名字找到id
extern void BackPatch(int p, int t);
extern int merge(int p1, int p2);
int sno = 1; //当前四元式序号
int curfunc; //当前函数序号
int temp = 0; //临时变量序号
void program();//1.程序
void func();//2.过程 +fun
void parameter();//3.参数列表 +var
void statementlists();//4.语句
void complexStatementlists();//5.复合语句
void varDefinition();//6.变量定义语句 +var
int datatype();//7.数据类型
void inputStatement();//8.输入语句
void outputStatement();//9.输出语句
void assignment();//10.赋值语句
void functioncall();//11.函数调用语句
void passparameter(char []);//12.传递参数
void switchStatement();//13.分支语句
void loopStatement();//14.循环语句
Val expression();//15.表达式 !!
Val Item();//16.项
Val Factor();//17.因子
TFexit booleaexpression();//18.布尔表达式
TFexit relationexpression();//19.关系表达式
int relation();//20.关系
extern void error(int n);
void advance() {
lookahead = getToken();
}
bool match(int Tok) {
bool matched = true;
if (lookahead != Tok) {
matched = false;
error(Tok);
}
advance();
return matched;
}
//1.<程序>—> <过程> [<过程>]
void program(){
func();
while (lookahead == sub){
func();
}
if (lookahead != FEOF) {
error(unexpectedFileEnd);
}
}
//2.<过程>—> sub id <参数列表> <语句> end sub
void func(){
match(sub);
char funname[MAXIDLEN];
strcpy(funname, token);
match(id);
//在表中插入定义的函数名
if (!enter(funname)){
strcpy(token, funname);
error(49);
}
//添加函数声明表
comtabs.back().type = FUN;
funtabs.push_back(funtab(funname));
//current function = funtabs.size()-1;
curfunc = funtabs.size();
parameter();
statementlists();
curfunc = 0;
match(end);
match(sub);
}
//3.<参数列表>—> ( id as <数据类型> [,id as <数据类型>] ) |ε
void parameter(){
if (lookahead == LP){
match(LP);
char tmpname[MAXIDLEN];
strcpy(tmpname, token);
match(id);
//在表中插入定义的变量
if (!enter(tmpname)){
strcpy(token, tmpname);
error(47);
}
match(as);
int tmptype = datatype();
comtabs.back().type = tmptype;
comtabs.back().funid = curfunc;
funtabs.back().para.push_back(tmptype); //函数表中添加形参类型
while (lookahead == comma){
match(comma);
strcpy(tmpname, token);
match(id);
//在表中插入定义的变量
if (!enter(tmpname)){
strcpy(token, tmpname);
error(47);
}
match(as);
int tmptype = datatype();
comtabs.back().type = tmptype;
comtabs.back().funid = curfunc;
funtabs.back().para.push_back(tmptype);
}
match(RP);
}
}
/*
4.<语句>—> <分支语句>
| <赋值语句>
| <循环语句>
| <输入语句>
| <输出语句>
| <函数调用语句>
| <变量定义语句>
| <复合语句>
|ε
*/
//**//
void statementlists(){
if (lookahead == IF){
//分支语句
switchStatement();
}
else if (lookahead == id){
//赋值语句
assignment();
}
else if (lookahead == WHILE){
//while语句
loopStatement();
}
else if (lookahead == input){
//输入语句
inputStatement();
}
else if (lookahead == output){
//输出
outputStatement();
}
else if (lookahead == call){
//函数调用
functioncall();
}
else if (lookahead == var){
//变量定义
varDefinition();
}
else if (lookahead == begin){
//复合语句
complexStatementlists();
}
}
//5. <复合语句>—> begin <语句> [ , <语句> ] end
void complexStatementlists(){
match(begin);
statementlists();
while (lookahead == comma){
match(comma);
statementlists();
}
match(end);
}
//6.<变量定义语句>—> var id [ , id ] as <数据类型> ;
void varDefinition(){
match(var);
char varname[MAXIDLEN];
strcpy(varname, token);
match(id);
int cnt = 1;
if (!enter(varname)){
strcpy(token, varname);
error(47);
}
while (lookahead == comma){
match(comma);
strcpy(varname, token);
match(id);
cnt++;
if (!enter(varname)){
strcpy(token,varname);
error(47);
}
}
match(as);
int typetmp = datatype();
for (int i = 1; i <= cnt; i++){
comtabs[comtabs.size() - i].type = typetmp;
}
match(semicolon);
}
//7.<数据类型>—> integer | float
int datatype(){
if (lookahead == INTEGER){
advance();
return INTEGER;
}
else if (lookahead == FLOAT){
advance();
return FLOAT;
}
else{
match(INTEGER); //出错信息
return -1;
}
}
//8.<输入语句>—> input id [ , id ] ;
void inputStatement(){
match(input);
char varname[MAXIDLEN];
strcpy(varname, token);
match(id);
if (!isexist(varname)){
strcpy(token, varname);
error(46);
}
while (lookahead == comma){
match(comma);
strcpy(varname, token);
match(id);
if (!isexist(varname)){
strcpy(token, varname);
error(46);
}
}
match(semicolon);
}
//9.<输出语句>—> output <表达式> [ , <表达式> ] ;
void outputStatement(){
match(output);
expression();
while (lookahead == comma){
match(comma);
expression();
}
match(semicolon);
}
//10.<赋值语句>—> id = <表达式> ;
void assignment(){
char varname[MAXIDLEN];
strcpy(varname, token);
Val v2 = Val(1, GetIdByName(varname));
match(id);
if (!isexist(varname)){
strcpy(token, varname);
error(46);
}
match(equl);
Val v = expression();
match(semicolon);
quadruples.push_back(Quadruple(sno++, equl, v, v2));
//生成四元式
/*printf("%3d (=,", sno++);
if (v.type == 1) printf("%s", GetNameByID(v.value1));
else if (v.type == 0) printf("%d", v.value1);
else if (v.type == 2) printf("%lf", v.value2);
else printf("t%d", v.value1);
printf(",%s)\n", varname);*/
}
//11.<函数调用语句>—> call id ( <传递参数> ) ;
void functioncall(){
match(call);
char funname[MAXIDLEN];
strcpy(funname, token);
match(id); //函数名
if (!isFuncExist(funname)){//函数是否存在
strcpy(token, funname);
error(48);
}
match(LP);
passparameter(funname);
match(RP);
match(semicolon);
}
//12.<传递参数>—> id [ , id ]| ε
void passparameter(char funname[]){
vector<int> tmptypes;//实参类型序列
char varname[MAXIDLEN];
strcpy(varname, token);
if (lookahead==id){
match(id);
if (!isexist(varname)){
strcpy(token, varname);
error(46);
}
int tmptype = FindTypeByName(varname);
tmptypes.push_back(tmptype);
while (lookahead == comma){
match(comma);
strcpy(varname, token);
match(id);
if (!isexist(varname)){
strcpy(token, varname);
error(46);
}
tmptype = FindTypeByName(varname);
tmptypes.push_back(tmptype);
}
int callfuncId = FindIdByName(funname);
if (tmptypes.size() != funtabs[callfuncId].para.size()){
error(51);
}
else{
for (int i = 0; i < tmptypes.size(); i++){
if (tmptypes[i] != funtabs[callfuncId].para[i]){
error(52);
}
}
}
}
}
//13.<分支语句>—> if <布尔表达式> then <语句>
//| if <布尔表达式> then <语句> else <语句>
void switchStatement(){
if (lookahead == IF){
match(IF);
TFexit tf = booleaexpression();//真假出口
match(THEN);
//填真出口
BackPatch(tf.TC, sno);
statementlists();
if (lookahead == ELSE){
// 1 无条件跳转
int j = sno;
quadruples.push_back(Quadruple(sno++, 0,Val(),Val()));
match(ELSE);
//回填假出口
BackPatch(tf.FC, sno);
statementlists();
//回填 1 无条件跳转
BackPatch(j, sno);
}
else {
// 回填假出口
BackPatch(tf.FC, sno);
}
}
else{
error(24);
}
}
//14.<循环语句>—> while <布尔表达式> do <语句>
void loopStatement(){
match(WHILE);
//int boolId = 序号全局变量
int boolId = sno;
TFexit tf = booleaexpression();//真假出口
match(DO);
//回填真出口
BackPatch(tf.TC, sno);
statementlists();
//(j boolId)
quadruples.push_back(Quadruple(sno++, 0, Val(), Val(), boolId));
//回填假出口
BackPatch(tf.FC, sno);
}
//15.<表达式>—> <项> [ +|- <项> ]
Val expression(){
Val v1 = Item();
while (lookahead == plus || lookahead == minus){
int op = lookahead;
if (lookahead == plus) match(plus);
else match(minus);
Val v2 = Item();
quadruples.push_back(Quadruple(sno++, op == plus ? plus : minus, v1,v2,temp));
//生成四元式
/*printf("%3d (%c,", sno++, op == plus ? '+' : '-');
if (v1.type == 1) printf("%s", GetNameByID(v1.value1));
else if (v1.type == 0) printf("%d", v1.value1);
else if (v1.type == 2) printf("%lf", v1.value2);
else printf("t%d", v1.value1);
printf(",");
if (v2.type == 1) printf("%s", GetNameByID(v2.value1));
else if (v2.type == 0) printf("%d", v2.value1);
else if (v2.type == 2) printf("%lf", v2.value2);
else printf("t%d", v2.value1);
printf(",t%d)\n", temp);*/
v1.type = -1;
v1.value1 = temp++;
}
return v1;
}
//16.<项>—> <因子> [ *|/ <因子> ]
Val Item(){
Val v1 = Factor();
while (lookahead == mutiply || lookahead == div){
int op = lookahead;
if (op == mutiply) match(mutiply);
else match(div);
Val v2 = Factor();
quadruples.push_back(Quadruple(sno++, op == mutiply ? mutiply : div, v1, v2, temp));
//生成四元式
/*printf("%3d (%c,", sno++, op == mutiply ? '*' : '/');
if (v1.type == 1) printf("%s", GetNameByID(v1.value1));
else if (v1.type == 0) printf("%d", v1.value1);
else if (v1.type == 2) printf("%lf", v1.value2);
else printf("t%d", v1.value1);
printf(",");
if (v2.type == 1) printf("%s", GetNameByID(v2.value1));
else if (v2.type == 0) printf("%d", v2.value1);
else if (v2.type == 2) printf("%lf", v2.value2);
else printf("t%d", v2.value1);
printf(",t%d)\n", temp);*/
v1.type = -1;
v1.value1 = temp++;
}
return v1;
}
//17.<因子>—> id | con | deci | (<表达式>)
Val Factor(){
Val val;
if (lookahead == id){
val.type = 1;//变量
char varname[MAXIDLEN];
strcpy(varname, token);
match(id);
if (!isexist(varname)){
strcpy(token, varname);
error(46);
}
val.value1 = GetIdByName(varname);
}
else if (lookahead == con){
val.type = 0;//整数
val.value1 = atoi(token);
match(con);
}
else if (lookahead == deci){
val.type = 2;//小数
val.value2 = atof(token);
match(deci);
}
else if (lookahead == LP){
match(LP);
val = expression();
match(RP);
}
else {
error(24);
}
return val;
}
//18.<布尔表达式>—> <关系表达式> [ and | or <布尔表达式> ]
TFexit booleaexpression(){
TFexit reexit = relationexpression();//1 关系表达式
TFexit tf = reexit;//指向当前真假出口
while (lookahead == and || lookahead==or){
if (lookahead == and){
match(and);
//关系表达式 1 的真出口确定
quadruples[reexit.TC].result = sno;
TFexit tmp = booleaexpression();
//TFexit tmp = relationexpression();
//quadruples[tmp.TC].result = sno;
tf.FC = merge(tmp.FC, tf.FC);
tf.TC = tmp.TC;//唯一的一个真出口
//合并真/假出口
return tf;
}
else if (lookahead == or){
match(or);
//关系表达式 1 的假出口确定
quadruples[reexit.FC].result = sno;
TFexit tmp = booleaexpression();
//TFexit tmp = relationexpression();
//quadruples[tmp.FC].result = sno;
tf.TC = merge(tmp.TC, tf.TC);
tf.FC = tmp.FC;//唯一的一个假出口
//返回真假出口
return tf;
}
}
return tf;
}
//19. <关系表达式>—> <表达式> <关系> <表达式>
TFexit relationexpression(){
Val v1 = expression();
int re = relation();
Val v2 = expression();
quadruples.push_back(Quadruple(sno++, re, v1, v2));
quadruples.push_back(Quadruple(sno++,0,Val(),Val()));//无条件跳转op=0
return TFexit(sno - 2, sno - 1);
}
//20. <关系>—> < | <= | > | >= | == | <>
int relation(){
if (lookahead == lessthan){
match(lessthan);
return lessthan;
}
else if (lookahead == lessequl){
match(lessequl);
return lessequl;
}
else if (lookahead == morethan){
match(morethan);
return morethan;
}
else if (lookahead == moreequl){
match(moreequl);
return moreequl;
}
else if (lookahead == equlequl){
match(equlequl);
return equlequl;
}
else if (lookahead == notequl){
match(notequl);
return notequl;
}
else {
error(24);
return ERROR;
}
}