-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.yy
1326 lines (1282 loc) · 41.9 KB
/
parser.yy
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
%{
/*
* Project 3
* Author: B10630221 Chang-Ting Kao
* Date: 2019/06/05
*/
#include <iostream>
#include <map>
#include <vector>
#include <stack>
#include <string>
#include "driver.h"
#include "token_node.h"
#include "symbol_table.h"
#define Trace(s, t) { if (isVerbose) std::cout << "[YACC] "<< s << " -> " << t << std::endl; }
#define Warn(loc, s) { error(loc, std::string("WARNING: ") + (s)); (*warningCount)++; }
#define ValidateDuplicateIdentifier(loc, s, e) { if (localSymbolTable->HasKey(s)) { Warn(loc, "Duplicate identifier found at scope "+localSymbolTable->scope+": "+s); } }
#define ValidateEntryType(loc, t, et) { if (t != et) { Warn(loc, "Invalid entry type: "+EnumToString(t)+", Expect: "+EnumToString(et)); } }
#define ValidateValueType(loc, t, et) { if (t != et) { Warn(loc, "Invalid value type: "+EnumToString(t)+", Expect: "+EnumToString(et)); } }
#define ValidateValueType2(loc, t, et1, et2) { if (t != et1 && t != et2) { Warn(loc, "Invalid value type: "+EnumToString(t) + ", Expect: "+EnumToString(et1)+" or "+EnumToString(et2)); } }
#define ValidateValueTypeNot(loc, t, et) { if (t == et) { Warn(loc, "Invalid value type: "+EnumToString(t)); } }
#define ValidateValueTypeSame(loc, n1, n2) { if (n1->valueType != n2->valueType) { Warn(loc, "Value type mismatch: "+EnumToString(n1->valueType) + ", " +EnumToString(n2->valueType)); } }
#define MakeIdentifierNode(loc, k, n) TokenNode* n = new TokenNode(loc); { \
n->operatorType = OperatorType::IdentifierValue; \
if (localSymbolTable->HasKey(k)) { \
n->symbolTableEntry = localSymbolTable->Find(k); \
n->valueType = n->symbolTableEntry->valueType; \
} else if (globalSymbolTable->HasKey(k)) { \
n->symbolTableEntry = globalSymbolTable->Find(k); \
n->valueType = n->symbolTableEntry->valueType; \
} else { Warn(loc, "Identifier not found: " + std::string(k)); } \
}
#define MakeStatementNode(loc, k, n) TokenNode* n = new TokenNode(loc); { \
n->operatorType = OperatorType::Statement; \
n->valueType = k->valueType; \
n->exp1 = k; \
}
SymbolTable* localSymbolTable;
SymbolTable* globalSymbolTable;
std::stack<TokenNode*> loopStack;
%}
/* Enable C++ Skeleton */
%skeleton "lalr1.cc"
/* Enable yyloc support */
%locations
/* Enable header file output */
%defines
/* Set output class name to BisonParser */
%define parser_class_name {BisonParser}
/* Code inside y.tab.hh */
%code requires {
#include "driver.h"
#include "symbol_table.h"
#include "token_node.h"
// Forward declarations required classes, since we need to inject scanner into praser
namespace yy {
class FlexScanner;
class BisonParser;
}
struct SymbolTableEntryListItem {
struct SymbolTableEntryListItem* next;
SymbolTableEntry* entry;
};
typedef struct SymbolTableEntryListItem SymbolTableEntryListItem;
}
/* Inject scanner into praser */
%parse-param {FlexScanner &scanner}
%parse-param {std::map<std::string, SymbolTable*> &symbolTableMap}
%parse-param {bool isVerbose}
%parse-param {int* warningCount}
%parse-param {TokenNode* programNode}
%code {
#include "lex.yy.hh"
#undef yylex
#define yylex scanner.yylex
}
/* Union variables for each node */
%union {
int iValue;
float fValue;
char *sValue;
bool bValue;
ValueType valueType;
SymbolTableEntryListItem* sValueList;
SymbolTableEntry* symbolTableEntry;
TokenNode* token;
}
/* Keywords */
%token KW_ARRAY KW_BOOLEAN KW_BEGIN KW_BY KW_CONST KW_CONTINUE
%token KW_DO KW_ELSE KW_ELSIF KW_END KW_EXIT KW_FALSE KW_FOR KW_IF KW_INTEGER
%token KW_LOOP KW_MODULE KW_OF KW_PRINT KW_PRINTLN KW_PROCEDURE
%token KW_READ KW_REPEAT KW_RETURN KW_REAL KW_STRING
%token KW_THEN KW_TO KW_TRUE KW_UNTIL KW_VAR KW_WHILE
/* Delimiters */
%token COMMA COLON PERIOD SEMICOLON
%token LEFT_PARENTHESIS RIGHT_PARENTHESIS
%token LEFT_CURLY_BRACKET RIGHT_CURLY_BRACKET
%token LEFT_SQUARE_BRACKET RIGHT_SQUARE_BRACKET
/* Operators */
%token PLUS HYPHEN_MINUS
%token ASTERISK SOLIDUS PERCENT
%token LESS_THAN LESS_EQUAL_THAN
%token GREATER_THAN GREATER_EQUAL_THAN
%token EQUAL NOT_EQUAL
%token LOGICAL_AND LOGICAL_OR
%token TILDE
%token ASSIGNMENT
/* Value */
%token IDENTIFIER
%token INTEGER
%token REAL
%token STRING
/* Associativity */
%nonassoc LESS_THAN LESS_EQUAL_THAN GREATER_THAN GREATER_EQUAL_THAN EQUAL NOT_EQUAL
%left PLUS HYPHEN_MINUS
%left ASTERISK SOLIDUS PERCENT
/* Type Declarations */
%type <sValue> STRING IDENTIFIER
%type <iValue> INTEGER
%type <fValue> REAL
%type <bValue> boolean
%type <sValueList> variable_identifier_list
%type <valueType> type_specifier return_type_optional
%type <symbolTableEntry> const_declarator array_specifier variable_type_specifier
%type <token> constant array_item function_call argument_expression_list argument_expression_list_optional function_declaration function_declaration_list function_declaration_list_optional
%type <token> expression primary_expression postfix_expression unary_expression multiplicative_expression additive_expression relational_expression not_expression logical_and_expression logical_or_expression conditional_expression
%type <token> statement_list_optional statement_list statement assignable_item assignment_statement print_statement read_statement return_statement
%type <token> conditional_statement elseif_declaration_list_optional elseif_declaration_list elseif_declaration else_declaration_optional
%type <token> while_statement repeat_statement for_statement by_declaration_optional loop_statement continue_exit_statement function_call_statement
%start program
%%
EMPTY: ;
constant:
STRING
{
$$ = new TokenNode(@1);
$$->operatorType = OperatorType::ConstValue;
$$->valueType = ValueType::String;
$$->value.sValue = $1;
}
| INTEGER
{
$$ = new TokenNode(@1);
$$->operatorType = OperatorType::ConstValue;
$$->valueType = ValueType::Integer;
$$->value.iValue = $1;
}
| REAL
{
$$ = new TokenNode(@1);
$$->operatorType = OperatorType::ConstValue;
$$->valueType = ValueType::Float;
$$->value.fValue = $1;
}
| boolean
{
$$ = new TokenNode(@1);
$$->operatorType = OperatorType::ConstValue;
$$->valueType = ValueType::Bool;
$$->value.bValue = $1;
}
;
boolean:
KW_TRUE { $$ = true; }
| KW_FALSE { $$ = false; }
;
type_specifier:
KW_STRING { $$ = ValueType::String; }
| KW_INTEGER { $$ = ValueType::Integer; }
| KW_REAL { $$ = ValueType::Float; }
| KW_BOOLEAN { $$ = ValueType::Bool; }
;
array_specifier:
KW_ARRAY LEFT_SQUARE_BRACKET INTEGER COMMA INTEGER
{
// VALIDATION: Array range
if ($3 > $5) {
Warn(@5, "Invalid array range [" + std::to_string($3) + "," + std::to_string($5) + "].");
}
}
RIGHT_SQUARE_BRACKET
KW_OF type_specifier
{
// Make array entry
SymbolTableEntry* entry = new SymbolTableEntry();
entry->valueType = ValueType::Array;
entry->arrayEntry.valueType = $9;
entry->arrayEntry.low = $3;
entry->arrayEntry.high = $5;
$$ = entry;
}
;
array_item:
IDENTIFIER LEFT_SQUARE_BRACKET expression RIGHT_SQUARE_BRACKET
{
MakeIdentifierNode(@1, $1, identifier);
$$ = new TokenNode(@1);
$$->symbolTableEntry = identifier->symbolTableEntry;
$$->exp1 = identifier;
$$->exp2 = $3;
// VALIDATION: Array index type check
ValidateValueType(@3, $3->valueType, ValueType::Integer);
if (identifier->symbolTableEntry != NULL) {
// VALIDATION: Static arraya index range check
if ($3->operatorType == OperatorType::ConstValue)
{
int idx = $3->value.iValue;
ArrayEntry arrayEntry = identifier->symbolTableEntry->arrayEntry;
if (idx < arrayEntry.low || idx > arrayEntry.high)
{
Warn(@3, "Invalid array index for: "+identifier->symbolTableEntry->name+"["+std::to_string(arrayEntry.low)+","+std::to_string(arrayEntry.high)+"] , got: " + std::to_string(idx));
}
}
$$->valueType = identifier->symbolTableEntry->arrayEntry.valueType;
}
}
;
/* module program */
program:
KW_MODULE IDENTIFIER
{
// Create new symbol table with global scope
localSymbolTable = new SymbolTable(GLOBAL_SYM_TABLE_NAME);
symbolTableMap[GLOBAL_SYM_TABLE_NAME] = localSymbolTable;
globalSymbolTable = localSymbolTable;
}
variable_const_declaration_unit
function_declaration_list_optional
{
// Dump global symbol table
globalSymbolTable->Dump();
}
KW_BEGIN
statement_list_optional
KW_END IDENTIFIER
PERIOD
{
// VALIDATION: Module begin/end identifier
std::string begin($2), end($10);
if (begin != end) {
Warn(@10, "Begin/End identifier mismatch for module: " + begin + " , got: " + end);
}
SymbolTableEntry* entry = new SymbolTableEntry();
entry->name = $2;
programNode->lineNumber = @1.begin.line;
programNode->operatorType = OperatorType::Program;
programNode->exp1 = $5;
programNode->exp2 = $8;
programNode->symbolTableEntry = entry;
}
;
variable_const_declaration_unit:
EMPTY
| const_declaration
| const_declaration variable_declaration
| variable_declaration
| variable_declaration const_declaration
;
/* constant declaration */
const_declaration:
KW_CONST const_declarator_list
{
Trace("const_declaration ", "CONST const_declarator_list");
}
;
const_declarator_list:
const_declarator
{
Trace("const_declarator_list", "const_declarator");
}
| const_declarator_list const_declarator
{
Trace("const_declarator_list", "const_declarator_list const_declarator");
}
;
const_declarator:
IDENTIFIER EQUAL constant SEMICOLON
{
Trace("const_declarator", "IDENTIFIER = constant;");
SymbolTableEntry* entry = new SymbolTableEntry();
entry->value = $3->value;
entry->valueType = $3->valueType;
entry->entryType = EntryType::Constant;
entry->name = $1;
delete($3); // Free TokenNode
// VALIDATION: Duplicate identifier
ValidateDuplicateIdentifier(@1, $1, entry);
localSymbolTable->Add($1, entry);
$$ = entry;
}
;
/* variable declaration */
variable_declaration:
KW_VAR variable_declarator_list
{
Trace("variable_declaration", "VAR variable_declarator_list");
}
;
variable_declarator_list:
variable_declarator
{
Trace("variable_declarator_list", "variable_declarator");
}
| variable_declarator_list variable_declarator
{
Trace("variable_declarator_list", "variable_declarator_list variable_declarator");
}
;
variable_type_specifier:
type_specifier
{
Trace("variable_type_specifier", "type_specifier");
// Make entry
SymbolTableEntry* entry = new SymbolTableEntry();
entry->valueType = $1;
entry->entryType = EntryType::Variable;
$$ = entry;
}
| array_specifier
{
Trace("variable_type_specifier", "array_specifier");
$$->entryType = EntryType::Variable;
}
;
variable_declarator:
variable_identifier_list COLON variable_type_specifier
{
Trace("variable_declarator", "variable_identifier_list : variable_type_specifier;");
// Assign variable type to each entry in list
SymbolTableEntryListItem* node = $1;
while (node != NULL)
{
SymbolTableEntryListItem* oldNode = node;
SymbolTableEntry* entry = node->entry;
entry->valueType = $3->valueType;
entry->arrayEntry = $3->arrayEntry;
node = node->next;
delete(oldNode); // Free link list node
}
delete($3); // Free variable_type_specifier
}
SEMICOLON
;
variable_identifier_list:
IDENTIFIER
{
// Add variable entry to symbol table
Trace("variable_identifier_list", "IDENTIFIER");
SymbolTableEntry* entry = new SymbolTableEntry();
entry->name = $1;
entry->entryType = EntryType::Variable;
// VALIDATION: Duplicate identifier
ValidateDuplicateIdentifier(@1, $1, entry);
localSymbolTable->Add($1, entry);
// Build variable link list
$$ = new SymbolTableEntryListItem();
$$->entry = entry;
}
| variable_identifier_list COMMA IDENTIFIER
{
// Add variable entry to symbol table
Trace("variable_identifier_list", "variable_identifier_list, IDENTIFIER");
SymbolTableEntry* entry = new SymbolTableEntry();
entry->name = $3;
entry->entryType = EntryType::Variable;
// VALIDATION: Duplicate identifier
ValidateDuplicateIdentifier(@3, $3, entry);
localSymbolTable->Add($3, entry);
// Build variable link list
$$ = new SymbolTableEntryListItem();
$$->entry = entry;
$$->next = $1;
}
;
/* #region function declarations */
function_declaration:
KW_PROCEDURE IDENTIFIER
{
// Add function entry to symbol table
SymbolTableEntry* entry = new SymbolTableEntry();
entry->name = $2;
entry->entryType = EntryType::Function;
// VALIDATION: Duplicate identifier
ValidateDuplicateIdentifier(@2, $2, entry);
localSymbolTable->Add($2, entry);
// Create new symbol table for function
localSymbolTable = new SymbolTable($2);
symbolTableMap[$2] = localSymbolTable;
}
formal_argument_optional return_type_optional
{
// Assign function return type
globalSymbolTable->Find($2)->valueType = $5;
}
variable_const_declaration_unit
KW_BEGIN
statement_list_optional
KW_END IDENTIFIER
SEMICOLON
{
Trace("function_declaration", "");
// VALIDATION: Procedure begin/end identifier
std::string begin($2);
std::string end($11);
if (begin != end) {
Warn(@11, "Begin/End identifier mismatch for procedure: " + begin + " , got " + end + ".");
}
// Dump procedure symbol table
localSymbolTable->Dump();
localSymbolTable = globalSymbolTable;
$$ = new TokenNode(@1);
$$->operatorType = OperatorType::FunctionDeclaration;
$$->symbolTableEntry = globalSymbolTable->Find($2);
$$->exp1 = $9;
}
;
function_declaration_list_optional:
EMPTY
{
Trace("function_declaration_list_optional", "EMPTY");
$$ = NULL;
}
| function_declaration_list
{
Trace("function_declaration_list_optional", "function_declaration_list");
$$ = $1;
}
;
function_declaration_list:
function_declaration
{
Trace("function_declaration_list", "function_declaration");
$$ = $1;
$$->last = $$;
}
| function_declaration_list function_declaration
{
Trace("function_declaration_list_optional", "function_declaration_list_optional function_declaration");
$$ = $1;
$$->last->next = $2;
$$->last = $2;
}
;
return_type_optional:
EMPTY
{
Trace("return_type_optional", "EMPTY");
$$ = ValueType::Void;
}
| COLON type_specifier
{
Trace("return_type_optional", "type_specifier");
$$ = $2;
}
;
formal_argument_optional:
EMPTY
{
Trace("formal_argument_optional", "EMPTY");
}
| LEFT_PARENTHESIS formal_argument_list RIGHT_PARENTHESIS
{
Trace("formal_argument_optional", "(formal_argument_list)");
}
;
formal_argument_list:
| formal_argument
{
Trace("formal_argument_list", "formal_argument");
}
| formal_argument_list COMMA formal_argument
{
Trace("formal_argument_list", "formal_argument_list, formal_argument");
}
;
formal_argument:
IDENTIFIER
{
// Add function argument entry to symbol table
SymbolTableEntry* entry = new SymbolTableEntry();
entry->name = $1;
entry->entryType = EntryType::Argument;
// VALIDATION: Duplicate identifier
ValidateDuplicateIdentifier(@1, $1, entry);
localSymbolTable->Add($1, entry);
}
COLON type_specifier
{
localSymbolTable->Find($1)->valueType = $4;
}
;
function_call:
IDENTIFIER LEFT_PARENTHESIS argument_expression_list_optional RIGHT_PARENTHESIS
{
Trace("postfix_expression", "IDENTIFIER(argument_expression_list)");
// VALIDATION: Function declaration
MakeIdentifierNode(@1, $1, identifier);
if (identifier->symbolTableEntry != NULL) {
ValidateEntryType(@1, identifier->symbolTableEntry->entryType, EntryType::Function);
// VALIDATION: Function arguments and index
std::vector<SymbolTableEntry*> argumentTable = symbolTableMap[$1]->GetArgumentTable();
TokenNode* node = $3;
for (int i = 0; i < argumentTable.size(); i++)
{
if (node == NULL)
{
Warn(@3, "Procedure argument mismatch: " + identifier->symbolTableEntry->name + " arg" + std::to_string(i) + " should be type " + EnumToString(argumentTable[i]->valueType) + " , got: void");
} else {
if (node->valueType != argumentTable[i]->valueType)
{
Warn(@3, "Procedure argument mismatch: " + identifier->symbolTableEntry->name + " arg" + std::to_string(i) + " should be type " + EnumToString(argumentTable[i]->valueType)+" , got: "+EnumToString(node->valueType));
}
node = node->next;
}
}
}
$$ = new TokenNode(@1);
$$->operatorType = OperatorType::FunctionInvoke;
$$->valueType = identifier->valueType;
$$->symbolTableEntry = identifier->symbolTableEntry;
$$->exp1 = identifier;
$$->exp2 = $3;
}
;
/* expression */
expression:
conditional_expression
{
$$ = $1;
}
;
primary_expression:
IDENTIFIER
{
Trace("primary_expression", "IDENTIFIER");
// VALIDATION: Variable declaration
MakeIdentifierNode(@1, $1, identifier);
// VALIDATION: Non function
if (identifier->symbolTableEntry != NULL && identifier->symbolTableEntry->entryType == EntryType::Function) {
Warn(@1, "Invalid procedure call, can not use procedure as variable: "+identifier->symbolTableEntry->name);
}
$$ = identifier;
}
| constant
{
Trace("primary_expression", "constant");
$$ = $1;
}
| array_item
{
Trace("primary_expression", "array_item");
$$ = $1;
$$->operatorType = OperatorType::ArrayLoad;
}
| LEFT_PARENTHESIS expression RIGHT_PARENTHESIS
{
Trace("primary_expression", "(expression)");
$$ = $2;
}
;
postfix_expression:
primary_expression
{
Trace("postfix_expression", "primary_expression");
$$ = $1;
}
| function_call
{
Trace("postfix_expression", "function_call");
// VALIDATION: Return type non-null
if ($1->valueType == ValueType::Void) {
Warn(@1, "Fuction return type of void is not allowed in expression: " + $1->symbolTableEntry->name);
}
$$ = $1;
}
;
argument_expression_list_optional:
EMPTY
{
Trace("argument_expression_list_optional", "EMPTY");
$$ = NULL;
}
| argument_expression_list
{
Trace("argument_expression_list_optional", "argument_expression_list");
$$ = $1;
}
;
argument_expression_list:
expression
{
Trace("argument_expression_list", "expression");
$$ = new TokenNode(@1);
$$->operatorType = OperatorType::FunctionArgument;
$$->valueType = $1->valueType;
$$->exp1 = $1;
$$->last = $$;
}
| argument_expression_list COMMA expression
{
Trace("argument_expression_list", "argument_expression_list, expression");
TokenNode* node = new TokenNode(@1);
node->operatorType = OperatorType::FunctionArgument;
node->valueType = $3->valueType;
node->exp1 = $3;
$$ = $1;
$$->last->next = node;
$$->last = node;
}
;
/* precedence 1 */
unary_expression:
postfix_expression
{
Trace("unary_expression", "postfix_expression");
$$ = $1;
}
| HYPHEN_MINUS unary_expression
{
Trace("unary_expression", "-unary_expression");
ValidateValueType2(@2, $2->valueType, ValueType::Integer, ValueType::Float);
$$ = new TokenNode(@1);
$$->operatorType = OperatorType::Negative;
$$->valueType = $2->valueType;
$$->exp1 = $2;
}
;
/* precedence 2 */
multiplicative_expression:
unary_expression
{
Trace("multiplicative_expression", "unary_expression");
$$ = $1;
}
| multiplicative_expression ASTERISK unary_expression
{
Trace("multiplicative_expression", "multiplicative_expression * unary_expression");
ValidateValueType2(@1, $1->valueType, ValueType::Integer, ValueType::Float);
ValidateValueType2(@3, $3->valueType, ValueType::Integer, ValueType::Float);
ValidateValueTypeSame(@2, $1, $3);
$$ = new TokenNode(@1);
$$->operatorType = OperatorType::Multiply;
$$->valueType = $1->valueType;
$$->exp1 = $1;
$$->exp2 = $3;
}
| multiplicative_expression SOLIDUS unary_expression
{
Trace("multiplicative_expression", "multiplicative_expression / unary_expression");
ValidateValueType2(@1, $1->valueType, ValueType::Integer, ValueType::Float);
ValidateValueType2(@3, $3->valueType, ValueType::Integer, ValueType::Float);
ValidateValueTypeSame(@2, $1, $3);
$$ = new TokenNode(@1);
$$->operatorType = OperatorType::Devide;
$$->valueType = $1->valueType;
$$->exp1 = $1;
$$->exp2 = $3;
}
| multiplicative_expression PERCENT unary_expression
{
Trace("multiplicative_expression", "multiplicative_expression % unary_expression");
ValidateValueType(@1, $1->valueType, ValueType::Integer);
ValidateValueType(@3, $3->valueType, ValueType::Integer);
$$ = new TokenNode(@1);
$$->operatorType = OperatorType::Reminder;
$$->valueType = $1->valueType;
$$->exp1 = $1;
$$->exp2 = $3;
}
;
/* precedence 3 */
additive_expression:
multiplicative_expression
{
Trace("additive_expression", "multiplicative_expression");
$$ = $1;
}
| additive_expression PLUS multiplicative_expression
{
Trace("additive_expression", "additive_expression + multiplicative_expression");
ValidateValueType2(@1, $1->valueType, ValueType::Integer, ValueType::Float);
ValidateValueType2(@3, $3->valueType, ValueType::Integer, ValueType::Float);
ValidateValueTypeSame(@2, $1, $3);
$$ = new TokenNode(@1);
$$->operatorType = OperatorType::Add;
$$->valueType = $1->valueType;
$$->exp1 = $1;
$$->exp2 = $3;
}
| additive_expression HYPHEN_MINUS multiplicative_expression
{
Trace("additive_expression", "additive_expression - multiplicative_expression");
ValidateValueType2(@1, $1->valueType, ValueType::Integer, ValueType::Float);
ValidateValueType2(@3, $3->valueType, ValueType::Integer, ValueType::Float);
ValidateValueTypeSame(@2, $1, $3);
$$ = new TokenNode(@1);
$$->operatorType = OperatorType::Minus;
$$->valueType = $1->valueType;
$$->exp1 = $1;
$$->exp2 = $3;
}
;
/* precedence 4 */
relational_expression:
additive_expression
{
Trace("relational_expression", "additive_expression");
$$ = $1;
}
| relational_expression LESS_THAN additive_expression
{
Trace("relational_expression", "relational_expression < additive_expression");
ValidateValueType2(@1, $1->valueType, ValueType::Integer, ValueType::Float);
ValidateValueType2(@3, $3->valueType, ValueType::Integer, ValueType::Float);
$$ = new TokenNode(@1);
$$->operatorType = OperatorType::LessThen;
$$->valueType = ValueType::Bool;
$$->exp1 = $1;
$$->exp2 = $3;
}
| relational_expression LESS_EQUAL_THAN additive_expression
{
Trace("relational_expression", "relational_expression > additive_expression");
ValidateValueType2(@1, $1->valueType, ValueType::Integer, ValueType::Float);
ValidateValueType2(@3, $3->valueType, ValueType::Integer, ValueType::Float);
$$ = new TokenNode(@1);
$$->operatorType = OperatorType::LessEqualThen;
$$->valueType = ValueType::Bool;
$$->exp1 = $1;
$$->exp2 = $3;
}
| relational_expression GREATER_THAN additive_expression
{
Trace("relational_expression", "relational_expression <= additive_expression");
ValidateValueType2(@1, $1->valueType, ValueType::Integer, ValueType::Float);
ValidateValueType2(@3, $3->valueType, ValueType::Integer, ValueType::Float);
$$ = new TokenNode(@1);
$$->operatorType = OperatorType::GreaterThen;
$$->valueType = ValueType::Bool;
$$->exp1 = $1;
$$->exp2 = $3;
}
| relational_expression GREATER_EQUAL_THAN additive_expression
{
Trace("relational_expression", "relational_expression >= additive_expression");
ValidateValueType2(@1, $1->valueType, ValueType::Integer, ValueType::Float);
ValidateValueType2(@3, $3->valueType, ValueType::Integer, ValueType::Float);
$$ = new TokenNode(@1);
$$->operatorType = OperatorType::GreaterEqualThen;
$$->valueType = ValueType::Bool;
$$->exp1 = $1;
$$->exp2 = $3;
}
| relational_expression EQUAL additive_expression
{
Trace("relational_expression", "relational_expression = additive_expression");
ValidateValueTypeSame(@2, $1, $3);
$$ = new TokenNode(@1);
$$->operatorType = OperatorType::Equal;
$$->valueType = ValueType::Bool;
$$->exp1 = $1;
$$->exp2 = $3;
}
| relational_expression NOT_EQUAL additive_expression
{
Trace("relational_expression", "relational_expression <> additive_expression");
ValidateValueTypeSame(@2, $1, $3);
$$ = new TokenNode(@1);
$$->operatorType = OperatorType::NotEqual;
$$->valueType = ValueType::Bool;
$$->exp1 = $1;
$$->exp2 = $3;
}
;
/* precedence 5 */
not_expression:
relational_expression
{
Trace("not_expression", "relational_expression");
$$ = $1;
}
| TILDE not_expression
{
Trace("not_expression", "~not_expression");
ValidateValueType(@2, $2->valueType, ValueType::Bool);
TokenNode* node = new TokenNode(@1);
node->operatorType = OperatorType::LogicalNot;
node->valueType = ValueType::Bool;
node->exp1 = $2;
$$ = node;
}
;
/* precedence 6 */
logical_and_expression:
not_expression
{
Trace("logical_and_expression", "not_expression");
$$ = $1;
}
| logical_and_expression LOGICAL_AND not_expression
{
Trace("logical_and_expression", "logical_and_expression && not_expression");
ValidateValueType(@1, $1->valueType, ValueType::Bool);
ValidateValueType(@3, $3->valueType, ValueType::Bool);
TokenNode* node = new TokenNode(@1);
node->operatorType = OperatorType::LogicalAnd;
node->valueType = ValueType::Bool;
node->exp1 = $1;
node->exp2 = $3;
$$ = node;
}
;
/* precedence 7 */
logical_or_expression:
logical_and_expression
{
Trace("logical_or_expression", "logical_and_expression");
$$ = $1;
}
| logical_or_expression LOGICAL_OR logical_and_expression
{
Trace("logical_or_expression", "logical_or_expression || logical_and_expression");
ValidateValueType(@1, $1->valueType, ValueType::Bool);
ValidateValueType(@3, $3->valueType, ValueType::Bool);
TokenNode* node = new TokenNode(@1);
node->operatorType = OperatorType::LogicalOr;
node->valueType = ValueType::Bool;
node->exp1 = $1;
node->exp2 = $3;
$$ = node;
}
;
conditional_expression:
logical_or_expression
{
Trace("conditional_expression", "logical_or_expression");
$$ = $1;
}
;
/* statements */
statement_list_optional:
EMPTY
{
Trace("statement_list_optional", "EMPTY");
$$ = NULL;
}
| statement_list
{
Trace("statement_list_optional", "statement_list");
$$ = $1;
}
;
statement_list:
statement
{
Trace("statement_list", "statement");
$$ = $1;
$$->last = $$;
}
| statement_list statement
{
Trace("statement_list", "statement_list_optional statement");
$$ = $1;
$$->last->next = $2;
$$->last = $2;
}
;
statement:
assignment_statement SEMICOLON
{
Trace("statement", "assignment_statement");
MakeStatementNode(@1, $1, node);
$$ = node;
}
| print_statement SEMICOLON
{
Trace("statement", "print_statement");
MakeStatementNode(@1, $1, node);
$$ = node;
}
| read_statement SEMICOLON
{
Trace("statement", "read_statement");
MakeStatementNode(@1, $1, node);
$$ = node;
}
| return_statement SEMICOLON
{
Trace("statement", "return_statement");
MakeStatementNode(@1, $1, node);
$$ = node;
}
| conditional_statement SEMICOLON
{
Trace("statement", "conditional_statement");
MakeStatementNode(@1, $1, node);
$$ = node;
}
| while_statement SEMICOLON
{
Trace("statement", "while_statement");
MakeStatementNode(@1, $1, node);
$$ = node;
}
| repeat_statement SEMICOLON
{
Trace("statement", "repeat_statement");
MakeStatementNode(@1, $1, node);
$$ = node;
}
| for_statement SEMICOLON
{
Trace("statement", "for_statement");
MakeStatementNode(@1, $1, node);
$$ = node;
}
| loop_statement SEMICOLON
{
Trace("statement", "loop_statement");
MakeStatementNode(@1, $1, node);
$$ = node;
}
| continue_exit_statement SEMICOLON
{
Trace("statement", "continue_exit_statement");
MakeStatementNode(@1, $1, node);
$$ = node;
}
| function_call_statement SEMICOLON
{
Trace("statement", "function_call_statement");
MakeStatementNode(@1, $1, node);
$$ = node;
}
;
assignable_item:
IDENTIFIER
{
Trace("assignable_item", "IDENTIFIER");
// VALIDATION: Variable declaration
MakeIdentifierNode(@1, $1, identifier);
// VALIDATION: Assignable types
ValueType valueType = identifier->valueType;
if (valueType != ValueType::Integer && valueType != ValueType::Float && valueType != ValueType::Bool && valueType != ValueType::String)
{
Warn(@1, "Can not assign type: " + EnumToString(valueType));
}
// VALIDATION: Non const
if (identifier->symbolTableEntry != NULL && identifier->symbolTableEntry->entryType == EntryType::Constant) {
Warn(@1, "Can not re-assign constant variable: "+identifier->symbolTableEntry->name);
}
$$ = identifier;
}
| array_item
{
Trace("assignable_item", "array_item");
$$ = $1;
$$->operatorType = OperatorType::ArrayStore;
}
;
assignment_statement:
assignable_item ASSIGNMENT expression
{
Trace("assignment_statement", "assignable_item := expression");
// VALIDATION: Same type check
ValidateValueTypeSame(@3, $1, $3);