forked from domhoward14/module_3
/
parser_generator.c
1706 lines (1547 loc) · 34.3 KB
/
parser_generator.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
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include "ArrayList.c"
#define MAX_SYMBOL_TABLE_SIZE 100
#define cons 1
#define var 2
#define proc 3
#include"module_1.h"
#define MAX_STACK_HEIGHT 2000
#define MAX_CODE_LENGTH 500
#define MAX_LEXI_LEVELS 3
#define NEXT_INSTRUCTION 3
enum symbols
{
nulsym = 1, identsym = 2, numbersym = 3, plussym = 4,
minussym = 5, multsym = 6, slashsym = 7, oddsym = 8,
eqlsym = 9, neqsym = 10, lessym = 11, leqsym = 12,
gtrsym = 13, geqsym = 14, lparentsym = 15, rparentsym = 16,
commasym = 17, semicolonsym = 18, periodsym = 19, becomessym
= 20, beginsym = 21, endsym = 22, ifsym = 23, thensym = 24,
whilesym = 25, dosym = 26, callsym = 27, constsym = 28,
varsym = 29, procsym = 30, writesym = 31, readsym = 32,
elsesym = 33,
};
typedef struct symbol {
int kind; // const = 1, var = 2, proc = 3
char name[12]; // name up to 11 chars
int val; // number (ASCII value)
int level; // L level
int addr; // M address
} symbol;
typedef struct Token
{
char word [30];
int sym;
int value;
}Token;
const char *errors[30] =
{
"period expected.",
//#1
"identifier expected",
//#2
"equal sign expected",
//#3
"number expected",
//#4
"semi-colon expected",
//#5
"becomes symbol expected",
//#6
"end expected",
//#7
"do keyword after while loop expected",
//#8
"relational operator expected",
//#9
"parenthesis is not closed",
//#10
"some factor symbol is expected",
//#11
"Variable has not been previously declared"
};
FILE *lexemeTableFile;
FILE *emitWriter;
Token tokenList [1000];
Token token;
int tokenSpot = 0;
int tokenCount = 0;
int symbolSpot = 0;
int symbolretriever;
int symbolTableSize = 0;
int codeSpot = 0;
int code3 [3000];
//THE START OF MOD2 CODE
char buffer[256];
FILE *codeFile;
FILE *output;
FILE *lexemeTable;
FILE *lexemeList;
int structIndex = 0;
int getter;
int codeCount = 0;
Token *tokenArray;
ArrayList *codeArray;
int tokenArrayCount = 0;
symbol symbol_table[MAX_SYMBOL_TABLE_SIZE];
symbol symbolHolder;
struct Instruction
{
int op;
int l;
int m;
};
int lexiLevel = 0;
int partition = 0;
int codeSize = 0;
int buffer1 = 0;
int sp = 0;
int bp = 1;
int pc = 0;
int stack [MAX_STACK_HEIGHT ];
int code [MAX_CODE_LENGTH ];
FILE *codeFile;
FILE *output;
char *opString;
struct Instruction ir;
int main()
{
project1();
program2();
project3();
return 0;
}
// THE START OF MOD3 CODE
void printOut()
{
printf("\nOUTPUT :");
printf("\n");
printf("OP\t");
printf("L\t");
printf("M\n");
int i =0;
while(i < codeSpot)
{
printf("%d\t%d\t%d\n",code3[(i *3)], code3[(i *3) + 1], code3[(i *3) + 2]);
fprintf(emitWriter, "%d %d %d \n", code3[(i *3)], code3[(i *3) + 1], code3[(i *3) + 2]);
i++;
}
}
void load()
{
char string [20];
char string2 [20];
int number;
lexemeTableFile = fopen("lexemeTable.txt", "r");
fscanf(lexemeTableFile, "%s %s", string, string2);
while(fscanf(lexemeTableFile, "%s %d", string, &number) != EOF)
{
if(number == 3)
tokenList[tokenCount].value = atoi(string);
else
tokenList[tokenCount].value = 0;
//printf("%s %d\n", string, tokenCount);
strcpy(tokenList[tokenCount].word,string);
tokenList[tokenCount].sym = number;
tokenCount += 1;
}
}
void program2()
{
load();
initEmit();
getToken();
block();
if (token.sym != periodsym)
getError(0);
emit(9, 0, 2);
printOut();
closeEmit();
printf("The program is syntactically correct.");
}
void block()
{
int varCount = 0;
//constant symbol function
if (token.sym == constsym)
{
do
{
getToken();
if(token.sym != identsym)
getError(1);
strcpy(symbolHolder.name, token.word);
getToken();
if(token.sym != eqlsym)
getError(2);
getToken();
if(token.sym != numbersym)
getError(3);
enter(cons, symbolHolder.name, 0, 0, token.value);
getToken();
}
while (token.sym == commasym);
if(token.sym != semicolonsym)
getError(4);
getToken();
}
//variable symbol function
if(token.sym == varsym)
{
do
{
getToken();
if(token.sym != identsym)
getError(1);
enter(var, token.word, 0, 4 + varCount, 0);
varCount ++;
getToken();
}
while(token.sym == commasym);
if(token.sym != semicolonsym)
getError(4);
getToken();
}
if(token.sym == procsym)
{
do
{
getToken();
if(token.sym != identsym)
getError(1);
enter(proc, token.word, 0, codeSpot, token.sym);
getToken();
if(token.sym != semicolonsym)
getError(4);
getToken();
block();
if(token.sym != semicolonsym)
getError(4);
getToken();
}
while(token.sym == procsym);
}
statement(varCount);
}
void getToken()
{
token = tokenList[tokenSpot];
//printf("This token being read is %s\n", token.word);
tokenSpot ++;
}
void getError(int error)
{
printf("%s", errors[error]);
exit(error);
}
void statement(int varCount)
{
int changeSpot = 0;
int sym;
symbol localSymbolHolder;
int localVarCount = varCount;
if(token.sym == identsym)
{
//printf("\n\nThe token word is : %s\n\n", token.word);
//printf("The symbol word is : %s\n", symbol_table[1].name);
if(isSymbol(token.word))
localSymbolHolder = symbol_table[getSymbol(token.word)];
else
getError(11);
getToken();
if(token.sym != becomessym)
getError(5);
getToken();
expression();
enter(var,localSymbolHolder.name,0,0,getTop());
emit(4, 0, localSymbolHolder.addr);
// printf("\n\nThe TOS IS %d\n\n", getTop());
}
else if(token.sym == callsym)
{
getToken();
if(token.sym != identsym)
getError(2);
getToken();
}
else if(token.sym == beginsym)
{
emit(6, 0, 4 + localVarCount);
getToken();
statement(localVarCount);
while(token.sym == semicolonsym)
{
getToken();
statement(localVarCount);
}
if(token.sym != endsym)
getError(6);
getToken();
}
else if(token.sym == ifsym)
{
getToken();
changeSpot = condition();
if(token.sym != thensym)
getError(6);
getToken();
statement(localVarCount);
}
else if(token.sym == whilesym)
{
getToken();
condition();
if(token.sym != dosym)
getError(7);
getToken();
statement(localVarCount);
}
// printf("\n\nThe change spot and codespot is: %d, %d\n\n", changeSpot, codeSpot);
if(changeSpot != 0)
code3[(changeSpot * 3) + 2] =codeSpot;
}
int condition()
{ Token op;
int changeSpot = 0;
if(token.sym == oddsym)
{
getToken();
expression();
//odd command
emit(2, 0, 6);
}
else
{
expression();
if(!isRelation())
getError(8);
op = token;
getToken();
expression();
changeSpot = writeRelation(op.sym);
}
return changeSpot;
}
void expression()
{
if (token.sym == plussym)
{
getToken();
term();
//add
emit(2, 0, 2);
}
else if (token.sym == minussym)
{
getToken();
term();
//sub
emit(2, 0, 3);
}
else
term();
while(token.sym == plussym || token.sym == minussym)
{
if (token.sym == plussym)
{
getToken();
term();
//add
emit(2, 0, 2);
}
else if (token.sym == minussym)
{
getToken();
term();
//sub
emit(2, 0, 3);
}
}
}
void term()
{
factor();
while(token.sym == multsym || token.sym == slashsym)
{
if (token.sym == multsym)
{
getToken();
factor();
//mult
emit(2, 0, 4);
}
else if (token.sym == slashsym)
{
getToken();
factor();
//div
emit(2, 0, 5);
}
}
}
void factor ()
{
if (token.sym == identsym)
{
emit(1,0,getSymbolValue(token.word));
getToken();
}
else if(token.sym == numbersym)
{
emit(1, 0, atoi(token.word));
getToken();
}
else if(token.sym == '(')
{
getToken();
expression();
if(token.sym != ')')
getError(9);
getToken();
}
else
getError(10);
}
int writeRelation(int opSym)
{
//write the correlated emit code for the op symbol
switch(opSym)
{
case 4:
emit(2, 0, 2);
emit(8, 0, 0);
return codeSpot - 1;
break;
case 5:
emit(2, 0, 3);
emit(8, 0, 0);
return codeSpot - 1;
break;
case 6:
emit(2, 0, 4);
emit(8, 0, 0);
return codeSpot - 1;
break;
case 7:
emit(2, 0, 5);
emit(8, 0, 0);
return codeSpot - 1;
break;
case 8:
emit(2, 0, 6);
emit(8, 0, 0);
return codeSpot - 1;
break;
case 9:
emit(2, 0, 8);
emit(8, 0, 0);
return codeSpot - 1;
break;
case 10:
emit(2, 0, 9);
emit(8, 0, 0);
return codeSpot - 1;
break;
case 11:
emit(2, 0, 10);
emit(8, 0, 0);
return codeSpot - 1;
break;
case 12:
emit(2, 0, 11);
emit(8, 0, 0);
return codeSpot - 1;
break;
case 13:
emit(2, 0, 12);
emit(8, 0, 0);
return codeSpot - 1;
break;
case 14:
emit(2, 0, 13);
emit(8, 0, 0);
return codeSpot - 1;
break;
default:
getError(12);
return 12;
}
}
void initEmit()
{
//setup the file pointer for emit to write to
emitWriter = fopen("mcode.txt", "w");
}
void emit(int op, int l, int m)
{
code3[codeSpot * 3] = op;
code3[(codeSpot * 3) + 1] = l;
code3[(codeSpot * 3) + 2] = m;
codeSpot ++;
//printf("%d %d %d \n", op, l, m);
}
void closeEmit()
{
fclose(emitWriter);
/*int i = 0;
while (i < symbolSpot)
{
printf("%s,", symbol_table[i].name);
printf("%d\n", symbol_table[i].val);
i ++;
}
*/
}
void enter(int kind, char *name, int l, int m, int val)
{
symbol tempSymbolHolder;
tempSymbolHolder.kind = kind;
strcpy(tempSymbolHolder.name, name);
tempSymbolHolder.level = l;
tempSymbolHolder.addr = m;
tempSymbolHolder.val = val;
//printf("The name that is being inputed is : %s", tempSymbolHolder.name);
if(isInTable(tempSymbolHolder.name) == 0)
{
symbol_table[symbolSpot] = tempSymbolHolder;
symbolSpot ++;
symbolTableSize ++;
//printf("The name that is being inputed is : %s\n", tempSymbolHolder.name);
//printf("The table size is being increased\n");
}
//update value if already in the table
else
{
symbol_table[getSymbol(name)].val = val;
// printf(" It is working for symbol %s and The value of the symbol is : %d\n", symbol_table[getSymbol(name)].name, symbol_table[getSymbol(name)].val);
}
}
int isInTable(char *name)
{
int i =0;
// printf("\nThe counter is at : %d and the table size is at : %d\n", i, symbolTableSize);
while(i <= symbolTableSize)
{
// printf("The name of the parameter is : %s\n", name);
//printf("The name of the symbol name is %s\n", symbol_table[i].name);
if(strcmp(name, symbol_table[i].name) == 0 )
{
return 1;
}
i ++;
}
return 0;
}
int getSymbol(char *name)
{
int i =0;
while(i < symbolTableSize)
{
if(strcmp(name, symbol_table[i].name) == 0 )
return i;
i ++;
// printf("The symbol %s has a position of %d\n", symbol_table[0].name, symbol_table[0].addr);
// printf("The symbol %s has a position of %d\n", symbol_table[1].name, symbol_table[1].addr);
}
return 0;
}
int getSymbolValue(char *name)
{
int i =0;
// printf("the token name the value that is being fetched is : %s\n", name);
while(i < symbolTableSize)
{
if(strcmp(name, symbol_table[i].name) == 0 )
return symbol_table[i].val;
i ++;
}
getError(11);
return 0;
}
int getTop()
{
return code3[((codeSpot - 1) * 3) + 2];
}
int isSymbol(char *name)
{
int i =0;
while(i < symbolTableSize)
{
if(strcmp(name, symbol_table[i].name) == 0)
return 1;
i ++;
}
return 0;
}
int isRelation()
{
switch(token.sym)
{
case 4:
return 1;
case 5:
return 1;
case 6:
return 1;
case 7:
return 1;
case 8:
return 1;
case 9:
return 1;
case 10:
return 1;
case 11:
return 1;
case 12:
return 1;
case 13:
return 1;
case 14:
return 1;
default:
getError(12);
return 0;
}
}
//THE START OF MOD 1 CODE
void project3()
{
output = fopen("output.txt", "w");
initArray();
//printf("It initialized the arrays\n");
load3();
//testFunction();
format2();
while(1==1)
{
fetch();
execute();
}
}
void load3()
{
stack[1] = 0;
stack[2] = 0;
stack[3] = 0;
getCode();
format();
fclose(codeFile);
}
void format ()
{
// printf("The codesize is : %d\n", codeSpot);
fprintf(output,"Line\t");
fprintf(output,"OP\t");
fprintf(output,"L\t");
fprintf(output,"M\n");
int i =0;
while(i < codeSpot)
{
fprintf(output, "%d\t%d\t%d\t%d\n",i, code3[(i *3)], code3[(i *3) + 1], code3[(i *3) + 2]);
i++;
}
// printf("sucessfully formatted\n");
}
void getCode()
{
int i = 0;
codeFile = fopen("mcode.txt", "r");
while(fscanf(codeFile, "%d", &buffer1)!=EOF)
{
code[i]=buffer1;
codeSize++;
//testing purposes
//printf("%d", code[i]);
i++;
if(i % 3 == 0)
printf("\n");
}
}
void fetch ()
{
getInstruction();
pc ++;
//printf("After it is %d \n",pc);
}
void format2()
{
fprintf(output, "\t\t\t\tpc\t\tbp\tsp\tstack\n");
fprintf(output, "Initial Values\t\t\t0\t\t1\t0\n");
}
void getInstruction()
{
//For testing purposes
//printf("during the fetch pc is %d \n",pc);
//print out the instruction number, opcode string, l, and m.
ir.op = code[pc * NEXT_INSTRUCTION];
ir.l = code[pc * NEXT_INSTRUCTION + 1];
ir.m = code[pc * NEXT_INSTRUCTION + 2];
opString = getOpString(ir.op);
fprintf(output, "%d\t%s\t%d\t%d", pc, opString, ir.l, ir.m );
}
char *getOpString(int op)
{
switch (op)
{
//lit
case 1:
return "lit";
break;
// opr will require another sub-function to decide
// which operation to run
case 2:
return "opr";
break;
//lod
case 3:
return "lod";
break;
//sto
case 4:
return "sto";
break;
//cal
case 5:
return "cal";
break;
//inc
case 6:
return "inc";
break;
//jmp
case 7:
return "jmp";
break;
//jpc
case 8:
return "jpc";
break;
//sio
//this will require another sub function to decide
//which i/o to run
case 9:
return "sio";
break;
default:
fprintf(output, "OP code input was invalid. ");
sio3();
}
}
int isDone ()
{
if(code[pc * NEXT_INSTRUCTION] == 9 && code[pc * NEXT_INSTRUCTION + 2] == 3)
return 1;
return 0;
}
void execute()
{
switch ( ir.op)
{
//lit
case 1:
lit(ir.m);
break;
// opr will require another sub-function to decide
// which operation to run
case 2:
opr();
break;
//lod
case 3:
lod(ir.l, ir.m);
break;
//sto
case 4:
sto(ir.l, ir.m);
break;
//cal
case 5:
cal(ir.l, ir.m);
break;
//inc
case 6:
inc(ir.m);
break;
//jmp
case 7:
jmp(ir.m);
break;
//jpc
case 8:
jpc(ir.m);
break;
//sio
//this will require another sub function to decide
//which i/o to run
case 9:
sio();
break;
default:
fprintf(output, "OP code input was invalid. ");
sio3();
}
//print pc, bp, sp
//use loop to print stack
fprintf(output, "\t%d\t\t%d\t%d\t", pc, bp, sp);
stackPrint();
}
//initializes the stack and code arrays.
void initArray()
{
for(int i = 0; i < MAX_STACK_HEIGHT; i ++)
stack[0] = 0;
for(int i = 0; i < MAX_CODE_LENGTH; i ++)
code[0] = 0;
}
//iterates through the dynamic link values to return the desired AR.
int getBase(int level, int base)
{
for(int i = level; i > 0; i --)
{
base = stack[base + 2];
}
return base;
}
//this is simply a push function
void lit (int pushValue)
{
sp ++;
stack[sp] = pushValue;
}
void testFunction()
{
printf(" the very last number should be 3? = %d", code[50]);
}
//lod function will do 3 things
//get the base value L levels down
//grab the number at offset M
//push that number
void lod (int levels, int offset)
{
int base = getBase(levels, bp);
int value = stack[base + offset];
lit(value);
}
//this function is simply a store function
void sto (int levels, int offset)
{
int base = getBase(levels, bp);
stack [base + offset] = stack[sp];
sp --;
}
void cal (int levels, int pro_Location)
{
stack[sp + 1] = 0;
stack[sp + 2] = getBase(levels, bp);
stack[sp + 3] = bp;
stack[sp + 4] = pc;
bp = sp + 1;
pc = pro_Location;
}
void inc (int space)
{
sp += space;
lexiLevel ++;
}
void jmp (int jumpSpot)
{
pc = jumpSpot;
}
void jpc (int jumpSpot)
{
if (stack[sp] == 0)
pc = jumpSpot;
sp --;
}
//this is the first standard i/o function which will pop and print
void sio1()
{
printf("%d" ,stack[sp]);
sp --;
}
//the second i/o function will take in input and push it to the top of the stack
void sio2()
{
int input;
scanf("%d" ,&input);
lit(input);
}
//Third standard i/o halts the system
void sio3()
{
fprintf(output, "\t%d\t\t%d\t%d\t", pc, bp, sp);
stackPrint();
exit(0);
}
void stackPrint()
{
if(lexiLevel == 1)
{
for(int i = 1; i <= sp; i ++)
fprintf(output, "%d ", stack[i]);
fprintf(output, "\n");
}
else if(lexiLevel == 2)
{
for(int i = 1; i <= sp; i ++)
{
if(i == bp)
fprintf(output, "| ");
fprintf(output, "%d ", stack[i]);
}
fprintf(output, "\n");
}
else
{
int base = getBase(1, bp);
for(int i = 1; i <= sp; i ++)
{
if(i == bp || base)
fprintf(output, "| ");
fprintf(output, "%d ", stack[i]);
}
fprintf(output, "\n");
}
}
void ret ()
{
sp = bp - 1;
pc = stack[sp + 4];
bp = stack[sp + 3];
lexiLevel --;
}