-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpcat.y
1637 lines (1547 loc) · 51.5 KB
/
pcat.y
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
%{
#define YYDEBUG 0
#include "pcat.hpp"
/* prototypes */
int yylex(void);
extern FILE *yyin;
nodeType *head;
nodeType *combine(char *description, int nops, ...);
nodeType *new_node(char *description);
nodeType *node_copy(nodeType *node);
nodeType* dfs(nodeType *now, int depth);
void dfs_print(nodeType *now, int depth);
varElement *createAndCopy(varElement *svE);
varElement *interpreter(nodeType *now);
void entryTextPrint(varElement *v, char *s);
context *programContext;
void yyerror(char *str);
%}
%union {
nodeType *v_nptr;
};
%token <v_nptr> INTEGER REAL STRING ID BBOOL
%token <v_nptr> ARRAY PROGRAMBEGIN BY DO ELSE ELSIF END FOR IF IN IS LOOP OF OUT PROCEDURE PROGRAM READ RECORD THEN TO TYPE VAR WHILE WRITE EXIT RETURN LBRACKET RBRACKET
%token <v_nptr> ':' ';' ',' '.' '(' ')' '[' ']' '{' '}' BACKSLASH ASSIGN NOT LE GE NE '<' '>' '=' OR '+' '-' '*' '/' MOD DIV AND
%left NOT LE GE NE '<' '>' '='
%left OR '+' '-'
%left '*' '/' MOD DIV AND
%nonassoc UNARY
%type <v_nptr> program
%type <v_nptr> body declarations statements
%type <v_nptr> declaration var_decls type_decls procedure_decls
%type <v_nptr> var_decl ids
%type <v_nptr> type_decl
%type <v_nptr> procedure_decl
%type <v_nptr> type components
%type <v_nptr> component
%type <v_nptr> formal_params fp_sections
%type <v_nptr> fp_section
%type <v_nptr> statement lvalues elsifs elsif
%type <v_nptr> write_params write_exprs
%type <v_nptr> write_expr
%type <v_nptr> expression
%type <v_nptr> lvalue
%type <v_nptr> actual_params expressions
%type <v_nptr> comp_values assign_expression_to_id_s
%type <v_nptr> array_values comma_sep_array_values
%type <v_nptr> array_value
%type <v_nptr> number
%%
program:
PROGRAM IS body ';' {
$$ = combine("program", 4, $1, $2, $3, $4);
head = dfs($$, 0);
dfs_print(head, 0);
interpreter(head);
}
| PROGRAM IS error ';'{
$$ = combine("program", 4, $1, $2, new_node("error body"), $4);
yyerror("syntax error in body");
head = dfs($$, 0);
dfs_print(head, 0);
}
| error ';' {
$$ = new_node("error program");
yyerror("syntax error in program");
head = dfs($$, 0);
dfs_print(head, 0);
}
;
body:
declarations PROGRAMBEGIN statements END {
$$ = combine("body", 4, $1, $2, $3, $4);
}
;
declarations:
declarations declaration {
$$ = combine("multi declaration", 2, $1, $2);
}
| {
$$ = new_node("empty declarations");
} /* empty */
;
statements:
statements statement {
$$ = combine("multi statement", 2, $1, $2);
}
| {
$$ = new_node("empty statements");
} /* empty */
;
declaration:
VAR var_decls {
$$ = combine("declaration", 2, $1, $2);
}
| TYPE type_decls {
$$ = combine("declaration", 2, $1, $2);
}
| PROCEDURE procedure_decls {
$$ = combine("declaration", 2, $1, $2);
}
;
var_decls:
var_decls var_decl {
$$ = combine("multi var_decl", 2, $1, $2);
}
| {
$$ = new_node("empty var_decls");
} /* empty */
;
type_decls:
type_decls type_decl {
$$ = combine("multi type_decl", 2, $1, $2);
}
| {
$$ = new_node("empty type_decls");
}/* empty */
;
procedure_decls:
procedure_decls procedure_decl {
$$ = combine("multi type_decl", 2, $1, $2);
}
| {
$$ = new_node("empty procedure_decls");
}/* empty */
;
var_decl:
ID ids ASSIGN expression ';' {
$$ = combine("var_decl", 5, $1, $2, $3, $4, $5);
}
| ID ids ':' type ASSIGN expression ';' {
$$ = combine("var_decl", 7, $1, $2, $3, $4, $5, $6, $7);
}
| error ';' {
$$ = new_node("error var_decl");
yyerror("syntax error in var declaration");
}
;
ids:
ids ',' ID {
$$ = combine("multi id", 3, $1, $2, $3);
}
| {
$$ = new_node("empty ids");
} /* empty */
;
type_decl:
ID IS type ';' {
$$ = combine("type_decl", 4, $1, $2, $3, $4);
}
| error ';'{
$$ = new_node("error type declaration");
yyerror("syntax error in type declaration");
}
;
procedure_decl:
ID formal_params IS body ';' {
$$ = combine("procedure_decl", 5, $1, $2, $3, $4, $5);
}
| ID formal_params ':' type IS body ';' {
$$ = combine("procedure_decl", 7, $1, $2, $3, $4, $5, $6, $7);
}
| error ';'{
$$ = new_node("error procedure declaration");
yyerror("syntax error in procedure declaration");
}
;
type:
ID {
$$ = combine("type", 1, $1);
}
| ARRAY OF type {
$$ = combine("type", 3, $1, $2, $3);
}
| RECORD component components END {
$$ = combine("type", 4, $1, $2, $3, $4);
}
| RECORD error END {
$$ = combine("type", 3, $1, new_node("error component"), $3);
yyerror("syntax error in component");
}
;
components:
components component {
$$ = combine("multi component", 2, $1, $2);
}
| {
$$ = new_node("empty components");
}/* empty */
;
component:
ID ':' type ';' {
$$ = combine("component", 4, $1, $2, $3, $4);
}
| error ';'{
$$ = new_node("error component");
yyerror("syntax error in component");
}
;
formal_params:
'(' fp_section fp_sections ')' {
$$ = combine("formal_params", 4, $1, $2, $3, $4);
}
| '(' ')' {
$$ = combine("formal_params", 2, $1, $2);
}
| '(' error ')' {
$$ = combine("type", 3, $1, new_node("error fp_section"), $3);
yyerror("syntax error in fp_section");
}
;
fp_sections:
fp_sections ';' fp_section {
$$ = combine("multi fp_section", 3, $1, $2, $3);
}
| {
$$ = new_node("empty fp_sections");
}/* empty */
;
fp_section:
ID ids ':' type {
$$ = combine("fp_section", 4, $1, $2, $3, $4);
}
statement:
lvalue ASSIGN expression ';' {
$$ = combine("statement", 4, $1, $2, $3, $4);
}
| ID actual_params ';' {
$$ = combine("statement", 3, $1, $2, $3);
}
| READ '(' lvalue lvalues ')' ';' {
$$ = combine("statement", 6, $1, $2, $3, $4, $5, $6);
}
| WRITE write_params ';' {
$$ = combine("statement", 3, $1, $2, $3);
}
| IF expression THEN statements elsifs END ';' {
$$ = combine("statement", 7, $1, $2, $3, $4, $5, $6, $7);
}
| IF expression THEN statements elsifs ELSE statements END ';' {
$$ = combine("statement", 9, $1, $2, $3, $4, $5, $6, $7, $8, $9);
}
| WHILE expression DO statements END ';' {
$$ = combine("statement", 6, $1, $2, $3, $4, $5, $6);
}
| LOOP statements END ';' {
$$ = combine("statement", 4, $1, $2, $3, $4);
}
| FOR ID ASSIGN expression TO expression DO statements END ';' {
$$ = combine("statement", 10, $1, $2, $3, $4, $5, $6, $7, $8, $9, $10);
}
| FOR ID ASSIGN expression TO expression BY expression DO statements END ';' {
$$ = combine("statement", 10, $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12);
}
| EXIT ';' {
$$ = combine("statement", 2, $1, $2);
}
| RETURN ';' {
$$ = combine("statement", 2, $1, $2);
}
| RETURN expression ';' {
$$ = combine("statement", 3, $1, $2, $3);
}
| error ';' {
$$ = new_node("error statement");
yyerror(" error in statement");
}
;
lvalues:
lvalues ',' lvalue {
$$ = combine("multi lvalue", 3, $1, $2, $3);
}
| {
$$ = new_node("empty lvalues");
}/* empty */
;
elsifs:
elsifs elsif {
$$ = combine("multi elsif", 2, $1, $2);
}
| {
$$ = new_node("empty elsifs");
}/* empty */
;
elsif:
ELSIF expression THEN statements {
$$ = combine("elsif", 4, $1, $2, $3, $4);
}
| ELSIF error THEN statements{
$$ = combine("type", 4, $1, new_node("error experession"), $3, $4);
yyerror("syntax error in expression");
}
;
write_params:
'(' write_expr write_exprs ')' {
$$ = combine("write_params", 4, $1, $2, $3, $4);
}
| '(' ')' {
$$ = combine("write_params", 2, $1, $2);
}
| '(' error ')' {
$$ = combine("type", 3, $1, new_node("error write_expr"), $3);
yyerror("syntax error in write_expr");
}
;
write_exprs:
write_exprs ',' write_expr {
$$ = combine("multi write_expr", 3, $1, $2, $3);
}
| {
$$ = new_node("empty write_exprs");
}/* empty */
;
write_expr:
STRING {
$$ = combine("write_expr", 1, $1);
}
| expression {
$$ = combine("write_expr", 1, $1);
}
;
expression:
number {
$$ = combine("expression", 1, $1);
}
| lvalue {
$$ = combine("expression", 1, $1);
}
| '(' expression ')' {
$$ = combine("expression", 3, $1, $2, $3);
}
| '+' expression %prec UNARY{
$$ = combine("expression", 2, $1, $2);
}
| '-' expression %prec UNARY{
$$ = combine("expression", 2, $1, $2);
}
| NOT expression %prec UNARY{
$$ = combine("expression", 2, $1, $2);
}
| expression '*' expression{
$$ = combine("expression", 3, $1, $2, $3);
}
| expression '/' expression{
$$ = combine("expression", 3, $1, $2, $3);
}
| expression AND expression{
$$ = combine("expression", 3, $1, $2, $3);
}
| expression DIV expression{
$$ = combine("expression", 3, $1, $2, $3);
}
| expression MOD expression{
$$ = combine("expression", 3, $1, $2, $3);
}
| expression '+' expression{
$$ = combine("expression", 3, $1, $2, $3);
}
| expression '-' expression{
$$ = combine("expression", 3, $1, $2, $3);
}
| expression OR expression{
$$ = combine("expression", 3, $1, $2, $3);
}
| expression '>' expression{
$$ = combine("expression", 3, $1, $2, $3);
}
| expression '<' expression{
$$ = combine("expression", 3, $1, $2, $3);
}
| expression '=' expression{
$$ = combine("expression", 3, $1, $2, $3);
}
| expression GE expression{
$$ = combine("expression", 3, $1, $2, $3);
}
| expression LE expression{
$$ = combine("expression", 3, $1, $2, $3);
}
| expression NE expression{
$$ = combine("expression", 3, $1, $2, $3);
}
| ID actual_params {
$$ = combine("expression", 2, $1, $2);
}
| ID comp_values {
$$ = combine("expression", 2, $1, $2);
}
| ID array_values {
$$ = combine("expression", 2, $1, $2);
}
;
lvalue:
ID {
$$ = combine("lvalue", 1, $1);
}
| lvalue '[' expression ']' {
$$ = combine("lvalue", 4, $1, $2, $3, $4);
}
| lvalue '.' ID {
$$ = combine("lvalue", 3, $1, $2, $3);
}
| lvalue '[' error ']'{
$$ = combine("error lvalue", 4, $1, $2, new_node("error expression"), $4);
yyerror("syntax error in lvalue");
}
;
actual_params:
'(' expression expressions ')' {
$$ = combine("actual_params", 4, $1, $2, $3, $4);
}
| '(' ')' {
$$ = combine("actual_params", 2, $1, $2);
}
| '(' error ')'{
$$ = combine("error actual_params", 3, $1, new_node("error expression"), $3);
yyerror("syntax error in actual_params");
}
;
expressions:
expressions ',' expression {
$$ = combine("multi expression", 3, $1, $2, $3);
}
| {
$$ = new_node("empty expressions");
}/* empty */
;
comp_values:
'{' ID ASSIGN expression assign_expression_to_id_s '}' {
$$ = combine("comp_values", 6, $1, $2, $3, $4, $5, $6);
}
;
assign_expression_to_id_s:
assign_expression_to_id_s ';' ID ASSIGN expression {
$$ = combine("multi assign_expression_to_id", 5, $1, $2, $3, $4, $5);
}
| {
$$ = new_node("empty assign_expression_to_id_s");
}/* empty */
;
array_values:
LBRACKET array_value comma_sep_array_values RBRACKET {
$$ = combine("array_values", 4, $1, $2, $3, $4);
}
;
comma_sep_array_values:
comma_sep_array_values ',' array_value {
$$ = combine("multi comma_sep_array_value", 3, $1, $2, $3);
}
| {
$$ = new_node("empty comma_sep_array_values");
}/* empty */
;
array_value:
expression {
$$ = combine("array_value", 1, $1);
}
| expression OF expression {
$$ = combine("array_value", 3, $1, $2, $3);
}
number:
INTEGER {
$$ = combine("number", 1, $1);
}
| REAL {
$$ = combine("number", 1, $1);
}
| BBOOL {
$$ = combine("number", 1, $1);
}
;
%%
nodeType *combine(char *label, int nops, ...) {
va_list ap;
nodeType *p;
int i;
if ( (p=malloc( sizeof(nodeType)+(nops-1)*sizeof(nodeType *) ) ) == NULL)
yyerror("out of memory");
p->type = typeNonterminal;
p->nt.label = strdup(label);
p->nt.nops = nops;
va_start(ap, nops);
for (i = 0; i < nops; i++)
p->nt.op[i] = va_arg(ap, nodeType*);
va_end(ap);
return p;
}
nodeType *new_node(char *label) {
nodeType *p;
if ( (p=malloc( sizeof(nodeType) ) ) == NULL)
yyerror("out of memory");
p->type = typeTerminal;
p->t.label = strdup(label);
return p;
}
nodeType *node_copy(nodeType *node) {
if(node->type==typeTerminal)
return node;
nodeType *p;
if ( (p=malloc( sizeof(nodeType)+(node->nt.nops-1)*sizeof(nodeType *) ) ) == NULL)
yyerror("out of memory");
p->type = node->type;
p->nt.label = strdup(node->nt.label);
p->nt.nops = node->nt.nops;
for (int i = 0; i < node->nt.nops; i++)
p->nt.op[i] = node->nt.op[i];
return p;
}
void yyerror(char *s) {
if (strcmp(s, "syntax error") == 0) return;
if (line_num == 1)
fprintf(stdout, "line %d col %d: %s\n", line_num, col_num, s);
else{
fprintf(stdout, "line %d col %d: %s:\n%s\n", line_num, col_num, s, line_buffer);
fprintf(stdout, "%*s\n",col_num,"^");
}
}
int printable(nodeType *node) {
if (node->type == typeTerminal) {
if (strstr(node->t.label, "empty") || strstr(node->t.label, "multi")) {
return 0;
} else {
return 1;
}
} else {
if (strstr(node->nt.label, "empty") || strstr(node->nt.label, "multi")) {
return 0;
} else {
return 1;
}
}
}
nodeType* dfs(nodeType *now, int depth) {
int i;
if (printable(now)) {
if (now->type==typeTerminal)
return now;
int tot=0;
for (i = 0; i < now->nt.nops; ++i) {
now->nt.op[i]=dfs(now->nt.op[i], depth + 1);
tot+=now->nt.op[i]->nt.nops;
}
nodeType *temp=node_copy(now);
return temp;
} else {
if (now->type==typeNonterminal) {
for (i = 0; i < now->nt.nops; ++i) {
now->nt.op[i]=dfs(now->nt.op[i], depth+1);
if(!printable(now->nt.op[i])&&strcmp(now->nt.op[i]->nt.label,now->nt.label)==0){
//the child is not printable and the node is ax the same type as its child
nodeType *p;
if ( (p=malloc(sizeof(nodeType)+(now->nt.nops+now->nt.op[i]->nt.nops-1)*sizeof(nodeType *))) == NULL)
yyerror("out of memory");
p->type = typeNonterminal;
p->nt.label = strdup(now->nt.label);
p->nt.nops = now->nt.nops+now->nt.op[i]->nt.nops-1;
int j=0;
for (j = 0; j < now->nt.op[i]->nt.nops; j++)
p->nt.op[j] = now->nt.op[i]->nt.op[j];
//paste the ops of the node's children's children
for (j = now->nt.op[i]->nt.nops; j < now->nt.nops+now->nt.op[i]->nt.nops-1; j++)
p->nt.op[j] = now->nt.op[j-now->nt.op[i]->nt.nops+1];
//paste the ops of the node's own children
now=p;
i+=now->nt.op[i]->nt.nops-1;
//skip the children's children
}
}
}
return now;
}
}
void dfs_print(nodeType *now, int depth) {
int i;
if (printable(now)) {
for (i = 0; i < depth-1; ++i) {
fprintf(stdout, "| ");
}
if (depth >= 1) {
fprintf(stdout, "|---");
}
if (now->type==typeTerminal) {
if(strcmp(now->t.label,"INTEGER")==0)
fprintf(stdout, "< %d >\n", now->t.v_int);
else if(strcmp(now->t.label,"REAL")==0)
fprintf(stdout, "< %lf >\n", now->t.v_real);
else if(strcmp(now->t.label,"ID")==0)
fprintf(stdout, "< %s >\n", now->t.v_id);
else if(strcmp(now->t.label,"STRING")==0)
fprintf(stdout, "< %s >\n", now->t.v_string);
else if (strcmp(now->t.label, "BOOL") == 0)
fprintf(stdout, "< %d >\n", now->t.v_bool);
else
fprintf(stdout, "< %s >\n", now->t.label);
return;
}
fprintf(stdout, "%s\n", now->nt.label);
for (i = 0; i < now->nt.nops; ++i) {
dfs_print(now->nt.op[i], depth + 1);
}
}
else{
if (now->type==typeNonterminal) {
for (i = 0; i < now->nt.nops; ++i)
dfs_print(now->nt.op[i], depth);
}
}
}
varElement *returnNullVar() {
varElement *returnVar = malloc(sizeof(varElement));
returnVar->type = varv;
returnVar->t.type = nullv;
returnVar->t.nullv = 1;
return returnVar;
}
context *createContext(context *address, int depth){
context *returnContext = malloc(sizeof(context));
returnContext->callFrom = address;//for main(), callFrom = NULL
returnContext->father = NULL;
returnContext->typeTableSize = returnContext->procedureTableSize = returnContext->varTableSize = 0;
returnContext->depth = depth + 1;
if(returnContext->depth > 100)
fprintf(stdout, "Stack overflow\n");
returnContext->returnValue = returnNullVar();
return returnContext;
}
varElement *findVar(context* con, char* x){
while(1){
for(int i = 0; i < con->varTableSize; i++){
if(strcmp(con->varTable[i]->label, x) == 0)
return con->varTable[i];
}
if(con->father != NULL)
con = con->father;
else
break;
}
fprintf(stdout, "no var: %s\n", x);
return returnNullVar();
}
nodeType *findProcedure(context *con, context *callcon, char *x) {
while(1){
for(int i = 0; i < con->procedureTableSize; i++){
if(strcmp(con->procedureTable[i].label, x) == 0){
callcon->father = con;
return con->procedureTable[i].address;
}
}
if(con->father != NULL)
con = con->father;
else
break;
}
fprintf(stdout,"no procedure\n");
return new_node("error");
}
void createTable(nodeType *now) {
for (int counter = 1; counter < now->nt.nops; counter++){
if (strcmp(now->nt.op[counter]->nt.op[0]->t.label, "TYPE") == 0){
nodeType *temp = now->nt.op[counter]->nt.op[1];
for (int tempCounter = 1; tempCounter < temp->nt.nops; tempCounter++){
programContext->typeTable[programContext->typeTableSize].label = malloc(strlen((temp->nt.op[tempCounter]->nt.op[0]->t.v_id)+1)*sizeof(char));
strcpy(programContext->typeTable[programContext->typeTableSize].label, temp->nt.op[tempCounter]->nt.op[0]->t.v_id);
programContext->typeTable[programContext->typeTableSize].address = temp->nt.op[tempCounter];
//fprintf(stdout, "get TYPE:%s\n", programContext->typeTable[programContext->typeTableSize].label);
programContext->typeTableSize++;
}
}
else if (strcmp(now->nt.op[counter]->nt.op[0]->t.label, "PROCEDURE") == 0){
nodeType *temp = now->nt.op[counter]->nt.op[1];
for (int tempCounter = 1; tempCounter < temp->nt.nops; tempCounter++){
programContext->procedureTable[programContext->procedureTableSize].label = malloc(strlen((temp->nt.op[tempCounter]->nt.op[0]->t.v_id)+1)*sizeof(char));
strcpy(programContext->procedureTable[programContext->procedureTableSize].label, temp->nt.op[tempCounter]->nt.op[0]->t.v_id);
programContext->procedureTable[programContext->procedureTableSize].address = temp->nt.op[tempCounter];
//fprintf(stdout, "get PROCEDURE:%s\n", programContext->procedureTable[programContext->procedureTableSize].label);
programContext->procedureTableSize++;
}
}
}
}
varElement *createAndCopy(varElement *svE){
varElement *tvE = malloc(sizeof(varElement));
tvE->label = svE->label;
tvE->type = svE->type;
if (svE->type == varv) {
tvE->t = svE->t;
} else if (svE->type == arrayv) {
tvE->arrayv.nops = svE->arrayv.nops;
for (int i = 0; i < svE->arrayv.nops; i++) {
tvE->arrayv.op[i] = createAndCopy(svE->arrayv.op[i]);
}
} else {
tvE->typev.nops = svE->typev.nops;
for (int i = 0; i < svE->typev.nops; i++) {
tvE->typev.label[i] = strdup(svE->typev.label[i]);
tvE->typev.op[i] = createAndCopy(svE->typev.op[i]);
}
}
return tvE;
}
varElement *getlvalue(nodeType *now){
//l-value := expr, get l-value's address and the do copyVarElement(l-vale, interpreter(expr))
//now is l-value
if (now->nt.nops == 1) { // if now -> ID
return findVar(programContext, now->nt.op[0]->t.v_id);
} else if (now->nt.nops == 3) { // if now --> l-value . ID
varElement *tmp = getlvalue(now->nt.op[0]);
for (int i = 0; i < tmp->typev.nops; ++i) {
if (strcmp(tmp->typev.label[i], now->nt.op[2]->t.v_id) == 0) {
return tmp->typev.op[i];
}
}
fprintf(stdout, "lvalue not found");
return returnNullVar();
} else if (now->nt.nops == 4) { // if now --> l-value [ expression ]
varElement *tmp = getlvalue(now->nt.op[0]);
int index = interpreter(now->nt.op[2])->t.intv;
return tmp->arrayv.op[index];
}
return returnNullVar();
}
void addArrayValueToVarElement(varElement *x, nodeType *node) { // node if an array_value node
if (node->nt.nops == 1) { // array_value --> expression
x->arrayv.op[x->arrayv.nops] = interpreter(node->nt.op[0]);
x->arrayv.nops += 1;
} else { // array_value --> expression of expression
int n = interpreter(node->nt.op[0])->t.intv;
for (int i = 0; i < n ; ++i) {
x->arrayv.op[x->arrayv.nops] = interpreter(node->nt.op[2]);
x->arrayv.nops += 1;
}
}
}
varElement *createVarElement(nodeType *now){
// only when ID comp-values and ID array-values
// now->nt.label == expression
varElement *ret = malloc(sizeof(varElement));
if (strcmp(now->nt.op[1]->nt.label, "comp_values") == 0) { // expression -> ID comp-values
ret->type = typev;
nodeType *comp_values_node = now->nt.op[1];
ret->typev.label[ret->typev.nops] = strdup(comp_values_node->nt.op[1]->t.v_id);
varElement *r = interpreter(comp_values_node->nt.op[3]);
ret->typev.op[ret->typev.nops] = r;
ret->typev.nops += 1;
if (comp_values_node->nt.op[4]->type == typeNonterminal) { // comp_values -> { R := expression multi }
nodeType *multi = comp_values_node->nt.op[4];
for (int i = 2; i < multi->nt.nops; i += 4) {
char *id_name = multi->nt.op[i]->t.v_id;
ret->typev.label[ret->typev.nops] = strdup(id_name);
varElement *r = createAndCopy(interpreter(multi->nt.op[i+2]));
ret->typev.op[ret->typev.nops] = r;
ret->typev.nops += 1;
}
}
} else { // expression -> ID array-values
ret->type = arrayv;
nodeType *array_values_node = now->nt.op[1];
addArrayValueToVarElement(ret, array_values_node->nt.op[1]);
if (array_values_node->nt.op[2]->type == typeNonterminal) {
// true: array_value -> [< array_value multi >]
// false: array_values -> [< array_value <empty> >]
nodeType *multi_array_value = array_values_node->nt.op[2];
for (int i = 0; i < multi_array_value->nt.nops; ++i) {
nodeType *node = multi_array_value->nt.op[i];
if (node->type == typeNonterminal) { // is array_value
addArrayValueToVarElement(ret, node);
}
}
}
}
return ret;
}
//------------------------begin write -------------------------
void write_expression(varElement *data) {
if (data->type == varv) {
switch(data->t.type) {
case nullv: fprintf(stdout, "[< not printable >]"); break;
case intv: fprintf(stdout, "%d", data->t.intv); break;
case realv: fprintf(stdout, "%f", data->t.realv); break;
case boolv:
if (data->t.boolv) {
fprintf(stdout, "TRUE");
} else {
fprintf(stdout, "FALSE");
}
break;
case stringv: fprintf(stdout, "%s", data->t.stringv); break;
case returnFlag: fprintf(stdout, "[< not printable >]"); break;
case exitFlag: fprintf(stdout, "[< not printable >]"); break;
}
} else {
fprintf(stdout, "[< not printable >]");
}
}
void write(nodeType *now) { // now is write_parames or multi_write_expr
for (int i = 0; i < now->nt.nops; ++i) {
nodeType *tmp = now->nt.op[i];
if (tmp->type == typeNonterminal) {
if (strcmp(tmp->nt.label, "write_expr") == 0) { // tmp is write_expr; write_expr -> STRING | expression
nodeType *to_print_node = tmp->nt.op[0];
if (to_print_node->type == typeNonterminal) {// to_print_node is expression
write_expression(interpreter(to_print_node));
} else { // to_print_node is STRING
fprintf(stdout, "%s", to_print_node->t.v_string);
}
} else { // tmp is multi_write_expr
write(tmp);
}
}
}
}
//-----------------------end write ----------------------
void testPrint(varElement *v){
if (v == NULL){
printf("NULL\n");
}
if (v->type == varv){
write_expression(v);
}
else if(v->type == typev){
for(int i = 0; i< v->typev.nops; i++){
printf("%s: ", v->typev.label[i]);
testPrint(v->typev.op[i]);
printf("\n");
}
}
else if(v->type == arrayv){
for (int i = 0 ;i< v->arrayv.nops; i++){
printf("%d: ", i );
testPrint(v->arrayv.op[i]);
printf("\n");
}
}
}
void entryTextPrint(varElement *v, char *s){
printf("start print %s\n", s);
testPrint(v);
printf("end print %s\n", s);
}
//-----------------------begin read ----------------------
void read_var_element(varElement *var_element_to_read) {
switch(var_element_to_read->t.type) {
case nullv: fprintf(stdout, "[< not readable >]"); break;
case intv: fscanf(stdin, "%d", &(var_element_to_read->t.intv)); break;
case realv:
fscanf(stdin, "%f", &(var_element_to_read->t.realv));
break;
case boolv: fscanf(stdin, "%d", &(var_element_to_read->t.boolv)); break;
case stringv:
var_element_to_read->t.stringv = malloc(128 * sizeof(char)); //[warnning] maxlength of string input is 128
fscanf(stdin, "%s", var_element_to_read->t.stringv);
break;
case returnFlag: fprintf(stdout, "[< not readable >]"); break;
case exitFlag: fprintf(stdout, "[< not readable >]"); break;
}
}
void read(nodeType *now) {
//read all the l-values under now;
//now is statement or multi_lvalue
for (int i = 0; i < now->nt.nops; ++i) {
nodeType *tmp = now->nt.op[i];
if (tmp->type == typeNonterminal) {
if (strcmp(tmp->nt.label, "lvalue") == 0) { // tmp is an l-value
varElement *var_element_to_read = getlvalue(tmp);
if (var_element_to_read->type == varv) {
read_var_element(var_element_to_read);
} else {
fprintf(stdout, "[< not readable >]");
}
} else { // tmp is a multi_value
read(tmp);
}
}
}
}
//-----------------------end read ------------------------
//-----------------------begin get_param_names --------------------------
void get_param_names(nodeType *now, char **names, int *len) {
// now is node of subtree node of formal_params
// find all ids in subtree formal_params.
// don't dfs when meet a node of <type>
for (int i = 0; i < now->nt.nops; ++i) {
if (now->nt.op[i]->type == typeNonterminal && strcmp(now->nt.op[i]->nt.label, "type") != 0) {
get_param_names(now->nt.op[i], names, len);
} else if (strcmp(now->nt.op[i]->t.label, "ID") == 0) {
names[*len] = strdup(now->nt.op[i]->t.v_id);
*len += 1;
}
}
}
//-----------------------end get_param_names --------------------------------------------
//-----------------------begin get_param_vals ------------------------------------------
void get_param_vals(nodeType *now, varElement **vars, int *len) {
// now is actual-params
for (int i = 0; i < now->nt.nops; ++i) {
nodeType *tmp = now->nt.op[i];
if (tmp->type == typeNonterminal) { // tmp is expression or multi_expressions
if (strcmp(tmp->nt.label, "expression") == 0) {
vars[*len] = interpreter(tmp);
*len +=1;
} else {
get_param_vals(tmp, vars, len);
}
}
}
}
//-----------------------end get_param_vals --------------------------------------------
varElement *interpreter(nodeType *now){
if (now->type == typeNonterminal) {
//fprintf(stdout, "%s\n", now->nt.label);
if (strcmp(now->nt.label, "program") == 0){
programContext = createContext(NULL, 0);
interpreter(now->nt.op[2]);
//fprintf(stdout, "Finish interpretering.\n");
programContext = NULL;
return returnNullVar();
}
else if (strcmp(now->nt.label, "body") == 0){