-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.c
1139 lines (769 loc) · 42.4 KB
/
parser.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
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: Implementation of imperative programming language compiler
* PART: Recursive top-down parser
*
* AUTHOR(S): Maksim Tikhonov (xtikho00)
*/
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "errorslist.h"
#include "string_processor.h"
#include "parser.h"
#include "symtable.h"
#include "code_generator.h"
#include "expression.h"
void q_init ( Queue *queue ) {
queue->top = NULL;
queue->end = NULL;
}
void q_push ( Queue *queue, Item_data *item ) {
Queue_item *newItem = malloc( sizeof ( Queue_item ) );
if (!newItem) return;
newItem->data = item;
newItem->next = NULL;
if (queue->top == NULL) queue->top = newItem;
else queue->end->next = newItem;
queue->end = newItem;
}
Item_data *q_pop ( Queue *queue ) {
if (queue->top) {
Item_data *returnData = queue->top->data;
Queue_item *itemToDelete = queue->top;
if (queue->top == queue->end) {
queue->end = NULL;
}
queue->top = queue->top->next;
free( itemToDelete );
return returnData;
}
return NULL;
}
void q_dispose( Queue *queue ) {
while (queue->top) {
q_pop( queue );
}
}
Data_type get_param_or_retval_type ( Item_data *function, bool mode, int pos ) {
int res = 0;
if (!mode) res = function->inputTypes->str[pos];
else res = function->outputTypes->str[pos];
switch (res) {
case ('i'):
return T_INT;
case ('n'):
return T_NUM;
case ('s'):
return T_STR;
case ('l'):
return T_NIL;
default:
return T_NDA;
}
}
char convert_data_type_to_char ( Keyword dataType ) {
switch (dataType) {
case KW_INTEGER:
return 'i';
case KW_NUMBER:
return 'n';
case KW_STRING:
return 's';
case KW_NIL:
return 'l';
default:
return ERR_SYNTAX;
}
}
int get_next_token_and_check_type ( Parser_data *parserData, Data_type type ) {
int tmp;
if ((tmp = get_next_token( &parserData->token ))) return tmp;
if (parserData->token.type != type) return ERR_SYNTAX;
return 0;
}
int get_next_token_and_check_keyword ( Parser_data *parserData, Keyword keyword ) {
int tmp;
if ((tmp = get_next_token( &parserData->token ))) return tmp;
if (parserData->token.type != T_KEY
|| parserData->token.attribute.keyword != keyword) return ERR_SYNTAX;
return 0;
}
int get_next_token_and_apply_rule ( Parser_data *parserData, char *rulename ) {
int tmp = 0;
if ((tmp = get_next_token( &parserData->token ))) return tmp;
if (!strcmp( rulename, "rule_expression" )) { if ((tmp = rule_expression( parserData ))) return tmp; }
else if (!strcmp( rulename, "rule_statementList")) { if ((tmp = rule_statementList( parserData ))) return tmp; }
else if (!strcmp( rulename, "rule_optionalDefinition")) { if ((tmp = rule_optionalDefinition( parserData ))) return tmp; }
else if (!strcmp( rulename, "rule_declarationParams" )) { if ((tmp = rule_declarationParams( parserData ))) return tmp; }
else if (!strcmp( rulename, "rule_output" )) { if ((tmp = rule_output( parserData ))) return tmp; }
else if (!strcmp( rulename, "rule_definitionParams" )) { if ((tmp = rule_definitionParams( parserData ))) return tmp; }
else if (!strcmp( rulename, "rule_definitionParam" )) { if ((tmp = rule_definitionParam( parserData ))) return tmp; }
else if (!strcmp( rulename, "rule_value")) { if ((tmp = rule_value( parserData ))) return tmp; }
else if (!strcmp( rulename, "rule_IDList" )) { if ((tmp = rule_IDList( parserData ))) return tmp; }
else if (!strcmp( rulename, "rule_otherID" )) { if ((tmp = rule_otherID( parserData ))) return tmp; }
else if (!strcmp( rulename, "rule_assignmentValues")) { if ((tmp = rule_assignmentValues( parserData ))) return tmp; }
else if (!strcmp( rulename, "rule_otherAssignmentValue" )) { if ((tmp = rule_otherAssignmentValue( parserData ))) return tmp; }
else if (!strcmp( rulename, "rule_functionCallParams" )) { if ((tmp = rule_functionCallParams( parserData ))) return tmp; }
else if (!strcmp( rulename, "rule_callParam" )) { if ((tmp = rule_callParam( parserData ))) return tmp; }
else if (!strcmp( rulename, "rule_assignmentValue" )) { if ((tmp = rule_assignmentValue( parserData ))) return tmp; }
else if (!strcmp( rulename, "rule_returnValues" )) { if ((tmp = rule_returnValues( parserData ))) return tmp; }
else if (!strcmp( rulename, "rule_returnValue" )) { if ((tmp = rule_returnValue( parserData ))) return tmp; }
else if (!strcmp( rulename, "rule_writeArgumentList" )) { if ((tmp = rule_writeArgumentList( parserData ))) return tmp; }
else if (!strcmp( rulename, "rule_otherWriteArgument" )) { if ((tmp = rule_otherWriteArgument( parserData ))) return tmp; }
return 0;
}
bool parser_data_init ( Parser_data *parserData ) {
int res = 0;
// Parser data parameters
parserData->lhsId = NULL;
parserData->currentVar = NULL;
parserData->currentFunc = NULL;
parserData->insideFunc = NULL;
parserData->expType = T_NDA;
parserData->ifIndex = 0;
parserData->inWhile = false;
parserData->whileIndex = 0;
parserData->currentDepth = 0;
parserData->paramIndex = 0;
// symbol table stack initialization
sts_init( &parserData->STStack );
parserData->globalSymTable = sts_push( &parserData->STStack, parserData->currentDepth );
// identifier queue initialization
q_init( &parserData->queue );
// add built-in functions to global symbol table
ADD_GLOBAL( "reads" );
if ((res = st_add_param( parserData->currentFunc->outputTypes, KW_STRING ))) return false;
parserData->currentFunc->ifdec = 1;
parserData->currentFunc->ifdef = 1;
ADD_GLOBAL( "readi" );
if ((res = st_add_param( parserData->currentFunc->outputTypes, KW_INTEGER ))) return false;
parserData->currentFunc->ifdec = 1;
parserData->currentFunc->ifdef = 1;
ADD_GLOBAL( "readn" );
if ((res = st_add_param( parserData->currentFunc->outputTypes, KW_NUMBER ))) return false;
parserData->currentFunc->ifdec = 1;
parserData->currentFunc->ifdef = 1;
ADD_GLOBAL( "write" );
if ((res = st_add_param( parserData->currentFunc->outputTypes, ANY ))) return false;
parserData->currentFunc->ifdec = 1;
parserData->currentFunc->ifdef = 1;
ADD_GLOBAL( "tointeger" );
if ((res = st_add_param( parserData->currentFunc->outputTypes, KW_INTEGER ))) return false;
if ((res = st_add_param( parserData->currentFunc->inputTypes, KW_NUMBER ))) return false;
parserData->currentFunc->ifdec = 1;
parserData->currentFunc->ifdef = 1;
ADD_GLOBAL( "substr" );
if ((res = st_add_param( parserData->currentFunc->inputTypes, KW_STRING ))) return false;
if ((res = st_add_param( parserData->currentFunc->inputTypes, KW_INTEGER ))) return false;
if ((res = st_add_param( parserData->currentFunc->inputTypes, KW_INTEGER ))) return false;
if ((res = st_add_param( parserData->currentFunc->outputTypes, KW_STRING ))) return false;
parserData->currentFunc->ifdec = 1;
parserData->currentFunc->ifdef = 1;
ADD_GLOBAL( "ord" );
if ((res = st_add_param( parserData->currentFunc->inputTypes, KW_STRING ))) return false;
if ((res = st_add_param( parserData->currentFunc->inputTypes, KW_INTEGER ))) return false;
if ((res = st_add_param( parserData->currentFunc->outputTypes, KW_INTEGER ))) return false;
parserData->currentFunc->ifdec = 1;
parserData->currentFunc->ifdef = 1;
ADD_GLOBAL( "chr" );
if ((res = st_add_param( parserData->currentFunc->inputTypes, KW_INTEGER ))) return false;
if ((res = st_add_param( parserData->currentFunc->outputTypes, KW_STRING ))) return false;
parserData->currentFunc->ifdec = 1;
parserData->currentFunc->ifdef = 1;
return true;
}
int parse ( ) {
int res = 0;
// initialization of parser data structure
Parser_data pData;
Parser_data *parserData = &pData;
if (!parser_data_init( parserData )) return ERR_INTERNAL;
// initialization of dynamic string for scanner
Dynamic_string str1;
Dynamic_string *scannerString = &str1;
if (!ds_init( scannerString )) return ERR_INTERNAL;
_token_string( scannerString );
// initialization of dynamic string for code generator
Dynamic_string str2;
Dynamic_string *codeString = &str2;
if (!ds_init( codeString )) return ERR_INTERNAL;
_code_string( codeString );
// <prologue>
if ((res = rule_prologue( parserData ))) return res;
// <function list>
if ((res = rule_functionList( parserData ))) return res;
if ((res = search_undefined( *parserData->globalSymTable ))) return res;
// deallocation of memory for auxiliary structures
q_dispose( &parserData->queue );
sts_dispose( parserData->STStack.top );
// print result program
cg_output( stdout );
ds_free( scannerString );
return res;
}
/*........................................ RECURSIVE TOP-DOWN PARSER ........................................ */
int rule_prologue ( Parser_data *parserData ) {
int tmp;
if ((tmp = get_next_token_and_check_keyword ( parserData, KW_REQUIRE ))) return tmp; // REQUIRE
if ((tmp = get_next_token( &parserData->token ))) return tmp; // "ifj21"
if (strcmp( parserData->token.attribute.string->str, "ifj21" )) return ERR_SEMANTIC_OTHER;
// prologue OK -> generate start of program
if (!cg_start( )) return ERR_INTERNAL;
if ((tmp = get_next_token( &parserData->token ))) return tmp;
return 0;
}
/*........................................ FUNCTION HEADERS ........................................ */
int rule_functionList ( Parser_data *parserData ) {
int res = 0;
parserData->lhsId = NULL;
parserData->currentVar = NULL;
parserData->currentFunc = NULL;
parserData->insideFunc = NULL;
if (parserData->token.type == T_EOF) {
return 0;
}
else if (parserData->token.attribute.keyword == KW_GLOBAL) {
if ((res = rule_functionDeclaration( parserData ))) return res; // <function declaration>
return rule_functionList( parserData ); // <function list>
}
else if (parserData->token.attribute.keyword == KW_FUNCTION) {
// reset label indexing
parserData->ifIndex = 0;
parserData->whileIndex = 0;
if ((res = rule_functionDefinition( parserData ))) return res; // <function definition>
return rule_functionList( parserData ); // <function list>
}
else if (parserData->token.type == T_IDE) {
if ((res = rule_entryPoint( parserData ))) return res; // <entry point>
return rule_functionList( parserData ); // <function list>
}
else return ERR_SYNTAX;
}
int rule_entryPoint ( Parser_data *parserData ) {
int res = 0; // ID
SEARCH_GLOBAL( parserData->token.attribute.string->str );
if (res == 0) return ERR_SEMANTIC_UNDEF_VAR;
res = 0;
if (!cg_frame_to_pass_param ( )) return ERR_INTERNAL;
if ((res = get_next_token_and_check_type( parserData, T_LBR ))) return res; // (
if ((res = get_next_token_and_apply_rule( parserData, "rule_functionCallParams" ))) return res; // <function call params>
if (parserData->token.type != T_RBR) return ERR_SYNTAX; // )
if (!cg_call( parserData->currentFunc->id )) return ERR_INTERNAL;
if ((res = get_next_token( &parserData->token ))) return res;
return 0;
}
int rule_functionDeclaration ( Parser_data *parserData ) {
int res = 0; // GLOBAL
if ((res = get_next_token_and_check_type( parserData, T_IDE ))) return res; // ID
SEARCH_GLOBAL( parserData->token.attribute.string->str );
if (res == 1) return ERR_SEMANTIC_UNDEF_VAR; // attempt of function redeclaration or declaration after definition
ADD_GLOBAL( parserData->token.attribute.string->str );
if ((res = get_next_token_and_check_type( parserData, T_COL ))) return res; // :
if ((res = get_next_token_and_check_keyword( parserData, KW_FUNCTION ))) return res; // FUNCTION
if ((res = get_next_token_and_check_type( parserData, T_LBR ))) return res; // (
if ((res = get_next_token_and_apply_rule( parserData, "rule_declarationParams" ))) return res; // <declaration params>
if (parserData->token.type != T_RBR) return ERR_SYNTAX; // )
if ((res = get_next_token_and_apply_rule( parserData, "rule_output" ))) return res; // <output>
parserData->currentFunc->ifdec = 1;
return 0;
}
int rule_declarationParams ( Parser_data *parserData ) {
int res = 0;
if (parserData->token.type == T_RBR) return res;
if ((res = st_add_param( parserData->currentFunc->inputTypes, parserData->token.attribute.keyword ))) return res;
return rule_otherDeclarationParam( parserData );
}
int rule_otherDeclarationParam ( Parser_data *parserData ) {
int res = 0;
if ((res = get_next_token( &parserData->token ))) return res;
if (parserData->token.type == T_RBR) return res;
if ((res = get_next_token( &parserData->token ))) return res;
if ((res = st_add_param( parserData->currentFunc->inputTypes, parserData->token.attribute.keyword ))) return res;
return rule_otherDeclarationParam( parserData );
}
int rule_functionDefinition ( Parser_data *parserData ) {
int res = 0; // FUNCTION
if ((res = get_next_token_and_check_type( parserData, T_IDE ))) return res; // ID
SEARCH_GLOBAL( parserData->token.attribute.string->str );
if (res == 0) {
ADD_GLOBAL( parserData->token.attribute.string->str );
parserData->currentFunc->ifdef = 1;
}
else { // attempt of function redefinition?
if (parserData->currentFunc->ifdef == 1) return ERR_SEMANTIC_UNDEF_VAR;
else parserData->currentFunc->ifdef = 1;
res = 0;
}
parserData->insideFunc = parserData->currentFunc;
if (!cg_function_header( parserData->insideFunc->id )) return ERR_INTERNAL;
if ((res = get_next_token_and_check_type( parserData, T_LBR ))) return res; // (
parserData->currentDepth++;
sts_push( &parserData->STStack, parserData->currentDepth );
if ((res = get_next_token_and_apply_rule( parserData, "rule_definitionParams" ))) return res; // <definition params>
if (parserData->token.type != T_RBR) return ERR_SYNTAX; // )
if ((res = get_next_token_and_apply_rule( parserData, "rule_output" ))) return res; // <output>
if ((res = rule_statementList( parserData ))) return res; // <statement list>
sts_pop( &parserData->STStack );
parserData->currentDepth--;
if (parserData->token.attribute.keyword != KW_END) return ERR_SYNTAX; // END
if (!cg_function_return( parserData->insideFunc->id )) return ERR_INTERNAL;
if ((res = get_next_token( &parserData->token ))) return res;
return 0;
}
int rule_definitionParams ( Parser_data *parserData ) {
int res = 0;
if (parserData->token.type == T_RBR) {
if (parserData->currentFunc->inputTypes->length == 0) return res;
else return ERR_SEMANTIC_FUNC_PAR;
}
parserData->paramIndex = 0;
if ((res = rule_definitionParam( parserData ))) return res;
return rule_otherDefinitionParam( parserData );
}
int rule_otherDefinitionParam ( Parser_data *parserData ) {
int res = 0;
if ((res = get_next_token( &parserData->token ))) return res; // ,
if (parserData->token.type != T_COM) {
if (parserData->currentFunc->ifdec) {
if (parserData->currentFunc->inputTypes->length == parserData->paramIndex) return res;
else return ERR_SEMANTIC_FUNC_PAR;
} else return res;
}
if ((res = get_next_token_and_apply_rule( parserData, "rule_definitionParam" ))) return res; // <definition param>
return rule_otherDefinitionParam( parserData ); // <other definition param>
}
int rule_definitionParam ( Parser_data *parserData ) {
int res = 0;
ADD_LOCAL( parserData->token.attribute.string->str ); // ID
if ((res = get_next_token_and_check_type( parserData, T_COL ))) return res; // :
if ((res = get_next_token( &parserData->token ))) return res; // <type>
// to control if types are the same both in declared and in defined versions
if (parserData->currentFunc->ifdec == 1) {
if (parserData->currentFunc->inputTypes->length == parserData->paramIndex) return ERR_SEMANTIC_FUNC_PAR;
if (parserData->currentFunc->inputTypes->str[parserData->paramIndex] != convert_data_type_to_char( parserData->token.attribute.keyword )) {
return ERR_SEMANTIC_FUNC_PAR;
}
}
// to add types to function
else {
if ((res = st_add_param( parserData->currentFunc->inputTypes, parserData->token.attribute.keyword ))) return res;
}
// assign type to variable
if ((res = st_add_type( &parserData->token, parserData->currentVar ))) return res;
if (parserData->currentFunc->ifdef) {
if (!cg_function_param( parserData )) return ERR_INTERNAL;
}
parserData->paramIndex++;
return 0;
}
int rule_output ( Parser_data *parserData ) {
int res = 0;
if (parserData->token.type != T_COL) {
if (parserData->currentFunc->ifdec) {
if (parserData->currentFunc->outputTypes->length == 0) return res;
else return ERR_SEMANTIC_FUNC_PAR;
} else return res;
}
parserData->paramIndex = 0;
if ((res = get_next_token( &parserData->token ))) return res; // <type>
if (parserData->currentFunc->ifdec) {
if (parserData->currentFunc->outputTypes->str[parserData->paramIndex] != convert_data_type_to_char( parserData->token.attribute.keyword )) return ERR_SEMANTIC_FUNC_PAR;
}
else {
if ((res = st_add_param( parserData->currentFunc->outputTypes, parserData->token.attribute.keyword ))) return res;
}
if (!cg_function_retval( parserData->paramIndex )) return ERR_INTERNAL;
parserData->paramIndex++;
if ((res = get_next_token( &parserData->token ))) return res;
return rule_otherOutputType( parserData );
}
int rule_otherOutputType ( Parser_data *parserData ) {
int res = 0;
if (parserData->token.type != T_COM) {
if (parserData->currentFunc->ifdec) {
if (parserData->paramIndex == parserData->currentFunc->outputTypes->length) return res;
else return ERR_SEMANTIC_FUNC_PAR;
} else return res;
}
if ((res = get_next_token( &parserData->token ))) return res; // <type>
if (parserData->currentFunc->ifdec) {
if (parserData->currentFunc->outputTypes->str[parserData->paramIndex] != convert_data_type_to_char( parserData->token.attribute.keyword )) return ERR_SEMANTIC_FUNC_PAR;
}
else {
if ((res = st_add_param( parserData->currentFunc->outputTypes, parserData->token.attribute.keyword ))) return res;
}
if (!cg_function_retval( parserData->paramIndex )) return ERR_INTERNAL;
parserData->paramIndex++;
if ((res = get_next_token( &parserData->token ))) return res;
return rule_otherOutputType( parserData );
}
/*.......................................... STATEMENT LIST .......................................... */
int rule_statementList ( Parser_data *parserData ) {
int res = 0;
// $1
// <statement> -> <ID list> <assignment values>
// <statement> -> ID (<function call param list>)
if (parserData->token.type == T_IDE) {
// currentFunc <- search var id
SEARCH_GLOBAL( parserData->token.attribute.string->str );
// $1.0
// <statement> -> <ID list> <assignment values>
if (res == 0) {
if ((res = rule_IDList( parserData ))) return res; // <ID list>
if ((res = rule_assignmentValues( parserData ))) return res; // <assignment values>
if (parserData->queue.top) return ERR_SEMANTIC_OTHER; // if queue is not empty -> semantic error
}
// $1.5
// <statement> -> ID (<function call param list>)
else if (res == 1) {
res = 0;
if (!cg_frame_to_pass_param( )) return ERR_INTERNAL;
if ((res = get_next_token_and_check_type( parserData, T_LBR ))) return res; // (
if ((res = get_next_token_and_apply_rule( parserData, "rule_functionCallParams" ))) return res; // <function call param list>
if (parserData->token.type != T_RBR) return ERR_SYNTAX; // )
if (!cg_call( parserData->currentFunc->id )) return ERR_INTERNAL;
}
if (parserData->token.type == T_RBR) {
if ((res = get_next_token( &parserData->token ))) return res;
}
parserData->lhsId = NULL;
parserData->currentFunc = NULL;
return rule_statementList( parserData );
}
// $2
// <statement> -> LOCAL ID : <type> <optional definition>
else if (parserData->token.attribute.keyword == KW_LOCAL) { // LOCAL
if ((res = get_next_token_and_check_type( parserData, T_IDE ))) return res; // ID
SEARCH_GLOBAL( parserData->token.attribute.string->str ); // variable with function name -> error
if (res == 1) return ERR_SEMANTIC_UNDEF_VAR;
SEARCH_LOCAL( parserData->token.attribute.string->str ); // attempt of variable redefinition on the same lvl
if (res == 1) return ERR_SEMANTIC_UNDEF_VAR;
ADD_LOCAL( parserData->token.attribute.string->str );
parserData->lhsId = parserData->currentVar;
if (parserData->inWhile) {
if (!cg_while_start_jump_over_declaration( parserData )) return ERR_INTERNAL;
}
if ((res = get_next_token_and_check_type( parserData, T_COL ))) return res; // :
if ((res = get_next_token( &parserData->token ))) return res; // <type>
if ((res = st_add_type( &parserData->token, parserData->lhsId ))) return res;
if (!cg_declare_var( parserData )) return ERR_INTERNAL;
if ((res = get_next_token_and_apply_rule( parserData, "rule_optionalDefinition" ))) return res; // <optional definition>
if (parserData->inWhile) {
if (!cg_while_end_jump_over_declaration( parserData )) return ERR_INTERNAL;
}
parserData->lhsId = NULL;
parserData->currentFunc = NULL;
return rule_statementList( parserData );
}
// $3
// <statement> -> IF <expression> THEN <statement list> ELSE <statement list> END <statement list>
else if (parserData->token.attribute.keyword == KW_IF) { // IF
int currentIndex = parserData->ifIndex;
if ((res = get_next_token_and_apply_rule( parserData, "rule_expression" ))) return res; // <expression>
if (parserData->token.attribute.keyword != KW_THEN) return ERR_SYNTAX; // THEN
parserData->ifIndex++;
parserData->currentDepth++;
sts_push( &parserData->STStack, parserData->currentDepth );
if (!cg_if_header( currentIndex, parserData->insideFunc->id )) return ERR_INTERNAL;
if ((res = get_next_token_and_apply_rule( parserData, "rule_statementList" ))) return res; // <statement list>
if (parserData->token.attribute.keyword != KW_ELSE) return ERR_SYNTAX; // ELSE
if (!cg_if_else( currentIndex, parserData->insideFunc->id )) return ERR_INTERNAL;
st_dispose( &parserData->STStack.top->symTable );
if ((res = get_next_token_and_apply_rule( parserData, "rule_statementList" ))) return res; // <statement list>
sts_pop( &parserData->STStack );
parserData->currentDepth--;
if (parserData->token.attribute.keyword != KW_END) return ERR_SYNTAX; // END
if (!cg_if_end( currentIndex, parserData->insideFunc->id )) return ERR_INTERNAL;
if ((res = get_next_token( &parserData->token ))) return res;
return rule_statementList( parserData );
}
// $4
// <statement> -> WHILE <expression> DO <statement list> END <statement list>
else if (parserData->token.attribute.keyword == KW_WHILE) { // WHILE
int currentIndex = parserData->whileIndex;
if (!cg_while_header( currentIndex, parserData->insideFunc->id )) return ERR_INTERNAL;
if ((res = get_next_token_and_apply_rule( parserData, "rule_expression" ))) return res; // <expression>
if (parserData->token.attribute.keyword != KW_DO) return ERR_SYNTAX; // DO
if (!cg_while_condition( currentIndex, parserData->insideFunc->id )) return ERR_INTERNAL;
parserData->inWhile = true;
parserData->whileIndex++;
parserData->currentDepth++;
sts_push( &parserData->STStack, parserData->currentDepth );
if ((res = get_next_token_and_apply_rule( parserData, "rule_statementList" ))) return res; // <statement list>
if (parserData->token.attribute.keyword != KW_END) return ERR_SYNTAX; // END
if (!cg_while_end( currentIndex, parserData->insideFunc->id )) return ERR_INTERNAL;
sts_pop( &parserData->STStack );
parserData->currentDepth--;
parserData->inWhile = false;
if ((res = get_next_token( &parserData->token ))) return res;
return rule_statementList( parserData ); // <statement list>
}
// $5
// <statement> -> RETURN <return list>
else if (parserData->token.attribute.keyword == KW_RETURN) { // RETURN
if ((res = get_next_token_and_apply_rule( parserData, "rule_returnValues" ))) return res; // <return list>
return rule_statementList( parserData ); // <statement list>
}
// $6
// <statement> -> WRITE (<write argument list>)
else if (parserData->token.attribute.keyword == KW_WRITE) { // WRITE
SEARCH_GLOBAL( "write" );
if ((res = get_next_token_and_check_type( parserData, T_LBR ))) return res; // (
if ((res = get_next_token_and_apply_rule( parserData, "rule_writeArgumentList" ))) return res; // <write argument list>
if (parserData->token.type != T_RBR) return ERR_SYNTAX; // )
if ((res = get_next_token( &parserData->token ))) return res;
return rule_statementList( parserData );
}
// $
// <statement> -> ε
else {
return 0;
}
}
/*.......................................... IDENTIFIER LIST ..........................................*/
int rule_IDList ( Parser_data *parserData ) {
int res = 0; // ID
SEARCH_ALL_LOCAL( parserData->token.attribute.string->str );
if (res == 0) return ERR_SEMANTIC_UNDEF_VAR;
res = 0;
q_push( &parserData->queue, parserData->currentVar );
if ((res = get_next_token( &parserData->token ))) return res;
return rule_otherID( parserData ); // <other ID>
}
int rule_otherID ( Parser_data *parserData ) {
int res = 0;
if (parserData->token.type != T_COM) return res; // ,
if ((res = get_next_token_and_check_type( parserData, T_IDE ))) return res; // ID
SEARCH_GLOBAL( parserData->token.attribute.string->str );
if (res == 1) return ERR_SEMANTIC_UNDEF_VAR;
SEARCH_ALL_LOCAL( parserData->token.attribute.string->str );
if (res == 0) return ERR_SEMANTIC_UNDEF_VAR;
res = 0;
q_push( &parserData->queue, parserData->currentVar );
if ((res = get_next_token( &parserData->token ))) return res;
return rule_otherID( parserData ); // <other ID>
}
/*.......................................... ASSIGNMENT VALUES ..........................................*/
int rule_assignmentValues( Parser_data *parserData ) {
int res = 0;
if (parserData->token.type != T_ASS) return ERR_SYNTAX; // =
if ((res = get_next_token_and_apply_rule( parserData, "rule_assignmentValue" ))) return res; // <assignment value>
return rule_otherAssignmentValue( parserData ); // <other assignment value>
}
int rule_otherAssignmentValue( Parser_data *parserData ) {
int res = 0;
if (parserData->token.type != T_COM) return res; // ,
if ((res = get_next_token_and_apply_rule( parserData, "rule_assignmentValue"))) return res; // <assignment value>
return rule_otherAssignmentValue( parserData ); // <other assignment value>
}
int rule_assignmentValue ( Parser_data *parserData ) {
int res = 0;
// built-in function
if (IS_BUILTIN(parserData->token)) {
SEARCH_BUILTIN( parserData->token );
res = 0;
if (!cg_frame_to_pass_param( )) return ERR_INTERNAL;
if ((res = get_next_token_and_check_type( parserData, T_LBR ))) return res; // (
if ((res = get_next_token_and_apply_rule( parserData, "rule_functionCallParams" ))) return res; // <function call params>
if (parserData->token.type != T_RBR) return ERR_SYNTAX; // )
if (!cg_call( parserData->currentFunc->id )) return ERR_INTERNAL;
parserData->currentVar = q_pop( &parserData->queue ); // value return
Data_type retvalType = get_param_or_retval_type( parserData->currentFunc, 1, 0 );
if (parserData->currentVar->type != retvalType ) {
if (retvalType == T_INT && parserData->currentVar->type == T_NUM) {
if (!cg_convert_res_int2num( m_ret )) return ERR_INTERNAL;
}
else return ERR_SEMANTIC_FUNC_PAR;
}
if (!cg_get_retval( parserData->currentVar, 0 )) return ERR_INTERNAL;
}
else if (parserData->token.type == T_IDE) {
SEARCH_GLOBAL( parserData->token.attribute.string->str );
// function
if (res == 1) {
res = 0;
if (!cg_frame_to_pass_param( )) return ERR_INTERNAL;
if ((res = get_next_token_and_check_type( parserData, T_LBR ))) return res; // (
if ((res = get_next_token_and_apply_rule( parserData, "rule_functionCallParams" ))) return res; // <function call params>
if (parserData->token.type != T_RBR) return ERR_SYNTAX; // )
if (!cg_call( parserData->currentFunc->id )) return ERR_INTERNAL;
for (int i = 0; i < parserData->currentFunc->outputTypes->length; i++) { // value return
parserData->currentVar = q_pop( &parserData->queue );
Data_type retvalType = get_param_or_retval_type( parserData->currentFunc, 1, i );
if (parserData->currentVar->type != retvalType ) {
if (retvalType == T_INT && parserData->currentVar->type == T_NUM) {
if (!cg_convert_res_int2num( m_ret )) return ERR_INTERNAL;
}
else return ERR_SEMANTIC_FUNC_PAR;
}
if (!cg_get_retval( parserData->currentVar, i )) return ERR_INTERNAL;
}
}
// variable
else {
parserData->lhsId = q_pop( &parserData->queue );
if ((res = rule_expression( parserData ))) return res;
}
}
// constant
else {
parserData->lhsId = q_pop( &parserData->queue );
if ((res = rule_expression( parserData ))) return res;
}
return 0;
}
/*.......................................... FUNCTION CALL ..........................................*/
int rule_functionCallParams( Parser_data *parserData ) {
int res = 0;
parserData->currentFunc->ifuse = 1;
if (parserData->token.type == T_RBR) {
if (parserData->currentFunc->inputTypes->length == 0) return res;
else return ERR_SEMANTIC_FUNC_PAR;
}
parserData->paramIndex = 0;
if ((res = rule_callParam( parserData ))) return res; // <call param>
if ((res = get_next_token( &parserData->token ))) return res;
return rule_otherCallParam( parserData ); // <other call param>
}
int rule_otherCallParam ( Parser_data *parserData ) {
int res = 0;
if (parserData->token.type != T_COM) { // ,
if (parserData->currentFunc->inputTypes->length == parserData->paramIndex)
return res;
else return ERR_SEMANTIC_FUNC_PAR;
}
if ((res = get_next_token_and_apply_rule( parserData, "rule_callParam" ))) return res; // <call param>
if ((res = get_next_token( &parserData->token ))) return res;
return rule_otherCallParam( parserData ); // <other call param>
}
int rule_callParam ( Parser_data *parserData ) {
int res = 0;
if (parserData->currentFunc->inputTypes->length == parserData->paramIndex) return ERR_SEMANTIC_FUNC_PAR;
if (parserData->token.type == T_IDE) {
// parserData->currentVar <- search
SEARCH_ALL_LOCAL( parserData->token.attribute.string->str );
if (res == 0) return ERR_SEMANTIC_UNDEF_VAR;
res = 0;
if (parserData->currentVar->type != get_param_or_retval_type( parserData->currentFunc, 0, parserData->paramIndex )) return ERR_SEMANTIC_FUNC_PAR;
if (!cg_pass_param_light( parserData )) return ERR_INTERNAL;
}
else if (IS_VAL( parserData->token )) {
if (parserData->token.type != get_param_or_retval_type( parserData->currentFunc, 0, parserData->paramIndex )) return ERR_SEMANTIC_FUNC_PAR;
if (!cg_pass_param_light( parserData )) return ERR_INTERNAL;
}
else return ERR_SYNTAX;
parserData->paramIndex++;
return 0;
}
int rule_optionalDefinition ( Parser_data *parserData ) {
int res = 0;
// <optional definition> -> e
if (parserData->token.type != T_ASS) {
return res;
}
// <optional definition> -> = <value>
else {
if ((res = get_next_token_and_apply_rule( parserData, "rule_value" ))) return res;
}
return res;
}
int rule_value ( Parser_data *parserData ) {
int res = 0;
if ((parserData->token.type == T_IDE || parserData->token.type == T_KEY) || IS_VAL( parserData->token )
|| parserData->token.type == T_LBR || parserData->token.type == T_LEN) {
if (IS_VAL( parserData->token ) || parserData->token.type == T_LBR || parserData->token.type == T_LEN) {
if ((res = rule_expression( parserData ))) return res;
return res;
}
// <value> -> ID
if (parserData->token.type != T_KEY) SEARCH_ALL_LOCAL( parserData->token.attribute.string->str );
if (res == 1) { // not func