-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.c
1414 lines (1366 loc) · 51.3 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
/*****************************************************************************************
*
* IFJ - interpret of IFJ15 language
*
* parser module
* do a syntax analysis
*
* author: Miroslav Karásek (xkaras31)
* created: 2015-11-17
* modified: 2015-11-22
*
*****************************************************************************************/
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "pointerstack.h"
#include "instlist.h"
#include "pointerstack.h"
#include "scanner.h"
#include "parser.h"
#include "ial.h"
typedef enum {
LEFT_PR, // <
RIGHT_PR, // >
EQUAL_PR, // =
SUCESS_PR, // S
ERROR_PR, // E
} precedent_T;
// table with functions
static symbolTable_T functions;
static token_T token;
static int errorCode = 0;
// alocated memory
static pointerStack_T blocks;
static pointerStack_T symbols;
block_T *allocBlock(block_T *block)
{
if (!block)
{
block = malloc(sizeof(block_T));
if (!block)
errorCode = 99;
else
pushPS(&blocks, block);
}
if (block)
{
block->parrent = NULL;
initST(&block->symbols);
initIL(&block->program);
}
return block;
}
symbol_T *allocSymbol()
{
symbol_T *newSymbol = malloc(sizeof(symbol_T));
if (!newSymbol)
errorCode = 99;
else
pushPS(&symbols, newSymbol);
return newSymbol;
}
// stack for constants
pointerStack_T constants;
// LL gramatic rules
static bool programLL();
static bool functionListLL();
static bool functionLL();
static bool nextFunctionListLL();
static bool parametrListLL(symbol_T *function, bool firstTime);
static bool parametrLL(symbol_T *function, bool firstTime, int *number);
static bool nextParameterListLL(symbol_T *function, bool firstTime, int *number);
static bool fBlockLL(symbol_T *function);
static bool blockLL(block_T *block);
static bool statListLL(block_T *block);
static bool statLL(block_T *block);
static bool declarationLL(block_T *block);
static bool definitionLL(block_T *block, void **value);
static bool outListLL(block_T *block);
static bool nextOutListLL(block_T *block);
static bool inListLL(block_T *block);
static bool nextInListLL(block_T *block);
static bool rvalLL(block_T *block, void **value);
static bool callParameterListLL(block_T *block, symbol_T *function);
static bool callParameterLL(block_T *block, void **value, int *number, symbol_T *function);
static bool nextCallParameterLL(block_T *block, int *number, symbol_T *function);
// precedent analysis for expression processing
static bool expression(block_T *block, void **value);
// get token form scanner and print error if fail
bool getToken(token_T *token)
{
if (getTokenSC(token))
{
fprintf(stderr, "%u:%u: bad token format\n", token->row, token->col);
token->type = EOF_TO;
errorCode = 1; // lexical error
return false;
}
return true;
}
// print syntax error to stderr
static void syntaxError(token_T *token)
{
fprintf(stderr, "%u:%u: syntax error: unexpected token %d\n", token->row, token->col, token->type);
errorCode = 2; // syntax error
}
// biuld in functions creating
void insertBuildIn(symbol_T *function)
{
block_T *block = allocBlock(NULL);
insertST(&functions, "substr", (symbol_T) {FUNCTION_ST + 4, STRING_TY, 3, true, {.data = block}}); // string substr(string s, int i, int n)
insertST(&block->symbols, "$$0", (symbol_T){VARIABLE_ST, STRING_TY, 0, false, {.intValue = 0}});
insertST(&block->symbols, "$$1", (symbol_T){VARIABLE_ST, INT_TY, 0, false, {.intValue = 1}});
insertST(&block->symbols, "$$2", (symbol_T){VARIABLE_ST, INT_TY, 0, false, {.intValue = 2}});
block = allocBlock(NULL);
insertST(&functions, "length", (symbol_T) {FUNCTION_ST + 2, INT_TY, 1, true, {.data = block}}); // int length(string s)
insertST(&block->symbols, "$$0", (symbol_T){VARIABLE_ST, STRING_TY, 0, false, {.intValue = 0}});
block = allocBlock(NULL);
insertST(&functions, "sort", (symbol_T) {FUNCTION_ST + 1, STRING_TY, 1, true, {.data = block}}); // string sort(string s)
block = allocBlock(NULL);
insertST(&functions, "find", (symbol_T) {FUNCTION_ST + 5, INT_TY, 2, true, {.data = block}}); // int find(string s, string search)
insertST(&block->symbols, "$$0", (symbol_T){VARIABLE_ST, STRING_TY, 0, false, {.intValue = 0}});
block = allocBlock(NULL);
insertST(&functions, "concat", (symbol_T) {FUNCTION_ST + 3, STRING_TY, 2, true, {.data = block}}); // string concat(string s1, string s2)
insertST(&block->symbols, "$$0", (symbol_T){VARIABLE_ST, STRING_TY, 0, false, {.intValue = 0}});
insertST(&block->symbols, "$$1", (symbol_T){VARIABLE_ST, STRING_TY, 0, false, {.intValue = 1}});
}
// parse 'programFile' an generate list of instructions 'program'
int parse(FILE *programFile, block_T *block)
{
initST(&functions); // initialization of global symbol table
initPS(&blocks);
initSC(programFile); // initialiration of scanner
allocBlock(block);
// insert main function to table of functions
symbol_T *function = insertST(&functions, "main", (symbol_T) {FUNCTION_ST, INT_TY, 0, false, {.data = block}});
insertBuildIn(function);
initPS(&constants);
if (getToken(&token))
if (programLL()) // starting nonterminal
if (!function->defined)
errorCode = 3;
// !destruction
freeSC();
return errorCode;
}
// parse 'programFile' an generate list of instructions 'program'
void freeParser()
{
destroyST(&functions);
// destroy blocks
block_T *block;
while ((block = topPS(&blocks)) != NULL)
{
destroyST(&block->symbols);
destroyIL(&block->program);
free(block);
popPS(&blocks);
}
destroyPS(&blocks);
// destroy symbols
void *symbol;
while ((symbol = topPS(&symbols)) != NULL)
{
free(symbol);
popPS(&symbols);
}
destroyPS(&symbols);
}
// implementation of rules
bool programLL()
{
if (token.type == TYPE_TO)
{
// PROGRAM -> FUNCTION_LIST
if (functionListLL())
return true;
}
else
syntaxError(&token);
return false;
}
bool functionListLL()
{
if (token.type == TYPE_TO)
{
// FUNCTION_LIST -> FUNCTION NEXT_FUNCTION_LIST
if (functionLL())
if (nextFunctionListLL())
return true;
}
else
syntaxError(&token);
return false;
}
bool functionLL()
{
if (token.type == TYPE_TO)
{
// FUNCTION -> type fid ( PARAMETER_LIST ) FBLOCK
dataType_T dataType = token.dataType;
symbol_T *function;
if (getToken(&token))
{
if (token.type == ID_TO)
{
bool firstTime = true;
function = searchST(&functions, token.stringValue, true);
if (!function)
{
function = insertST(&functions, token.stringValue, (symbol_T){FUNCTION_ST, dataType, 0, false, {.data = NULL}});
if ((function->data = allocBlock(NULL)) == NULL)
return false;
}
else
{
firstTime = false;
if (function->dataType != dataType)
{
errorCode = 3;
fprintf(stderr, "return code of function %s is not same as in previous declaration\n", token.stringValue);
return false;
}
}
//printf("data type: %d\n", function->dataType);
if (getToken(&token))
{
if (token.type == LBRACKET_TO)
{
if (getToken(&token))
if (parametrListLL(function, firstTime))
{
if (token.type == RBRACKET_TO)
{
if (getToken(&token))
if (fBlockLL(function))
return true;
}
else
syntaxError(&token);
}
}
else
syntaxError(&token);
}
}
else
syntaxError(&token);
}
}
else
syntaxError(&token);
return false;
}
bool nextFunctionListLL()
{
if (token.type == TYPE_TO)
{
// NEXT_FUNCTION_LIST -> FUNCTION NEXT_FUNCTION_LIST
if (functionLL())
if (nextFunctionListLL())
return true;
}
else if (token.type == EOF_TO)
{
// NEXT_FUNCTION_LIST -> ε
return true;
}
else
syntaxError(&token);
return false;
}
bool parametrListLL(symbol_T *function, bool firstTime)
{
if (token.type == TYPE_TO)
{
int parameterNumber = 0;
// PARAMETER_LIST -> PARAMETER NEXT_PARAMETER_LIST
if (parametrLL(function, firstTime, ¶meterNumber))
if (nextParameterListLL(function, firstTime, ¶meterNumber))
{
if (parameterNumber != function->count)
{
errorCode = 3;
fprintf(stderr, "difirent parameter count\n");
return false;
}
return true;
}
}
else if (token.type == RBRACKET_TO)
{
// PARAMETER_LIST -> ε
return true;
}
else
syntaxError(&token);
return false;
}
bool parametrLL(symbol_T *function, bool firstTime, int *number)
{
if (token.type == TYPE_TO)
{
// PARAMETER -> type id
dataType_T dataType = token.dataType;
if (getToken(&token))
{
if (token.type == ID_TO)
{
char tempName[10];
sprintf(tempName, "$$%d", *number);
int offset = (*number)++;
//printf("%d %d\n", *number, offset);
if (!firstTime)
{
symbol_T *symbol;
symbol_T *symbol2;
if ((symbol = searchST(&((block_T *) function->data)->symbols, token.stringValue, false)) == NULL || symbol->dataType != dataType\
|| (symbol2 = searchST(&((block_T *) function->data)->symbols, tempName, false)) == NULL || symbol->intValue != symbol2->intValue)
{
errorCode = 3;
fprintf(stderr, "bad argument %s of function\n", token.stringValue);
return false;
}
}
else
{
insertST(&((block_T *) function->data)->symbols, token.stringValue, (symbol_T){VARIABLE_ST, dataType, 0, false, {.intValue = offset}});
insertST(&((block_T *) function->data)->symbols, tempName, (symbol_T){VARIABLE_ST, dataType, 0, false, {.intValue = offset}});
function->count++;
}
if (getToken(&token))
return true;
}
else
syntaxError(&token);
}
}
else
syntaxError(&token);
return false;
}
bool nextParameterListLL(symbol_T *function, bool firstTime, int *number)
{
if (token.type == COMMA_TO)
{
if (getToken(&token))
if (token.type == TYPE_TO)
{
// NEXT_PARAMETER_LIST -> , PARAMETER NEXT_PARAMETER_LIST
if (parametrLL(function, firstTime, number))
if (nextParameterListLL(function, firstTime, number))
return true;
}
}
else if (token.type == RBRACKET_TO)
{
// NEXT_PARAMETER_LIST -> ε
return true;;
}
else
syntaxError(&token);
return false;
}
bool fBlockLL(symbol_T *function)
{
if (token.type == LCBRACKET_TO)
{
// FBLOCK -> BLOCK
if (function->defined)
{
errorCode = 3;
fprintf(stderr, "redefinition of function\n");
}
function->defined = true;
block_T *block = function->data;
if (blockLL(block))
{
insertLastIL(&block->program, (instruction_T){RETURN_I, NULL, NULL, NULL});
if (errorCode)
return false;
return true;
}
}
else if (token.type == SEMI_TO)
{
// FBLOCK -> ;
if (getToken(&token))
return true;
}
else
syntaxError(&token);
return false;
}
bool blockLL(block_T *block)
{
if (token.type == LCBRACKET_TO)
{
// BLOCK -> { STAT_LIST }
if (getToken(&token))
if (statListLL(block))
{
if (token.type == RCBRACKET_TO)
{
if (getToken(&token))
return true;
}
else
syntaxError(&token);
}
}
else
syntaxError(&token);
return true;
}
bool statListLL(block_T *block)
{
if (token.type == LCBRACKET_TO || token.type == ID_TO || token.type == CIN_TO || token.type == COUT_TO || \
token.type == RETURN_TO || token.type == IF_TO || token.type == FOR_TO || token.type == TYPE_TO)
{
// STAT_LIST -> STAT STAT_LIST
if (statLL(block))
if (statListLL(block))
return true;
}
else if (token.type == RCBRACKET_TO)
{
// STAT_LIST -> ε
return true;
}
else
syntaxError(&token);
return false;
}
bool statLL(block_T *block)
{
if (token.type == LCBRACKET_TO)
{
// STAT -> BLOCK
block_T *newBlock = allocBlock(NULL);
if (newBlock == NULL)
return false;
instruction_T newInstruction;
newInstruction.type = BLOCK_I;
newInstruction.destination = newBlock;
newInstruction.source1 = NULL;
newInstruction.source2 = NULL;
insertLastIL(&block->program, newInstruction);
if (errorCode)
return false;
newBlock->symbols.parrent = &block->symbols;
newBlock->symbols.count = block->symbols.count;
newBlock->parrent = block;
if (blockLL(newBlock))
{
block->symbols.count = newBlock->symbols.count;
return true;
}
}
else if (token.type == ID_TO)
{
// STAT -> id = RVAL ;
instruction_T newInstruction;
newInstruction.type = ASSIGN_I;
symbol_T *symbol = searchST(&block->symbols, token.stringValue, true);
if (symbol == NULL)
{
fprintf(stderr, "udefined symbol %s\n", token.stringValue);
errorCode = 3;
return false;
}
newInstruction.destination = symbol;
if (getToken(&token))
{
if (token.type == ASSIGN_TO)
{
void *expressionOutput = symbol;
if (getToken(&token))
if (rvalLL(block, &expressionOutput))
{
newInstruction.source1 = expressionOutput;
newInstruction.source2 = NULL;
if (((symbol_T *) newInstruction.destination)->dataType == AUTO_TY)
((symbol_T *) newInstruction.destination)->dataType = ((symbol_T *) newInstruction.source1)->dataType;
if (token.type == SEMI_TO)
{
if (newInstruction.source1 != newInstruction.destination)
{
insertLastIL(&block->program, newInstruction);
if (errorCode)
return false;
}
if (getToken(&token))
return true;
}
else
syntaxError(&token);
}
}
else
syntaxError(&token);
}
}
else if (token.type == IF_TO)
{
// STAT -> if ( EXPRESSION ) BLOCK else BLOCK
instruction_T newInstruction;
newInstruction.type = IF_I;
if (getToken(&token))
{
if (token.type == LBRACKET_TO)
{
void *expressionOutput;
if (getToken(&token))
if (expression(block, &expressionOutput))
{
newInstruction.source1 = expressionOutput;
if (token.type == RBRACKET_TO)
{
block_T *newBlock = allocBlock(NULL);
if (newBlock == NULL)
return false;
newBlock->parrent = block;
newBlock->symbols.parrent = &block->symbols;
newBlock->symbols.count = block->symbols.count;
newInstruction.destination = newBlock;
if (getToken(&token))
if (blockLL(newInstruction.destination))
{
block->symbols.count = newBlock->symbols.count;
if (token.type == ELSE_TO)
{
newBlock = allocBlock(NULL);
if (newBlock == NULL)
return false;
newBlock->parrent = block;
newBlock->symbols.parrent = &block->symbols;
newBlock->symbols.count = block->symbols.count;
newInstruction.source2 = newBlock;
if (getToken(&token))
if (blockLL(newInstruction.source2))
{
block->symbols.count = newBlock->symbols.count;
insertLastIL(&block->program, newInstruction);
if (errorCode)
return false;
return true;
}
}
else
syntaxError(&token);
}
}
else
syntaxError(&token);
}
}
else
syntaxError(&token);
}
}
else if (token.type == FOR_TO)
{
// STAT -> for ( DECLARATION ; EXPRESSION ; id = EXPRESSION ) BLOCK
instruction_T newInstruction;
newInstruction.type = BLOCK_I;
block_T *newBlock = allocBlock(NULL);
if (newBlock == NULL)
return false;
block_T *tempBlock = allocBlock(NULL);
if (tempBlock == NULL)
return false;
newInstruction.destination = newBlock;
newInstruction.source1 = NULL;
newInstruction.source2 = NULL;
newBlock->symbols.parrent = &block->symbols;
newBlock->parrent = block;
newBlock->symbols.parrent = &block->symbols;
//printf("CO %d\n", block->symbols.count);
newBlock->symbols.count = block->symbols.count;
tempBlock->symbols.parrent = &newBlock->symbols;
if (getToken(&token))
{
if (token.type == LBRACKET_TO)
{
if (getToken(&token))
if (declarationLL(newBlock))
{
unsigned int declarationLength = newBlock->program.count;
if (token.type == SEMI_TO)
{
getToken(&token);
void *expressionOutput;
if (expression(newBlock, &expressionOutput))
{
insertLastIL(&newBlock->program, (instruction_T) {IF_NOT_BREAK_I, NULL, expressionOutput, NULL});
if (errorCode)
return false;
if (token.type == SEMI_TO)
{
getToken(&token);
if (token.type == ID_TO)
{
symbol_T *symbol = searchST(&newBlock->symbols, token.stringValue, true);
getToken(&token);
if (token.type == ASSIGN_TO)
{
tempBlock->symbols.count = newBlock->symbols.count;
if (getToken(&token))
if (expression(tempBlock, &expressionOutput))
{
newBlock->symbols.count = tempBlock->symbols.count;
if (symbol->dataType == AUTO_TY)
symbol->dataType = ((symbol_T *) expressionOutput)->dataType;
insertLastIL(&tempBlock->program, (instruction_T) {ASSIGN_I, symbol, expressionOutput, NULL});
if (errorCode)
return false;
if (token.type == RBRACKET_TO)
{
if (getToken(&token))
if (blockLL(newBlock))
{
block->symbols.count = newBlock->symbols.count;
startIL(&tempBlock->program);
instruction_T *instruction;
//printf("%d\n", tempBlock->program.count);
while ((instruction = getActiveIL(&tempBlock->program)) != NULL)
{
insertLastIL(&newBlock->program, *instruction);
if (errorCode)
return false;
nextIL(&tempBlock->program);
}
insertLastIL(&newBlock->program, (instruction_T) {JUMP_START_I, (void *) declarationLength, NULL, NULL});
insertLastIL(&block->program, newInstruction);
if (errorCode)
return false;
return true;
}
}
else
syntaxError(&token);
}
}
else
syntaxError(&token);
}
else
syntaxError(&token);
}
else
syntaxError(&token);
}
}
else
syntaxError(&token);
}
}
else
syntaxError(&token);
}
}
else if (token.type == RETURN_TO)
{
// STAT -> return EXPRESSION ;
void *expressionOutput;
if (getToken(&token))
if (expression(block, &expressionOutput))
{
if (token.type == SEMI_TO)
{
insertLastIL(&block->program, (instruction_T) {RETURN_I, NULL, expressionOutput, NULL});
if (errorCode)
return false;
if (getToken(&token))
return true;
}
else
syntaxError(&token);
}
}
else if (token.type == CIN_TO)
{
// STAT -> cin >> IN_LIST ;
//
if (getToken(&token))
{
if (token.type == IN_TO)
{
if (getToken(&token))
if (inListLL(block))
{
if (token.type == SEMI_TO)
{
if (getToken(&token))
return true;
}
else
syntaxError(&token);
}
}
else
syntaxError(&token);
}
}
else if (token.type == COUT_TO)
{
// STAT -> cout << OUT_LIST ;
if (getToken(&token))
{
if (token.type == OUT_TO)
{
if (getToken(&token))
if (outListLL(block))
{
if (token.type == SEMI_TO)
{
if (getToken(&token))
return true;
}
else
syntaxError(&token);
}
}
else
syntaxError(&token);
}
}
else if (token.type == TYPE_TO)
{
// STAT -> DECLARATION ;
//
if (declarationLL(block))
{
if (token.type == SEMI_TO)
{
if (getToken(&token))
return true;
}
else
syntaxError(&token);
}
}
else
syntaxError(&token);
return false;
}
bool declarationLL(block_T *block)
{
if (token.type == TYPE_TO)
{
// DECLARATION -> type id DEFINITION
symbol_T *symbol;
dataType_T dataType = token.dataType;
if (getToken(&token))
{
if (token.type == ID_TO)
{
//printf("CO %d\n", block->symbols.count);
int offset = block->symbols.count;
symbol = searchST(&block->symbols, token.stringValue, false);
if (symbol)
{
errorCode = 3;
fprintf(stderr, "redefinition of symbol %s\n", token.stringValue);
return false;
}
symbol = insertST(&block->symbols, token.stringValue, (symbol_T){VARIABLE_ST, dataType, 0, false, {.intValue = offset}});
//printf("%s %p offset %d\n",token.stringValue, symbol, offset);
void *value = NULL;
if (getToken(&token))
if (definitionLL(block, &value))
{
if (symbol->dataType == AUTO_TY)
{
if (value)
symbol->dataType = ((symbol_T *) value)->dataType;
else
{
errorCode = 5;
fprintf(stderr, "auto error\n");
return false;
}
}
if (value)
{
instruction_T newInstruction;
newInstruction.type = ASSIGN_I;
newInstruction.destination = symbol;
newInstruction.source1 = value;
newInstruction.source2 = NULL;
insertLastIL(&block->program, newInstruction);
if (errorCode)
return false;
}
return true;
}
}
else
syntaxError(&token);
}
}
else
syntaxError(&token);
return false;
}
bool definitionLL(block_T *block, void **value)
{
if (token.type == ASSIGN_TO)
{
// DEFINITION -> = EXPRESSION
if (getToken(&token))
if (expression(block, value))
return true;
}
else if (token.type == SEMI_TO)
{
// DEFINITION -> ε
*value = NULL;
return true;
}
else
syntaxError(&token);
return false;
}
bool outListLL(block_T *block)
{
if (token.type == ID_TO || token.type == INT_TO || token.type == DOUBLE_TO || token.type == STRING_TO)
{
// OUT_LIST -> CALL_PARAMETER NEXT_OUT_LIST
void *outputOperand;
instruction_T newInstruction;
newInstruction.type = PRINT_I;
if (callParameterLL(block, &outputOperand, NULL, NULL))
{
newInstruction.destination = NULL;
newInstruction.source1 = outputOperand;
newInstruction.source2 = NULL;
insertLastIL(&block->program, newInstruction);
if (errorCode)
return false;
if (nextOutListLL(block))
return true;
}
}
else
syntaxError(&token);
return false;
}
bool nextOutListLL(block_T *block)
{
if (token.type == OUT_TO)
{
// NEXT_OUT_LIST -> << CALL_PARAMETER NEXT_OUT_LIST
void *outputOperand;
instruction_T newInstruction;
newInstruction.type = PRINT_I;
if (getToken(&token))
if (callParameterLL(block, &outputOperand, NULL, NULL))
{
newInstruction.destination = NULL;
newInstruction.source1 = outputOperand;
newInstruction.source2 = NULL;
insertLastIL(&block->program, newInstruction);
if (errorCode)
return false;
if (nextOutListLL(block))
return true;
}
}
else if (token.type == SEMI_TO)
{
// NEXT_OUT_LIST -> ε
return true;
}
else
syntaxError(&token);
return false;
}
bool inListLL(block_T *block)
{
if (token.type == ID_TO)
{
// IN_LIST -> id NEXT_IN_LIST
instruction_T newInstruction;
newInstruction.type = SCAN_I;
symbol_T *symbol = searchST(&block->symbols, token.stringValue, true);
if (symbol == NULL)
{
errorCode = 3;
fprintf(stderr, "udefined symbol %s\n", token.stringValue);
return false;
}
newInstruction.destination = symbol;
newInstruction.source1 = NULL;
newInstruction.source2 = NULL;
insertLastIL(&block->program, newInstruction);
if (errorCode)
return false;
if (getToken(&token))
if (nextInListLL(block))
return true;
}
else
syntaxError(&token);
return false;
}
bool nextInListLL(block_T *block)
{
if (token.type == IN_TO)
{
// NEXT_IN_LIST -> >> id NEXT_IN_LIST
if (getToken(&token))
{
if (token.type == ID_TO)
{
instruction_T newInstruction;
newInstruction.type = SCAN_I;
symbol_T *symbol = searchST(&block->symbols, token.stringValue, true);
if (symbol == NULL)
{
errorCode = 3;
fprintf(stderr, "udefined symbol %s\n", token.stringValue);
return false;
}
newInstruction.destination = symbol;
newInstruction.source1 = NULL;
newInstruction.source2 = NULL;
insertLastIL(&block->program, newInstruction);
if (errorCode)
return false;
if (getToken(&token))
if (nextInListLL(block))
return true;
}
else
syntaxError(&token);
}
}
else if (token.type == SEMI_TO)
{
// NEXT_IN_LIST -> ε
return true;
}
else
syntaxError(&token);
return false;
}
bool rvalLL(block_T *block, void **value)
{
symbol_T *function;
//printf("%d\n", token.type);
if (token.type == ID_TO)
{
//printf("search %s\n", token.stringValue);
if ((function = searchST(&functions, token.stringValue, true)) != NULL)
{
if (function->defined)