-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathPCG.h
887 lines (585 loc) · 16.8 KB
/
PCG.h
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
/*** Parser/Code Generator - PL/0 Programming Language
Samir Ketema
July 6, 2014
Systems Software - COP 3402
This is a parser and code generator for the PL/0
Programming Language. This is to work with the other included
files provided in the compiler zip folder.
Input: LAout.txt (lexeme list from Lexical Analyzer)
Output: code.txt (output PL/0 machine code)
***/
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define MAX_SYMBOL_TABLE_SIZE 10000
#define MAX_CODE_SIZE 500
//Declaring Structs and Variables.
//Tokens for our parser
typedef struct{
char type[3]; //Type of token (1 - 33).
char value[12]; //Value of token (identifier, or number value).
} token;
//This is the struct for our symbol table.
typedef struct{
int kind;
char name[12];
int val;
int level;
int addr;
} symbol;
//Make the symbol table.
symbol symbol_table[MAX_SYMBOL_TABLE_SIZE];
//Instruction register format struct.
typedef struct{
int op; //opcode.
int r; //register.
int l; //lexicographical level.
int m; //modifier.
} code_struct;
//Code array
code_struct codearray[MAX_CODE_SIZE];
//Code index
int cx = 0;
//Here we keep track of the register we are working with.
int register_ptr;
//Stack pointer.
int sp;
//Maximum Lexicographical Level.
int level;
//Current Lexicographical Level.
int curr_lex_level;
//This is the token we are working with as we read in tokens.
token current_token; //Current token to analyze.
//Keeps track of symbols in symbol table.
int symbol_cnt;
//File pointer to token (internal representation) file.
FILE *PCGinput;
//Retrieves next token.
int get_next_token();
//Inserts a symbol to the symbol table.
void add_to_symbol_table( int k, char name[], int val, int addr );
//Prints out error message.
void ERR( int n );
//Generates PL/0 code line with input fields.
void emit(int op, int r, int l, int m);
//procedures defined in the grammar
void program();
void block();
void statement();
void condition();
int rel_op();
void expression();
void term();
void factor();
//This is the main function of the Parser/Code Generator.
void PCG(int flag){
int i;
//Open lexeme list
PCGinput = fopen("LAout.txt", "r");
//Initialize values
symbol_cnt = 0;
register_ptr = 0;
cx = 0;
sp = 1;
level = curr_lex_level = 1;
//Initialize code array
for( i = 0; i < MAX_CODE_SIZE; i++ ){
codearray[i].op = 0;
codearray[i].r = 0;
codearray[i].l = 0;
codearray[i].m = 0;
}
//Parse tokens
program();
//If it went well, no errors.
printf( "\n\nProgram is correct syntactically!\n" );
//Declare output file.
FILE *fout = fopen( "code.txt", "w" );
//Print the assembly code out to the file
int x = 0;
while( !( codearray[x].op == 0 && codearray[x].r == 0 && codearray[x].l == 0 && codearray[x].m == 0 ) ){
if(flag)
printf("%d %d %d %d\n", codearray[x].op, codearray[x].r, codearray[x].l, codearray[x].m );
fprintf(fout, "%d %d %d %d\n", codearray[x].op, codearray[x].r, codearray[x].l, codearray[x].m );
x++;
}
//Close files.
fclose(PCGinput);
fclose(fout);
return;
}
//Program
void program(){
get_next_token();
block();
//if( strcmp(current_token.type, "19") != 0 ) ERR(9);
emit(11, 0, 0, 3);
}
void block(){
char name[12];
int val;
int procedure_spot;
sp = 3;
int space = 4; //Keep track of space
int proc_x; //Index of Procedure
//Jump to procedure code. Need to save address of jump instruction generated.
int jump_address = cx;
//emit jmp
emit( 7, 0, 0, 0 );
//Constant
if( !strcmp(current_token.type, "28" /*constsym*/) ){
do{
get_next_token();
//Identsym
if( strcmp(current_token.type, "2" ) != 0 ) ERR(4);
strcpy(name, current_token.value);
get_next_token();
//eqlsym
if( strcmp(current_token.type, "9" ) != 0 ) ERR(3);
get_next_token();
//numbersym
if( strcmp(current_token.type, "3" ) != 0 ) ERR(2);
//Take current value and add symbol to symbol table
val = atoi(current_token.value);
add_to_symbol_table( 1 /*constant*/, name, val, 0);
get_next_token();
//while no commasym
} while( !strcmp(current_token.type, "17") );
//if semicolonsym, error
if( strcmp(current_token.type, "18" ) != 0 ) ERR(5);
get_next_token();
}
//var-declaration
if( !strcmp(current_token.type, "29") ){
do{
get_next_token();
if( strcmp(current_token.type, "2") != 0 ) ERR(4);
strcpy(name, current_token.value);
//Add the symbol table.
add_to_symbol_table( 2 /*variable*/, name, 0, sp );
symbol_table[ symbol_cnt - 1 ].level = level;
space++;
sp++;
get_next_token();
//While no commasym
}while( !strcmp(current_token.type, "17") );
//if no semicolon, error
if( strcmp(current_token.type, "18") != 0 ) ERR(5);
get_next_token();
}
//procedure
while( !strcmp( current_token.type, "30") ){
get_next_token();
//identifier must go after procedure
if( strcmp( current_token.type, "2") != 0 ) ERR( 4 );
strcpy(name, current_token.value);
//Create symbol table entry
add_to_symbol_table( 3, name, 0, 0 );
proc_x = symbol_cnt - 1; //Procedure index.
symbol_table[ proc_x ].level = level;
symbol_table[ proc_x ].addr = jump_address + 1;
get_next_token();
//semicolon expected
if( strcmp( current_token.type, "18") != 0 ) ERR( 17 );
get_next_token();
level++;
block();
//semicolon expected
//if( strcmp( current_token.type, "18" ) != 0 ) ERR( 17 );
get_next_token();
}
codearray[ jump_address ].m = cx;
emit( 6 /*inc*/, 0, 0, space );
//statement.
statement();
curr_lex_level--;
}
void statement(){
int i; //index.
//Boolean to check if identifier is declared. If so, = 1; else, = 0.
int declared = 0;
//Save symbol table index for found identifier.
int ident_index;
//ident
if( !strcmp(current_token.type, "2") ){
//Check if it's in the symbol table.
for( i = symbol_cnt - 1; i >= 0; i-- )
if( !strcmp( current_token.value, symbol_table[i].name ) ){
if( symbol_table[i].kind == 1 ) ERR(12);
else if( symbol_table[i].kind == 2 ){
declared = 1;
ident_index = i;
}
}
//Undeclared identifier.
if( !declared ) ERR(11);
get_next_token();
if( strcmp(current_token.type, "20") != 0 ) ERR(3);
get_next_token();
expression();
//store at necessary mem address
emit( 4 /*sto*/, register_ptr - 1, curr_lex_level - symbol_table[ ident_index ].level, symbol_table[ ident_index ].addr - 1 );
register_ptr--;
}
//"call" ident
else if( !strcmp(current_token.type, "27" /*callsym*/) ){
int declared = 0;
get_next_token();
//identifier expected.
if( strcmp(current_token.type, "2" /*identsym*/) ) ERR(14);
//Check if identifier has been declared.
for( i = symbol_cnt - 1; i >= 0; i-- )
if( !strcmp( current_token.value, symbol_table[i].name ) ){ //Found!
ident_index = i; //Save identifier index.
declared = 1;
}
if( !declared ) { ERR(11 ); }
if( symbol_table[ident_index].kind== 3 /*proc*/ ){
emit( 5 /*cal*/, 0, level, symbol_table[ ident_index ].addr );
curr_lex_level ++;
}
else
ERR( 14 ); //Call must be followed by a procedure identifier.
get_next_token();
}
//begin
else if( !strcmp(current_token.type, "21") ){
get_next_token();
statement();
while( !strcmp(current_token.type, "18") ){
get_next_token();
statement();
}
get_next_token();
}
// if (cond) -> stmt
else if( !strcmp(current_token.type, "23") ){
get_next_token();
condition();
if( strcmp(current_token.type, "24" ) != 0 ) ERR(16);
get_next_token();
//start code gen.
int ctemp = cx; //Save current code index.
emit( 8, register_ptr - 1, 0, 0 );
statement();
get_next_token();
//else
if( !strcmp( current_token.type, "33") ){
//current cx
int ctemp2 = cx;
emit( 7 , 0, 0, 0 );
codearray[ ctemp ].m = cx;
get_next_token();
statement();
codearray[ ctemp2 ].m = cx;
register_ptr--;
}
else{
codearray[ ctemp ].m = cx;
register_ptr--;
}
}
// while(cond) do (stmt)
else if( !strcmp(current_token.type, "25") ){
int cx1 = cx;
get_next_token();
condition();
int cx2 = cx;
emit( 8 , register_ptr - 1, 0, 0 );
if( strcmp(current_token.type, "26" ) != 0 ) ERR(18);
get_next_token();
statement();
emit( 7 , 0, 0, cx1 );
codearray[ cx2 ].m = cx;
register_ptr--;
}
//read ident
else if( !strcmp( current_token.type, "32" ) ){
get_next_token();
if( strcmp( current_token.type, "2") != 0 ) ERR(29);
//Check if identifier is in symbol table.
for( i = symbol_cnt - 1; i >= 0; i-- )
if( !strcmp( current_token.value, symbol_table[i].name ) ){
declared = 1;
ident_index = i;
}
//Undeclared ident.
if( !declared ) ERR(11);
//take user input for register.
emit( 10 /*sio*/, register_ptr, 0, 2 /*read*/ );
//Read into var.
if( symbol_table[ ident_index ].kind == 2 /*var*/ )
emit( 4 /*sto*/, register_ptr, curr_lex_level - symbol_table[ ident_index ].level, symbol_table[ ident_index ].addr - 1 ); //Register to memory.
//error if read into const
else if( symbol_table[ ident_index ].kind == 1 )
ERR( 12 );
get_next_token();
}
// write ident
else if( !strcmp( current_token.type, "31") ){
get_next_token();
if( strcmp( current_token.type, "2") != 0 ) ERR(29);
//Check if ident is in symbol table.
for( i = symbol_cnt - 1; i >= 0; i-- )
if( !strcmp( current_token.value, symbol_table[i].name ) ){
if( symbol_table[i].kind == 1 || symbol_table[i].kind == 2 ){
declared = 1;
ident_index = i;
}
}
//Undeclared ident.
if( !declared ) ERR(11);
//Get var from main mem.
if( symbol_table[ ident_index ].kind == 2){
emit( 3 , register_ptr, curr_lex_level - symbol_table[ ident_index ].level, symbol_table[ ident_index ].addr - 1 ); //Memory to register.
emit( 9 , register_ptr, 0, 1 ); //Register to screen.
}
//Get const from symbol table.
else if( symbol_table[ ident_index ].kind == 1){
emit( 1 , register_ptr, 0, symbol_table[ ident_index ].val );
emit( 9 , register_ptr, 0, 1 );
}
get_next_token();
}
}
//condition
void condition(){
if( !strcmp(current_token.type, "8") ){
get_next_token();
expression();
emit( 17 /*odd*/, register_ptr - 1, register_ptr - 1, 0 );
}
//rel-op
else{
expression();
int op = rel_op();
if( !op ) ERR(20);
get_next_token();
expression();
emit( op, register_ptr - 2, register_ptr - 2, register_ptr - 1 );
register_ptr--;
}
}
int rel_op(){
//eql
if( !strcmp( current_token.type, "9" ))
return 19;
//neq
else if( !strcmp( current_token.type, "10" ) )
return 20;
//les
else if( !strcmp( current_token.type, "11" ) )
return 21;
//leq
else if( !strcmp( current_token.type, "12" ) )
return 22;
//gtr
else if( !strcmp( current_token.type, "13" ) )
return 23;
//geq
else if( !strcmp( current_token.type, "14" ) )
return 24;
else
return 0;
}
void expression(){
char add_op[3];
if( !strcmp(current_token.type, "4" ) || !strcmp(current_token.type, "5" ) ){
strcpy( add_op, current_token.type );
get_next_token();
term();
if( !strcmp( add_op, "5" ) )
emit( 12 /*neg*/, register_ptr - 1, register_ptr - 1, 0 );
}
else term();
while( !strcmp(current_token.type, "4" ) || !strcmp(current_token.type, "5" ) ){
strcpy( add_op, current_token.type );
get_next_token();
term();
if( !strcmp( add_op, "4" ) ){
emit( 13 , register_ptr - 2, register_ptr - 2, register_ptr - 1 );
register_ptr--;
}
else{
emit( 14 /*sub*/, register_ptr - 2, register_ptr - 2, register_ptr - 1 );
register_ptr--;
}
}
}
void term(){
char mulop[3];
factor();
while( !strcmp(current_token.type, "6" ) || !strcmp(current_token.type, "7" ) ){
strcpy( mulop, current_token.type );
get_next_token();
factor();
if( !strcmp( mulop, "6" ) ){
emit( 15, register_ptr - 2, register_ptr - 2, register_ptr - 1 );
register_ptr--;
}
else{
emit( 16 , register_ptr - 2, register_ptr - 2, register_ptr - 1 );
register_ptr--;
}
}
}
void factor(){
int i;
int declared = 0;
int ident_index = 0;
if( !strcmp(current_token.type, "2" ) ){
//Check in sym table.
for( i = symbol_cnt - 1; i >= 0; i-- )
if( !strcmp( current_token.value, symbol_table[i].name ) ){
declared = 1; //Declared!
ident_index = i; //Save index.
}
//Undeclared identifier
if( !declared ) ERR(11);
if( symbol_table[ident_index].kind == 2 /*var*/ )
emit( 3 , register_ptr, curr_lex_level - symbol_table[ ident_index ].level, symbol_table[ident_index].addr - 1 );
else if( symbol_table[ident_index].kind == 1 /*const*/ )
emit( 1 , register_ptr, 0, symbol_table[ident_index].val );
register_ptr++;
get_next_token();
}
else if( !strcmp(current_token.type, "3") ){
emit( 1 /*lit*/, register_ptr, 0, atoi(current_token.value) );
register_ptr++;
get_next_token();
}
else if( !strcmp(current_token.type, "15" ) ){
get_next_token();
expression();
if( strcmp(current_token.type, "16" ) != 0 ) ERR(22);
get_next_token();
}
else
ERR(23);
}
int get_next_token(){
if( fscanf( PCGinput, "%s", current_token.type ) != EOF ){
if( !strcmp(current_token.type, "2") || !strcmp(current_token.type, "3") )
fscanf( PCGinput, "%s", current_token.value );
else current_token.value[0] = '\0';
return 1;
}
//No more tokens.
else{
current_token.type[0] = '\0';
current_token.value[0] = '\0';
return 0;
}
}
void ERR( int n ){
printf("Error: ");
switch( n ){
case 1:
printf("Use = instead of :=\n");
break;
case 2:
printf("= must be followed by a number\n");
break;
case 3:
printf("Identifier must be followed by =\n");
break;
case 4:
printf("Const, int, procedure must be followed by identifier\n");
break;
case 5:
printf("Semicolon or comma missing\n");
break;
case 6:
printf("Incorrect symbol after procedure declaration\n");
break;
case 7:
printf("Statement expected\n");
break;
case 8:
printf("Incorrect symbol after statement part in block\n");
break;
case 9:
printf("Period expected\n");
break;
case 10:
printf("Semicolon between statements missing\n");
break;
case 11:
printf("Undeclared identifier\n");
break;
case 12:
printf("Assignment to constant or procedure is not allowed\n");
break;
case 13:
printf("Assignment operator expected\n");
break;
case 14:
printf("Call must be followed by an identifier\n");
break;
case 15:
printf("Call of a constant or variable is meaningless\n");
break;
case 16:
printf("Then expected\n");
break;
case 17:
printf("Semicolon or } expected\n");
break;
case 18:
printf("Do expected\n");
break;
case 19:
printf("Incorrect symbol following statement\n");
break;
case 20:
printf("Relational operator expected\n");
break;
case 21:
printf("Expression must not contain a procedure identifier\n");
break;
case 22:
printf("Right parenthesis missing\n");
break;
case 23:
printf("The preceding factor cannot begin with this symbol\n");
break;
case 24:
printf("Expression cannot begin with this symbol\n");
break;
case 25:
printf("Number is too large\n");
break;
case 26:
printf("End expected\n");
break;
case 27:
printf("All available registers in use\n");
break;
case 28:
printf("Variable not initialized\n");
break;
case 29:
printf("Identifier expected after read or write\n");
break;
}
//close files
fclose(PCGinput);
//end program
exit(0);
}
void emit(int op, int r, int l, int m){
codearray[cx].op = op;
codearray[cx].r = r;
codearray[cx].l = l;
codearray[cx].m = m;
cx++;
}
void add_to_symbol_table( int k, char name[], int val, int addr ){
//add to symbol table.
symbol_table[symbol_cnt].kind = k;
strcpy( symbol_table[symbol_cnt].name, name );
symbol_table[symbol_cnt].val = val;
symbol_table[symbol_cnt].addr = addr;
symbol_cnt++;
}