-
Notifications
You must be signed in to change notification settings - Fork 1
/
symtable.c
1672 lines (1605 loc) · 46.9 KB
/
symtable.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
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 "node.h"
#include "list.h"
#include "vector.h"
#include "symtable.h"
#include <string.h>
#define HT_SIZE 233
#define HT_SEED 32
#define NEW(p) ((p) = malloc(sizeof *(p)))
#define NEW0(p) memset(NEW(p), 0, sizeof *(p))
List funcTable[HT_SIZE], paraTable[HT_SIZE];
const char *RESERVE_WORDS[] = {(const char *)"int\0", (const char *)"void\0", (const char *)"NULL\0"};
int scope = 0, whileCnt = 0;
vector scopeTable;
int errorcnt = 0;
void handleError(int no, int lineno)
{
errorcnt ++;
printf("Error %d on Line %d: ", no, lineno);
switch (no)
{
case 1: { printf("storage size of this variable isn't known. \n"); break; }
case 2: { printf("division by zero. \n"); break; }
case 3: { printf("function cannot be called in a constant expression. \n"); break;}
case 4: { printf("size of array is negative. \n"); break; }
case 5: { printf("excess elements in scalar initializer. \n"); break; }
case 6: { printf("invalid initializer. \n"); break; }
case 7: { printf("the variable has't been defined. \n"); break; }
case 8: { printf("too many braces around scalar initializer. \n"); break; }
case 9: { printf("const type variable is required here. \n"); break; }
case 10: { printf("excess elements in scalar initializer. \n"); break; }
case 11: { printf("an identifier can not be the same as reserve names. \n"); break; }
case 12: { printf("repeated definition. \n"); break; }
case 13: { printf("the continue/break statement is not in while loop. \n"); break; }
case 14: { printf("NOT '!' operation can only appear in condition expressions. \n"); break; }
case 15: { printf("the actual parameter type is different from the formal parameter type defined by the function. \n"); break; }
case 16: { printf("function parameter type cannot be void. \n"); break; }
case 17: { printf("the return type is different from the return type defined by the function. \n"); break; }
case 18: { printf("when lvalue is passed as a function argument, the reference of the array variable is illegal. \n"); break; }
case 19: { printf("lvalue subscript out of bounds. \n"); break; }
case 20: { printf("lvalue passed as a function argument here should be an array/constarray. \n"); break; }
case 21: { printf("lvalue in assignment statements can only be a non-array variable, or locate an array element. \n"); break; }
case 22: { printf("too many function arguments. \n"); break; }
case 23: { printf("too many function arguments. \n"); break; }
case 24: { printf("the function has't been defined. \n"); break; }
case 25: { printf("missing function arguments. \n"); break; }
default: { printf("undefined error. \n"); break; }
}
}
void printType(List type)
{
if (listsize(type) == 0)
{
printf("List is empty\n");
return;
}
int blanknum = 0, j;
ListItr i = getGListItr(type, 0);
while (hasNext(i))
{
for (j = 0; j < blanknum; j++)
printf(" ");
Type tmp = nextItem(i);
switch (tmp->kind)
{
case constant:
printf("constant\n");
break;
case basic:
printf("basic\n");
break;
case array:
printf("array: %d\n", tmp->size);
break;
case constArray:
printf("constArray: %d\n", tmp->size);
break;
}
blanknum += 2;
}
}
int myHash(char *key) //自定义hash函数
{
int res = 0, p = 1, i;
for (i = 0; i < strlen(key); i++)
{
res = (res + key[i] * p % HT_SIZE) % HT_SIZE;
p = p * HT_SEED % HT_SIZE;
}
return res;
}
int isConst(List type)
{
ListItr i = getGListItr(type, 0);
Type tmp = nextItem(i);
if (tmp->kind == constant)
return 1;
if (tmp->kind == constArray)
return 1;
return 0;
}
Type isNotArray(List type)
{
ListItr i = getGListItr(type, 0);
Type tmp = nextItem(i);
if (tmp->kind == constant)
{
Type res;
NEW0(res);
res->kind = constant;
return res;
}
if (tmp->kind == basic)
{
Type res;
NEW0(res);
res->kind = basic;
return res;
}
return NULL;
}
int cmpType(List type1, List type2) // 判断两个类型是否完全一致,如const int-const int,int[2][3]-int[2][3]
{
int res = 0;
if (listsize(type1) != listsize(type2))
{
return 1;
}
int blanknum = 0, k;
ListItr i = newListItr(type1, 0);
ListItr j = newListItr(type2, 0);
while (hasNext(i))
{
Type tmp1 = nextItem(i);
Type tmp2 = nextItem(j);
if (tmp1->kind != tmp2->kind)
{
res = 1;
break;
}
if (tmp1->kind == array && tmp1->size != tmp2->size)
{
res = 1;
break;
}
if (tmp1->kind == constArray && tmp1->size != tmp2->size)
{
res = 1;
break;
}
}
destroyListItr(&i);
destroyListItr(&j);
return res;
}
int cmpSingleTypeConst(Type t1, Type t2) // 后者可以是const的Type比较,没有比size
{
switch (t1->kind)
{
case basic:
if (t2->kind == basic || t2->kind == constant)
return 0;
break;
case array:
if (t2->kind == array || t2->kind == constArray)
return 0;
break;
default:
if (t2->kind == t1->kind)
return 0;
break;
}
return 1;
}
int cmpTypeConst(List type, List constType) // 后者可以是const的比较,如int=const int,int[2][3]=const int[2][3]
{
int res = 0;
if (listsize(type) != listsize(constType))
{
return 1;
}
ListItr i = newListItr(type, 0);
ListItr j = newListItr(constType, 0);
// ListItr i = getGListItr(type, 0);
// ListItr j = getGListItr(constType, 0);
while (hasNext(i))
{
Type tmp1 = nextItem(i);
Type tmp2 = nextItem(j);
if (cmpSingleTypeConst(tmp1, tmp2) > 0)
{
res = 1;
break;
}
if (tmp1->kind == array && tmp1->size != tmp2->size)
{
res = 1;
break;
}
if (tmp1->kind == constArray && tmp1->size != tmp2->size)
{
res = 1;
break;
}
}
return res;
}
int cmpTypeFunc(List funcType, List type) // 前者为函数形参,后者为函数实参,判断能否传入,如int-int,int[]-int[3]
{
int res = 0;
if (listsize(funcType) != listsize(type))
{
return 1;
}
int blanknum = 0, k;
ListItr i = newListItr(funcType, 0);
ListItr j = newListItr(type, 0);
int flag = 0;
while (hasNext(i))
{
Type tmp1 = nextItem(i);
Type tmp2 = nextItem(j);
if (cmpSingleTypeConst(tmp1, tmp2) > 0)
{
res = 1;
break;
}
if (flag == 0)
flag = 1;
else
{ // 函数传参为数组时 从第二项才开始比较size
if (tmp1->kind == array && tmp1->size != tmp2->size)
{
res = 1;
break;
}
if (tmp1->kind == constArray && tmp1->size != tmp2->size)
{
res = 1;
break;
}
}
}
destroyListItr(&i);
destroyListItr(&j);
return res;
}
List combineType(List defType, List callType)
{
if (defType == NULL)
return NULL;
List resType = newList();
Type def = isNotArray(defType);
if (def != NULL) // 数值 其实用不到这部分
{
if (callType->size == 0)
{
handleError(30, 0);
return defType;
}
int res = cmpSingleTypeConst((Type)getFirst(callType), (Type)getFirst(defType));
if (res)
{
def->kind = empty;
def->size = 0;
} // error
addFirst(resType, def);
}
else // array
{
if (callType->size == 0)
return defType;
// 举个例子 deftype是a[4][2][3] list : 4 -> 2 -> 3
// calltype a[1][2] list : 1 -> 2
// 从第一项开始判断 1合法 2不合法 返回错误类型empty
// calltype a[1][1] list : 1 -> 1
// 判断 1 合法 1 合法 下一项没了 deftype还剩3 所以返回类型是 list : 3 检验到
// 合法的时候再新建一个List返回
if (listsize(defType) < listsize(callType))
{
Type error;
NEW0(error);
error->kind = empty;
error->size = 1; // error
addFirst(resType, error);
return resType;
}
ListItr i = newListItr(defType, 0);
ListItr j = newListItr(callType, 0);
int count = listsize(defType);
while (hasNext(j))
{
count--;
Type tmp1 = nextItem(i); // def
Type tmp2 = nextItem(j); // call
if (tmp1->size <= tmp2->size)
{
Type error;
NEW0(error);
error->kind = empty;
error->size = 2; // error
addFirst(resType, error);
destroyListItr(&i);
destroyListItr(&j);
return resType;
}
}
if (count == 0)
{
Type tmp;
NEW0(tmp);
if (isConst(defType))
tmp->kind = constant;
else
tmp->kind = basic;
addFirst(resType, tmp);
}
else
{
while (hasNext(i))
{
Type tmp = nextItem(i);
Type copyTmp;
NEW0(copyTmp);
copyTmp->size = tmp->size;
copyTmp->kind = tmp->kind;
addLast(resType, copyTmp);
}
}
destroyListItr(&i);
destroyListItr(&j);
}
return resType;
}
void initScopeTable()
{
scope = 0;
scopeTable = newVector();
vector scope0 = newVector();
addItem(scopeTable, scope0);
}
void insertScopeItem(listNode r)
{
vector scope = (vector)(getLastItem(scopeTable));
addItem(scope, r);
}
void addScope()
{
scope++;
vector newScope = newVector();
addItem(scopeTable, newScope);
}
void exitScope()
{
vector lastScope = (vector)(getLastItem(scopeTable));
for (int i = 0; i < lastScope->size; i++)
{
listNode item = (listNode)getItem(lastScope, i);
removeNode(item);
item = NULL;
}
destoryVector(lastScope);
removeLastItem(scopeTable);
scope--;
}
void clearScope0()
{
if (scope != 0)
{
printf("scope is not zero!\n");
return;
}
vector lastScope = (vector)(getLastItem(scopeTable));
for (int i = 0; i < lastScope->size; i++)
{
listNode item = (listNode)getItem(lastScope, i);
removeNode(item);
item = NULL;
}
destoryVector(lastScope);
removeLastItem(scopeTable);
for (int i = 0; i < HT_SIZE; i++)
{
if (paraTable[i] != NULL)
{
free(paraTable[i]->first);
free(paraTable[i]);
paraTable[i] = NULL;
}
}
}
void initSymTable()
{
int i;
for (i = 0; i < HT_SIZE; i++)
{
funcTable[i] = paraTable[i] = NULL;
}
}
// int insertSymTable(treeNode *root)
// {
// int pos = myHash(root->name); //hash表的下标
// int flag = checkConflict(pos, root->name);
// if (flag) //有冲突
// {
// return -1;
// }
// if (hashTable[pos] == NULL)
// {
// hashTable[pos] = newList();
// addFirst(hashTable[pos], );
// }
// }
int checkReserveNames(char *name)
{
int i;
for (i = 0; i < sizeof(RESERVE_WORDS) / sizeof(const char *); i++)
{
if (strcmp(name, RESERVE_WORDS[i]) == 0)
{
return 1;
}
}
return 0;
}
Para querySymTable_para(char *name)
{
int pos = myHash(name);
List list = paraTable[pos];
if (list == NULL || listsize(list) == 0)
return NULL;
ListItr i = getGListItr(list, 0);
while (hasNext(i))
{
Para tmp = (Para)(nextItem(i));
if (strcmp(tmp->name, name) == 0)
{
return tmp;
}
}
return NULL;
}
int para_exist(char *name, int scope)
{
int t = checkReserveNames(name);
if (t != 0)
{
return 1; // handleError(11, 0);
}
Para query = querySymTable_para(name);
if (query != NULL && query->scope == scope)
{
return 2; // handleError(12, 0);
}
return 0;
}
int insertSymTable_para(Para r, int lineno)
{
int t = para_exist(r->name, r->scope);
if (t == 1)
{
handleError(11, lineno);
return 1;
}
if (t == 2)
{
handleError(12, lineno);
return 1;
}
int pos = myHash(r->name);
if (paraTable[pos] == NULL)
{
paraTable[pos] = newList();
}
addFirst(paraTable[pos], r);
listNode n = getFirstNode(paraTable[pos]);
insertScopeItem(n);
return 0;
}
int sdtParseExp(treeNode *r, int isIdentConst, int flag, List list)
{
return sdtParseAddExp(r->child, isIdentConst, flag, list, 0);
}
void sdtParseExp_rep(treeNode *r, List *type) // Exp_rep LB Exp RB | empty
{
if (r->child == NULL) //Exp_rep -> empty
{
return;
}
else if (r->child != NULL) //Exp_rep -> Exp_rep LB Exp RB
{
treeNode *Exp = r->child->next->next;
Type newType;
NEW0(newType);
newType->kind = array;
newType->size = sdtParseExp(Exp, 0, 0, NULL);
printf("%d\n", newType->size);
if (newType->size < 0) //数组的维度必须是正数
{
handleError(4, r->lineNo);
newType->size = 0;
}
addFirst(*type, newType);
sdtParseExp_rep(r->child, type);
}
}
int sdtParseLVal(treeNode *r, int isIdentConst, int flag, List list) // LVal : ID Exp_rep
{
//查询符号表,若isIdentConst = 1,则Ident必须为Constant,否则可以是任意(在符号表中存在即可)
// 若flag=1,说明是形参-数组的情况
int res = 0;
Para p = querySymTable_para(r->child->value);
if (p == NULL)
{
handleError(7, r->lineNo); // not exist
return 0;
}
List ptype = p->type;
if (!isConst(ptype) && isIdentConst == 1) // 必须是const但ID不是const
{
handleError(9, r->lineNo);
}
else
{
if (flag == 0) // 数值
{
if (isNotArray(ptype) != NULL)
return p->const_val[0];
List type = newList();
sdtParseExp_rep(r->child->next, &type);
if (listsize(type) != listsize(ptype)) // LVal如果是取数组必须取到一个元素
{
handleError(21, r->lineNo);
}
ListItr i = newListItr(ptype, 0);
ListItr j = newListItr(type, 0);
int index = 0, factor = 1, flag1 = 0;
while (hasNext(j))
{
Type tmp1 = nextItem(i); // def
Type tmp2 = nextItem(j); // call
if (tmp1->size <= tmp2->size)
{
if (tmp1->size != -1 || flag1 != 0)
{
handleError(19, r->lineNo);
destroyListItr(&i);
destroyListItr(&j);
return 0;
}
else
{
flag1 = 2;
}
}
if (flag1 == 0)
flag1 = 1;
else
factor *= tmp1->size;
index = index + factor * tmp2->size;
}
destroyListItr(&i);
destroyListItr(&j);
if (flag1 != 2)
res = p->const_val[index];
else
res = 0;
}
else // 形参-数组
{
if (isNotArray(ptype) != NULL)
{
handleError(20, r->lineNo); // 应该输入数组,但输入的是数值
}
List type = newList();
sdtParseExp_rep(r->child->next, &type);
List callType = combineType(ptype, type);
ListItr i = getGListItr(callType, 0);
Type tmp = nextItem(i);
if (tmp->kind == empty)
{
handleError(18, r->lineNo); // 调用不合法,例如int a[2]调用的时候使用了a[2]
res = 1;
}
*list = *callType;
}
}
return res;
}
int sdtParseNumber(treeNode *r)
{
return r->child->int_val;
}
int sdtParsePrimaryExp(treeNode *r, int isIdentConst, int flag, List list)
{
if (strcmp(r->child->name, "LP") == 0) //PrimaryExp -> '(' Exp ')'
{
return sdtParseExp(r->child->next, isIdentConst, flag, list);
}
else if (strcmp(r->child->name, "LVal") == 0) //PrimaryExp -> LVal
{
return sdtParseLVal(r->child, isIdentConst, flag, list);
}
else //PrimaryExp -> Number
{
return sdtParseNumber(r->child);
}
}
int sdtParseUnaryExp(treeNode *r, int isIdentConst, int flag, List list, int isCond)
{
if (strcmp(r->child->name, "PrimaryExp") == 0) //UnaryExp -> PrimaryExp
{
return sdtParsePrimaryExp(r->child, isIdentConst, flag, list);
}
else if (strcmp(r->child->name, "UnaryOp") == 0) //UnaryExp -> UnaryOp UnaryExp
{
int a = sdtParseUnaryExp(r->child->next, isIdentConst, 0, NULL, isCond);
if (strcmp(r->child->child->name, "PLUS") == 0)
{
return a;
}
else if (strcmp(r->child->child->name, "MINUS") == 0)
{
return -a;
}
else
{
if (isCond == 0)
handleError(14, r->lineNo);
return (a == 0);
}
}
else //UnaryExp -> Ident '(' [FuncRParams] ')'
{
if (isIdentConst) //常量表达式里不可能出现函数,因为函数的返回值是变量
{
handleError(3, r->lineNo);
}
else
{
//判断Ident是函数,并且返回值是int变量,否则报错
Func f = querySymTable_func(r->child->value);
if (f == NULL)
{
handleError(24, r->lineNo);
return 0;
}
// 解析FuncRParams
if (r->child->next->next->next == NULL)
{
if (f->paraNum != 0)
handleError(25, r->lineNo);
}
else if (f->paraNum == 0)
handleError(22, r->lineNo);
else
{
sdtParseFuncRParams(r->child->next->next, f->paraList);
}
}
}
}
int sdtParseMulExp(treeNode *r, int isIdentConst, int flag, List list, int isCond)
{
if (strcmp(r->child->name, "UnaryExp") == 0) //MulExp -> UnaryExp
{
return sdtParseUnaryExp(r->child, isIdentConst, flag, list, isCond);
}
else //MulExp -> MulExp ('*'|'/'|'%') UnaryExp
{
int a = sdtParseMulExp(r->child, isIdentConst, 0, NULL, isCond);
int b = sdtParseUnaryExp(r->child->next->next, isIdentConst, 0, NULL, isCond); // 应该是这样?
if (strcmp(r->child->next->name, "STAR") == 0) //乘法
{
return a * b;
}
else
{
if (b == 0) //除运算和模运算,被除数不能是0
{
handleError(2, r->lineNo);
}
else if (strcmp(r->child->next->name, "DIV") == 0)
{
return a / b;
}
else
{
return a % b;
}
}
}
}
int sdtParseAddExp(treeNode *r, int isIdentConst, int flag, List list, int isCond)
{
if (strcmp(r->child->name, "MulExp") == 0) //AddExp -> MulExp
{
return sdtParseMulExp(r->child, isIdentConst, flag, list, isCond);
}
else //AddExp -> AddExp ('+'|'-') MulExp
{
int a = sdtParseAddExp(r->child, isIdentConst, 0, NULL, isCond);
int b = sdtParseMulExp(r->child->next->next, isIdentConst, 0, NULL, isCond);
if (strcmp(r->child->child->name, "PLUS") == 0)
{
return a + b;
}
else
{
return a - b;
}
}
}
int sdtParseConstExp(treeNode *r)
{
return sdtParseAddExp(r->child, 1, 0, NULL, 0); //ConstExp -> AddExp
}
void sdtParseComConstDef_rep(treeNode *r) //ComConstDef_rep -> ComConstDef_rep COMMA ConstDef | empty
{
if (r == NULL || r->child == NULL)
return;
sdtParseComConstDef_rep(r->child);
sdtParseConstDef(r->child->next->next);
}
void sdtParseConstDecl(treeNode *r) //ConstDecl : CONST FuncType ConstDef ComConstDef_rep SEMI
{
if (r == NULL)
return;
if (strcmp(r->name, "ConstDecl") == 0)
{
if (strcmp(r->child->next->child->name, "VOID") == 0) //常量类型不能是void
{
handleError(1, r->lineNo);
return;
}
sdtParseConstDef(r->child->next->next);
sdtParseComConstDef_rep(r->child->next->next->next);
}
}
void sdtParseConstExp_rep(treeNode *r, List *type)
{
if (r->child == NULL) //constExp_rep -> empty
{
return;
}
else if (r->child != NULL) //constExp_rep -> constExp_rep LB ConstExp RB
{
treeNode *constExp = r->child->next->next;
Type newType;
NEW0(newType);
newType->kind = constArray;
newType->size = sdtParseConstExp(constExp);
printf("%d\n", newType->size);
if (newType->size <= 0) //数组的维度必须是正数
{
handleError(4, r->lineNo);
newType->size = 1;
}
addFirst(*type, newType);
sdtParseConstExp_rep(r->child, type);
}
}
//返回r之后最近的一个'{', '}', ConstExp或者Exp
treeNode *getNextToken(treeNode *r)
{
if (r == NULL)
return NULL;
//r=r->next;
while (r != NULL)
{
if (strcmp(r->name, "LC") == 0 || strcmp(r->name, "RC") == 0 || strcmp(r->name, "ConstExp") == 0 || strcmp(r->name, "Exp") == 0)
{
return r;
}
else if (strcmp(r->name, "ConstInitVal") == 0 || strcmp(r->name, "ComConstInitVal_rep") == 0 || strcmp(r->name, "InitVal") == 0 || strcmp(r->name, "ComInitVal_rep") == 0)
{
if (r->child != NULL)
return getNextToken(r->child);
}
if (r->next != NULL)
{
r = r->next;
}
else
{
while (r)
{
r = r->father;
if (r->next != NULL)
{
r = r->next;
break;
}
}
}
}
}
int getArraySize(int *a, int index, int size)
{
int arraySize = 1;
for (; index < size; index++)
arraySize *= a[index];
return arraySize;
}
void getArrayDimList(int *a, Para para)
{
ListItr i = getGListItr(para->type, 0);
int size = 0;
while (hasNext(i))
{
a[size++] = ((Type)(nextItem(i)))->size;
}
}
treeNode *sdtParseConstInitVal_Array(treeNode *r, Para *para, int *a, int arrayDim, int index, int paraCount, int *numCount, int numCountLim)
{
/*
ConstInitVal : ConstExp | LC ConstInitVal ComConstInitVal_rep RC | LC RC
ComConstInitVal_rep : ComConstInitVal_rep COMMA ConstInitVal | empty
*/
printf("sdtParseConstInitVal_Array(arrayDim=%d, index=%d, paraCount=%d, numCount=%d, numCountLim=%d\n",
arrayDim, index, paraCount, *numCount, numCountLim);
if (r == NULL)
return NULL;
while (1)
{
if (r->next != NULL)
{
r = r->next;
}
else
{
while (r)
{
r = r->father;
if (r->next != NULL)
{
r = r->next;
break;
}
}
}
if (r == NULL)
return NULL;
r = getNextToken(r);
printf("nextToken: %s\n", r->name);
if (strcmp(r->name, "LC") == 0)
{
if (index + 1 >= arrayDim) //大括号嵌套层数超过数组维数
{
handleError(8, r->lineNo);
}
if ((*numCount) % getArraySize(a, index + 1, arrayDim) != 0) //如果当前维度没有填满,需要隐式初始化
{
int dim = getArraySize(a, index + 1, arrayDim);
(*numCount) += dim - (*numCount) % dim;
}
if ((*numCount) >= numCountLim) //同一层大括号的数量超过了这一层的维数
{
handleError(10, r->lineNo);
}
r = sdtParseConstInitVal_Array(r, para, a, arrayDim, index + 1, paraCount + 1, numCount, *numCount + getArraySize(a, index + 1, arrayDim));
}
else if (strcmp(r->name, "RC") == 0)
{
if ((*numCount) % getArraySize(a, index, arrayDim) != 0) //当前维没填满
{
(*numCount) = numCountLim;
}
(*numCount) = numCountLim;
return r;
}
else if (strcmp(r->name, "ConstExp") == 0)
{
int res = sdtParseConstExp(r);
printf("res=%d\n", res);
if (*numCount >= numCountLim) //常量数量超过当前维限制
{
handleError(10, r->lineNo);
}
(*para)->const_val[*numCount] = res;
*numCount = *numCount + 1;
}
}
}
void sdtParseConstInitVal(treeNode *r, Para *para)
{
/*
ConstInitVal : ConstExp | LC ConstInitVal ComConstInitVal_rep RC | LC RC
ComConstInitVal_rep : ComConstInitVal_rep COMMA ConstInitVal | empty
*/
Type t = getFirst((*para)->type);
if (t->kind == constant) //常量的初始化必须是一个单一int值
{
if (strcmp(r->child->name, "ConstExp") == 0)
{
(*para)->const_val = (int *)malloc(sizeof(int));
(*para)->const_val[0] = sdtParseConstExp(r->child);
}
else
{
handleError(5, r->lineNo);
}
}
else // constArray
{
int arrayDim = listsize((*para)->type);
printf("arrayDim: %d\n", arrayDim);
int *arrayDimList = (int *)malloc(sizeof(int) * arrayDim);
getArrayDimList(arrayDimList, *para);
printf("arrayDimList: {");
int i;
for (i = 0; i < arrayDim; i++)
printf("%d,", arrayDimList[i]);
printf("}\n");
int arraySize = getArraySize(arrayDimList, 0, arrayDim);
printf("arraySize: %d\n", arraySize);
(*para)->const_val = (int *)malloc(sizeof(int) * arraySize);
memset((*para)->const_val, 0, sizeof((*para)->const_val));
if (strcmp(r->child->next->name, "RC") == 0) //仅一个{},全部初始化为0,直接返回
{
printf("All zero\n");
return;
}
else if (strcmp(r->child->name, "ConstExp") == 0)
{
handleError(6, r->lineNo);
}
else
{
int tmp = 0;
sdtParseConstInitVal_Array(r->child, para, arrayDimList, arrayDim, 0, 1, &tmp, arraySize);
int i;
printf("{");
for (i = 0; i < arraySize; i++)
printf("%d ", (*para)->const_val[i]);
printf("}\n");
}
free(arrayDimList);
}
}
void sdtParseConstDef(treeNode *r)
{
if (r == NULL)
return;
if (strcmp(r->name, "ConstDef") == 0) //ConstDef -> ID ConstExp_rep ASSIGNOP ConstInitVal
{
Para item;
NEW0(item);
item->type = newList();
item->scope = scope;
strcpy(item->name, r->child->value);
if (r->child->next->child == NULL) //ID is a constant
{
Type newType;
NEW0(newType);
newType->kind = constant;
addFirst(item->type, newType);
sdtParseConstInitVal(r->child->next->next->next, &item);
int tmp = insertSymTable_para(item, r->lineNo);
if (!tmp)
printf("constant: %d\n", item->const_val[0]);
}