/
parser2.c
913 lines (777 loc) · 17.1 KB
/
parser2.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
//
// parser2.c -- parser for tinyj
// (outside of the block)
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "token.h"
#include "sym.h"
#include "type.h"
#include "ast.h"
/*
Grammar
program ::= classdecls
classdecls ::= [ classdecl ]*
classdecl ::= classhead '{' vardecls funcdecls '}'
classhead ::= CLASS ID
vardecls ::= [ vardecl ]*
funcdecls ::= [ funcdecl ]*
// here is not LL(1)
vardecl ::= typedecl id ';'
funcdecl ::= typedecl id '(' argdecls ')' block
// end
argdecls ::= e | argdecl [ ',' argdecl ]*
argdecl ::= typedecl var
typedecl ::= primtype mod?
mod ::= { '[' con ']' }*
primtype ;;= INT | CHAR | FLOAT | STRING
---------------------
block ::= '{' vardecls stmts '}'
stmts ::= [ stmt ]*
stmt ::= expr ';' | ifstmt | asnstmt | block
ifstmt ::= IF '(' bexpr ')' stmt ELSE stmt
// here is not LL(1)
asnstmt ::= lval '=' expr
callstmt ::= name '(' argrefs ')
// end
argrefs ::= argref { ',' argref }*
argref ::= expr
lval ::= vref exprs?
exprs ::= { '[' expr ']' }*
expr ::= term [ '+' term ]*
term ::= factor [ '*' factor ]*
factor ::= lval | con | '(' expr ')' | mesexpr
bexpr ::= TRUE | FALSE | expr relop expr
relop ::= EQEQ | NOTEQ | GT | GTEQ | LT | LTEQ
vref ::= ID
con ::= LIT
*/
extern Token *gettoken();
extern void skiptoken(int);
extern void skiptoken2(int,int);
static AST program(void);
static AST classdecls(void);
static AST classdecl(void);
static AST classhead(void);
static AST structdecls(AST sdl);
static AST vardecls(AST vdl, AST fdl);
static AST funcdecls(AST fdl);
static AST structdecl(void);
static AST vardecl(void);
static AST funcdecl(void);
static AST typedecl(void);
static AST retdecl(void);
static AST mod(AST);
static AST vartype(void);
static AST rettype(void);
static bool isprimtype(int k);
static bool isclasstype();
static bool isstructtype();
static bool isrettype(int k);
static AST argdecls(void);
static AST argdecl(void);
static AST block(void);
static AST stmts(void);
static AST stmt(void);
static AST asnstmt(AST);
static AST ifstmt(void);
static AST callstmt(AST);
static AST returnstmt(void);
static AST argrefs();
static AST argref(void);
static AST var(void);
static AST name(void);
static AST classname(void);
static AST structname(void);
static AST vName(void);
static AST vref(void);
static AST lval(void);
static AST exprs(void);
static AST con(void);
static AST expr(void);
static AST bexpr(void);
static AST ast_root;
static int ast_debug = false;
#ifdef TEST_PARSER
int main() {
initline();
init_AST();
init_SYM();
init_LOC();
gettoken();
ast_root = program();
print_AST(ast_root);
printf("\n\n");
if (ast_debug) {
dump_AST(stdout);
printf("\n");
}
dump_SYM(stdout);
printf("\n");
dump_LOC(stdout);
printf("\n");
dump_STR(stdout);
return 0;
}
#else
int start_parser() {
gettoken();
return program();
}
#endif
static AST program() {
Token *t = &tok;
AST a=0;
AST a1=0, a2=0;
a1 = classdecls();
a = make_AST(nPROG, a1, 0, 0, 0);
return a;
}
static AST classdecls() {
Token *t = &tok;
AST a=0;
AST a1=0, a2=0;
/* at least one class is required */
a = new_list(nCLASSDECLS);
a1 = classdecl();
if (a1) a = append_list(a, a1);
while (true) {
if (t->sym != tCLASS) break;
a1 = classdecl();
if (a1) a = append_list(a, a1);
}
return a;
}
static AST classdecl() {
Token *t = &tok;
AST a=0;
AST a1=0, a2=0, a3=0, a4 = 0, a5 = 0;
AST sdl = 0, vdl=0, fdl=0; /* struct, var and func decl list */
a1 = classhead();
if (t->sym == '{') {
gettoken();
enter_block();
sdl = new_list(nSTRUCTS);
vdl = new_list(nVARDECLS);
fdl = new_list(nFUNCDECLS);
a2 = structdecls(sdl);
if (t->sym == tCLASS) a3 = classdecls();
a4 = vardecls(vdl, fdl);
a5 = funcdecls(fdl);
leave_block();
if (t->sym == '}') {
gettoken();
a = make_AST(nCLASSBODY, a2, a3, a4, a5);
a = make_AST(nCLASSDECL, a1, a, 0, 0);
} else {
parse_error("expected }");
}
} else {
parse_error("expected {");
}
return a;
}
static AST classhead() {
Token *t = &tok;
AST a=0;
AST a1=0, a2=0;
if (t->sym == tCLASS) {
gettoken();
a1 = classname();
if (a1) {
insert_SYM(get_text(a1), 0, tGLOBAL, 0); /* dummy */
}
a = make_AST(nCLASSHEAD, a1, 0, 0, 0);
} else {
parse_error("expected class");
}
return a;
}
static bool isprimtype(int k) {
return (k==tINT || k==tCHAR || k==tFLOAT || k==tSTRING);
}
static bool isclasstype(){
Token *t = &tok;
if (t->sym == ID) {
char *text = strdup(t->text);
AST idx = lookup_SYM_all(text);
if (idx == 0) return false;
AST type = make_AST_name(text);
if (nodetype(type) == tCLASS) return true;
}
return false;
}
static bool isstructtype(){
Token *t = &tok;
if (t->sym == ID) {
char *text = strdup(t->text);
AST idx = lookup_SYM_all(text);
if (idx == 0) return false;
AST type = make_AST_name(text);
if (nodetype(type) == tSTRUCT) return true;
}
return false;
}
static bool isrettype(int k){
return isprimtype(k) || k == tVOID;
}
static AST structdecls(AST sdl){
Token *t = &tok;
AST a = 0;
while (true){
if (t->sym != tSTRUCT) break;
gettoken();
a = structdecl();
if (a) sdl = append_list(sdl, a);
}
return sdl;
}
static AST vardecls(AST vdl, AST fdl) {
Token *t = &tok;
AST a=0;
AST a1=0;
bool isfuncdecl = false;
while (true) {
if (!isprimtype(t->sym) && !isclasstype() && !isstructtype()) break;
a1 = vardecl();
if (a1 && nodetype(a1) == nFUNCDECL) /* expect var, but it was func */
{ isfuncdecl = true; break; }
if (a1) vdl = append_list(vdl, a1);
if (t->sym == ';') gettoken();
else parse_error("expected ;");
}
if (isfuncdecl) {
fdl = append_list(fdl, a1); /* add first elem of fdl */
}
return vdl;
}
static AST funcdecls(AST fdl) {
Token *t = &tok;
AST a=0;
AST a1=0;
while (true) {
if ( !isrettype(t->sym) && !isclasstype() && !isstructtype()) break;
a1 = funcdecl();
if(a1) fdl = append_list(fdl, a1);
}
return fdl;
}
static AST vardecl() { /* TODO: allow vars */
Token *t = &tok;
AST a=0;
AST a1=0,a2=0,a3=0,a4=0;
AST ft;
int idx;
a2 = typedecl();
a1 = var();
if (t->sym == '(') { /* oops, it was func */
gettoken();
set_nodetype(a1, nNAME); /* change to NAME */
insert_SYM(get_text(a1), ft=func_type(gen(fLOCAL),a2), fLOCAL,0 /* dummy */ );
a3 = argdecls();
set_argtypeofnode(ft,a3);
if (t->sym == ')') gettoken();
else parse_error("expected )");
a4 = block();
a = make_AST_funcdecl(a1, a2, a3, a4);
} else if (t->sym == ';') { /* vardecl */
a = make_AST_vardecl(a1, a2, 0, 0);
idx = insert_SYM(get_text(a1), a2, vLOCAL, a1);
set_ival(a1,idx);
/*
} else if (t->sym == ',') { // multiple vars
* contents must be made
*
*/
} else {
parse_error("expected ; or (");
}
return a;
}
static AST structdecl(){
Token *t = &tok;
AST struct_name = structname();
insert_SYM(get_text(struct_name), 0, tGLOBAL, 0); /* dummy */
AST a = new_list(nVARDECLS);
if (t->sym == '{'){
enter_block();
gettoken();
a = vardecls(a, 0);
leave_block();
if (t->sym == '}') gettoken();
else parse_error("Expected }");
if (t->sym == ';') gettoken();
else parse_error("Expected ;");
a = make_AST_structdecl(struct_name, a);
} else {
parse_error("Expected {");
}
return a;
}
static AST funcdecl() {
Token *t = &tok;
AST a=0;
AST a1=0,a2=0,a3=0,a4=0;
AST ftype;
a2 = retdecl();
a1 = name();
ftype = func_type(gen(fLOCAL),a2);
insert_SYM(get_text(a1), ftype, fLOCAL, 0/* dummy */);
if (t->sym == '(') { /* must be func */
gettoken();
mark_args();
a3 = argdecls();
if (checkFuncExist(get_text(a1), a3)) parse_error("Duplicated function definition");
set_argtypeofnode(ftype,a3);
if (t->sym == ')') gettoken();
else parse_error("expected )");
a4 = block();
unmark_args();
a = make_AST_funcdecl(a1,a2,a3,a4);
} else {
parse_error("expected (");
}
return a;
}
static AST typedecl() {
Token *t = &tok;
AST a=0;
a = vartype();
a = mod(a);
return a;
}
static AST retdecl(){
Token *t = &tok;
AST a=0;
a = rettype();
a = mod(a);
return a;
}
static AST mod(AST elem) {
Token *t = &tok;
AST a,a1=0;
int sz;
a = elem;
if (t->sym == '[') {
gettoken();
a1 = con();
sz = get_ival(a1);
if (sz <= 0) { parse_error("size must be positive"); sz = 1; }
if (t->sym == ']') gettoken();
else parse_error("expected ]");
a = array_type(gen(tGLOBAL), elem, sz);
a = mod(a);
}
return a;
}
static AST vartype() {
Token *t = &tok;
AST a=0, idx = 0;
char *text;
switch (t->sym) {
case tINT: a = make_AST_name("int"); break;
case tCHAR: a = make_AST_name("char"); break;
case tFLOAT: a = make_AST_name("float"); break;
case tSTRING: a = make_AST_name("string"); break;
case ID:
text = strdup(t->text);
idx = lookup_SYM_all(text);
if (idx != 0){
a = make_AST_name(text); break;
}
default: parse_error("expected primtype, struct or a class type"); break;
}
gettoken();
return a;
}
static AST rettype(){
Token *t = &tok;
AST a=0;
switch (t->sym) {
case tVOID: a = make_AST_name("void"); break;
default: return vartype();
}
gettoken();
return a;
}
static AST argdecls() {
Token *t = &tok;
AST a=0;
AST a1=0;
a = new_list(nARGDECLS);
while (true) {
if (t->sym == ')') break;
if ( !isprimtype(t->sym) ) break;
a1 = argdecl();
if(a1) a = append_list(a, a1);
if (t->sym == ',') gettoken();
}
return a;
}
static AST argdecl() {
Token *t = &tok;
AST a=0;
AST a1=0,a2=0;
int idx;
a2 = typedecl();
a1 = var();
a = make_AST_argdecl(a1, a2);
idx = insert_SYM(get_text(a1), a2, vARG, a1);
set_ival(a1,idx);
return a;
}
static AST block() {
Token *t = &tok;
AST a=0;
AST vdl, fdl, sts;
vdl = new_list(nVARDECLS);
fdl = new_list(nFUNCDECLS);
if(t->sym == '{') {
gettoken();
enter_block();
vardecls(vdl,fdl);
funcdecls(fdl);
sts = stmts();
a = make_AST(nBLOCK, vdl, fdl, sts, 0);
leave_block();
if (t->sym == '}') gettoken();
else parse_error("expected }");
} else {
parse_error("expected {");
}
return a;
}
static AST stmts() {
Token *t = &tok;
AST a=0;
AST a1=0, a2=0 ;
a = new_list(nSTMTS);
while (true) {
switch(t->sym) {
case ID: case tIF: case tRETURN: case '{':
break;
default:
return a;
}
a1 = stmt();
a = append_list(a, a1);
}
return a;
}
static AST stmt() {
Token *t = &tok;
AST a=0;
AST a1=0;
AST n;
AST type;
switch (t->sym) {
case ID: /* ASN or CALL */
n = vName();
char *s = get_text(n);
if (t->sym == ASNOP || t->sym == '[') {
int idx = lookup_SYM_all(s);
if (idx == 0) parse_error("Undefined symbol");
a1 = asnstmt(n);
if (t->sym == ';') gettoken();
} else if (t->sym == '(') {
a1 = callstmt(n);
AST args = 0;
get_sons(a1, 0, 0, &args, 0);
if (!checkFuncExistAll(s, args)) parse_error("No defined functions matches types of passed arguments");
if (t->sym == ';') gettoken();
} else if (t->sym == '.'){
gettoken();
type = typeof_AST(n);
if (nodetype(type) == tCLASS){
AST function_name = vName();
a1 = callstmt(function_name);
AST args = 0;
get_sons(a1, 0, 0, &args, 0);
/*set_sons(a1, n, function_name, args, 0);*/
if (!checkFuncExistClass(type, get_text(function_name), args)) parse_error("No defined functions of the class matches types of passed arguments");
if (t->sym == ';') gettoken();
} else {
parse_error("Symbol must be instance of a class");
skiptoken(';');
}
}else {
parse_error("expected ASNOP or Funcall");
skiptoken(';');
}
break;
case tIF:
a1 = ifstmt();
break;
case tRETURN:
a1 = returnstmt();
if (t->sym == ';') gettoken();
break;
case '{':
a1 = block();
break;
default:
break;
}
a = make_AST(nSTMT,a1,0,0,0);
return a;
}
static AST asnstmt(AST name) {
Token *t = &tok;
AST a=0;
AST a1=0,a2=0;
int idx,op;
switch (t->sym) {
case ASNOP: /* change name to VREF */
set_nodetype(name, nVREF);
idx = lookup_SYM_all(get_text(name));
set_ival(name,idx);
a1 = name;
break;
case '[': /* change name to LVAL */
set_nodetype(name, nVREF);
a2 = exprs();
a1 = make_AST(nLVAL, name, a2, 0, 0) ;
break;
default:
break;
}
op = t->ival;
gettoken();
a2 = expr();
a = make_AST_asn(op,a1,a2);
return a;
}
static AST ifstmt() {
Token *t = &tok;
AST a=0;
AST a1=0,a2=0,a3=0;
if (t->sym == tIF) {
gettoken();
if (t->sym == '(') {
gettoken();
a1 = bexpr();
if (t->sym == ')') gettoken();
else parse_error("expected )");
a2 = stmt();
if (t->sym == tELSE) { /* else is optional */
gettoken();
a3 = stmt();
}
} else {
parse_error("expected (");
}
a = make_AST_if(a1,a2,a3);
} else {
parse_error("expected if");
skiptoken(';');
}
return a;
}
static AST callstmt(AST name) {
Token *t = &tok;
AST a=0;
AST a1=0,a2=0;
if (t->sym == '(') gettoken();
else parse_error("expected (");
a2 = argrefs();
if (t->sym == ')') gettoken();
else parse_error("expected (");
a = make_AST(nCALL, 0, name, a2, 0);
return a;
}
static AST argrefs() {
Token *t = &tok;
AST a,a1=0;
a = new_list(nARGS);
while (true) {
if (t->sym == ')') break;
a1 = argref();
if (a1) a = append_list(a,a1);
if (t->sym == ',') gettoken();
}
return a;
}
static AST argref() {
Token *t = &tok;
AST a,a1=0;
a1 = expr();
a = make_AST(nARG, a1, 0, 0, 0);
return a;
}
static AST returnstmt() {
Token *t = &tok;
AST a,a1=0;
gettoken();
a1 = expr();
a = make_AST(nRET, a1, 0, 0, 0);
return a;
}
static AST var() {
Token *t = &tok;
AST a=0;
int idx;
if (t->sym == ID) {
char *s = strdup(t->text);
gettoken();
/* ival may be changed later */
a = make_AST_var(s,0);
} else {
parse_error("expected ID");
}
return a;
}
static AST vref() {
Token *t = &tok;
AST a=0;
int idx;
if (t->sym == ID) {
char *s = strdup(t->text);
gettoken();
idx = lookup_SYM_all(s);
if (idx == 0) parse_error("undefined variable");
a = make_AST_vref(s,idx);
} else {
parse_error("expected ID");
}
return a;
}
static AST lval() {
Token *t = &tok;
AST a=0;
AST a1=0,a2=0;
a1 = vref();
if (t->sym == '[') {
a2 = exprs();
a = make_AST(nLVAL, a1, a2, 0, 0);
} else
a = a1;
return a;
}
static AST name() {
Token *t = &tok;
AST a=0;
if (t->sym == ID) {
char *s = strdup(t->text);
gettoken();
a = make_AST_name(s);
} else {
parse_error("expected ID");
}
return a;
}
static AST structname() {
Token *t = &tok;
AST a=0;
if (t->sym == ID) {
char *s = strdup(t->text);
int idx = lookup_SYM(s);
if (idx != 0) {
parse_error("Already used symbol");
return a;
}
gettoken();
a = make_AST_struct(s);
} else {
parse_error("expected ID");
}
return a;
}
static AST classname(){
Token *t = &tok;
AST a=0;
if (t->sym == ID) {
char *s = strdup(t->text);
int idx = lookup_SYM(s);
if (idx != 0) {
parse_error("Already used symbol");
return a;
}
gettoken();
a = make_AST_class(s);
} else {
parse_error("expected ID");
}
return a;
}
static AST vName(){
Token *t = &tok;
AST a=0;
if (t->sym == ID) {
char *s = strdup(t->text);
gettoken();
a = make_AST_name(s);
} else {
parse_error("expected ID");
}
return a;
}
static AST con() {
Token *t = &tok;
int v;
int idx;
char *p;
char *s;
AST ty=0;
AST a=0;
v = t->ival;
p = gen(cLOCAL);
s = insert_STR(t->text);
if (t->sym >= ILIT && t->sym <= SLIT)
ty = (t->sym - ILIT) +1;
switch (t->sym) {
case ILIT: case CLIT: /* int, char */
gettoken();
idx = insert_SYM(p, 0, cLOCAL, v); /* immediate */
a = make_AST_con(p,idx);
break;
case FLIT: case SLIT: /* float, string */
gettoken();
idx = insert_SYM(p, ty, cLOCAL, get_STR_offset(s));
a = make_AST_con(p,idx);
break;
default:
parse_error("expected LIT");
break;
}
set_typeofnode(a,ty);
return a;
}
static AST exprs() {
Token *t = &tok;
AST a,a1=0;
a = new_list(nEXPRS);
while (true) {
if (t->sym != '[') break;
gettoken();
a1 = expr();
if (a1) a = append_list(a,a1);
if (t->sym == ']') gettoken();
else parse_error("expected ]");
}
return a;
}
/* simplest version */
static AST expr() {
Token *t = &tok;
switch (t->sym) {
case ID: return lval();
case ILIT: return con();
default: return 0;
}
}
/* dummy */
static AST bexpr() {
Token *t = &tok;
char *s = strdup(t->text);
switch (t->sym) {
case tTRUE:
gettoken();
return make_AST_con(s,1);
case tFALSE:
gettoken();
return make_AST_con(s,0);
default:
return 0;
}
}