-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGrammarAnalyzer.cpp
2004 lines (1889 loc) · 52.1 KB
/
GrammarAnalyzer.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
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include"GrammarAnalyzer.h"
/*构造器函数*/
GrammarAnalyzer::GrammarAnalyzer(FaultHandler& _f, SymbolTable& _s, LexicalAnalyzer& _l,MidCodeFramework& _raw,string file)
:f(_f),table(_s),raw(_raw), lex(_l) {
if (file == "") {
file = "/dev/null";
}
out.open(file,ios_base::trunc|ios_base::out);
currentScope="";
}
/*获取下一词素,并输出上一个词素*/
Lexical GrammarAnalyzer::getNextSym() {
if (lexicalTest) { lex.printResult(out); }
return lex.getNextSym();
}
/*一直跳到下一个分号为止,用于局部化处理*/
void GrammarAnalyzer::toNextSemicon() {
while (1) {
Lexical tmp = lex.getNextSym();
if (tmp == END || tmp == SEMICN) {
break;
}
}
}
/*一直跳到下一个分号为止,用于局部化处理*/
void GrammarAnalyzer::toNextBrace() {
while (1) {
Lexical tmp = lex.getNextSym();
if (tmp == END || tmp == RBRACE) {
break;
}
}
}
/*输出语法分析作业的输出*/
void GrammarAnalyzer::homeworkOn(bool _c, bool _l) {
course = _c;
lexicalTest = _l;
}
/*<整数>*/
int GrammarAnalyzer::integer() {
int sign = 1;
Lexical tk = lex.sym().type;//检查符号
if (tk == PLUS || tk == MINU) {
sign = tk == MINU ? -1 : 1;
getNextSym();
}
if (lex.sym().type != INTCON) {
f.handleFault(lex.lineNumber(), "需要无符号整数");
throw 0;
}
int value = lex.sym().value;//获取数字
getNextSym();
if (course) {
out << "<无符号整数>" << endl;
out << "<整数>" << endl;
}
return value*sign;
}
/*<常量声明>
不会抛出异常,在此函数中进行局部化处理*/
void GrammarAnalyzer::constDeclearation() {
bool init = true;
while (1) {
try {
if (lex.sym().type != CONSTTK) {
break;//检查是否是const,若不是则结束
}
init = false;
getNextSym();
//完成读取const符号
constDefination();//检查常量定义
if (lex.sym().type != SEMICN) {//检查是否为分号
f.handleCourseFault(lex.lineNumber(), NOSEMICN);
f.handleFault(lex.lineNumber(), "缺少 ;");
}
else {
getNextSym();
}
//完成读取分号
}
catch (int e) {
toNextSemicon();//跳读
lex.getNextSym();
}
}
if (course&&!init) {out << "<常量说明>" << endl;}
}
/*<常量定义>*/
void GrammarAnalyzer::constDefination() {
bool init = true, error = false;;
if (lex.sym().type != INTTK && lex.sym().type != CHARTK) {//检查int或char
f.handleFault(lex.lineNumber(), "需要类型标识符");
throw 0;
}
Lexical vartype = lex.sym().type;
getNextSym();
//读取标识符完成
while (1) {
if (init) {
init = false;//第一个变量定义不能没有且不是逗号开头
}
else {
if (lex.sym().type != COMMA) {
break;//若没有逗号说明定义结束
}
else {
getNextSym();
}
//读取逗号完成
}
if (lex.sym().type != IDENFR) {//检查并获取标识符
f.handleFault(lex.lineNumber(), "需要变量名称");
throw 0;
}
string varname = lex.sym().str;
getNextSym();
//读取标识符完成
SymbolEntry* entry = table.addSymbol(currentScope, varname, false);
if (entry == NULL) {
f.handleCourseFault(lex.lineNumber(), REDEFINED);
f.handleFault(lex.lineNumber(), "常量定义中有符号重定义");
error = true;
}//检查是否为重定义
if (lex.sym().type != ASSIGN) {//获取等号
f.handleFault(lex.lineNumber(), "需要=");
throw 0;
}
getNextSym();
//读取等号完成
if (vartype == INTTK) {/*根据类型读取值并写入符号表*/
int intvalue;
try {
intvalue = integer();
}
catch (int e) {
f.handleCourseFault(lex.lineNumber(), ILLEGALCONSTDEFINITION);
f.handleFault(lex.lineNumber(), "常量定义中=后面只能是整型或字符型常量");
throw 0;
}
//读取整数完成
if (!error) {
entry->type = TYPEINTCONST;
entry->initValue = intvalue;
//写入符号表完成
}
}
else {
if (lex.sym().type != CHARCON) {
f.handleCourseFault(lex.lineNumber(), ILLEGALCONSTDEFINITION);
f.handleFault(lex.lineNumber(), "常量定义中=后面只能是整型或字符型常量");
throw 0;
}
char charvalue = (char)(lex.sym().value);
getNextSym();
//读取字符完成
if (!error) {
entry->type = TYPECHARCONST;
entry->initValue = charvalue;
//写入符号表完成
}
}
}
if (course) { out << "<常量定义>" << endl; }
}
/*<变量说明>,供非全局段的变量声明所使用*/
void GrammarAnalyzer::variableDeclearation() {
bool init = true;
while (1) {
try {
if (lex.sym().type != INTTK && lex.sym().type != CHARTK) {
break;
}
//预读标识符
init = false;
variableDefination();
if (lex.sym().type != SEMICN) {//检查是否为分号
f.handleCourseFault(lex.lineNumber(), NOSEMICN);
f.handleFault(lex.lineNumber(), "缺少 ;");
}
else {
getNextSym();
}
//读取分号完成
}
catch (int e) {
toNextSemicon();//跳读
lex.getNextSym();
break;
}
}
if (course && !init) { out << "<变量说明>" << endl; }
}
/*<变量说明>,供declearationHeader包装调用使用*/
void GrammarAnalyzer::variableDeclearation(Lexical type,string varname) {
try {
variableDefination(true,type,varname);
if (lex.sym().type != SEMICN) {//检查是否为分号
f.handleCourseFault(lex.lineNumber(), NOSEMICN);
f.handleFault(lex.lineNumber(), "缺少 ;");
}
else {
getNextSym();
}
}
catch (int e) {
toNextSemicon();//跳读
lex.getNextSym();
}
}
/*<变量定义>,供各种情况使用
第一个参数代表是否是被包装器调用的,第二个参数传入包装器预读的变量类型和变量名称
*/
void GrammarAnalyzer::variableDefination(bool wraper,Lexical type,string name) {
Lexical vartype;
int error = false;
if (!wraper) {
if (lex.sym().type != INTTK && lex.sym().type != CHARTK) {
f.handleFault(lex.lineNumber(), "需要类型标识符");//未被包装:检测变量类型标识符
throw 0;
}
vartype = lex.sym().type;
getNextSym();
//读取标识符完成
}
else {
vartype = type;//已被包装:不需要检测变量类型标识符
}
bool init = true;
string varname;
while (1) {
if (init) {//是循环中读取的第一个变量
init = false;
if (wraper) {//需要处理被包装器预读的第一个变量
varname = name;
goto LABEL;//跳过读取标识符的阶段
}
}
else {
if (lex.sym().type != COMMA) {
break;
}
else {
getNextSym();
}
}
if (lex.sym().type != IDENFR) {
f.handleFault(lex.lineNumber(), "需要变量名称");//读变量名称
throw 0;
}
varname = lex.sym().str;
getNextSym();
//完成标识符读取
LABEL:
SymbolEntry* entry = table.addSymbol(currentScope, varname, false);//插入符号表
if (entry == NULL) {
f.handleCourseFault(lex.lineNumber(), REDEFINED);
f.handleFault(lex.lineNumber(), varname + "重定义");
error = true;
}
int dimension = 0;
if (lex.sym().type == LBRACK) {//判断是不是数组元素
if (currentScope != "") {
inlineable = false;
}
getNextSym();
if (lex.sym().type != INTCON) {
f.handleCourseFault(lex.lineNumber(), ILLEGALINDEX);
f.handleFault(lex.lineNumber(), "需要维数:数组元素的下标只能是整型表达式");
throw 0;
}
dimension = lex.sym().value;
getNextSym();
//读取维数完成
if (course) { out << "<无符号整数>"<<endl; }
if(lex.sym().type!=RBRACK){
f.handleCourseFault(lex.lineNumber(), NORBRACK);
f.handleFault(lex.lineNumber(), "缺少]");
}
else {
getNextSym();
}
//读取右中括号完成
}
if(!error){
entry->dimension = dimension;
if (vartype == INTTK) {
entry->type = dimension == 0 ? TYPEINT : TYPEINTARRAY;
}
else if (vartype == CHARTK) {
entry->type = dimension == 0 ? TYPECHAR : TYPECHARARRAY;
}
}
//向符号表中填入信息
}
if (course) { out << "<变量定义>" << endl; }
}
/*<有返回值函数定义>,供非变量声明区的有返回值函数声明*/
void GrammarAnalyzer::nonVoidFunctionDefination() {
hasReturned = false;
try {
Lexical retType;
string functionName;
declearationHeader(retType,functionName);
//获取变量名和返回类型完成
if (lex.sym().type != LPARENT) {//参数表的左括号
f.handleFault(lex.lineNumber(), "缺少参数表");
//todo 错误处理待定;
f.terminate();
}
getNextSym();
//处理左括号完成
raw.functionStart(functionName);
SymbolEntry* entry = table.addSymbol(currentScope, functionName, true);//插入符号表项
if (entry == NULL) {
f.handleCourseFault(lex.lineNumber(), REDEFINED);
f.handleFault(lex.lineNumber(), "函数名重定义" + functionName);
}
else {
entry->link->returnType = retType == INTTK ? RETINT : RETCHAR;
entry->type = TYPEFUNCTION;
}
if (entry != NULL) {
raw.midCodeInsert(MIDFUNC, MIDUNUSED,
entry->id, false,
(retType == INTTK ? MIDINT : MIDCHAR), false, MIDNOLABEL);
}
parameterList(entry);
//填入符号表信息
if (lex.sym().type != RPARENT) {
f.handleCourseFault(lex.lineNumber(), NORPARENT);
f.handleFault(lex.lineNumber(), "缺少)");
}
else {
getNextSym();
}
//参数表右括号完成
if (lex.sym().type != LBRACE) {//读取左大括号
f.handleFault(lex.lineNumber(), "缺少{");
throw 0;
}
else {
getNextSym();
}
//左大括号完成
currentScope = functionName;//变更当前作用域
compoundSentence();//复合语句
currentScope = "";//作用域变回来
if (!hasReturned) {
f.handleCourseFault(lex.lineNumber(), ILLEGALRETURNINNONVOID);
f.handleFault(lex.lineNumber(), "有返回值函数缺少返回语句");
}
if (lex.sym().type != RBRACE) {
f.handleFault(lex.lineNumber(), "缺少}");
throw 0;
}
getNextSym();
//获取右大括号
raw.functionEnd();
}
catch (int e) {
toNextBrace();
getNextSym();
}
if (course) { out << "<有返回值函数定义>" << endl; }
}
/*有返回值函数定义,供包装器调用使用,不会抛出异常*/
void GrammarAnalyzer::nonVoidFunctionDefination(Lexical retType,string functionName) {
hasReturned = false;
raw.functionStart(functionName);
try {
if (lex.sym().type != LPARENT) {//参数表的左括号
f.handleFault(lex.lineNumber(), "缺少参数表");
//todo 错误处理待定;
f.terminate();
}
getNextSym();
//左括号读取完成
SymbolEntry* entry = table.addSymbol(currentScope, functionName, true);//插入符号表项
if (entry == NULL) {
f.handleCourseFault(lex.lineNumber(), REDEFINED);
f.handleFault(lex.lineNumber(), "函数名重定义" + functionName);
}
else {
entry->link->returnType = retType == INTTK ? RETINT : RETCHAR;
entry->type = TYPEFUNCTION;
//写入符号表
}
if (entry != NULL) {
raw.midCodeInsert(MIDFUNC, MIDUNUSED,
entry->id, false,
(retType == INTTK ? MIDINT : MIDCHAR), false, MIDNOLABEL);
}
parameterList(entry);
//参数表
if (lex.sym().type != RPARENT) {
f.handleCourseFault(lex.lineNumber(), NORPARENT);//参数表右括号
f.handleFault(lex.lineNumber(), "缺少)");
}
else {
getNextSym();
}
//读取右括号
if (lex.sym().type != LBRACE) {//左大括号
f.handleFault(lex.lineNumber(), "缺少{");
throw 0;
}
else {
getNextSym();
}
//读取左大括号完成
currentScope = functionName;//作用域变过来
compoundSentence();//复合语句
currentScope = "";//作用域变回去
if (!hasReturned) {
f.handleCourseFault(lex.lineNumber(), ILLEGALRETURNINNONVOID);
f.handleFault(lex.lineNumber(), "有返回值函数缺少返回语句");
}
if (lex.sym().type != RBRACE) {//右大括号
f.handleFault(lex.lineNumber(), "缺少{");
throw 0;
}
getNextSym();
//右大括号读取完成
raw.functionEnd();
}
catch (int e) {
toNextBrace();
getNextSym();
}
if (course) { out << "<有返回值函数定义>" << endl; }
}
/*无返回值定义,不会抛出异常*/
void GrammarAnalyzer::voidFunctionDefination() {
hasReturned = false;
try {
//这个地方没有void符号因为已经被programme预读掉了
if (lex.sym().type != IDENFR) {
f.handleFault(lex.lineNumber(), "缺少函数名");
throw 0;
}
string functionName = lex.sym().str;
getNextSym();
//函数名读取完成
SymbolEntry* entry = table.addSymbol(currentScope, functionName, true);
if (entry == NULL) {
f.handleCourseFault(lex.lineNumber(), REDEFINED);
f.handleFault(lex.lineNumber(), "函数名重定义" + functionName);
}
else {
entry->link->returnType = RETVOID;
entry->type = TYPEFUNCTION;
}
raw.functionStart(functionName);
if (lex.sym().type != LPARENT) {
f.handleFault(lex.lineNumber(), "缺少参数表");
throw 0;
}
getNextSym();
//读走了左括号
if (entry != NULL) {
raw.midCodeInsert(MIDFUNC, MIDUNUSED,
entry->id, false,
MIDVOID, false, MIDNOLABEL);
}
parameterList(entry);
//参数表读取完成
if (lex.sym().type != RPARENT) {
f.handleCourseFault(lex.lineNumber(), NORPARENT);
f.handleFault(lex.lineNumber(), "缺少)");
}
else {
getNextSym();
}
//右括号读取完成
if (lex.sym().type != LBRACE) {
f.handleFault(lex.lineNumber(), "缺少{");
throw 0;
}
else {
getNextSym();
}
//读取左大括号完成
currentScope = functionName;
compoundSentence();
currentScope = "";
if (entry != NULL) {
raw.midCodeInsert(MIDRET, MIDUNUSED,
MIDUNUSED, false,
MIDUNUSED, false, MIDNOLABEL);
}
if (lex.sym().type != RBRACE) {
f.handleFault(lex.lineNumber(), "缺少}");
throw 0;
}
getNextSym();
//读取右大括号完成
raw.functionEnd();
}
catch (int e) {
toNextBrace();
getNextSym();
}
if (course) { out << "<无返回值函数定义>" << endl; }
}
/*<主函数>,不会抛出异常*/
void GrammarAnalyzer::mainFunctionDefination() {
hasReturned = false;
try {
//这个地方没有void符号因为已经被programme预读掉了
if (lex.sym().type != MAINTK) {
f.handleFault(lex.lineNumber(), "缺少函数名");
throw 0;
}
string functionName = "main";
getNextSym();
//读取main标识符
raw.functionStart(functionName);
SymbolEntry* entry = table.addSymbol(currentScope, functionName, true);
if (entry == NULL) {
f.handleCourseFault(lex.lineNumber(), REDEFINED);
f.handleFault(lex.lineNumber(), "main函数重定义" + functionName);
}
else {
entry->link->returnType = RETVOID;
entry->type = TYPEFUNCTION;
// 写入符号表完成
}
if (entry != NULL) {
raw.midCodeInsert(MIDFUNC, MIDUNUSED,
entry->id, false,
MIDVOID, false, MIDNOLABEL);
}
if (lex.sym().type != LPARENT) {
f.handleFault(lex.lineNumber(), "缺少参数表");
throw 0;
}
getNextSym();
//读入左括号完成
if (lex.sym().type != RPARENT) {
f.handleCourseFault(lex.lineNumber(), NORPARENT);
f.handleFault(lex.lineNumber(), "缺少)");
}
else {
getNextSym();
}
//读入右括号完成
if (lex.sym().type != LBRACE) {
f.handleFault(lex.lineNumber(), "缺少{");
throw 0;
}
else {
getNextSym();
}
//读入左大括号完成
currentScope = functionName;
compoundSentence();
currentScope = "";
if (lex.sym().type != RBRACE) {
f.handleFault(lex.lineNumber(), "缺少}");
throw 0;
}
getNextSym();
//读入右大括号完成
raw.functionEnd();
}
catch (int e) {
toNextBrace();
getNextSym();
}
if (course) { out << "<主函数>" << endl; }
}
/*<声明头部>,供非变量声明区的有返回值函数调用,会抛出异常*/
void GrammarAnalyzer::declearationHeader(Lexical& retType,string&functionName) {
if (lex.sym().type != INTTK && lex.sym().type != CHARTK) {
f.handleFault(lex.lineNumber(), "缺少类型标识符");
throw 0;
}
retType = lex.sym().type;
getNextSym();
// 读取类型完成
if (lex.sym().type != IDENFR) {
f.handleFault(lex.lineNumber(), "缺少函数名称");
throw 0;
}
functionName = lex.sym().str;
getNextSym();
//读取函数名完成
if (course) { out << "<声明头部>" << endl; }
}
/*变量声明和有返回值函数定义在尚无法判明的情况下进行预读用的函数*/
Lexical GrammarAnalyzer::declearationHeader() {
Lexical type;
if (lex.sym().type != INTTK && lex.sym().type != CHARTK) {
f.handleFault(lex.lineNumber(), "缺少类型标识符");
throw 0;
}
type = lex.sym().type;
//getNextSym();
Result tmp1 = lex.sym();
lex.getNextSym();
//读取类型标识符完成
if (lex.sym().type != IDENFR) {
f.handleFault(lex.lineNumber(), "缺少标识符");
throw 0;
}
string name = lex.sym().str;
Result tmp2 = lex.sym();
lex.getNextSym();
//读取标识符完成
if (lex.sym().type == LPARENT) {
if (globalVariableDeclearation) {
if (course) { out << "<变量说明>" << endl; }
globalVariableDeclearation = false;
//如果这是我们碰见的第一个有返回值函数
//并且前面出现了全局变量声明,那就要输出变量声明这一问题
}
if (lexicalTest) {
lex.printResult(out, tmp1);
lex.printResult(out, tmp2);//输出憋住的信息
}
if (course) { out << "<声明头部>" << endl; }
nonVoidFunctionDefination(type, name);
}
else if (lex.sym().type == COMMA||lex.sym().type==LBRACK||lex.sym().type==SEMICN) {
globalVariableDeclearation = true;//设置存在变量声明的标记位
if (lexicalTest) {
lex.printResult(out, tmp1);
lex.printResult(out, tmp2);//输出憋住的信息
}
try {
variableDeclearation(type, name);
}
catch (int e) {
toNextSemicon();
getNextSym();
}
}
else {
f.handleFault(lex.lineNumber(), "非法的标识符声明");
throw 0;
}
return lex.sym().type;
}
/*<参数表>*/
void GrammarAnalyzer::parameterList(SymbolEntry* entry) {
bool init = true, error = false;
int paraNum = 0;
if (entry == NULL) { error = true; }
while (1) {
if (init) {
if (lex.sym().type != INTTK && lex.sym().type != CHARTK) {
break;
}
//要是在开头而且还没有类型标识符说明是空列表
init = false;
}
else {
if (lex.sym().type != COMMA) {
break;
}
else {
getNextSym();
}
//读取逗号完成
}
if (lex.sym().type != INTTK && lex.sym().type != CHARTK) {
f.handleFault(lex.lineNumber(), "需要提供参数类型");
throw 0;
}
SymbolType paratype = lex.sym().type == INTTK ? TYPEINT : TYPECHAR;
getNextSym();
//读取参数类型
if (lex.sym().type != IDENFR) {
f.handleFault(lex.lineNumber(), "需要提供参数名称");
throw 0;
}
string paraname = lex.sym().str;
SymbolEntry* paraentry=NULL;
if (!error) {
entry->link->paras.push_back(paratype);//存储参数类型
string scope = entry->name;//获取函数作用域
paraentry = table.addSymbol(scope, paraname, false);
if (paraentry == NULL) {
f.handleCourseFault(lex.lineNumber(), REDEFINED);
f.handleFault(lex.lineNumber(), "参数名重定义" + paraname);
}
else {
paraentry->type = paratype;
paraentry->isParameter = true;
entry->link->paraIds.push_back(paraentry->id);//存储参数是谁
//写入符号表
}
}
getNextSym();
//读取参数名称完成
paraNum++;
if (paraentry != NULL) {
raw.midCodeInsert(MIDPARA, MIDUNUSED,
paraentry->id, false,
paratype == TYPEINT ? MIDINT : MIDCHAR, false, MIDNOLABEL);
}
}
if (!error) { entry->link->paraNum = paraNum; }
if (course) { out << "<参数表>" << endl; }
}
/*<复合语句>*/
void GrammarAnalyzer::compoundSentence(){
inlineable = inlineSwitch;
if (currentScope == "main") {
inlineable = false;
}
constDeclearation();
variableDeclearation();
sentenceSeries();
SymbolEntry* entry = table.getSymbolByName("", currentScope);
if (inlineable == true) {
entry->link->inlineable = true;
}
if (course) { out << "<复合语句>" << endl; }
}
/*<因子>*/
ReturnBundle GrammarAnalyzer::factor() {
bool error = false;
ReturnBundle res;
if (lex.sym().type == IDENFR) {//标识符,标识符数组,有返回值函数调用语句
string varname = lex.sym().str;
getNextSym();
//标识符读取完成
SymbolEntry* entry = table.getSymbolByName(currentScope, varname);
if (entry == NULL) {
f.handleCourseFault(lex.lineNumber(), UNDEFINED);
f.handleFault(lex.lineNumber(), "未定义的符号" + varname);
error = true;
}
//查询符号表,检索是否存在这个符号
if (lex.sym().type == LBRACK) {
//数组
inlineable = false;
if (!error && entry->type != TYPECHARARRAY && entry->type != TYPEINTARRAY) {
f.handleFault(lex.lineNumber(), varname + "变量不是数组类型");
error = true;
}
//检索符号表,检查该符号是否为数组类型
getNextSym();
//完成读取中括号
ReturnBundle index;
try {
index=expression();
if (index.isChar) {
throw 0;
}
}
catch (int e) {
f.handleCourseFault(lex.lineNumber(), ILLEGALINDEX);
f.handleFault(lex.lineNumber(), "数组元素的下标只能是整型表达式");
}
//下标表达式
if (lex.sym().type != RBRACK) {
f.handleCourseFault(lex.lineNumber(), NORBRACK);
f.handleFault(lex.lineNumber(), "缺少]");
error = true;
}
else {
getNextSym();
}
//完成读取右括号
if (!error&&entry->type == TYPECHARARRAY) {
res.isChar = true;
}
if (!error) {
res.id = MidCode::tmpVarAlloc();
res.isImmediate = false;
}
if (!error) {
raw.midCodeInsert(MIDARRAYGET, res.id,
entry->id, false, index.id, index.isImmediate, MIDNOLABEL);
//生成中间代码
}
}
else if (lex.sym().type == LPARENT) {
//有返回值函数
res=functionCall(varname,true);
}
else {//标识符
if (!error && !(entry->type == TYPECHAR || entry->type == TYPEINT ||
entry->type == TYPECHARCONST || entry->type == TYPEINTCONST)) {
f.handleCourseFault(lex.lineNumber(), PARATYPEERROR);
f.handleFault(lex.lineNumber(), "参数中非法的类型");
error = true;
}
if (!error&&(entry->type == TYPECHAR || entry->type == TYPECHARCONST)) {
res.isChar = true;
}
if (!error) {
if (constantSubstitutionSwitch&&(entry->type == TYPECHARCONST || entry->type == TYPEINTCONST)) {
res.id = entry->initValue;
res.isImmediate = true;
}
else {
res.id = entry->id;
res.isImmediate = false;
}
}
//生成中间代码
}
}
else if (lex.sym().type == LPARENT) {//(<表达式>)
getNextSym();
//完成读取左括号
ReturnBundle tmp=expression();
//读取表达式
if (lex.sym().type != RPARENT) {
f.handleCourseFault(lex.lineNumber(), NORPARENT);
f.handleFault(lex.lineNumber(),"缺少)");
}
else {
getNextSym();
}
res.id = tmp.id;
res.isImmediate = tmp.isImmediate;
}
else if (lex.sym().type == INTCON|| lex.sym().type == PLUS|| lex.sym().type == MINU) {
//整数(由+-或整数开头)
int value = integer();
res.id = value;
res.isImmediate = true;
//读取整数
}
else if (lex.sym().type == CHARCON) {
char c = (char)(lex.sym().value);
getNextSym();
res.isChar = true;
res.id = c;
res.isImmediate = true;
//完成读取字符
}
else {
f.handleFault(lex.lineNumber(), "不被识别的表达式");
throw 0;
}
if (course) { out << "<因子>" << endl; }
return res;
}
/*<项>*/
ReturnBundle GrammarAnalyzer::term() {
ReturnBundle res;
ReturnBundle res1=factor();
res.isChar = res1.isChar;
res.id = res1.id;
res.isImmediate = res1.isImmediate;
while (1) {
if (lex.sym().type != MULT && lex.sym().type != DIV) {
break;
}
res.isChar = false;
MidCodeOp op = lex.sym().type==MULT?MIDMULT:MIDDIV;
getNextSym();
//读取完运算符号
ReturnBundle res2=factor();
//读取因子
int tmpVar = MidCode::tmpVarAlloc();
raw.midCodeInsert(op, tmpVar, res.id, res.isImmediate,
res2.id, res2.isImmediate, MIDNOLABEL);
res.id = tmpVar;
res.isImmediate = false;
}
if (course) { out << "<项>" << endl; }
return res;
}
/*<表达式>*/
ReturnBundle GrammarAnalyzer::expression() {
ReturnBundle res;
int sign = 1;
bool calculated = false;//标记是不是只有一项,供类型判断使用
if (lex.sym().type == PLUS || lex.sym().type == MINU) {
if (lex.sym().type == MINU) {
sign = -1;
}
getNextSym();
calculated = true;
}
//读取开头的加减号完成
ReturnBundle res1= term();
res.isChar = res1.isChar;
res.id = res1.id;
res.isImmediate = res1.isImmediate;
if (sign == -1) {
if (res.isImmediate) {
res.id = -res.id;
}
else {
int tmpVar = MidCode::tmpVarAlloc();
raw.midCodeInsert(MIDNEGATE, tmpVar,
res.id, res.isImmediate, MIDUNUSED, false, MIDNOLABEL);
res.id = tmpVar;
res.isImmediate = false;
}
}
//读取第一项
while (1) {
if (lex.sym().type != PLUS && lex.sym().type != MINU) {
break;
}
MidCodeOp op = lex.sym().type==PLUS?MIDADD:MIDSUB;
getNextSym();
//读取运算符号完成
ReturnBundle res2=term();
calculated = true;
int tmpVar = MidCode::tmpVarAlloc();
raw.midCodeInsert(op, tmpVar,
res.id, res.isImmediate, res2.id, res2.isImmediate, MIDNOLABEL);
res.id = tmpVar;
res.isImmediate = false;
}
res.isChar = res.isChar&&!calculated;
if (course) { out << "<表达式>" << endl; }
return res;
}
/*针对赋值语句和函数调用语句的预读分支*/
void GrammarAnalyzer::assignAndCall() {
if (lex.sym().type != IDENFR) {
f.handleFault(lex.lineNumber(), "需要标识符");
throw 0;
}
string name = lex.sym().str;
getNextSym();
//读取标识符完成
if (lex.sym().type == LBRACK || lex.sym().type == ASSIGN) {
//接下来是=或者[的是赋值语句
assignSentence(name);
}
else if (lex.sym().type == LPARENT) {//是函数调用语句
functionCall(name,false);
}
else {
f.handleFault(lex.lineNumber(), "assignAndCall无意义的符号");
throw 0;
}
}
/*<赋值语句>,已经过预读*/
void GrammarAnalyzer::assignSentence(string varname) {