-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparserCodeGenerator.c
More file actions
1723 lines (1491 loc) · 42.3 KB
/
parserCodeGenerator.c
File metadata and controls
1723 lines (1491 loc) · 42.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
COP3402 Homework #4: Parser & Code Generator Assignment
4/14/2017
Philip Rodriguez & Steven Chen
ph644520 & st140537
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/*
Note that actual capacities listed below are one
less due to null terminator requirements.
*/
#define MAX_INPUT_SIZE 32768
#define MAX_TOKEN_SIZE 64
#define MAX_SYMBOL_TABLE_SIZE 256
#define MAX_NUMBER_SIZE 5
/*
A set, in this program, is a size SET_SIZE integer array
filled with -1 for empty slots. Use setInit([set array])
to fill a new set with -1's.
*/
#define SET_SIZE 256
/*
If this is set to 0, then declared variables have
a default value of 0. If it is set to 1, then
declared variables must be assigned before they can
be used.
*/
#define ENFORCE_VARIABLE_ASSIGNMENT 0
// --------------------------------------------------Begin Random Additional Code--------------------------------------------------
void eprintf(char * message, int token)
{
FILE * errorFile = fopen("ef", "a");
fprintf(errorFile, "An error occurred while parsing token number %d: %s\n", token, message);
fclose(errorFile);
}
/*
This method appends to the error file the error with code [code].
*/
void error(int code, int token)
{
if (code == 1)
eprintf("Use of = instead of :=.\n", token);
if (code == 2)
eprintf("= must be followed by a number.\n", token);
if (code == 3)
eprintf("Identifier must be followed by =.\n", token);
if (code == 4)
eprintf("const, var, procedure must be followed by identifier.\n", token);
if (code == 5)
eprintf("Semicolon or comma missing.\n", token);
if (code == 6)
eprintf("Incorrect symbol after procedure declaration.\n", token);
if (code == 7)
eprintf("Statement expected.\n", token);
if (code == 8)
eprintf("Incorrect symbol after statement part in block.\n", token);
if (code == 9)
eprintf("Period expected.\n", token);
if (code == 10)
eprintf("Semicolon between statements missing.\n", token);
if (code == 11)
eprintf("Undeclared identifier.\n", token);
if (code == 12)
eprintf("Assignment to constant or procedure is not allowed.\n", token);
if (code == 13)
eprintf("Assignment operator expected.\n", token);
if (code == 14)
eprintf("Call must be followed by an identifier.\n", token);
if (code == 15)
eprintf("Call of a constant or a variable is meaningless.\n", token);
if (code == 16)
eprintf("then expected.\n", token);
if (code == 17)
eprintf("Semicolon expected.\n", token); // I modified this one to not include "or }" because that makes no sense to me.
if (code == 18)
eprintf("do expected.\n", token);
if (code == 19)
eprintf("Incorrect symbol following statement.\n", token);
if (code == 20)
eprintf("Relational operator expected.\n", token);
if (code == 21)
eprintf("Expression must not contain a procedure identifier.\n", token);
if (code == 22)
eprintf("Right parenthesis missing.\n", token);
if (code == 23)
eprintf("The preceding factor cannot begin with this symbol.\n", token);
if (code == 24)
eprintf("An expression cannot begin with this symbol.\n", token);
if (code == 25)
eprintf("This number is too large.\n", token);
if (code == 26)
eprintf("The input file is too large.\n", token);
if (code == 27)
eprintf("Call must be followed by an identifier for a procedure.\n", token);
if (code == 28)
eprintf("Expected a factor, did not find one!\n", token);
if (code == 29)
eprintf("Too many symbols, or a conflicting symbol was found!\n", token);
if (code == 30)
eprintf("Read and write require an identifier after them!\n", token);
if (code == 31)
eprintf("You can only read into a variable!\n", token);
if (code == 32)
eprintf("You can only write a variable or constant!\n", token);
if (code == 33)
eprintf("Use of unassigned variable!\n", token);
if (code == 34)
eprintf("Program requires too many registers to run on the vm!\n", token);
if (code == 35)
eprintf("Semicolon required after procedure declaration.\n", token);
if (code == 36)
eprintf("Semicolon required after procedure block.\n", token);
}
/*
This method does what it sounds like. It appends one character, [newChar], to
the end of the passed in string, [string]. Assumes the passed in string is
large enough to accomodate the extra character.
*/
void appendCharToString(char * string, char newChar)
{
int len = (int) strlen(string);
string[len] = newChar;
string[len+1] = '\0';
}
// --------------------------------------------------End Random Additional Code--------------------------------------------------
// --------------------------------------------------Begin File Management Code--------------------------------------------------
//Our files!
FILE * inputFile;
FILE * outputFile;
/*
This method will simply open the files, and thus populate
the variables inputFile and outputFile with meaningful
addresses.
*/
void openFiles(char* inputFilePath, char* outputFilePath)
{
inputFile = fopen(inputFilePath, "r");
outputFile = fopen(outputFilePath, "w");
}
// --------------------------------------------------End File Management Code--------------------------------------------------
// --------------------------------------------------Begin Lexeme List Management Code--------------------------------------------------
//The actual input file data handling...
char lexemeList[MAX_INPUT_SIZE];
/*
Clears the lexemeList array out with all null terminators.
*/
void clearLexemeList()
{
for(int i = 0; i < MAX_INPUT_SIZE; i++)
{
lexemeList[i] = '\0';
}
}
/*
This method will read from the file pointed to by inputFile, and
populate the variable lexemeList with the last line from the inputFile.
*/
void readInputFile()
{
//Clear the lexeme list because we're about to write to it
clearLexemeList();
//Seek to the end and get the length of the input file in characters...
fseek(inputFile, 0, SEEK_END);
int inputSize = (int) ftell(inputFile)-1;
//Seek back to the beginning
fseek(inputFile, 0, SEEK_SET);
//Are we within bounds?
if (inputSize > MAX_INPUT_SIZE)
{
error(26, -1);
}
//Iterate character by character through the file...
int i, place = 0;
for(i = 0; i < inputSize; i++)
{
//Read a character into lexemeList at place...
fscanf(inputFile, "%c", &lexemeList[place]);
//Was that character a newline? If so, clear the
//lexemeList (because there must be another line below)
if (lexemeList[place] == '\n')
{
place = -1;
clearLexemeList();
}
//Increment place...
place++;
}
//Make sure our lexemeList string has a null terminator at the end!
lexemeList[place] = '\0';
}
// --------------------------------------------------End Lexeme List Management Code--------------------------------------------------
// --------------------------------------------------Begin Token List Management Code--------------------------------------------------
/*
Here is a list of the token types
*/
int nulsym = 1;
int identsym = 2;
int numbersym = 3;
int plussym = 4;
int minussym = 5;
int multsym = 6;
int slashsym = 7;
int oddsym = 8;
int eqlsym = 9;
int neqsym = 10;
int lesssym = 11;
int leqsym = 12;
int gtrsym = 13;
int geqsym = 14;
int lparentsym = 15;
int rparentsym = 16;
int commasym = 17;
int semicolonsym = 18;
int periodsym = 19;
int becomesym = 20;
int beginsym = 21;
int endsym = 22;
int ifsym = 23;
int thensym = 24;
int whilesym = 25;
int dosym = 26;
int callsym = 27;
int constsym = 28;
int varsym = 29;
int procsym = 30;
int writesym = 31;
int readsym = 32;
int elsesym = 33;
//This list will hold our tokens.
char tokenList[MAX_INPUT_SIZE][MAX_TOKEN_SIZE];
/*
This method wipes out everything in [tokenList] with null terminators.
*/
void clearTokenList()
{
for(int t = 0; t < MAX_INPUT_SIZE; t++)
{
for(int tc = 0; tc < MAX_TOKEN_SIZE; tc++)
{
tokenList[t][tc] = '\0';
}
}
}
/*
This method goes through the contents of [lexemeList] and separates it
nicely into tokens in [tokenList].
*/
void populateTokenList()
{
clearTokenList();
int len = (int) strlen(lexemeList);
int curToken = 0;
for(int i = 0; i < len; i++)
{
if (lexemeList[i] == ' ')
{
curToken++;
continue;
}
else
{
appendCharToString(tokenList[curToken], lexemeList[i]);
}
}
}
/*
This method is for debugging sanity. It prints the contents of [tokenList] nicely.
*/
void printTokenList()
{
printf("Token list contents:\n");
for(int t = 0; t < MAX_INPUT_SIZE; t++)
{
if (strlen(tokenList[t]) > 0)
{
printf("Token %d: \"%s\"\n", t, tokenList[t]);
}
}
printf("\n");
}
/*
Returns an integer representing the type of the token
at the index [tokenIndex]. Returns 0 if an invalid token
is at [tokenIndex], meaning the token had no integer value.
*/
int getTokenType(int tokenIndex)
{
return atoi(tokenList[tokenIndex]);
}
// --------------------------------------------------End Token List Management Code--------------------------------------------------
// --------------------------------------------------Begin Symbol Table Code--------------------------------------------------
typedef struct symbol
{
int kind;
char name[12]; // Up to 11 characters, plus one for null terminator
int val;
int level;
int addr;
}
symbol;
symbol symbolTable[MAX_SYMBOL_TABLE_SIZE];
void clearSymbolTable()
{
for(int i = 0; i < MAX_SYMBOL_TABLE_SIZE; i++)
{
symbolTable[i].kind = -1;
for(int c = 0; c < 12; c++)
{
symbolTable[i].name[c] = '\0';
}
symbolTable[i].val = -1;
symbolTable[i].level = -1;
symbolTable[i].addr = -1;
}
}
/*
This method returns the index in the symbol table of the symbol
that has the name of name with the highest lexicographical level.
Returns -1 if the symbol does not exist in the symbol table.
*/
int findSymbolByName(char * name)
{
int best = -1;
for(int i = 0; i < MAX_SYMBOL_TABLE_SIZE; i++)
{
if (strcmp(name, symbolTable[i].name) == 0)
{
if (best == -1 || symbolTable[i].level > symbolTable[best].level)
best = i;
}
}
return best;
}
/*
Returns 1 if the symbol table had an entry with the name [name],
and the level [level].
*/
int containsConflict(char * name, int level)
{
for(int i = 0; i < MAX_SYMBOL_TABLE_SIZE; i++)
{
if (strcmp(name, symbolTable[i].name) == 0 && symbolTable[i].level == level)
{
return 1;
}
}
return 0;
}
/*
This method puts the symbol [s] in the first available spot in the symbol
table. Returns 1 if there was a spot, and 0 if the symbol table was full
or the symbol already existed at that level.
*/
int addSymbol(symbol s)
{
//If the symbol you are trying to add already existed, fail!
if (containsConflict(s.name, s.level))
return 0;
for(int i = 0; i < MAX_SYMBOL_TABLE_SIZE; i++)
{
if (symbolTable[i].kind == -1)
{
//This is a valid spot.
symbolTable[i].kind = s.kind;
strcpy(symbolTable[i].name, s.name);
symbolTable[i].val = s.val;
symbolTable[i].level = s.level;
symbolTable[i].addr = s.addr;
return 1;
}
}
return 0;
}
/*
Finds all symbols with level [level] and removes them from
the symbol table (purges the data in the spot).
*/
void removeSymbolsByLevel(int level)
{
for(int i = 0; i < MAX_SYMBOL_TABLE_SIZE; i++)
{
if (symbolTable[i].level == level)
{
//We want to remove the symbol at i...
symbolTable[i].kind = -1;
for(int c = 0; c < 12; c++)
{
symbolTable[i].name[c] = '\0';
}
symbolTable[i].val = -1;
symbolTable[i].level = -1;
symbolTable[i].addr = -1;
}
}
}
/*
This method prints out the symbol table.
*/
void printSymbolTable()
{
printf("Symbol table:\n");
for(int i = 0; i < MAX_SYMBOL_TABLE_SIZE; i++)
{
if (symbolTable[i].kind != -1)
{
printf("Entry at %d:\n\tkind = %d\n\tname = %s\n\tval = %d\n\tlevel = %d\n\taddr = %d\n", i, symbolTable[i].kind, symbolTable[i].name, symbolTable[i].val, symbolTable[i].level, symbolTable[i].addr);
}
}
printf("\n");
}
// --------------------------------------------------End Symbol Table Code--------------------------------------------------
// --------------------------------------------------Begin Set Stuff--------------------------------------------------
/*
So, a "set" in the world is an int array of size 256 (SET_SIZE) that maps to symbols...
*/
int firstFactor[SET_SIZE];
int firstTerm[SET_SIZE];
int firstExpression[SET_SIZE];
int firstCondition[SET_SIZE];
int firstStatement[SET_SIZE];
int firstProcedureDeclaration[SET_SIZE];
int firstVarDeclaration[SET_SIZE];
int firstConstDeclaration[SET_SIZE];
int firstBlock[SET_SIZE];
int firstProgram[SET_SIZE];
int followBlock[SET_SIZE];
int followConstantDeclaration[SET_SIZE];
int followVariableDeclaration[SET_SIZE];
int followProcedureDeclaration[SET_SIZE];
int followStatement[SET_SIZE];
int followCondition[SET_SIZE];
int followRelOp[SET_SIZE];
int followExpression[SET_SIZE];
int followTerm[SET_SIZE];
int followFactor[SET_SIZE];
void setInit(int * set)
{
for(int i = 0; i < SET_SIZE; i++)
set[i] = -1;
}
void populateSets()
{
setInit(firstFactor);
firstFactor[0] = identsym;
firstFactor[1] = numbersym;
firstFactor[2] = lparentsym;
setInit(firstTerm);
firstTerm[0] = identsym;
firstTerm[1] = numbersym;
firstTerm[2] = lparentsym;
setInit(firstExpression);
firstExpression[0] = plussym;
firstExpression[1] = minussym;
firstExpression[2] = identsym;
firstExpression[3] = numbersym;
firstExpression[4] = lparentsym;
setInit(firstCondition);
firstCondition[0] = oddsym;
firstCondition[1] = plussym;
firstCondition[2] = minussym;
firstCondition[3] = identsym;
firstCondition[4] = numbersym;
firstCondition[5] = lparentsym;
setInit(firstStatement);
firstStatement[0] = identsym;
firstStatement[1] = callsym;
firstStatement[2] = beginsym;
firstStatement[3] = ifsym;
firstStatement[4] = whilesym;
firstStatement[5] = readsym;
firstStatement[6] = writesym;
setInit(firstProcedureDeclaration);
firstProcedureDeclaration[0] = procsym;
setInit(firstVarDeclaration);
firstVarDeclaration[0] = varsym;
setInit(firstConstDeclaration);
firstConstDeclaration[0] = constsym;
setInit(firstBlock);
firstBlock[0] = constsym;
firstBlock[1] = varsym;
firstBlock[2] = procsym;
firstBlock[3] = identsym;
firstBlock[4] = callsym;
firstBlock[5] = beginsym;
firstBlock[6] = ifsym;
firstBlock[7] = whilesym;
firstBlock[8] = readsym;
firstBlock[9] = writesym;
setInit(firstProgram);
firstProgram[0] = constsym;
firstProgram[1] = varsym;
firstProgram[2] = procsym;
firstProgram[3] = identsym;
firstProgram[4] = callsym;
firstProgram[5] = beginsym;
firstProgram[6] = ifsym;
firstProgram[7] = whilesym;
firstProgram[8] = readsym;
firstProgram[9] = writesym;
firstProgram[10] = periodsym;
//Follow Sets
setInit(followBlock);
followBlock[0] = periodsym;
followBlock[1] = semicolonsym;
setInit(followConstantDeclaration);
followConstantDeclaration[0] = varsym;
followConstantDeclaration[1] = procsym;
followConstantDeclaration[2] = identsym;
followConstantDeclaration[3] = callsym;
followConstantDeclaration[4] = beginsym;
followConstantDeclaration[5] = ifsym;
followConstantDeclaration[6] = whilesym;
followConstantDeclaration[7] = readsym;
followConstantDeclaration[8] = writesym;
followConstantDeclaration[9] = periodsym;
followConstantDeclaration[10] = semicolonsym;
setInit(followVariableDeclaration);
followVariableDeclaration[0] = procsym;
followVariableDeclaration[1] = periodsym;
followVariableDeclaration[2] = identsym;
followVariableDeclaration[3] = callsym;
followVariableDeclaration[4] = beginsym;
followVariableDeclaration[5] = ifsym;
followVariableDeclaration[6] = whilesym;
followVariableDeclaration[7] = readsym;
followVariableDeclaration[8] = writesym;
followVariableDeclaration[9] = semicolonsym;
setInit(followProcedureDeclaration);
followProcedureDeclaration[0] = identsym;
followProcedureDeclaration[1] = callsym;
followProcedureDeclaration[2] = beginsym;
followProcedureDeclaration[3] = ifsym;
followProcedureDeclaration[4] = whilesym;
followProcedureDeclaration[5] = readsym;
followProcedureDeclaration[6] = writesym;
followProcedureDeclaration[7] = periodsym;
followProcedureDeclaration[8] = semicolonsym;
setInit(followStatement);
followStatement[0] = semicolonsym;
followStatement[1] = endsym;
followStatement[2] = elsesym;
followStatement[3] = periodsym;
setInit(followCondition);
followCondition[0] = dosym;
followCondition[1] = thensym;
setInit(followRelOp);
followRelOp[0] = plussym;
followRelOp[1] = minussym;
followRelOp[2] = identsym;
followRelOp[3] = numbersym;
followRelOp[4] = lparentsym;
setInit(followExpression);
followExpression[0] = eqlsym;
followExpression[1] = lesssym;
followExpression[2] = leqsym;
followExpression[3] = gtrsym;
followExpression[4] = geqsym;
followExpression[5] = neqsym;
followExpression[6] = dosym;
followExpression[7] = thensym;
followExpression[8] = semicolonsym;
followExpression[9] = endsym;
followExpression[10] = elsesym;
followExpression[11] = periodsym;
followExpression[12] = rparentsym;
setInit(followTerm);
followTerm[0] = eqlsym;
followTerm[1] = lesssym;
followTerm[2] = leqsym;
followTerm[3] = gtrsym;
followTerm[4] = geqsym;
followTerm[5] = neqsym;
followTerm[6] = dosym;
followTerm[7] = thensym;
followTerm[8] = semicolonsym;
followTerm[9] = endsym;
followTerm[10] = elsesym;
followTerm[11] = periodsym;
followTerm[12] = rparentsym;
followTerm[13] = plussym;
followTerm[14] = minussym;
setInit(followFactor);
followFactor[0] = multsym;
followFactor[1] = slashsym;
followFactor[2] = plussym;
followFactor[3] = minussym;
followFactor[4] = periodsym;
followFactor[5] = semicolonsym;
followFactor[6] = endsym;
followFactor[7] = elsesym;
followFactor[8] = thensym;
followFactor[9] = dosym;
followFactor[10] = eqlsym;
followFactor[11] = neqsym;
followFactor[12] = lesssym;
followFactor[13] = leqsym;
followFactor[14] = gtrsym;
followFactor[15] = geqsym;
followFactor[16] = rparentsym;
}
int setContains(int * set, int val)
{
for(int i = 0; i < SET_SIZE; i++)
{
if (set[i] == val)
return 1;
}
return 0;
}
void setUnion(int * result, int * a, int * b)
{
int tempresult[SET_SIZE];
setInit(tempresult);
int rp = 0;
for(int i = 0; i < SET_SIZE; i++)
{
if (a[i] != -1)
{
tempresult[rp] = a[i];
rp++;
}
}
for(int i = 0; i < SET_SIZE; i++)
{
if (b[i] != -1 && !setContains(tempresult, b[i]))
{
tempresult[rp] = b[i];
rp++;
}
}
//Copy tempresult into result.
for(int i = 0; i < SET_SIZE; i++)
{
result[i] = tempresult[i];
}
}
void setPrint(int * set)
{
printf("{");
for(int i = 0; i < SET_SIZE; i++)
{
if (set[i] != -1)
{
printf(" %d", set[i]);
}
}
printf(" }\n");
}
// --------------------------------------------------End Set Stuff--------------------------------------------------
// --------------------------------------------------Begin Parser/Code Generator Code--------------------------------------------------
/*
This variable represents the next open register at all times.
*/
int rc = 0;
void rcCheck()
{
if (rc > 16)
{
// If rc is 17, that implies we tried to store at location 16, which
// does not exist since our registers only go from 0-15!
error(34, -1);
//This is an error that cannot be recovered from!
exit(1);
}
}
typedef struct code
{
int op;
int r;
int l;
int m;
} code;
code code_array[MAX_INPUT_SIZE]; // change later
int cx = 0;
void clearCodeArray()
{
for(int i = 0; i < MAX_INPUT_SIZE; i++)
{
code_array[i].op = -1;
code_array[i].r = -1;
code_array[i].l = -1;
code_array[i].m = -1;
}
}
void printCodeArray()
{
printf("Code Generation:\n");
for(int i = 0; i < MAX_INPUT_SIZE; i++)
{
if (code_array[i].op == -1)
{
break;
}
else
{
printf("Code Entry %d: %d %d %d %d\n", i, code_array[i].op, code_array[i].r, code_array[i].l, code_array[i].m);
}
}
}
void writeCodeArray()
{
for(int i = 0; i < MAX_INPUT_SIZE; i++)
{
if (code_array[i].op == -1)
{
break;
}
else
{
fprintf(outputFile, "%d %d %d %d\n", code_array[i].op, code_array[i].r, code_array[i].l, code_array[i].m);
}
}
}
/*
This variable represents, globally, and at all times, the NEXT token
to be evaluated. Thus, to get the next token to be evaluated, just
use tokenList[curToken], which will be the string containing the next
token to be evaluated. Use curToken++ to move up one token, etc...
Also! There is a method above called getTokenType that takes an index,
which curToken is, and returns the type (number) of that token. It is
useful for checking if your token is a periodsym, for example, by doing
if (getTokenType(curToken) == periodsym).
*/
int curToken = 0;
void test(int * firstset, int * stopset, int errorCode)
{
if (!setContains(firstset, getTokenType(curToken)))
{
//printf("Skipping on token %d\n", curToken);
error(errorCode, curToken);
while (!setContains(firstset, getTokenType(curToken)) && !setContains(stopset, getTokenType(curToken)))
{
//This is statement serves to prevent runaway! Without this, some calls to test may cause a segfault!
if (curToken >= MAX_INPUT_SIZE || strlen(tokenList[curToken]) <= 0)
{
return;
}
curToken++;
}
//printf("Skipped to token %d\n", curToken);
}
}
/*
This variable should represent the current lexicographical level at all times...
Starts at -1 because main's block will increment it by 1 to get 0...
*/
int lexLevel = -1;
void emit(int op, int r, int l, int m);
void doTheAwesomeParsingAndCodeGenerating();
void program();
void block(int * stopset);
void statement(int * stopset);
void condition(int * stopset);
void expression(int * stopset);
void term(int * stopset);
void factor(int * stopset);
void emit(int op, int r, int l, int m)
{
if (cx > MAX_INPUT_SIZE)
{
//Too much code generated!
error(26, curToken);
}
else
{
code_array[cx].op = op;
code_array[cx].r = r;
code_array[cx].l = l;
code_array[cx].m = m;
cx++;
}
}
void doTheAwesomeParsingAndCodeGenerating()
{
program();
}
void program()
{
//So, if block has a block-level failure, then scan up until you hit a
//follow symbol for block, or a period, since we expect a period next!
int pass[SET_SIZE];
setInit(pass);
pass[0] = periodsym;
setUnion(pass, pass, followBlock);
block(pass);
//If current token is NOT a period
if (getTokenType(curToken) != periodsym)
{
//Program must end with a period
error(9, curToken);
}
//Always make the last instruction a program termination just in case!
emit(11, 0, 0, 3);
}
void block(int * stopset)
{
symbol s;
//Increment lexLevel and offsetCounter; generate code to increment for FV, SL, DL, RA
//We strictly set to 4 because it should always begin at 4 for a block...
lexLevel += 1;
int offsetCounter = 4;
int blockJump = cx;
emit(7, 0, 0, 0);
//A block can be a constant-declaration, variable declaration, or a statement
//If current token is a constant symbol...
if (getTokenType(curToken) == constsym)
{
do
{
curToken++;
if (getTokenType(curToken) != identsym)
{
//Expected an identifier after constsym...
error(4, curToken);
}
else
{
curToken++;
//Symbol table kind
s.kind = 1;
//Symbol table identifier
strcpy(s.name, tokenList[curToken]);
curToken++;
if (getTokenType(curToken) != eqlsym)
{
//Expected equal symbol (NOT become symbol because it's constant declaration)
error(3, curToken);
}
else
{
curToken++;
}
if (getTokenType(curToken) != numbersym)
{
//Expected a number!
error(2, curToken);
}
else
{
curToken++;
//Symbol table value
s.val = atoi(tokenList[curToken]);
s.level = lexLevel;
s.addr = offsetCounter;
offsetCounter++;
curToken++;
if (!addSymbol(s))
{
//Symbol table full or conflicting symbol!
error(29, curToken);
}
}
}
}
while(getTokenType(curToken) == commasym);
//Now we expect a semicolon...
if (getTokenType(curToken) != semicolonsym)
{
//Missing a semicolon!
error(5, curToken);
}
else
{
curToken++;
}
}
//If we've got a variable declaration
if (getTokenType(curToken) == varsym)
{
do
{
curToken++;
if (getTokenType(curToken) != identsym)
{
//Following var we expected an identifier...
error(4, curToken);
}
else
{
curToken++;
//Symbol table kind
s.kind = 2;
//Symbol table identifier
strcpy(s.name, tokenList[curToken]);