-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspiveyt.y
More file actions
2599 lines (2359 loc) · 85.7 KB
/
spiveyt.y
File metadata and controls
2599 lines (2359 loc) · 85.7 KB
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
/*
minir.y
flex minir.l
bison minir.y
g++ minir.tab.c -o parser
./parser < inputFileName
*/
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <string>
#include <stack>
#include "SymbolTable.h"
#include <math.h>
#include <cmath>
using namespace std;
#define ARITHMETIC_OP 1
#define LOGICAL_OP 2
#define RELATIONAL_OP 3
#define MOD 4
#define DIV 5
#define MUL 6
#define ADD 7
#define SUB 8
#define POW 9
#define LESS 10
#define GREATER 11
#define LEQ 12
#define GEQ 13
#define NEQ 14
#define EQL 15
#define OR 16
#define AND 17
#define ERR_CANNOT_BE_FUNCT_NULL_LIST_OR_STR 0
#define ERR_CANNOT_BE_FUNCT 1
#define ERR_CANNOT_BE_FUNCT_OR_NULL 2
#define ERR_CANNOT_BE_FUNCT_OR_NULL_OR_LIST 3
#define ERR_CANNOT_BE_LIST 4
#define ERR_MUST_BE_LIST 5
#define ERR_MUST_BE_FUNCT 6
#define ERR_MUST_BE_INTEGER 7
#define ERR_MUST_BE_INT_FLOAT_OR_BOOL 8
#define ERR_TOO_FEW_PARAMS 9
#define ERR_TOO_MANY_PARAMS 10
#define ERR_NON_INT_FUNCT_PARAM 11
#define ERR_MULTIPLY_DEFINED_IDENT 12
#define ERR_UNDEFINED_IDENT 13
#define ERR_ERROR 14
const int NUM_ERR_MESSAGES = 15; // should be ERR_ERROR + 1
const string ERR_MSG[NUM_ERR_MESSAGES] = {
"cannot be function or null or list or string",
"cannot be function",
"cannot be function or null",
"cannot be function or null or list",
"cannot be list",
"must be list",
"must be function",
"must be integer",
"must be integer or float or bool",
"Too few parameters in function call",
"Too many parameters in function call",
"Function parameters must be integer",
"Multiply defined identifier",
"Undefined identifier",
"<undefined error>"
};
// constant to suppress token printing
const bool suppressTokenOutput = true;
int line_num = 1;
int numExprs = 0;
stack<SYMBOL_TABLE> scopeStack; // stack of scope hashtables
bool isIntOrFloatOrBoolCompatible(const int theType);
bool isIntCompatible(const int theType);
bool isBoolCompatible(const int theType);
bool isFloatCompatible(const int theType);
bool isListCompatible(const int theType);
bool isInvalidOperandType(const int theType);
bool isArith(const int theType);
bool isRel(const int theType);
bool isLog(const int theType);
void beginScope();
void endScope();
void cleanUp();
void printValue(TYPE_INFO type_info);
TYPE_INFO findEntryInAnyScope(const string the_name);
void semanticError(const int argNum, const int errNum);
void printTokenInfo(const char* token_type, const char* lexeme);
void printRule(const char *, const char *);
int yyerror(const char *s)
{
printf("Line %d: %s\n", line_num, s);
cleanUp();
exit(1);
}
extern "C"
{
int yyparse(void);
int yylex(void);
int yywrap() { return 1; }
}
%}
%union {
char* text;
int num;
bool flag;
TYPE_INFO typeInfo;
};
%token T_IDENT T_INTCONST T_FLOATCONST T_UNKNOWN T_STRCONST
%token T_IF T_ELSE
%token T_WHILE T_FUNCTION T_FOR T_IN T_NEXT T_BREAK
%token T_TRUE T_FALSE T_QUIT
%token T_PRINT T_CAT T_READ T_LPAREN T_RPAREN T_LBRACE
%token T_RBRACE T_LBRACKET
%token T_RBRACKET T_SEMICOLON T_COMMA T_ADD T_SUB
%token T_MULT T_DIV T_MOD
%token T_POW T_LT T_LE T_GT T_GE T_EQ T_NE T_NOT T_AND
%token T_OR T_ASSIGN T_LIST
%type <text> T_IDENT T_STRCONST T_INTCONST T_FLOATCONST T_TRUE T_FALSE
%type <typeInfo> N_EXPR N_IF_EXPR N_THEN_EXPR N_COND_IF
%type <typeInfo> N_COMPOUND_EXPR N_ARITHLOGIC_EXPR
%type <typeInfo> N_ASSIGNMENT_EXPR N_FOR_EXPR N_WHILE_EXPR
%type <typeInfo> N_INPUT_EXPR N_OUTPUT_EXPR N_LIST_EXPR
%type <typeInfo> N_FUNCTION_DEF N_FUNCTION_CALL
%type <typeInfo> N_QUIT_EXPR N_CONST N_EXPR_LIST
%type <typeInfo> N_SIMPLE_ARITHLOGIC N_TERM N_ADD_OP_LIST
%type <typeInfo> N_FACTOR N_MULT_OP_LIST N_VAR
%type <typeInfo> N_SINGLE_ELEMENT N_ENTIRE_VAR N_CONST_LIST N_INDEX
%type <num> N_REL_OP N_ADD_OP N_MULT_OP
%type <num> N_ARG_LIST N_ARGS
/*
* To eliminate ambiguity in if/else
*/
%nonassoc T_RPAREN
%nonassoc T_ELSE
%start N_START
%%
N_START : N_EXPR
{
printRule("START", "EXPR");
printf("\n---- Completed parsing ----\n");
printf("\nValue of the expression is: ");
printValue($1);
return 0;
}
;
N_EXPR : N_IF_EXPR
{
printRule("EXPR", "IF_EXPR");
$$.type = $1.type;
$$.numParams = $1.numParams;
$$.returnType = $1.returnType;
$$.is_param = $1.is_param;
}
| N_WHILE_EXPR
{
printRule("EXPR", "WHILE_EXPR");
$$.type = $1.type;
$$.numParams = $1.numParams;
$$.returnType = $1.returnType;
$$.is_param = $1.is_param;
}
| N_FOR_EXPR
{
printRule("EXPR", "FOR_EXPR");
$$.type = $1.type;
$$.numParams = $1.numParams;
$$.returnType = $1.returnType;
$$.is_param = $1.is_param;
}
| N_COMPOUND_EXPR
{
printRule("EXPR", "COMPOUND_EXPR");
$$.type = $1.type;
$$.numParams = $1.numParams;
$$.returnType = $1.returnType;
$$.is_param = $1.is_param;
$$.val_bool = $1.val_bool;
$$.val_int = $1.val_int;
$$.val_float = $1.val_float;
strcpy($$.val_string, $1.val_string);
$$.is_null= $1.is_null;
$$.tlist = new Trial;
Trial *temp = $1.tlist;
Trial *newT = $$.tlist;
while(temp!=NULL)
{
newT->type = temp->type;
newT->val_bool = temp->val_bool;
newT->val_int = temp->val_int;
newT->val_float = temp->val_float;
strcpy(newT->val_string, temp->val_string);
newT->length = temp->length;
temp = temp->tlist;
if(temp!=NULL)
newT->tlist = new Trial;
else
newT->tlist = NULL;
newT = newT->tlist;
}
}
| N_ARITHLOGIC_EXPR
{
printRule("EXPR", "ARITHLOGIC_EXPR");
$$.type = $1.type;
$$.numParams = $1.numParams;
$$.returnType = $1.returnType;
$$.is_param = $1.is_param;
$$.val_bool = $1.val_bool;
$$.val_int = $1.val_int;
$$.val_float = $1.val_float;
strcpy($$.val_string, $1.val_string);
$$.is_null= $1.is_null;
}
| N_ASSIGNMENT_EXPR
{
printRule("EXPR", "ASSIGNMENT_EXPR");
$$.type = $1.type;
$$.numParams = $1.numParams;
$$.returnType = $1.returnType;
$$.is_param = $1.is_param;
$$.val_bool = $1.val_bool;
$$.val_int = $1.val_int;
$$.val_float = $1.val_float;
strcpy($$.val_string, $1.val_string);
$$.is_null= $1.is_null;
$$.tlist = new Trial;
Trial *temp = $1.tlist;
Trial *newT = $$.tlist;
while(temp!=NULL)
{
newT->type = temp->type;
newT->val_bool = temp->val_bool;
newT->val_int = temp->val_int;
newT->val_float = temp->val_float;
strcpy(newT->val_string, temp->val_string);
newT->length = temp->length;
temp = temp->tlist;
if(temp!=NULL)
newT->tlist = new Trial;
else
newT->tlist = NULL;
newT = newT->tlist;
}
}
| N_OUTPUT_EXPR
{
printRule("EXPR", "OUTPUT_EXPR");
$$.type = $1.type;
$$.numParams = $1.numParams;
$$.returnType = $1.returnType;
$$.is_param = $1.is_param;
$$.type = $1.type;
$$.numParams = $1.numParams;
$$.returnType = $1.returnType;
$$.is_param = $1.is_param;
$$.val_bool = $1.val_bool;
$$.val_int = $1.val_int;
$$.val_float = $1.val_float;
strcpy($$.val_string, $1.val_string);
$$.is_null= $1.is_null;
$$.tlist = new Trial;
Trial *temp = $1.tlist;
Trial *newT = $$.tlist;
while(temp!=NULL)
{
newT->type = temp->type;
newT->val_bool = temp->val_bool;
newT->val_int = temp->val_int;
newT->val_float = temp->val_float;
strcpy(newT->val_string, temp->val_string);
newT->length = temp->length;
temp = temp->tlist;
if(temp!=NULL)
newT->tlist = new Trial;
else
newT->tlist = NULL;
newT = newT->tlist;
}
}
| N_INPUT_EXPR
{
printRule("EXPR", "INPUT_EXPR");
$$.type = $1.type;
$$.numParams = $1.numParams;
$$.returnType = $1.returnType;
$$.is_param = $1.is_param;
}
| N_LIST_EXPR
{
printRule("EXPR", "LIST_EXPR");
$$.type = $1.type;
$$.numParams = $1.numParams;
$$.returnType = $1.returnType;
$$.is_param = $1.is_param;
}
| N_FUNCTION_DEF
{
printRule("EXPR", "FUNCTION_DEF");
$$.type = $1.type;
$$.numParams = $1.numParams;
$$.returnType = $1.returnType;
$$.is_param = $1.is_param;
}
| N_FUNCTION_CALL
{
printRule("EXPR", "FUNCTION_CALL");
$$.type = $1.type;
$$.numParams = $1.numParams;
$$.returnType = $1.returnType;
$$.is_param = $1.is_param;
}
| N_QUIT_EXPR
{
printRule("EXPR", "QUIT_EXPR");
$$.type = $1.type;
$$.numParams = NOT_APPLICABLE;
$$.returnType = NOT_APPLICABLE;
$$.is_param = false;
exit(1);
}
;
N_CONST : T_INTCONST
{
printRule("CONST", "INTCONST");
$$.type = INT;
$$.numParams = NOT_APPLICABLE;
$$.returnType = NOT_APPLICABLE;
$$.is_param = false;
$$.val_int = atoi($1);
}
| T_STRCONST
{
printRule("CONST", "STRCONST");
$$.type = STR;
$$.numParams = NOT_APPLICABLE;
$$.returnType = NOT_APPLICABLE;
$$.is_param = false;
strcpy($$.val_string, $1);
}
| T_FLOATCONST
{
printRule("CONST", "FLOATCONST");
$$.type = FLOAT;
$$.numParams = NOT_APPLICABLE;
$$.returnType = NOT_APPLICABLE;
$$.is_param = false;
$$.val_float = atof($1);
}
| T_TRUE
{
printRule("CONST", "TRUE");
$$.type = BOOL;
$$.numParams = NOT_APPLICABLE;
$$.returnType = NOT_APPLICABLE;
$$.is_param = false;
$$.val_bool = true;
}
| T_FALSE
{
printRule("CONST", "FALSE");
$$.type = BOOL;
$$.numParams = NOT_APPLICABLE;
$$.returnType = NOT_APPLICABLE;
$$.is_param = false;
$$.val_bool = false;
}
;
N_COMPOUND_EXPR : T_LBRACE N_EXPR N_EXPR_LIST T_RBRACE
{
printRule("COMPOUND_EXPR",
"{ EXPR EXPR_LIST }");
if ($3.type == NOT_APPLICABLE)
{
$$.type = $2.type;
$$.numParams = $2.numParams;
$$.returnType = $2.returnType;
$$.is_param = $2.is_param;
$$.val_bool = $2.val_bool;
$$.val_int = $2.val_int;
$$.val_float = $2.val_float;
strcpy($$.val_string, $2.val_string);
$$.is_null= $2.is_null;
$$.tlist = new Trial;
Trial *temp = $2.tlist;
Trial *newT = $$.tlist;
while(temp!=NULL)
{
newT->type = temp->type;
newT->val_bool = temp->val_bool;
newT->val_int = temp->val_int;
newT->val_float = temp->val_float;
strcpy(newT->val_string, temp->val_string);
newT->length = temp->length;
temp = temp->tlist;
if(temp!=NULL)
newT->tlist = new Trial;
else
newT->tlist = NULL;
newT = newT->tlist;
}
}
else
{
$$.type = $3.type;
$$.numParams = $3.numParams;
$$.returnType = $3.returnType;
$$.is_param = $3.is_param;
$$.val_bool = $3.val_bool;
$$.val_int = $3.val_int;
$$.val_float = $3.val_float;
strcpy($$.val_string, $3.val_string);
$$.is_null= $3.is_null;
$$.tlist = new Trial;
Trial *temp = $3.tlist;
Trial *newT = $$.tlist;
while(temp!=NULL)
{
newT->type = temp->type;
newT->val_bool = temp->val_bool;
newT->val_int = temp->val_int;
newT->val_float = temp->val_float;
strcpy(newT->val_string, temp->val_string);
newT->length = temp->length;
temp = temp->tlist;
if(temp!=NULL)
newT->tlist = new Trial;
else
newT->tlist = NULL;
newT = newT->tlist;
}
}
}
;
N_EXPR_LIST : T_SEMICOLON N_EXPR N_EXPR_LIST
{
printRule("EXPR_LIST", "; EXPR EXPR_LIST");
if ($3.type == NOT_APPLICABLE)
{
$$.type = $2.type;
$$.numParams = $2.numParams;
$$.returnType = $2.returnType;
$$.is_param = $2.is_param;
$$.val_bool = $2.val_bool;
$$.val_int = $2.val_int;
$$.val_float = $2.val_float;
strcpy($$.val_string, $2.val_string);
$$.is_null= $2.is_null;
$$.tlist = new Trial;
Trial *temp = $2.tlist;
Trial *newT = $$.tlist;
while(temp!=NULL)
{
newT->type = temp->type;
newT->val_bool = temp->val_bool;
newT->val_int = temp->val_int;
newT->val_float = temp->val_float;
strcpy(newT->val_string, temp->val_string);
newT->length = temp->length;
temp = temp->tlist;
if(temp!=NULL)
newT->tlist = new Trial;
else
newT->tlist = NULL;
newT = newT->tlist;
}
}
else
{
$$.type = $3.type;
$$.numParams = $3.numParams;
$$.returnType = $3.returnType;
$$.is_param = $3.is_param;
$$.val_bool = $3.val_bool;
$$.val_int = $3.val_int;
$$.val_float = $3.val_float;
strcpy($$.val_string, $3.val_string);
$$.is_null= $3.is_null;
$$.tlist = new Trial;
Trial *temp = $3.tlist;
Trial *newT = $$.tlist;
while(temp!=NULL)
{
newT->type = temp->type;
newT->val_bool = temp->val_bool;
newT->val_int = temp->val_int;
newT->val_float = temp->val_float;
strcpy(newT->val_string, temp->val_string);
newT->length = temp->length;
temp = temp->tlist;
if(temp!=NULL)
newT->tlist = new Trial;
else
newT->tlist = NULL;
newT = newT->tlist;
}
}
}
| /* epsilon */
{
printRule("EXPR_LIST", "epsilon");
$$.type = NOT_APPLICABLE;
$$.numParams = NOT_APPLICABLE;
$$.returnType = NOT_APPLICABLE;
$$.is_param = false;
$$.is_null = true;
}
;
N_IF_EXPR : N_COND_IF T_RPAREN N_THEN_EXPR
{
printRule("IF_EXPR", "IF ( EXPR ) EXPR");
$$.numParams = NOT_APPLICABLE;
$$.returnType = NOT_APPLICABLE;
$$.is_param = $3.is_param;
if($1.val_bool || $1.val_int != 0 || $1.val_float != 0)
{
$$.type = $3.type;
$$.is_param = $3.is_param;
$$.val_bool = $3.val_bool;
$$.val_int = $3.val_int;
$$.val_float = $3.val_float;
strcpy($$.val_string, $3.val_string);
$$.is_null= $3.is_null;
}
else
{
$$.type = NULL_TYPE;
$$.is_null = true;
}
}
| N_COND_IF T_RPAREN N_THEN_EXPR T_ELSE N_EXPR
{
printRule("IF_EXPR",
"IF ( EXPR ) EXPR ELSE EXPR");
if($5.type == FUNCTION)
semanticError(3, ERR_CANNOT_BE_FUNCT);
$$.type = $3.type ^ $5.type;
$$.numParams = NOT_APPLICABLE;
$$.returnType = NOT_APPLICABLE;
$$.is_param = $3.is_param || $5.is_param;
if($1.val_bool || $1.val_int != 0 || $1.val_float != 0)
{
$$.type = $3.type;
$$.is_param = $3.is_param;
$$.val_bool = $3.val_bool;
$$.val_int = $3.val_int;
$$.val_float = $3.val_float;
strcpy($$.val_string, $3.val_string);
$$.is_null= $3.is_null;
}
else
{
$$.type = $5.type;
$$.is_param = $5.is_param;
$$.val_bool = $5.val_bool;
$$.val_int = $5.val_int;
$$.val_float = $5.val_float;
strcpy($$.val_string, $5.val_string);
$$.is_null= $5.is_null;
}
}
;
N_COND_IF : T_IF T_LPAREN N_EXPR
{
if(($3.type == FUNCTION)
|| ($3.type == LIST)
|| ($3.type == NULL_TYPE)
|| ($3.type == STR))
{
printValue($3);
semanticError(1,
ERR_CANNOT_BE_FUNCT_NULL_LIST_OR_STR);
}
$$.val_bool = $3.val_bool;
$$.val_int = $3.val_int;
$$.val_float = $3.val_float;
strcpy($$.val_string, $3.val_string);
$$.is_null= $3.is_null;
}
;
N_THEN_EXPR : N_EXPR
{
if($1.type == FUNCTION)
semanticError(2, ERR_CANNOT_BE_FUNCT);
$$.type = $1.type;
$$.numParams = NOT_APPLICABLE;
$$.returnType = NOT_APPLICABLE;
$$.is_param = $1.is_param;
}
;
N_WHILE_EXPR : T_WHILE T_LPAREN N_EXPR
{
printRule("WHILE_EXPR",
"WHILE ( EXPR ) EXPR");
if(($3.type == FUNCTION)
|| ($3.type == LIST)
|| ($3.type == NULL_TYPE)
|| ($3.type == STR))
semanticError(1,
ERR_CANNOT_BE_FUNCT_NULL_LIST_OR_STR);
}
T_RPAREN N_EXPR
{
$$.type = $6.type;
$$.numParams = $6.numParams;
$$.returnType = $6.returnType;
$$.is_param = $6.is_param;
$$.is_null = true;
}
;
N_FOR_EXPR : T_FOR T_LPAREN T_IDENT
{
printRule("FOR_EXPR",
"FOR ( IDENT IN EXPR ) EXPR");
string lexeme = string($3);
TYPE_INFO exprTypeInfo =
scopeStack.top().findEntry(lexeme);
if(exprTypeInfo.type == UNDEFINED)
{
if(!suppressTokenOutput)
printf("___Adding %s to symbol"
" table\n", $3);
scopeStack.top().addEntry(
SYMBOL_TABLE_ENTRY(lexeme,
{INT_OR_STR_OR_FLOAT_OR_BOOL,
NOT_APPLICABLE, NOT_APPLICABLE,
false}));
}
else
{
if((exprTypeInfo.type == FUNCTION) ||
(exprTypeInfo.type == NULL_TYPE) ||
(exprTypeInfo.type == LIST))
semanticError(1,
ERR_CANNOT_BE_FUNCT_OR_NULL_OR_LIST);
}
}
T_IN N_EXPR
{
if($6.type != LIST)
semanticError(2, ERR_MUST_BE_LIST);
}
T_RPAREN N_EXPR
{
$$.type = $9.type;
$$.numParams = $9.numParams;
$$.returnType = $9.returnType;
$$.is_param = $9.is_param;
$$.is_null = true;
}
;
N_LIST_EXPR : T_LIST T_LPAREN N_CONST_LIST T_RPAREN
{
printRule("LIST_EXPR",
"LIST ( CONST_LIST )");
$$.type = LIST;
$$.numParams = NOT_APPLICABLE;
$$.returnType = NOT_APPLICABLE;
$$.is_param = false;
$$.tlist = $3.tlist;
}
;
N_CONST_LIST : N_CONST T_COMMA N_CONST_LIST
{
printRule("CONST_LIST",
"CONST, CONST_LIST");
Trial *filler = NULL;
filler = new Trial;
filler->type = $1.type;
filler->val_bool = $1.val_bool;
filler->val_int = $1.val_int;
filler->val_float = $1.val_float;
strcpy(filler->val_string, $1.val_string);
filler->tlist = $3.tlist;
filler->length = $3.tlist->length+1;
$$.tlist = filler;
}
| N_CONST
{
Trial *filler = NULL;
filler = new Trial;
filler->type = $1.type;
filler->val_bool = $1.val_bool;
filler->val_int = $1.val_int;
filler->val_float = $1.val_float;
strcpy(filler->val_string, $1.val_string);
filler->tlist = NULL;
filler->length = 1;
$$.tlist = filler;
printRule("CONST_LIST", "CONST");
}
;
N_ASSIGNMENT_EXPR : T_IDENT N_INDEX
{
printRule("ASSIGNMENT_EXPR",
"IDENT INDEX ASSIGN EXPR");
string lexeme = string($1);
TYPE_INFO exprTypeInfo =
scopeStack.top().findEntry(lexeme);
if(exprTypeInfo.type == UNDEFINED)
{
if(!suppressTokenOutput)
printf("___Adding %s to symbol table\n",
$1);
// add in as N/A type until the
// N_EXPR can be processed below to
// get the correct type
scopeStack.top().addEntry(
SYMBOL_TABLE_ENTRY(lexeme,
{NOT_APPLICABLE, NOT_APPLICABLE,
NOT_APPLICABLE, false}));
// set flag that ident didn't already
// exist
$<flag>$ = false;
}
else
{
// set flag that ident already existed
$<flag>$ = true;
}
}
T_ASSIGN N_EXPR
{
string lexeme = string($1);
TYPE_INFO exprTypeInfo =
scopeStack.top().findEntry(lexeme);
if(($2.is_index) &&
(!isListCompatible(
exprTypeInfo.type)))
semanticError(1, ERR_MUST_BE_LIST);
if ($<flag>3) // ident already existed
{
if (exprTypeInfo.is_param && !isIntCompatible($5.type))
semanticError(1, ERR_MUST_BE_INTEGER);
if(!$2.is_index)
{
TYPE_INFO type_info;
type_info.type = $5.type;
type_info.numParams = $5.numParams;
type_info.returnType = $5.returnType;
type_info.is_param = $5.is_param;
type_info.val_bool = $5.val_bool;
type_info.val_int = $5.val_int;
type_info.val_float = $5.val_float;
strcpy(type_info.val_string, $5.val_string);
type_info.is_null= $5.is_null;
type_info.tlist = new Trial;
Trial *temp = $5.tlist;
Trial *newT = type_info.tlist;
while(temp!=NULL)
{
newT->type = temp->type;
newT->val_bool = temp->val_bool;
newT->val_int = temp->val_int;
newT->val_float = temp->val_float;
strcpy(newT->val_string, temp->val_string);
newT->length = temp->length;
temp = temp->tlist;
if(temp!=NULL)
newT->tlist = new Trial;
else
newT->tlist = NULL;
newT = newT->tlist;
}
scopeStack.top().changeEntry(
SYMBOL_TABLE_ENTRY(lexeme,
type_info));
}
else
{
if($2.val_int<1 || $2.val_int>exprTypeInfo.tlist->length)
{
yyerror("Subscript out of bounds");
}
//printValue(exprTypeInfo);
TYPE_INFO type_info;
type_info.type = exprTypeInfo.type;
type_info.numParams = exprTypeInfo.numParams;
type_info.returnType = exprTypeInfo.returnType;
type_info.is_param = exprTypeInfo.is_param;
type_info.val_bool = exprTypeInfo.val_bool;
type_info.val_int = exprTypeInfo.val_int;
type_info.val_float = exprTypeInfo.val_float;
strcpy(type_info.val_string, exprTypeInfo.val_string);
type_info.is_null= exprTypeInfo.is_null;
type_info.tlist = new Trial;
Trial *temp = exprTypeInfo.tlist;
Trial *newT = type_info.tlist;
while(temp!=NULL)
{
newT->type = temp->type;
newT->val_bool = temp->val_bool;
newT->val_int = temp->val_int;
newT->val_float = temp->val_float;
strcpy(newT->val_string, temp->val_string);
newT->length = temp->length;
temp = temp->tlist;
if(temp!=NULL)
newT->tlist = new Trial;
else
newT->tlist = NULL;
newT = newT->tlist;
}
int count = $2.val_int;
Trial* node = type_info.tlist;
while(count > 1)
{
node = node->tlist;
count -= 1;
}
node->type = $5.type;
node->val_bool = $5.val_bool;
node->val_int = $5.val_int;
node->val_float = $5.val_float;
strcpy(node->val_string, $5.val_string);
scopeStack.top().changeEntry(
SYMBOL_TABLE_ENTRY(lexeme,
type_info));
exprTypeInfo =
scopeStack.top().findEntry(lexeme);
}
}
else
{
TYPE_INFO type_info;
type_info.type = $5.type;
type_info.numParams = $5.numParams;
type_info.returnType = $5.returnType;
type_info.is_param = $5.is_param;
type_info.val_bool = $5.val_bool;
type_info.val_int = $5.val_int;
type_info.val_float = $5.val_float;
strcpy(type_info.val_string, $5.val_string);
type_info.is_null= $5.is_null;
type_info.tlist = new Trial;
Trial *temp = $5.tlist;
Trial *newT = type_info.tlist;
while(temp!=NULL)
{
newT->type = temp->type;
newT->val_bool = temp->val_bool;
newT->val_int = temp->val_int;
newT->val_float = temp->val_float;
strcpy(newT->val_string, temp->val_string);
newT->length = temp->length;
temp = temp->tlist;
if(temp!=NULL)
newT->tlist = new Trial;
else
newT->tlist = NULL;
newT = newT->tlist;
}
scopeStack.top().changeEntry(
SYMBOL_TABLE_ENTRY(lexeme,
type_info));
}
if (($2.is_index) &&
($5.type == LIST))
semanticError(1, ERR_CANNOT_BE_LIST);
$$.type = $5.type;
$$.numParams = $5.numParams;
$$.returnType = $5.returnType;
$$.is_param = $5.is_param;
$$.val_bool = $5.val_bool;
$$.val_int = $5.val_int;
$$.val_float = $5.val_float;
strcpy($$.val_string, $5.val_string);
$$.is_null= $5.is_null;
$$.tlist = new Trial;
Trial *temp = $5.tlist;
Trial *newT = $$.tlist;
while(temp!=NULL)
{
newT->type = temp->type;
newT->val_bool = temp->val_bool;
newT->val_int = temp->val_int;
newT->val_float = temp->val_float;
strcpy(newT->val_string, temp->val_string);
newT->length = temp->length;
temp = temp->tlist;
if(temp!=NULL)
newT->tlist = new Trial;
else
newT->tlist = NULL;
newT = newT->tlist;
}
}