-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.c
888 lines (805 loc) · 22.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
// Noah Beightol,COP 3402, Fall 2022, UCF ID 5388648
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define ARRAY_SIZE 500
#define MAX_IDT_LEN 11
typedef enum token_type {
identifier = 1, number, keyword_const, keyword_var, keyword_procedure,
keyword_call, keyword_begin, keyword_end, keyword_if, keyword_then,
keyword_else, keyword_while, keyword_do, keyword_read, keyword_write,
keyword_def, period, assignment_symbol, minus, semicolon,
left_curly_brace, right_curly_brace, equal_to, not_equal_to, less_than,
less_than_or_equal_to, greater_than, greater_than_or_equal_to, plus, times,
division, left_parenthesis, right_parenthesis
} token_type;
typedef enum opcode_name {
LIT = 1, OPR = 2, LOD = 3, STO = 4, CAL = 5, INC = 6, JMP = 7, JPC = 8,
SYS = 9, WRT = 1, RED = 2, HLT = 3,
RTN = 0, ADD = 1, SUB = 2, MUL = 3, DIV = 4, EQL = 5, NEQ = 6,
LSS = 7, LEQ = 8, GTR = 9, GEQ = 10
} opcode_name;
typedef struct lexeme {
token_type type;
char identifier_name[12];
int number_value;
int error_type;
} lexeme;
typedef struct instruction {
int op;
int l;
int m;
} instruction;
typedef struct symbol {
int kind;
char name[12];
int value;
int level;
int address;
int mark;
} symbol;
lexeme *tokens;
int token_index = 0;
symbol *table;
int table_index = 0;
instruction *code;
int code_index = 0;
int error = 0;
int level;
//global count for read in tokens
int count = 0;
void emit(int op, int l, int m);
void add_symbol(int kind, char name[], int value, int level, int address);
void mark();
int multiple_declaration_check(char name[]);
int find_symbol(char name[], int kind);
void block();
void program();
int declarations();
void varFunc(int num_vars);
void constFunc();
void proc();
void statementFunc();
void factor();
void print_parser_error(int error_code, int case_code);
void print_assembly_code();
void print_symbol_table();
int main(int argc, char *argv[])
{
// variable setup
int i;
tokens = calloc(ARRAY_SIZE, sizeof(lexeme));
table = calloc(ARRAY_SIZE, sizeof(symbol));
code = calloc(ARRAY_SIZE, sizeof(instruction));
FILE *ifp;
int buffer;
// read in input
/*
if (argc < 2)
{
printf("Error : please include the file name\n");
return 0;
}
*/
ifp = fopen("ptext.txt", "r");
// argv[1]
while(fscanf(ifp, "%d", &buffer) != EOF)
{
tokens[token_index].type = buffer;
if (buffer == identifier)
fscanf(ifp, "%s", tokens[token_index].identifier_name);
else if (buffer == number)
fscanf(ifp, "%d", &(tokens[token_index].number_value));
token_index++;
count++;
}
fclose(ifp);
token_index = 0;
// loop runs while there are no errors
while(error != -1)
{
program();
// manually ending loop if there is no errors
error = -1;
}
free(tokens);
free(table);
free(code);
return 0;
}
// starts program
void program()
{
// adding main to the symbol table first
add_symbol(3, "main", 0, 0,0);
//setting our current level
level = -1;
// we have to emit the first function and jmp straight to the main function
emit(JMP, table[table_index].level, 0);
block();
for(int i = 0; i < code_index; i++)
{
if(code[i].op == CAL)
{
code[i].m = table[i].address;
}
}
if(tokens[token_index].type != period)
{
print_parser_error(1, 1);
error = -1;
}
// for each cal in code blah blah
int j = 0;
for(int i = 0; i < code_index; i++)
{
if(code[i].op == STO || code[i].op == LIT || code[i].op == RED)
{
j++;
}
if(code[i].op == CAL)
{
printf("\n TEST: %d \n", j);
code[i].m = table[j].address;
}
}
emit(SYS, 0 ,HLT);
table[0].address = code_index * 2;
code[0].m = code_index * 2;
// final print
print_assembly_code();
print_symbol_table();
}
void block()
{
level++;
// first call
int inc_m_val = declarations();
//call procedure
proc();
table[code_index].address = code_index * 3;
//emitting INC call
emit(INC, 0, inc_m_val);
statementFunc();
// marks all visited symbols at this level
mark();
level--;
}
int declarations()
{
// # of vars to pass in
int numVars = 0;
// loop while a const or var
while(tokens[token_index].type == keyword_const || tokens[token_index].type == keyword_var)
{
// if its a const we call const function
if(tokens[token_index].type == keyword_const)
{
constFunc();
}
// its a var call var function
if(tokens[token_index].type == keyword_var)
{
varFunc(numVars);
numVars++;
}
}
// remove
// returns current ar level
return numVars + 3;
}
void varFunc(int num_vars)
{
char name[MAX_IDT_LEN];
// process var
token_index++;
// checking if value after var is an identifer
if(tokens[token_index].type != identifier)
{
print_parser_error(2,2);
error = -1;
}
// checks if variable is unique
if(multiple_declaration_check(tokens[token_index].identifier_name) != -1)
{
// print error and end program
print_parser_error(3, 3);
error = -1;
}
// saving name and moving to next token
strcpy(name,tokens[token_index].identifier_name);
token_index++;
// adding previous identifier to symbol table
add_symbol(2, name, 0, level, num_vars+3);
// making sure next symbol is a semiolon
if(tokens[token_index].type != semicolon)
{
print_parser_error(6, 2);
error = -1;
}
token_index++;
}
void constFunc()
{
// 0 = false, 1 = true
int minus_flag = 0;
char name[MAX_IDT_LEN];
//move to next token
token_index++;
//check if next token is valid
if(tokens[token_index].type != identifier)
{
print_parser_error(2,1);
error = -1;
}
// checks if variable is unique
if(multiple_declaration_check(tokens[token_index].identifier_name) != -1)
{
// print error and end program
print_parser_error(3, 3);
error = -1;
}
// saving name and moving to next token
strcpy(name,tokens[token_index].identifier_name);
token_index++;
// makes sure next symbol is valid
if(tokens[token_index].type != assignment_symbol)
{
print_parser_error(4, 1);
error = -1;
}
// moves to next token
token_index++;
// checks if next token is a minus
if(tokens[token_index].type == minus)
{
minus_flag = 1;
token_index++;
}
// checks for valid next token
if(tokens[token_index].type != number)
{
print_parser_error(5, 0);
error = -1;
}
// making value negative in the symbol table
if(minus_flag == 1)
tokens[token_index].number_value = tokens[token_index].number_value * -1;
// adding to symbol table
add_symbol(1, name, tokens[token_index].number_value, level, 0);
// move to next symbol
token_index++;
// makes sure current value is a semicolon
if(tokens[token_index].type != semicolon)
{
print_parser_error(6, 1);
error = -1;
}
// move to next symbol
token_index++;
}
void proc()
{
// while we are on a procedure
while(tokens[token_index].type == keyword_procedure)
{
char name[MAX_IDT_LEN];
token_index++;
// making sure next token is valid
if(tokens[token_index].type != identifier)
{
print_parser_error(2, 3);
error = -1;
}
// checks if variable is unique
if(multiple_declaration_check(tokens[token_index].identifier_name) != -1)
{
// print error and end program
print_parser_error(3, 3);
error = -1;
}
// saving name and moving to next token
strcpy(name,tokens[token_index].identifier_name);
token_index++;
add_symbol(3, name, 0, level, 0);
// checks for validity
if(tokens[token_index].type != left_curly_brace)
{
print_parser_error(14, 14);
error = -1;
}
token_index++;
block();
emit(OPR, 0, RTN);
// checks for validity
if(tokens[token_index].type != right_curly_brace)
{
print_parser_error(15, 1);
error = -1;
}
// move to next token
token_index++;
}
}
void statementFunc()
{
int sym_index = 0;
if(tokens[token_index].type == keyword_def)
{
// move to next token
token_index++;
// checking next token validity
if(tokens[token_index].type != identifier)
{
print_parser_error(2, 6);
error = -1;
}
sym_index = find_symbol(tokens[token_index].identifier_name, 2);
// if we cant find symbol in our table
if(sym_index == -1)
{
if(find_symbol(tokens[token_index].identifier_name, 1) == find_symbol(tokens[token_index].identifier_name, 3))
{
print_parser_error(8, 1);
error = -1;
}
else
{
print_parser_error(7, 7);
error = -1;
}
}
// move to the next token
token_index++;
// checking for validity
if(tokens[token_index].type != assignment_symbol)
{
print_parser_error(4, 2);
error = -1;
}
// move to next token
token_index++;
//call factor
factor();
emit(STO, (level - table[sym_index].level), table[sym_index].address);
}
else if(tokens[token_index].type == keyword_call)
{
token_index++;
// checking next token validity
if(tokens[token_index].type != identifier)
{
print_parser_error(2, 4);
error = -1;
}
// locating symbol
sym_index = find_symbol(tokens[token_index].identifier_name, 3);
// if we cant find the symbol
if(sym_index == -1)
{
// temp variables hold our symbols
int num1 = find_symbol(tokens[token_index].identifier_name, 1);
int num2 = find_symbol(tokens[token_index].identifier_name, 2);
// checking if there isnt a constant and no variable with the name
if(num1 == num2)
{
print_parser_error(8, 2);
error = -1;
}
else
{
print_parser_error(9, 9);
error = -1;
}
}
// moving to the next token
token_index++;
printf("test");
emit(CAL,(level - table[table_index].level), sym_index);
printf("%d", code[code_index - 1].m);
}
else if(tokens[token_index].type == keyword_begin)
{
do
{
token_index++;
statementFunc();
}while(tokens[token_index].type == semicolon);
if(tokens[token_index].type != keyword_end)
{
if(tokens[token_index].type == identifier || tokens[token_index].type == keyword_call
|| tokens[token_index].type == keyword_read || tokens[token_index].type == keyword_def)
{
print_parser_error(6, 3);
error = -1;
}
else
{
print_parser_error(10, 10);
error = -1;
}
}
token_index++;
}
else if(tokens[token_index].type == keyword_read)
{
// moving to next token
token_index++;
// checking next token validity
if(tokens[token_index].type != identifier)
{
print_parser_error(2, 5);
error = -1;
}
// finding current symbol
sym_index = find_symbol(tokens[token_index].identifier_name, 2);
// if we don;t find it
if(sym_index == -1)
{
// temp variables hold our symbols
int num1 = find_symbol(tokens[token_index].identifier_name, 1);
int num3 = find_symbol(tokens[token_index].identifier_name, 3);
// checking if there isnt a constant and no variable with the name
if(num1 == num3)
{
print_parser_error(8, 3);
error = -1;
}
else
{
print_parser_error(13, 13);
error = -1;
}
}
// moving to next token
token_index++;
emit(SYS, 0, RED);
emit(STO, level - table[sym_index].level, table[sym_index].address);
}
}
void factor()
{
// checking validity
if(tokens[token_index].type == identifier)
{
// temp variables
int const_index = find_symbol(tokens[token_index].identifier_name, 1);
int var_index = find_symbol(tokens[token_index].identifier_name, 2);
if(const_index == var_index)
{
// checking if there is a valid procedure
if(find_symbol(tokens[token_index].identifier_name, 3) != -1)
{
print_parser_error(17, 17);
error = -1;
}
else
{
print_parser_error(8, 4);
error = -1;
}
}
if(const_index == -1)
{
emit(LOD, level - table[var_index].level, table[var_index].address);
}
else if(var_index == -1)
{
emit(LIT, 0, table[const_index].value);
}
else if(table[const_index].level > table[var_index].level)
{
emit(LIT, 0, table[const_index].value);
}
else
{
emit(LOD, level - table[var_index].level, table[var_index].address);
}
// move to next token
token_index++;
}
else if(tokens[token_index].type == number)
{
emit(LIT, 0, tokens[token_index].number_value);
token_index++;
}
else
{
print_parser_error(19, 19);
error = -1;
}
}
// adds a new instruction to the end of the code
void emit(int op, int l, int m)
{
code[code_index].op = op;
code[code_index].l = l;
code[code_index].m = m;
code_index++;
}
// adds a new symbol to the end of the table
void add_symbol(int kind, char name[], int value, int level, int address)
{
table[table_index].kind = kind;
strcpy(table[table_index].name, name);
table[table_index].value = value;
table[table_index].level = level;
table[table_index].address = address;
table[table_index].mark = 0;
table_index++;
}
// marks all of the current procedure's symbols
void mark()
{
int i;
for (i = table_index - 1; i >= 0; i--)
{
if (table[i].mark == 1)
continue;
if (table[i].level < level)
return;
table[i].mark = 1;
}
}
// returns -1 if there are no other symbols with the same name within this procedure
int multiple_declaration_check(char name[])
{
int i;
for (i = 0; i < table_index; i++)
if (table[i].mark == 0 && table[i].level == level && strcmp(name, table[i].name) == 0)
return i;
return -1;
}
// returns the index of the symbol with the desi name and kind, prioritizing
// symbols with level closer to the current level
int find_symbol(char name[], int kind)
{
int i;
int max_idx = -1;
int max_lvl = -1;
for (i = 0; i < table_index; i++)
{
if (table[i].mark == 0 && table[i].kind == kind && strcmp(name, table[i].name) == 0)
{
if (max_idx == -1 || table[i].level > max_lvl)
{
max_idx = i;
max_lvl = table[i].level;
}
}
}
return max_idx;
}
void print_parser_error(int error_code, int case_code)
{
switch (error_code)
{
case 1 :
printf("Parser Error 1: missing . \n");
break;
case 2 :
switch (case_code)
{
case 1 :
printf("Parser Error 2: missing identifier after keyword const\n");
break;
case 2 :
printf("Parser Error 2: missing identifier after keyword var\n");
break;
case 3 :
printf("Parser Error 2: missing identifier after keyword procedure\n");
break;
case 4 :
printf("Parser Error 2: missing identifier after keyword call\n");
break;
case 5 :
printf("Parser Error 2: missing identifier after keyword read\n");
break;
case 6 :
printf("Parser Error 2: missing identifier after keyword def\n");
break;
default :
printf("Implementation Error: unrecognized error code\n");
}
break;
case 3 :
printf("Parser Error 3: identifier is declared multiple times by a procedure\n");
break;
case 4 :
switch (case_code)
{
case 1 :
printf("Parser Error 4: missing := in constant declaration\n");
break;
case 2 :
printf("Parser Error 4: missing := in assignment statement\n");
break;
default :
printf("Implementation Error: unrecognized error code\n");
}
break;
case 5 :
printf("Parser Error 5: missing number in constant declaration\n");
break;
case 6 :
switch (case_code)
{
case 1 :
printf("Parser Error 6: missing ; after constant declaration\n");
break;
case 2 :
printf("Parser Error 6: missing ; after variable declaration\n");
break;
case 3 :
printf("Parser Error 6: missing ; after statement in begin-end\n");
break;
default :
printf("Implementation Error: unrecognized error code\n");
}
break;
case 7 :
printf("Parser Error 7: procedures and constants cannot be assigned to\n");
break;
case 8 :
switch (case_code)
{
case 1 :
printf("Parser Error 8: undeclared identifier used in assignment statement\n");
break;
case 2 :
printf("Parser Error 8: undeclared identifier used in call statement\n");
break;
case 3 :
printf("Parser Error 8: undeclared identifier used in read statement\n");
break;
case 4 :
printf("Parser Error 8: undeclared identifier used in arithmetic expression\n");
break;
default :
printf("Implementation Error: unrecognized error code\n");
}
break;
case 9 :
printf("Parser Error 9: variables and constants cannot be called\n");
break;
case 10 :
printf("Parser Error 10: begin must be followed by end\n");
break;
case 11 :
printf("Parser Error 11: if must be followed by then\n");
break;
case 12 :
printf("Parser Error 12: while must be followed by do\n");
break;
case 13 :
printf("Parser Error 13: procedures and constants cannot be read\n");
break;
case 14 :
printf("Parser Error 14: missing {\n");
break;
case 15 :
printf("Parser Error 15: { must be followed by }\n");
break;
case 16 :
printf("Parser Error 16: missing relational operator\n");
break;
case 17 :
printf("Parser Error 17: procedures cannot be used in arithmetic\n");
break;
case 18 :
printf("Parser Error 18: ( must be followed by )\n");
break;
case 19 :
printf("Parser Error 19: invalid expression\n");
break;
default:
printf("Implementation Error: unrecognized error code\n");
}
}
void print_assembly_code()
{
int i;
printf("Assembly Code:\n");
printf("Line\tOP Code\tOP Name\tL\tM\n");
for (i = 0; i < code_index; i++)
{
printf("%d\t%d\t", i, code[i].op);
switch(code[i].op)
{
case LIT :
printf("LIT\t");
break;
case OPR :
switch (code[i].m)
{
case RTN :
printf("RTN\t");
break;
case ADD : // DO NOT ATTEMPT TO IMPLEMENT THIS, YOU WILL GET A ZERO IF YOU DO
printf("ADD\t");
break;
case SUB : // DO NOT ATTEMPT TO IMPLEMENT THIS, YOU WILL GET A ZERO IF YOU DO
printf("SUB\t");
break;
case MUL : // DO NOT ATTEMPT TO IMPLEMENT THIS, YOU WILL GET A ZERO IF YOU DO
printf("MUL\t");
break;
case DIV : // DO NOT ATTEMPT TO IMPLEMENT THIS, YOU WILL GET A ZERO IF YOU DO
printf("DIV\t");
break;
case EQL : // DO NOT ATTEMPT TO IMPLEMENT THIS, YOU WILL GET A ZERO IF YOU DO
printf("EQL\t");
break;
case NEQ : // DO NOT ATTEMPT TO IMPLEMENT THIS, YOU WILL GET A ZERO IF YOU DO
printf("NEQ\t");
break;
case LSS : // DO NOT ATTEMPT TO IMPLEMENT THIS, YOU WILL GET A ZERO IF YOU DO
printf("LSS\t");
break;
case LEQ : // DO NOT ATTEMPT TO IMPLEMENT THIS, YOU WILL GET A ZERO IF YOU DO
printf("LEQ\t");
break;
case GTR : // DO NOT ATTEMPT TO IMPLEMENT THIS, YOU WILL GET A ZERO IF YOU DO
printf("GTR\t");
break;
case GEQ : // DO NOT ATTEMPT TO IMPLEMENT THIS, YOU WILL GET A ZERO IF YOU DO
printf("GEQ\t");
break;
default :
printf("err\t");
break;
}
break;
case LOD :
printf("LOD\t");
break;
case STO :
printf("STO\t");
break;
case CAL :
printf("CAL\t");
break;
case INC :
printf("INC\t");
break;
case JMP :
printf("JMP\t");
break;
case JPC : // DO NOT ATTEMPT TO IMPLEMENT THIS, YOU WILL GET A ZERO IF YOU DO
printf("JPC\t");
break;
case SYS :
switch (code[i].m)
{
case WRT : // DO NOT ATTEMPT TO IMPLEMENT THIS, YOU WILL GET A ZERO IF YOU DO
printf("WRT\t");
break;
case RED :
printf("RED\t");
break;
case HLT :
printf("HLT\t");
break;
default :
printf("err\t");
break;
}
break;
default :
printf("err\t");
break;
}
printf("%d\t%d\n", code[i].l, code[i].m);
}
printf("\n");
}
void print_symbol_table()
{
int i;
printf("Symbol Table:\n");
printf("Kind | Name | Value | Level | Address | Mark\n");
printf("---------------------------------------------------\n");
for (i = 0; i < table_index; i++)
printf("%4d | %11s | %5d | %5d | %5d | %5d\n", table[i].kind, table[i].name, table[i].value, table[i].level, table[i].address, table[i].mark);
printf("\n");
}