-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.y
963 lines (850 loc) · 25.4 KB
/
parser.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
/* BEGIN_BANNER */
/*******************************************************************************************************
sclp : A C Language Processor
--------------------------------
About:
Originally implemented by Tanu Kanvar and Uday Khedker (http://www.cse.iitb.ac.in/~uday)
for the courses cs302+cs316 Language Processors (theory and lab) at IIT Bombay.
Initial release date Jan 15, 2013.
Updated by N. Venkatesh in Jan 2019. The main updates included
- Changing the input language from a basic-block-based GCC GIMPLE dump to a subset of C.
- Replacing the flexc++ and bisonc++ scripts by flex and bison.
- Supporting print operation for variables.
Updated by Uday Khedker with the help of Nisha Biju and Saari Rajan in Jan 2020. The main
updates include
- Introduction of three address code (TAC) phase between abstract syntax tree (AST)
and Register Transfer Language (RTL) which was originally called icode.
- Introduction of string data type with constant strings.
- Support for read operation for integer and floating point variables, and print
operation for all variables (including string variables) and string constants.
- Significant changes in command line switches for printing each representation (tokens,
AST, TAC, RTL, and assembly) as well as stopping after each phase (scanning, parsing,
AST construction (i.e. semantic analysis), TAC generation and RTL generation).
- Substantial clean up of the code including removal of AST to icode translation,
and interpretation (which was buggy and had not been tested thorougly after the front
end and the input langauge changed in 2019).
Copyrights reserved by Uday Khedker. This implemenation has been made available purely
for academic purposes without any warranty of any kind.
*******************************************************************************************************/
/* END_BANNER */
%{
extern "C" void yyerror(char *s);
extern int yylineno;
extern int yylex(void);
#include "common-classes.hh"
#include "error-display.hh"
#include "user-options.hh"
#include "symbol-table.hh"
#include "tac.hh"
#include "ast.hh"
#include "rtl.hh"
#include "procedure.hh"
#include "program.hh"
#include <iostream>
Procedure * current_procedure = NULL; /* This global variable is used through out the script */
int dummy_no=0;
%}
%union
{
int integer_value;
double double_value;
std::string * string_value;
list<Ast *> * ast_list;
Ast * ast;
Symbol_Table * symbol_table;
Symbol_Table_Entry * symbol_entry;
Procedure * procedure;
Data_Type type_name;
};
%token CLASS PRIVATE PUBLIC
%token <integer_value> INTEGER_NUMBER
%token <double_value> DOUBLE_NUMBER
%token <string_value> NAME
%token <string_value> STRING_CONSTANT
%token INTEGER FLOAT STRING BOOL
%token ASSIGN VOID
%token IF WHILE DO ELSE
%token WRITE READ
%token RETURN
%token AT
%right '?' ':'
%left OR
%left AND
%left NE EQ
%left LT LE GT GE
%left '+' '-'
%left '*' '/'
%right NOT
%right UMINUS
%nonassoc '(' ')'
%left ELSE
%type <symbol_table> global_decl_statement
%type <symbol_table> var_decl
%type <symbol_table> var_decl_list
%type <symbol_table> variables_list
%type <ast_list> statement_list
%type <ast> statement
%type <ast> assignment_statement
%type <ast> operand
%type <ast> variable
%type <ast> constant
%type <ast> arith_expression
%type <ast> expression_term
%type <ast> rel_expression
%type <ast> logical_expression
%type <ast> if_statement
%type <ast> do_while_statement
%type <ast> while_statement
%type <ast> compound_statement
%type <ast> print_statement
%type <ast> read_statement
%type <ast> call_statement
%type <ast> return_statement
%type <ast> func_call
%type <type_name> param_type
%type <type_name> type
%type <symbol_table> formal_param
%type <symbol_table> formal_param_list
%type <ast_list> non_empty_arg_list
%type <ast> actual_arg
%type <symbol_table> dummy_formal_param
%type <symbol_table> dummy_formal_param_list
%start program
%%
program
: global_decl_statement
func_def_list
{ if (command_options.construct_ast())
{
bool main_absent = !((program_object.get_main_procedure(cout) == NULL ||
(program_object.get_main_procedure(cout))->get_return_type() != void_data_type));
CHECK_INVARIANT(main_absent, "Main should be defined and it's type should be void")
}
}
| func_def_list
{ /* This action is only to suppress $$=$1 assignment which is
type incorrect in this case.
- Uday Tue Dec 31 17:54:52 IST 2019
*/
}
;
global_decl_statement
: global_decl_statement
func_decl
{ if (command_options.construct_ast())
$$=$1;
/* This action does not include the func_decl in the value of
global_decl_statement because the function names go in a
separate list in the program.
The Symbol_Table stores variables names only and not function
names because it is used to record the declarations inside a
procedure too.
- Uday Mon Dec 30 14:39:00 IST 2019
*/
}
| global_decl_statement
var_decl
{ if (command_options.construct_ast())
{
// if declaration is local then no need to check in global list
// if declaration is global then this list is global list
Symbol_Table * decl_list_1 = $1;
Symbol_Table * decl_list_2 = $2;
if(decl_list_1!=NULL && decl_list_2!=NULL)
{
decl_list_1->append_list(*decl_list_2,yylineno);
$$=decl_list_1;
}
else
{
if(decl_list_1!=NULL)
$$ = decl_list_1;
else if(decl_list_2!=NULL)
$$= decl_list_2;
else $$=NULL;
/* See the comment in the action of the first RHS above for the meaning of NULL */
}
Symbol_Table * global_table = $$;
if(global_table !=NULL)
program_object.set_global_table(*global_table);
}
}
| var_decl
{ if (command_options.construct_ast())
{
$$=$1;
Symbol_Table * global_table = $$;
if(global_table !=NULL)
program_object.set_global_table(*global_table);
}
}
| func_decl
{ if (command_options.construct_ast())
$$=NULL;
/* See the comment in the action of the first RHS above for the meaning of NULL */
}
| class_declaration_stat
{
}
;
class_declaration_stat: CLASS NAME '{' block '}'';'
;
block: access_specifier_type':'
global_decl_statement
;
access_specifier_type: PUBLIC
|PRIVATE
;
func_decl
: type NAME '(' dummy_formal_param_list ')' ';'
{ if (command_options.construct_ast())
{
CHECK_INVARIANT(($2 != NULL), "Procedure name cannot be null");
string proc_name = *$2;
Data_Type recent_type_name = $1;
CHECK_INPUT ((program_object.variable_in_symbol_list_check(proc_name) == false),
"Procedure name cannot be same as global variable", yylineno);
Procedure *pdecl = new Procedure(recent_type_name, proc_name, yylineno);
Symbol_Table * formal_param_table = $4;
pdecl->set_formal_param_list(*formal_param_table);
program_object.set_proc_to_map(proc_name,pdecl);
dummy_no=0;
}
}
| type NAME '('')' ';'
{ if (command_options.construct_ast())
{
CHECK_INVARIANT(($2 != NULL), "Procedure name cannot be null");
string proc_name = *$2;
Data_Type recent_type_name = $1;
Procedure *pdecl = new Procedure(recent_type_name, proc_name, yylineno);
program_object.set_proc_to_map(proc_name, pdecl);
}
}
| type NAME '(' formal_param_list ')' ';'
{ if (command_options.construct_ast())
{
CHECK_INVARIANT(($2 != NULL), "Procedure name cannot be null");
string proc_name = *$2;
Data_Type recent_type_name = $1;
Procedure *pdecl = new Procedure(recent_type_name, proc_name, yylineno);
Symbol_Table * formal_param_table = $4;
pdecl->set_formal_param_list(*formal_param_table);
program_object.set_proc_to_map(proc_name,pdecl);
}
}
;
dummy_formal_param_list
: dummy_formal_param_list ',' dummy_formal_param
{ if (command_options.construct_ast())
{
Symbol_Table * decl_list_1 = $1;
Symbol_Table * decl_list_2 = $3;
decl_list_1->append_list(*decl_list_2,yylineno);
$$=decl_list_1;
}
}
| dummy_formal_param
{ $$=$1; }
;
dummy_formal_param
: param_type
{ if (command_options.construct_ast())
{
dummy_no++;
string name = "dummy"+to_string(dummy_no);
Symbol_Table_Entry * decl_entry = new Symbol_Table_Entry(name, $1, yylineno);
Symbol_Table * decl_list = new Symbol_Table();
decl_list->push_symbol(decl_entry);
$$ = decl_list;
}
}
;
func_def_list
: func_def_list func_def
| func_def
;
func_def
: type NAME '(' formal_param_list')'
'{' '}'
| type NAME '(' formal_param_list')'
'{' var_decl_list
statement_list '}'
| type NAME '(' ')'
'{' '}'
| type NAME '(' ')'
'{' var_decl_list
statement_list '}'
| type NAME '(' formal_param_list')'
'{' var_decl_list '}'
| type NAME '(' ')'
'{' var_decl_list '}'
| type NAME '(' formal_param_list')'
'{' statement_list '}'
| type NAME '(' ')'
'{' statement_list '}'
;
formal_param_list
: formal_param_list ',' formal_param
{ if (command_options.construct_ast())
{
Symbol_Table * decl_list_1 = $1;
Symbol_Table * decl_list_2 = $3;
decl_list_1->append_list(*decl_list_2,yylineno);
$$=decl_list_1;
}
}
| formal_param
{ if (command_options.construct_ast())
{ $$ = $1;}
}
;
formal_param
: param_type NAME
{ if (command_options.construct_ast())
{
string name = *$2;
Symbol_Table_Entry * decl_entry = new Symbol_Table_Entry(name, $1, yylineno);
Symbol_Table * decl_list = new Symbol_Table();
decl_list->push_symbol(decl_entry);
$$ = decl_list;
}
}
;
param_type
: INTEGER
{ if (command_options.construct_ast())
{ $$ = int_data_type; }
}
| FLOAT
{ if (command_options.construct_ast())
{ $$ = double_data_type; }
}
| BOOL
{ if (command_options.construct_ast())
{ $$ = bool_data_type; }
}
;
statement_list
: statement_list statement
{ if(command_options.construct_ast())
{
list<Ast *> * temp_ast_list = $1;
Ast * stmt = $2;
list<Ast *> * temp_ast_list_new;
if (temp_ast_list == NULL)
temp_ast_list_new = new list<Ast *>;
else
temp_ast_list_new = temp_ast_list;
if(stmt!=NULL)
temp_ast_list_new->push_back(stmt);
$$ = temp_ast_list_new;
}
}
| statement
{
}
;
statement
: assignment_statement
{ if (command_options.construct_ast())
$$ = $1;
}
| if_statement
{ if (command_options.construct_ast())
$$ = $1;
}
| do_while_statement
{ if (command_options.construct_ast())
$$ = $1;
}
| while_statement
{ if (command_options.construct_ast())
$$ = $1;
}
| compound_statement
{ if (command_options.construct_ast())
$$ = $1;
}
| print_statement
{ if (command_options.construct_ast())
$$ = $1;
}
| read_statement
{ if (command_options.construct_ast())
$$ = $1;
}
| call_statement
{ if (command_options.construct_ast())
$$ = $1;
}
| return_statement
{ if (command_options.construct_ast())
$$ = $1;
}
;
call_statement
: func_call ';'
{ if (command_options.construct_ast())
$$=$1;
}
;
func_call
: NAME '(' non_empty_arg_list ')'
{
}
| NAME '(' ')'
{
}
;
non_empty_arg_list
: non_empty_arg_list ',' actual_arg
{ if (command_options.construct_ast())
{
list<Ast *> * arg_list = $1;
arg_list->push_back($3);
$$= arg_list;
}
}
| actual_arg
{ if (command_options.construct_ast())
{
list<Ast *> * arg_list = new list<Ast *>;
arg_list->push_back($1);
$$= arg_list;
}
}
;
actual_arg
: arith_expression
{ if (command_options.construct_ast())
$$= $1;
}
;
return_statement
: RETURN ';'
{ if (command_options.construct_ast())
{
Return_Ast* return_stmt = new Return_Ast(NULL, current_procedure->get_proc_name(), yylineno);
$$ = return_stmt;
}
}
| RETURN arith_expression ';'
{ if (command_options.construct_ast())
{
Return_Ast* return_stmt = new Return_Ast($2, current_procedure->get_proc_name(), yylineno);
$$ = return_stmt;
}
}
;
var_decl_list
: var_decl
{ if (command_options.construct_ast())
$$ = $1;
}
| var_decl_list var_decl
{ if (command_options.construct_ast())
{
// if declaration is local then no need to check in global list
// if declaration is global then this list is global list
Symbol_Table * decl_list_1 = $1;
Symbol_Table * decl_list_2 = $2;
if(decl_list_1!=NULL && decl_list_2!=NULL)
{
decl_list_1->append_list(*decl_list_2,yylineno);
$$=decl_list_1;
}
else
{
if(decl_list_1!=NULL)
$$ = decl_list_1;
else if(decl_list_2!=NULL)
$$= decl_list_2;
else $$=NULL;
}
}
}
;
var_decl
: type variables_list ';'
{ if (command_options.construct_ast())
{
Data_Type recent_type_name = $1;
Symbol_Table * decl_list = $2;
CHECK_INVARIANT (decl_list != NULL, "List of variables is NULL in type declarations");
decl_list->set_type_of_all_syms(recent_type_name);
$$ = decl_list;
}
}
;
type
: INTEGER
{ if (command_options.construct_ast())
$$ = int_data_type;
}
| FLOAT
{ if (command_options.construct_ast())
$$ = double_data_type;
}
| VOID
{ if (command_options.construct_ast())
$$ = void_data_type;
}
| STRING
{ if (command_options.construct_ast())
$$ = string_data_type;
}
| BOOL
{ if (command_options.construct_ast())
$$ = bool_data_type;
}
| NAME
{
}
;
variables_list
: NAME
{ if (command_options.construct_ast())
{
string name = *$1;
Symbol_Table_Entry * decl_entry = new Symbol_Table_Entry(name, yylineno);
Symbol_Table * decl_list = new Symbol_Table();
decl_list->push_symbol(decl_entry);
$$=decl_list;
}
}
| variables_list ',' NAME
{ if (command_options.construct_ast())
{
string name = *$3;
Symbol_Table * decl_list = $1;
CHECK_INPUT((decl_list->variable_in_symbol_list_check(name) == false),
"Variable is declared twice", yylineno);
Symbol_Table_Entry * decl_entry = new Symbol_Table_Entry(name, yylineno);
decl_list->push_symbol(decl_entry);
$$=decl_list;
}
}
;
assignment_statement
: variable ASSIGN arith_expression ';'
{ if (command_options.construct_ast())
{
CHECK_INVARIANT(($1 && $3), "Lhs/Rhs of assignment expression cannot be null");
$$ = new Assignment_Ast($1, $3, yylineno);
$$->type_check_ast();
}
}
| variable ASSIGN func_call ';'
{ if (command_options.construct_ast())
{
CHECK_INVARIANT(($1 && $3), "Lhs/Rhs of assignment expression cannot be null");
$$ = new Assignment_Ast($1, $3, yylineno);
$$->type_check_ast();
}
}
| variable ASSIGN logical_expression ';'
{ if (command_options.construct_ast())
{
CHECK_INVARIANT(($1 && $3), "Lhs/Rhs of assignment expression cannot be null");
$$ = new Assignment_Ast($1, $3, yylineno);
$$->type_check_ast();
}
}
;
if_statement
: IF '(' logical_expression ')' statement ELSE statement
{ if (command_options.construct_ast())
$$ = new Selection_Statement_Ast($3,$5,$7,yylineno);
}
| IF '(' logical_expression ')' statement
{ if (command_options.construct_ast())
$$ = new Selection_Statement_Ast($3,$5,NULL,yylineno);
}
;
do_while_statement
: DO statement WHILE '(' logical_expression ')' ';'
{ if (command_options.construct_ast())
$$ = new Iteration_Statement_Ast($5,$2,yylineno,true);
}
;
while_statement
: WHILE '(' logical_expression ')' statement
{ if (command_options.construct_ast())
$$ = new Iteration_Statement_Ast($3,$5,yylineno,false);
}
;
compound_statement
: '{' statement_list '}'
{
}
| '{' '}'
{
}
;
print_statement
: WRITE variable ';'
{ if (command_options.construct_ast())
{
CHECK_INVARIANT($2 , "A variable cannot be null in a print statement");
$$ = new Write_Ast($2,yylineno);
}
}
| WRITE STRING_CONSTANT ';'
{ if (command_options.construct_ast())
{
string s = *$2;
int str_num = program_object.add_string_to_string_table(s);
String_Constant_Ast * str_ast = new String_Constant_Ast(str_num, string_data_type, yylineno);
$$ = new Write_Ast(str_ast,yylineno);
}
}
;
read_statement
: READ variable ';'
{ if (command_options.construct_ast())
{
CHECK_INVARIANT($2 , "A variable cannot be null in a read statement");
$$ = new Read_Ast($2,yylineno);
}
}
;
arith_expression
: operand '+' operand
{ if (command_options.construct_ast())
{
CHECK_INVARIANT(($1 && $3), "An arith_operand cannot have null entries");
$$ = new Plus_Ast($1, $3, yylineno);
$$->set_data_type($1->get_data_type());
$$->type_check_ast();
}
}
| operand '-' operand
{ if (command_options.construct_ast())
{
CHECK_INVARIANT(($1 && $3), "An arith_operand cannot have null entries");
$$ = new Minus_Ast($1, $3, yylineno);
$$->set_data_type($1->get_data_type());
$$->type_check_ast();
}
}
| operand '*' operand
{ if (command_options.construct_ast())
{
CHECK_INVARIANT(($1 && $3), "An arith_operand cannot have null entries");
$$ = new Mult_Ast($1, $3, yylineno);
$$->set_data_type($1->get_data_type());
$$->type_check_ast();
}
}
| operand '/' operand
{ if (command_options.construct_ast())
{
CHECK_INVARIANT(($1 && $3), "An arith_operand cannot have null entries");
$$ = new Divide_Ast($1, $3, yylineno);
$$->set_data_type($1->get_data_type());
$$->type_check_ast();
}
}
| '-' operand %prec UMINUS
{ if (command_options.construct_ast())
{
CHECK_INVARIANT($2, "An arith_operand cannot have null entries");
$$ = new UMinus_Ast($2, NULL, yylineno);
$$->set_data_type($2->get_data_type());
$$->type_check_ast();
}
}
| '(' operand ')'
{ if (command_options.construct_ast())
{
$$ = $2;
$$->set_data_type($2->get_data_type());
}
}
| logical_expression '?' operand ':' operand
{ if (command_options.construct_ast())
{
CHECK_INVARIANT(($1 != NULL) && ($3 != NULL) && ($5 != NULL), "lhs/rhs of arithmetic expression cannot be null");
Ast * cond = $1;
Ast * lhs = $3;
Ast * rhs = $5;
Ast * cond_ast = new Conditional_Expression_Ast(cond, lhs, rhs, yylineno);
cond_ast->type_check_ast();
$$ = cond_ast;
}
}
| expression_term
{ if (command_options.construct_ast())
{
CHECK_INVARIANT(($1 != NULL), "An expression cannot have null values");
$$ = $1;
$$->set_data_type($1->get_data_type());
}
}
;
operand
: arith_expression
{ if (command_options.construct_ast())
{
CHECK_INVARIANT(($1 != NULL), "Arithmetic expression cannot be null");
$$ = $1;
$$->set_data_type($1->get_data_type());
}
}
;
expression_term
: variable
{ if (command_options.construct_ast())
{
CHECK_INVARIANT(($1 != NULL), "An expression term cannot be null");
$$ = $1;
}
}
| constant
{ if (command_options.construct_ast())
{
CHECK_INVARIANT(($1 != NULL), "An expression term cannot be null");
$$ = $1;
}
}
;
rel_expression
: operand LT operand
{ if (command_options.construct_ast())
{
CHECK_INVARIANT(($1 && $3), "A rel_operand cannot have null entries");
$$ = new Relational_Expr_Ast($1, less_than, $3, yylineno);
$$->set_data_type(bool_data_type);
$$->type_check_ast();
}
}
| operand LE operand
{ if (command_options.construct_ast())
{
CHECK_INVARIANT(($1 && $3), "A rel_operand cannot have null entries");
$$ = new Relational_Expr_Ast($1, less_equalto, $3, yylineno);
$$->set_data_type(bool_data_type);
$$->type_check_ast();
}
}
| operand GT operand
{ if (command_options.construct_ast())
{
CHECK_INVARIANT(($1 && $3), "A rel_operand cannot have null entries");
$$ = new Relational_Expr_Ast($1, greater_than, $3, yylineno);
$$->set_data_type(bool_data_type);
$$->type_check_ast();
}
}
| operand GE operand
{ if (command_options.construct_ast())
{
CHECK_INVARIANT(($1 && $3), "A rel_operand cannot have null entries");
$$ = new Relational_Expr_Ast($1, greater_equalto, $3, yylineno);
$$->set_data_type(bool_data_type);
$$->type_check_ast();
}
}
| operand NE operand
{ if (command_options.construct_ast())
{
CHECK_INVARIANT(($1 && $3), "A rel_operand cannot have null entries");
$$ = new Relational_Expr_Ast($1, not_equalto, $3, yylineno);
$$->set_data_type(bool_data_type);
$$->type_check_ast();
}
}
| operand EQ operand
{ if (command_options.construct_ast())
{
CHECK_INVARIANT(($1 && $3), "A rel_operand cannot have null entries");
$$ = new Relational_Expr_Ast($1, equalto, $3, yylineno);
$$->set_data_type(bool_data_type);
$$->type_check_ast();
}
}
;
logical_expression
: logical_expression AND logical_expression
{ if (command_options.construct_ast())
{
CHECK_INVARIANT(($1 != NULL) && ($3 != NULL), "lhs/rhs of boolean expression cannot be null");
Logical_Op lop = _logical_and;
Logical_Expr_Ast * logical_expr_ast = new Logical_Expr_Ast($1, lop, $3, yylineno);
$$->set_data_type(bool_data_type);
$$ = logical_expr_ast;
$$->type_check_ast();
}
}
| logical_expression OR logical_expression
{ if (command_options.construct_ast())
{
CHECK_INVARIANT(($1 != NULL) && ($3 != NULL), "lhs/rhs of boolean expression cannot be null");
Logical_Op lop = _logical_or;
Logical_Expr_Ast * logical_expr_ast = new Logical_Expr_Ast($1, lop, $3, yylineno);
$$->set_data_type(bool_data_type);
$$ = logical_expr_ast;
$$->type_check_ast();
}
}
| NOT logical_expression
{ if (command_options.construct_ast())
{
CHECK_INVARIANT(($2 != NULL) , "rhs of boolean expression cannot be null");
Logical_Op lop = _logical_not;
Logical_Expr_Ast * logical_expr_ast = new Logical_Expr_Ast(NULL, lop, $2, yylineno);
$$->set_data_type(bool_data_type);
$$ = logical_expr_ast;
$$->type_check_ast();
}
}
| '(' logical_expression ')'
{ if (command_options.construct_ast())
$$ = $2;
}
| rel_expression
{ if (command_options.construct_ast())
$$ = $1;
}
| AT variable
{ if (command_options.construct_ast())
$$ = $2;
}
;
variable
: NAME
{ if (command_options.construct_ast())
{
Symbol_Table_Entry * var_table_entry;
CHECK_INVARIANT(($1 != NULL), "Variable name cannot be null");
if (current_procedure->variable_in_symbol_list_check(*$1))
var_table_entry = &(current_procedure->get_symbol_table_entry(*$1));
else if (current_procedure->variable_in_formal_list_check(*$1))
var_table_entry = &(current_procedure->get_formal_param_entry(*$1));
else if (program_object.variable_in_symbol_list_check(*$1))
var_table_entry = &(program_object.get_symbol_table_entry(*$1));
else
CHECK_INPUT_AND_ABORT(CONTROL_SHOULD_NOT_REACH, "Variable has not been declared", yylineno);
$$ = new Name_Ast(*$1, *var_table_entry, yylineno);
}
}
;
constant
: INTEGER_NUMBER
{ if (command_options.construct_ast())
$$ = new Number_Ast<int>($1, int_data_type, yylineno);
}
| DOUBLE_NUMBER
{ if (command_options.construct_ast())
$$ = new Number_Ast<double>($1, double_data_type, yylineno);
}
| STRING_CONSTANT
{ if (command_options.construct_ast())
{
string s = *$1;
int str_num = program_object.add_string_to_string_table(s);
$$ = new String_Constant_Ast(str_num, string_data_type, yylineno);
}
}
;