-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyntaxAnalysis.cpp
1542 lines (1476 loc) · 51.8 KB
/
syntaxAnalysis.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 "compiler.h"
functionTable functions;//函数符号表
identityTable globalTable;/* NOLINT */
int num;
int label_num;
//char syntaxCategoryString[37][40]
// = {
// "<字符串>", "<程序>", "<常量说明>", "<常量定义>", "<无符号整数>", "<整数>", "<声明头部>", "<常量>",
// "<变量说明>", "<变量定义>", "<变量定义无初始化>", "<变量定义及初始化>", "<有返回值函数定义>", "<无返回值函数定义>",
// "<复合语句>", "<参数表>", "<主函数>", "<表达式>", "<项>", "<因子>", "<语句>", "<赋值语句>", "<条件语句>",
// "<条件>", "<循环语句>", "<步长>", "<情况语句>", "<情况表>", "<情况子语句>", "<缺省>", "<有返回值函数调用语句>",
// "<无返回值函数调用语句>", "<值参数表>", "<语句列>", "<读语句>", "<写语句>", "<返回语句>"};
int synAnalysis() {
getChar();
getToken();
program();
return 0;
}
int String(int optional) {//字符串
if (optional)
if (Token != STRCON)
return 0;
if (Token == STRCON) {
//outputAns.emplace_back(syntaxCategoryString[0]);
return 1;
}
return 0;
}
int program() {//程序
function f0;
if (constDescription(1, f0))
getToken();
if (variableDescription(1, f0))
getToken();
while (functionDefineWithReturn(1) || functionDefineWithoutReturn(1))
getToken();
mainFunction();
//outputAns.emplace_back(syntaxCategoryString[1]);
return 1;
}
int constDescription(int optional, function &fn) {//常量说明
if (optional)
if (Token != CONSTTK)
return 0;
//Token == CONSTTK
getToken();
constDefine(fn);
getToken();
if (Token != SEMICN) {
error_syntax(to_string(formerAns.at(formerAns.size() - 1 - tokenPointer).second) + " k");
retractTokens(1);
}
getToken();
while (Token == CONSTTK) {
getToken();
constDefine(fn);
getToken();
if (Token != SEMICN) {
error_syntax(to_string(formerAns.at(formerAns.size() - 1 - tokenPointer).second) + " k");
retractTokens(1);
}
getToken();
}
retractTokens(1);
//outputAns.emplace_back(syntaxCategoryString[2]);
return 1;
}
int constDefineInt(function &fn) {
getToken();//IDENFR
int l = formerAns.at(formerAns.size() - 1 - tokenPointer).second;
string id = formerAns.at(formerAns.size() - 1 - tokenPointer).first;
getToken();//ASSIGN
getToken();
integer(0, 1);
Identifier con(id, INTTK);
con.setValue(num);
con.setDimension(1, 1);
con.setDimension(2, 1);
con.setIdentifierCategory(CONST);
if (fn.getName().empty())
globalTable.addToken(con, l);
else
fn.addToken(con, l);
getToken();
return 1;
}
int constDefineChar(function &fn) {
getToken();//IDENFR
int l = formerAns.at(formerAns.size() - 1 - tokenPointer).second;
string id = formerAns.at(formerAns.size() - 1 - tokenPointer).first;
getToken();//ASSIGN
getToken();//CHARCON
Identifier con(id, CHARTK);
con.setValue(formerAns.at(formerAns.size() - 1 - tokenPointer).first[0]);
con.setDimension(1, 1);
con.setDimension(2, 1);
con.setIdentifierCategory(CONST);
if (fn.getName().empty())
globalTable.addToken(con, l);
else
fn.addToken(con, l);
getToken();
return 1;
}
int constDefine(function &fn) {//常量定义
if (Token == INTTK) {
constDefineInt(fn);
while (Token == COMMA) {
constDefineInt(fn);
}
} else if (Token == CHARTK) {
constDefineChar(fn);
while (Token == COMMA) {
constDefineChar(fn);
}
} else {}//impossible to reach
retractTokens(1);
//outputAns.emplace_back(syntaxCategoryString[3]);
return 1;
}
int unsignedInteger(bool output, int optional, int context) {//无符号整数
if (optional)
if (Token != INTCON)
return 0;
if (!output)
if (Token == INTCON)
return 1;
if (Token == INTCON) {
//outputAns.emplace_back(syntaxCategoryString[4]);
return 1;
}
if (context == 1)
error_syntax(to_string(formerAns.at(formerAns.size() - 1 - tokenPointer).second) + " o");
else
error_syntax(to_string(formerAns.at(formerAns.size() - 1 - tokenPointer).second) + " i");
return 0;
}
int integer(int optional, int context) {//整数
if (optional)
if (!(Token == PLUS || Token == MINU || unsignedInteger(false, 1, context)))
return 0;
int sym = 1;
if (Token == PLUS || Token == MINU) {
sym = (Token == PLUS) ? 1 : -1;
getToken();
}
unsignedInteger(true, 0, context);
//outputAns.emplace_back(syntaxCategoryString[5]);
num = stoi(formerAns.at(formerAns.size() - 1 - tokenPointer).first) * sym;
return 1;
}
int statementHead(function &fn) {//声明头部
//Token == INTTK || Token == CHARTK
enum tokenCategory type = Token;
getToken();
string fName = formerAns.at(formerAns.size() - 1 - tokenPointer).first;
fn.iniTokenType(fName, type);
//outputAns.emplace_back(syntaxCategoryString[6]);
return 1;
}
int Const() {//常量
if (integer(1, 1)) {
//outputAns.emplace_back(syntaxCategoryString[7]);
return 1;//INTTK
} else if (isCharacter()) {
//outputAns.emplace_back(syntaxCategoryString[7]);
return 2;//CHARTK
}
return 0;
}
int variableDescription(int optional, function &fn) {//变量说明
if (optional)
if (!isTypeIdentifier())
return 0;
int judge = judgeSection();
if (!(judge == VARIABLE_DESCRIPTION_WITHOUT_INITIAL ||
judge == VARIABLE_DESCRIPTION_WITH_INITIAL))
return 0;
variableDefine(0, fn);
getToken();
while (Token == SEMICN) {
getToken();
if (!variableDefine(1, fn)) {
break;
}
getToken();
}
retractTokens(1);
if (Token != SEMICN)
error_syntax(to_string(formerAns.at(formerAns.size() - 1 - tokenPointer).second) + " k");
//outputAns.emplace_back(syntaxCategoryString[8]);
return 1;
}
int variableDefine(int optional, function &fn) {//变量定义
if (optional) {
int judge = judgeSection();
if (!(judge == VARIABLE_DESCRIPTION_WITHOUT_INITIAL ||
judge == VARIABLE_DESCRIPTION_WITH_INITIAL))
return 0;
}
if (variableDefineWithoutInitial(fn) || variableDefineWithInitial(fn)) {
//outputAns.emplace_back(syntaxCategoryString[9]);
return 1;
}
return 0;
}
int variableDefineWithoutInitialIm(enum tokenCategory ty, function &fn) {
string name = formerAns.at(formerAns.size() - 1 - tokenPointer).first;
int l = formerAns.at(formerAns.size() - 1 - tokenPointer).second;
Identifier var(name, ty);
getToken();
if (Token == LBRACK) {//<标识符>'['<无符号整数>']'
var.setIsArray();
getToken();
unsignedInteger(true, 0, 2);
int d1 = stoi(formerAns.at(formerAns.size() - 1 - tokenPointer).first);//第一维数
var.setDimension(1, d1);
getToken();
if (Token != RBRACK) {
error_syntax(to_string(formerAns.at(formerAns.size() - 1 - tokenPointer).second) + " m");
retractTokens(1);
}
getToken();
if (Token == LBRACK) {
getToken();
unsignedInteger(true, 0, 2);
int d2 = stoi(formerAns.at(formerAns.size() - 1 - tokenPointer).first);//第二维数
var.setDimension(2, d2);
getToken();
if (Token != RBRACK) {
error_syntax(to_string(formerAns.at(formerAns.size() - 1 - tokenPointer).second) + " m");
retractTokens(1);
}
getToken();
} else
var.setDimension(2, 1);
} else {//<标识符>
var.setDimension(1, 1);
var.setDimension(2, 1);
}
var.setIdentifierCategory(VAR);
if (fn.getName().empty()) {
globalTable.addToken(var, l);
if (var.getTokenCategory() == INTTK)
for (int i = 0; i < var.getDimension(1) * var.getDimension(2); i++)
var.setValue(0);
else
for (int i = 0; i < var.getDimension(1) * var.getDimension(2); i++)
var.setValue('\0');
} else
fn.addToken(var, l);
return 1;
}
int variableDefineWithoutInitial(function &fn) {//变量定义无初始化
if (judgeSection() != VARIABLE_DESCRIPTION_WITHOUT_INITIAL)
return 0;
//isTypeIdentifier()
enum tokenCategory ty = Token;
getToken();
while (Token == IDENFR) {
variableDefineWithoutInitialIm(ty, fn);
if (Token == COMMA)
getToken();
}
retractTokens(1);
//outputAns.emplace_back(syntaxCategoryString[10]);
return 1;
}
int variableDefineWithInitialIm2(enum tokenCategory type, int d1, Identifier &var) {
getToken();
int ty = 0;//1: o 2: n
for (int i = 0; i < d1; i++) {
int constRet = Const();
if (!constRet) {
error_syntax(to_string(formerAns.at(formerAns.size() - 1 - tokenPointer).second) + " n");
ty = 2;
break;
}
if (!equalType(type, constRet) && ty == 0) {
error_syntax(to_string(formerAns.at(formerAns.size() - 1 - tokenPointer).second) + " o");
ty = 1;
}
if (type == INTTK)
var.setValue(num);
else
var.setValue(formerAns.at(formerAns.size() - 1 - tokenPointer).first[0]);
getToken();//COMMA
getToken();
}
if (!ty) {
if (Const()) {
error_syntax(to_string(formerAns.at(formerAns.size() - 1 - tokenPointer).second) + " n");
getToken();
while (Token == COMMA) {
getToken();
getToken();//COMMA OR RBRACE
}
} else
retractTokens(1);
} else {
retractTokens(1);
}
return 1;
}
int variableDefineWithInitialIm(enum tokenCategory type, function &fn) {
string name = formerAns.at(formerAns.size() - 1 - tokenPointer).first;
int l = formerAns.at(formerAns.size() - 1 - tokenPointer).second;
Identifier var(name, type);
getToken();
if (Token == LBRACK) {//若为数组
var.setIsArray();
getToken();
unsignedInteger(true, 0, 1);
int d1 = stoi(formerAns.at(formerAns.size() - 1 - tokenPointer).first);//第一维数
var.setDimension(1, d1);
getToken();
if (Token != RBRACK) {
error_syntax(to_string(formerAns.at(formerAns.size() - 1 - tokenPointer).second) + " m");
retractTokens(1);
}
getToken();
if (Token == LBRACK) {//若为二维数组
getToken();
unsignedInteger(true, 0, 1);
int d2 = stoi(formerAns.at(formerAns.size() - 1 - tokenPointer).first);//第二维数
var.setDimension(2, d2);
getToken();
if (Token != RBRACK) {
error_syntax(to_string(formerAns.at(formerAns.size() - 1 - tokenPointer).second) + " m");
retractTokens(1);
}
getToken();//ASSIGN
int counter1 = 0;
getToken();//LBRACE
getToken();
while (Token == LBRACE) {
counter1++;
variableDefineWithInitialIm2(type, d2, var);
if (counter1 == d1) {
break;
}
getToken();//COMMA
getToken();
}
if (counter1 != d1) {
retractTokens(1);
error_syntax(to_string(formerAns.at(formerAns.size() - 1 - tokenPointer).second) + " n");
}
getToken();
if (Token != RBRACE) {
getToken();
while (Token == LBRACE) {
variableDefineWithInitialIm2(type, d2, var);
getToken();//COMMA
getToken();
}
retractTokens(1);
error_syntax(to_string(formerAns.at(formerAns.size() - 1 - tokenPointer).second) + " n");
}
} else if (Token == ASSIGN) {//若为一维数组
var.setDimension(2, 1);
getToken();//LBRACE
variableDefineWithInitialIm2(type, d1, var);
}
} else if (Token == ASSIGN) {//若为简单变量
getToken();
var.setDimension(1, 1);
var.setDimension(2, 1);
int constRet = Const();
if (constRet) {
if (!equalType(type, constRet)) {
error_syntax(to_string(formerAns.at(formerAns.size() - 1 - tokenPointer).second) + " o");
}
}
if (type == INTTK)
var.setValue(num);
else
var.setValue(formerAns.at(formerAns.size() - 1 - tokenPointer).first[0]);
} else {}//impossible to reach
var.setIdentifierCategory(VAR);
if (fn.getName().empty()) {
globalTable.addToken(var, l);
} else
fn.addToken(var, l);
getToken();
return 1;
}
int variableDefineWithInitial(function &fn) {//变量定义及初始化
if (judgeSection() != VARIABLE_DESCRIPTION_WITH_INITIAL)
return 0;
//isTypeIdentifier()
enum tokenCategory type = Token;//标识符类型(int,char)
getToken();
while (Token == IDENFR) {
variableDefineWithInitialIm(type, fn);
}
retractTokens(1);
//outputAns.emplace_back(syntaxCategoryString[11]);
return 1;
}
int functionDefineWithReturn(int optional) {//有返回值函数定义
if (optional)
if (!isTypeIdentifier())
return 0;
if (judgeSection() != FUNCTION_DEFINE_WITH_RETURN)
return 0;
function fn;
getToken();
int l = formerAns.at(formerAns.size() - 1 - tokenPointer).second;
retractTokens(1);
statementHead(fn);
addSpace();
addFunction(fn.getName());
getToken();//LPARENT
getToken();
parametersTable(fn);
getToken();
if (Token != RPARENT) {
error_syntax(to_string(formerAns.at(formerAns.size() - 1 - tokenPointer).second) + " l");
retractTokens(1);
}
getToken();//LBRACE
getToken();
compoundStatement(fn);
getToken();//RBRACE
if (!fn.returnNum)
error_syntax(to_string(formerAns.at(formerAns.size() - 1 - tokenPointer).second) + " h");
functions.addToken(fn, l);
//outputAns.emplace_back(syntaxCategoryString[12]);
return 1;
}
int functionDefineWithoutReturn(int optional) {//无返回值函数定义
if (optional)
if (Token != VOIDTK)
return 0;
if (judgeSection() != FUNCTION_DEFINE_WITHOUT_RETURN)
return 0;
//Token==VOIDTK
getToken();// IDENFR
int l = formerAns.at(formerAns.size() - 1 - tokenPointer).second;
string fName = formerAns.at(formerAns.size() - 1 - tokenPointer).first;
function fn(fName, VOIDTK);
addSpace();
addFunction(fName);
getToken();//LPARENT
getToken();
parametersTable(fn);
getToken();
if (Token != RPARENT) {
error_syntax(to_string(formerAns.at(formerAns.size() - 1 - tokenPointer).second) + " l");
retractTokens(1);
}
getToken();//LBRACE
getToken();
compoundStatement(fn);
getToken();//RBRACE
functions.addToken(fn, l);
if (!fn.returnNum)
addQuaternion(vector<string>{"return"});
//outputAns.emplace_back(syntaxCategoryString[13]);
return 1;
}
int compoundStatement(function &fn) {//复合语句
if (constDescription(1, fn))
getToken();
if (variableDescription(1, fn))
getToken();
int retNum = statementList(fn);
//outputAns.emplace_back(syntaxCategoryString[14]);
return retNum;
}
int parametersTable(function &fn) {//参数表
while (isTypeIdentifier()) {
enum tokenCategory ty = Token;
getToken();//IDENFR
int l = formerAns.at(formerAns.size() - 1 - tokenPointer).second;
Identifier arg(formerAns.at(formerAns.size() - 1 - tokenPointer).first, ty);
arg.setDimension(1, 1);
arg.setDimension(2, 1);
arg.setIdentifierCategory(ARG);
fn.setArgument(arg);
fn.addToken(arg, l);
getToken();
addQuaternion(vector<string>{"@para", reserves[arg.getTokenCategory() - INTTK + 1], arg.getName()});
if (Token != COMMA) {
break;
}
getToken();
}
retractTokens(1);
//outputAns.emplace_back(syntaxCategoryString[15]);
return 1;
}
int mainFunction() {//主函数
//Token == VOIDTK
getToken();//MAINTK
function mainFunc(formerAns.at(formerAns.size() - 1 - tokenPointer).first, VOIDTK);
int l = formerAns.at(formerAns.size() - 1 - tokenPointer).second;
getToken();//LPARENT
getToken();
if (Token != RPARENT) {
error_syntax(to_string(formerAns.at(formerAns.size() - 1 - tokenPointer).second) + " l");
retractTokens(1);
}
addSpace();
addFunction(mainFunc.getName());
getToken();//LBRACE
getToken();
compoundStatement(mainFunc);
getToken();//RBRACE
functions.addToken(mainFunc, l);
//outputAns.emplace_back(syntaxCategoryString[16]);
return 1;
}
int expression(function &fn, string &vn) {//表达式
int ret = 0;
if (isCharExp(fn, vn)) {
return 2;
}
string op0;
if (Token == PLUS || Token == MINU) {
op0 = formerAns.at(formerAns.size() - 1 - tokenPointer).first;
getToken();
}
string vt0;
ret -= term(fn, vt0);
string vt1 = "$t" + to_string(fn.tempNum++) + "_" + fn.getName();
addQuaternion(vector<string>{vt1, "=", op0, vt0});
getToken();
while (Token == PLUS || Token == MINU) {
string op = formerAns.at(formerAns.size() - 1 - tokenPointer).first;
getToken();
string vt2;
ret -= term(fn, vt2);
string vtn = "$t" + to_string(fn.tempNum++) + "_" + fn.getName();
addQuaternion(vector<string>{vtn, "=", vt1, op, vt2});
vt1 = vtn;
getToken();
}
vn = vt1;
retractTokens(1);
//outputAns.emplace_back(syntaxCategoryString[17]);
return ret;
}
int term(function &fn, string &vn) {//项
string vt1;
int ret = factor(fn, vt1);
vn = vt1;
getToken();
while (Token == MULT || Token == DIV) {
string op = formerAns.at(formerAns.size() - 1 - tokenPointer).first;
getToken();
string vt2;
ret += factor(fn, vt2);
string vtn = "$t" + to_string(fn.tempNum++) + "_" + fn.getName();
addQuaternion(vector<string>{vtn, "=", vt1, op, vt2});
vt1 = vtn;
vn = vtn;
getToken();
}
retractTokens(1);
//outputAns.emplace_back(syntaxCategoryString[18]);
return ret;
}
int factor(function &fn, string &vn) {//因子
int ret = 0;
if (Token == IDENFR) {//<标识符>
string idName = formerAns.at(formerAns.size() - 1 - tokenPointer).first;
if (!fn.contains(idName) && !globalTable.contains(idName) && !functions.contains(idName) &&
fn.getName() != idName) {
error_syntax(to_string(formerAns.at(formerAns.size() - 1 - tokenPointer).second) + " c");
ret = 'c' - 'a' + 1;
}
getToken();
if (Token == LBRACK) {//<标识符>'['<表达式>']'
getToken();
string vt1;
if (expression(fn, vt1) == 2) {
error_syntax(to_string(formerAns.at(formerAns.size() - 1 - tokenPointer).second) + " i");
ret = 'i' - 'a' + 1;
}
getToken();
if (Token != RBRACK) {
error_syntax(to_string(formerAns.at(formerAns.size() - 1 - tokenPointer).second) + " m");
retractTokens(1);
ret = 'm' - 'a' + 1;
}
getToken();
if (Token == LBRACK) {//<标识符>'['<表达式>']''['<表达式>']'
getToken();
string vt2;
if (expression(fn, vt2) == 2) {
error_syntax(to_string(formerAns.at(formerAns.size() - 1 - tokenPointer).second) + " i");
ret = 'i' - 'a' + 1;
}
getToken();
if (Token != RBRACK) {
error_syntax(to_string(formerAns.at(formerAns.size() - 1 - tokenPointer).second) + " m");
retractTokens(1);
ret = 'm' - 'a' + 1;
}
vn = idName + "[" + vt1 + "]" + "[" + vt2 + "]";
} else {
retractTokens(1);
vn = idName + "[" + vt1 + "]";
}
} else if (Token == LPARENT) {//<有返回值函数调用语句>
retractTokens(1);
string vtn = "$t" + to_string(fn.tempNum++) + "_" + fn.getName();
string fName = formerAns.at(formerAns.size() - 1 - tokenPointer).first;
vn = vtn;
functionCallStatementWithReturn(fn);
addQuaternion(vector<string>{vtn, "=", "@RET_" + fName});
} else {
retractTokens(1);
vn = idName;
}
} else if (Token == LPARENT) {//'('<表达式>')'
getToken();
expression(fn, vn);
if (vn.at(0) == '\'') {
vn = to_string(vn.at(1) - 'a' + 97);
}
getToken();
if (Token != RPARENT) {
error_syntax(to_string(formerAns.at(formerAns.size() - 1 - tokenPointer).second) + " l");
retractTokens(1);
ret = 'l' - 'a' + 1;
}
} else if (isCharacter()) {//<字符>
vn = "\'" + formerAns.at(formerAns.size() - 1 - tokenPointer).first + "\'";
} else if (integer(1, 2)) {//<整数>
vn = to_string(num);
}
//outputAns.emplace_back(syntaxCategoryString[19]);
return ret;
}
int statement(int optional, function &fn) {//语句
if (optional)
if (!isStatement())
return 0;
if (statementsWithSemi(fn)) {
getToken();
if (Token != SEMICN) {
error_syntax(to_string(formerAns.at(formerAns.size() - 2 - tokenPointer).second) + " k");
retractTokens(1);
}
} else if (Token == LBRACE) {
getToken();
statementList(fn);
getToken();//RBRACE
} else if (Token == SEMICN);
else {
if (statementsWithoutSemi(fn));
else {
error_syntax("statement 4");
return 0;
}
}
//outputAns.emplace_back(syntaxCategoryString[20]);
return 1;
}
int assignmentStatement(int optional, function &fn) {//赋值语句
if (optional)
if (Token != IDENFR)
return 0;
string idName = formerAns.at(formerAns.size() - 1 - tokenPointer).first;
int idLine = formerAns.at(formerAns.size() - 1 - tokenPointer).second;
int fnContains = fn.contains(idName);
int gtContains = globalTable.contains(idName);
if (!fnContains && !gtContains) {//IDENFR
error_syntax(to_string(idLine) + " c");
}
if ((fnContains && fn.getToken(idName).getIdCategory() == CONST) ||
(!fnContains && gtContains && globalTable.getToken(idName).getIdCategory() == CONST)) {
error_syntax(to_string(idLine) + " j");
}
getToken();
if (Token == ASSIGN) {//=<表达式>
getToken();
string vt1;
expression(fn, vt1);
addQuaternion(vector<string>{idName, "=", vt1});
} else if (Token == LBRACK) {
getToken();
string vt1;
expression(fn, vt1);
getToken();
if (Token != RBRACK) {
error_syntax(to_string(formerAns.at(formerAns.size() - 1 - tokenPointer).second) + " m");
retractTokens(1);
}
getToken();
if (Token == ASSIGN) {//'['<表达式>']'=<表达式>
getToken();
string vt2;
expression(fn, vt2);
addQuaternion(vector<string>{idName + "[" + vt1 + "]", "=", vt2});
} else if (Token == LBRACK) {//'['<表达式>']''['<表达式>']' =<表达式>
getToken();
string vt2;
if (expression(fn, vt2) == 2) {
error_syntax(to_string(formerAns.at(formerAns.size() - 1 - tokenPointer).second) + " i");
}
getToken();
if (Token != RBRACK) {
error_syntax(to_string(formerAns.at(formerAns.size() - 1 - tokenPointer).second) + " m");
retractTokens(1);
}
getToken();//ASSIGN
getToken();
string vt3;
expression(fn, vt3);
addQuaternion(vector<string>{idName + "[" + vt1 + "]" + "[" + vt2 + "]", "=", vt3});
}
} else {}//impossible to reach
//outputAns.emplace_back(syntaxCategoryString[21]);
return 1;
}
int conditionStatement(int optional, function &fn) {//条件语句
if (optional)
if (Token != IFTK)
return 0;
//Token == IFTK
bool hasElse = false;
getToken();//LPARENT
getToken();
int condition_num = ++label_num;
string label_condition_else = "@Label_condition_else" + to_string(condition_num);
string label_condition_end = "@Label_condition_end" + to_string(condition_num);
vector<vector<string>> intermediateInner;
condition(fn, label_condition_else, intermediateInner);
getToken();
if (Token != RPARENT) {
error_syntax(to_string(formerAns.at(formerAns.size() - 1 - tokenPointer).second) + " l");
retractTokens(1);
}
getToken();
statement(0, fn);
getToken();
addQuaternion(vector<string>{"@j", label_condition_end});
if (Token == ELSETK) {
getToken();
hasElse = true;
addQuaternion(vector<string>{label_condition_else + ":"});
statement(0, fn);
} else retractTokens(1);
if (!hasElse)
addQuaternion(vector<string>{label_condition_else + ":"});
addQuaternion(vector<string>{label_condition_end + ":"});
addSpace();
//outputAns.emplace_back(syntaxCategoryString[22]);
return 1;
}
vector<string>
condition(function &fn, string &label, vector<vector<string>> &intermediateInner) {//条件
int pos1 = intermediateCodeInner.size();
string vt1;
if (expressTypeEqual(expression(fn, vt1), CHARTK)) {
error_syntax(to_string(formerAns.at(formerAns.size() - 1 - tokenPointer).second) + " f");
}
getToken();//isRelation();
enum tokenCategory relation = Token;
getToken();
string vt2;
if (expressTypeEqual(expression(fn, vt2), CHARTK)) {
error_syntax(to_string(formerAns.at(formerAns.size() - 1 - tokenPointer).second) + " f");
}
for (int i = pos1; i < intermediateCodeInner.size(); i++) {
intermediateInner.push_back(intermediateCodeInner.at(i));
}
//outputAns.emplace_back(syntaxCategoryString[23]);
return addCondition(vt1, vt2, label, relation);
}
int loopStatement(int optional, function &fn) {//循环语句
if (optional)
if (Token != WHILETK && Token != FORTK)
return 0;
if (Token == WHILETK) {
getToken();//LPARENT
getToken();
int while_num = ++label_num;
string label_while_start = "@Label_while_start" + to_string(while_num);
string label_while_end = "@Label_while_end" + to_string(while_num);
string label_while_entry = "@Label_while_entry" + to_string(while_num);
addQuaternion(vector<string>{label_while_start + ":"});
vector<vector<string>> intermediateInner;
vector<string> quaternion =
condition(fn, label_while_end, intermediateInner);
getToken();
if (Token != RPARENT) {
error_syntax(to_string(formerAns.at(formerAns.size() - 1 - tokenPointer).second) + " l");
retractTokens(1);
}
getToken();
addQuaternion(vector<string>{label_while_entry + ":"});
statement(0, fn);
quaternion.emplace_back(label_while_entry);
addQuaternions(intermediateInner);
vector<string> revQuaternion = reverseRelation(quaternion);
addQuaternion(revQuaternion);
addQuaternion(vector<string>{label_while_end + ":"});
addSpace();
} else if (Token == FORTK) {
int for_num = ++label_num;
string label_for_start = "@Label_for_start" + to_string(for_num);
string label_for_entry = "@Label_for_entry" + to_string(for_num);
string label_for_end = "@Label_for_end" + to_string(for_num);
addQuaternion(vector<string>{label_for_start + ":"});
getToken();//LPARENT
getToken();
string idName = formerAns.at(formerAns.size() - 1 - tokenPointer).first;
if (!fn.contains(idName) && !globalTable.contains(idName)) {//IDENFR
error_syntax(to_string(formerAns.at(formerAns.size() - 1 - tokenPointer).second) + " c");
}
getToken();//ASSIGN
getToken();
string vt1;
expression(fn, vt1);
addQuaternion(vector<string>{idName, "=", vt1});
getToken();
if (Token != SEMICN) {
error_syntax(to_string(formerAns.at(formerAns.size() - 2 - tokenPointer).second) + " k");//应为分号
retractTokens(1);
}
getToken();
vector<vector<string>> intermediateInner;
vector<string> quaternion =
condition(fn, label_for_end, intermediateInner);
getToken();
if (Token != SEMICN) {
error_syntax(to_string(formerAns.at(formerAns.size() - 2 - tokenPointer).second) + " k");//应为分号
retractTokens(1);
}
getToken();
idName = formerAns.at(formerAns.size() - 1 - tokenPointer).first;
if (!fn.contains(idName) && !globalTable.contains(idName)) {//IDENFR
error_syntax(to_string(formerAns.at(formerAns.size() - 1 - tokenPointer).second) + " c");
}
getToken();//ASSIGN
getToken();
idName = formerAns.at(formerAns.size() - 1 - tokenPointer).first;
if (!fn.contains(idName) && !globalTable.contains(idName)) {//IDENFR
error_syntax(to_string(formerAns.at(formerAns.size() - 1 - tokenPointer).second) + " c");
}
getToken();//Token == PLUS || Token == MINU
string op;
if (Token == PLUS)
op = "+";
else
op = "-";
getToken();
string step = stride();
getToken();
if (Token != RPARENT) {
error_syntax(to_string(formerAns.at(formerAns.size() - 1 - tokenPointer).second) + " l");
retractTokens(1);
}
getToken();
addQuaternion(vector<string>{label_for_entry + ":"});
statement(0, fn);
addQuaternion(vector<string>{idName, "=", idName, op, step});
quaternion.emplace_back(label_for_entry);
addQuaternions(intermediateInner);
vector<string> revQuaternion = reverseRelation(quaternion);
addQuaternion(revQuaternion);
addQuaternion(vector<string>{label_for_end + ":"});
addSpace();
} else {}//impossible to reach
//outputAns.emplace_back(syntaxCategoryString[24]);
return 1;
}
string stride() {//步长
unsignedInteger(true, 0, 1);
//outputAns.emplace_back(syntaxCategoryString[25]);
return formerAns.at(formerAns.size() - 1 - tokenPointer).first;
}
int switchStatement(function &fn) {//情况语句
int switch_num = ++label_num;
string label_switch_start = "@Label_switch_start" + to_string(switch_num);
string label_switch_entry = "@Label_switch_entry" + to_string(switch_num);
string label_switch_end = "@Label_switch_end" + to_string(switch_num);
string label_switch = "@Label_switch" + to_string(switch_num);
addQuaternion(vector<string>{label_switch_start + ":"});
int ret;
//Token == SWITCHTK
getToken();//LPARENT
getToken();
enum tokenCategory type;
string vt1;
ret = expression(fn, vt1);
if (ret == 2)
type = CHARTK;
else
type = INTTK;
getToken();
if (Token != RPARENT) {
error_syntax(to_string(formerAns.at(formerAns.size() - 1 - tokenPointer).second) + " l");
retractTokens(1);
}
getToken();//LBRACE
getToken();
caseTable(fn, type, vt1, label_switch, label_switch_end);
getToken();
defaultStatement(fn);
getToken();//RBRACE
addQuaternion(vector<string>{label_switch_end + ":"});
addSpace();
//outputAns.emplace_back(syntaxCategoryString[26]);
return 1;
}
int caseTable(function &fn, enum tokenCategory type, string &vt1, string &label_switch, string &label_switch_end) {//情况表
string label = label_switch + "_case1_end";
int case_num = 2;
caseStatement(0, fn, type, vt1, label, label_switch_end);
getToken();
label = label_switch + "_case" + to_string(case_num) + "_end";
while (caseStatement(1, fn, type, vt1, label, label_switch_end)) {
label = label_switch + "_case" + to_string(++case_num) + "_end";
getToken();
}
retractTokens(1);
//outputAns.emplace_back(syntaxCategoryString[27]);
return 1;
}
int caseStatement(int optional, function &fn, enum tokenCategory type,
string &vt1, string &label_switch, string &label_switch_end) {//情况子语句
if (optional)
if (Token != CASETK)
return 0;
//Token == CASETK
getToken();
int constRet = Const();
if (constRet) {
if (!equalType(type, constRet)) {
error_syntax(to_string(formerAns.at(formerAns.size() - 1 - tokenPointer).second) + " o");
}
}
string con;
if (constRet == 1) {
con = to_string(num);
} else {
con = "\'" + formerAns.at(formerAns.size() - 1 - tokenPointer).first + "\'";
}
getToken();//COLON
getToken();
addQuaternion(vector<string>{"@bne", vt1, con, label_switch});
statement(0, fn);
addQuaternion(vector<string>{"@j", label_switch_end});
addQuaternion(vector<string>{label_switch + ":"});
//outputAns.emplace_back(syntaxCategoryString[28]);
return 1;
}
int defaultStatement(function &fn) {//缺省
if (Token != DEFAULTTK) {
error_syntax(to_string(formerAns.at(formerAns.size() - 1 - tokenPointer).second) + " p");
retractTokens(1);
return 0;
}
getToken();//COLON
getToken();