-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.c
More file actions
607 lines (493 loc) · 13.8 KB
/
Copy pathparser.c
File metadata and controls
607 lines (493 loc) · 13.8 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
// Ishmael Perez
// Prof. Montagne
// This is the parser created for HW3. It's called by hw3tc.c, which handles
// the entire compilation, assembly, and execution process.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "header.h"
#define MAX_SYM_TABLE_SIZE 100
#define MAX_LEXI_LIST_SIZE 100
typedef struct symbol {
int kind; // const = 1, var = 2, proc = 3
char name[10]; // name up to 11 chars
int val; // number (ASCII value)
int level; // L level
int addr; // M address
int mark; // to indicate that code has been generated already for a block.
} symbol;
symbol symTable[MAX_SYM_TABLE_SIZE] = {0};
instruction code[MAX_CODE_LENGTH];
int sp = 1, level = 1, currLexLevel = 1, cx = 0, codeLength = 0;
int symbolIndex = 0, currRegister = 0;
tokenStruct currToken;
FILE *input;
FILE *parser_out;
void program();
void block();
void declareConst();
void declareVar(int *space);
void declareProc(int *jumpAddress, int *procedureIndex);
void statement();
void condition();
int checkTokenOp();
void expression();
void term();
void factor();
void token();
void emit(int op, int r, int l, int m);
void putInTable(int kind, char *name, int val, int addr);
void doParser(char *fileIn)
{
input = fopen(fileIn, "r");
if (input == NULL)
throwError(28); // file not found
parser_out = fopen("parser_out.txt", "w");
program();
// If we got this far, there were no issues with code generation!
fprintf(stdout, "Code generated successfully; no errors found!\n");
for (int i = 0; i < codeLength; i++)
fprintf(parser_out, "%i %i %i %i\n", code[i].op, code[i].r, code[i].l, code[i].m);
fclose(input);
fclose(parser_out);
}
void program()
{
// Token is a global int, so we can update it from anywhere in the program.
token();
// Begin parsing the input file from the first character.
block();
if (currToken.type != periodsym)
throwError(9);
}
// This and the other parse___ functions follow the assignment pdf's instructions.
void block()
{
sp = 3;
int space = 4, procedureIndex;
int jumpAddress = cx;
// Emit a jump. We'll replace it later if we don't actually need it.
emit(7, 0, 0, 0);
if (currToken.type == constsym)
declareConst();
if (currToken.type == varsym)
declareVar(&space);
if (currToken.type == procsym)
declareProc(&jumpAddress, &procedureIndex);
code[jumpAddress].m = cx;
emit(6, 0, 0, space);
statement();
currLexLevel--;
}
// make sure the constant follows rules for constants -- no variable attached, etc.
void declareConst()
{
char name[12];
int val;
do
{
token();
// check for identifier
if (currToken.type != identsym)
throwError(4); // err: const int proc must be followed by identifier
strcpy(name, currToken.ident);
token();
// check for ':='
if (currToken.type != becomessym)
throwError(3); // err: ident must be followed by =
token();
// check for number
if (currToken.type != numbersym)
throwError(2); // err: '=' must be followed by a number
val = currToken.value;
putInTable(constsym, name, val, 0);
token();
} while (currToken.type == commasym);
if (currToken.type != semicolonsym)
throwError(5); // err: expected semicolon
token();
}
void declareVar(int *space)
{
char name[12];
int val;
do
{
token();
if (currToken.type != identsym)
throwError(4); // err: missing identifier
strcpy(name, currToken.ident);
putInTable(varsym, name, 0, sp);
symTable[symbolIndex - 1].level = level;
*space++;
sp++;
token();
} while (currToken.type == commasym);
if (currToken.type != semicolonsym)
throwError(5); // err: expected semicolon
token();
}
void declareProc(int *jumpAddress, int *procedureIndex)
{
char name[12];
while (currToken.type == procsym)
{
token();
if (currToken.type != identsym)
throwError(4); // err: identifier missing
strcpy(name, currToken.ident);
putInTable(procsym, name, 0, 0);
*procedureIndex = symbolIndex - 1;
symTable[*procedureIndex].level = level;
symTable[*procedureIndex].addr = *jumpAddress + 1;
token();
if (currToken.type != semicolonsym)
throwError(17); // err: semicolon expected
token();
level++;
block();
if (currToken.type != semicolonsym)
throwError(17);
token();
}
}
void statement()
{
int currIndex, declared = 0, identIndex;
if (currToken.type == identsym)
{
for (int i = symbolIndex - 1; i >= 0; i--)
{
if (currToken.ident == symTable[i].name)
{
if (symTable[i].kind == constsym || symTable[i].kind == procsym)
throwError(12); // err: assignment to const/procedure not allowed
}
else if (symTable[i].kind == varsym)
{
declared = 1;
identIndex = i;
break;
}
}
if (declared == 0)
throwError(11); // err: undeclared ident
token();
if (currToken.type != becomessym)
throwError(3); // err: ident must be followed by =
token();
expression();
emit(4, currRegister - 1, currLexLevel - symTable[identIndex].level, symTable[identIndex].addr - 1);
currRegister--;
}
else if (currToken.type == callsym)
{
declared = 0;
token();
if (currToken.type != identsym)
throwError(14); // err: call must be followed by ident
for (int i = symbolIndex - 1; i>= 0; i--)
{
if (currToken.ident == symTable[i].name)
{
identIndex = i;
declared = 1;
break;
}
}
if (declared == 0)
throwError(11); // err: ident undeclared
if (symTable[identIndex].kind == procsym)
{
emit(5, 0, level, symTable[identIndex].addr);
currLexLevel++;
}
else
throwError(14); // err: call must be followed by ident
token();
}
else if (currToken.type == beginsym)
{
token();
statement();
while (currToken.type == semicolonsym)
{
token();
statement();
}
if (currToken.type != endsym)
throwError(17); // err: end expected
token();
}
else if (currToken.type == ifsym)
{
token();
condition();
if (currToken.type != thensym)
throwError(16); // then expected
token();
int tempCX = cx;
emit(8, currRegister - 1, 0, 0);
statement();
token();
if (currToken.type == elsesym)
{
int tempCX2 = cx;
emit(7, 0, 0, 0);
code[tempCX].m = cx;
token();
statement();
code[tempCX2].m = cx;
currRegister--;
}
else
{
code[tempCX].m = cx;
currRegister--;
}
}
else if (currToken.type == whilesym)
{
int tempCX = cx, tempCX2 = cx;
token();
condition();
emit(8, currRegister - 1, 0, 0);
if (currToken.type != dosym)
throwError(18); // do expected
token();
statement();
emit(7, 0, 0, tempCX);
code[tempCX2].m = cx;
currRegister--;
}
else if (currToken.type == readsym)
{
token();
if (currToken.type != identsym)
throwError(27); // err: invalid symbol after read
for (int i = symbolIndex - 1; i >= 0; i--)
{
if (currToken.ident == symTable[i].name)
{
identIndex = i;
declared = 1;
break;
}
}
if (declared == 0)
throwError(11);
emit(10, currRegister, 0, 2);
if (symTable[identIndex].kind == varsym)
emit(4, currRegister, currLexLevel - symTable[identIndex].level, symTable[identIndex].addr - 1);
else if (symTable[identIndex].kind == 1)
throwError(12); // err: illegal assignment to const/proc
token();
}
else if (currToken.type == writesym)
{
token();
if (currToken.type != identsym)
throwError(27); // err: invalid symbol after read
for (int i = symbolIndex - 1; i >= 0; i--)
{
if (currToken.ident == symTable[i].name)
{
identIndex = i;
declared = 1;
break;
}
}
if (declared == 0)
throwError(11);
if (symTable[identIndex].kind == varsym)
{
emit(3, currRegister, currLexLevel - symTable[identIndex].level, symTable[identIndex].addr - 1);
emit(9, currRegister, 0, 1);
}
else if (symTable[identIndex].kind == constsym)
{
emit(1, currRegister, 0, symTable[identIndex].val);
emit(9, currRegister, 0, 1);
}
token();
}
}
void condition()
{
if (currToken.type == oddsym)
{
token();
expression();
emit(17, currRegister - 1, currRegister - 1, 0);
}
else
{
expression();
int relOp = checkTokenOp();
if (relOp == 0)
throwError(20); // err: expected relationship op
token();
expression();
emit(relOp, currRegister - 2, currRegister - 2, currRegister - 1);
currRegister--;
}
}
int checkTokenOp()
{
switch(currToken.type)
{
case eqsym:
return 19;
break;
case neqsym:
return 20;
break;
case lessym:
return 21;
break;
case leqsym:
return 22;
break;
case gtrsym:
return 23;
break;
case geqsym:
return 24;
break;
default:
return 0;
}
}
void expression()
{
int addOp;
if (currToken.type == plussym || currToken.type == minussym)
{
addOp = currToken.type;
token();
term();
if (addOp == minussym)
emit(12, currRegister - 1, currRegister - 1, 0);
}
else
term();
while (currToken.type == plussym || currToken.type == minussym)
{
addOp = currToken.type;
token();
term();
if (addOp == plussym)
{
emit(13, currRegister - 2, currRegister - 2, currRegister - 1);
currRegister--;
}
// else addOp is minussym
else
{
emit(14, currRegister - 2, currRegister - 2, currRegister - 1);
currRegister--;
}
}
}
// parse multiplication and division symbols
void term()
{
int multOp;
factor();
while (currToken.type == multsym || currToken.type == slashsym)
{
multOp = currToken.type;
token();
factor();
if (multOp == multsym)
{
emit(15, currRegister - 2, currRegister - 2, currRegister - 1 );
currRegister--;
}
// else multOp is slashsym
else
{
emit(16, currRegister - 2, currRegister - 2, currRegister - 1 );
currRegister--;
}
}
}
void factor()
{
int declared = 0, identIndex = 0;
if (currToken.type == varsym)
{
for (int i = symbolIndex - 1; i>= 0; i--)
{
if (currToken.ident == symTable[i].name)
{
identIndex = i;
declared = 1;
break;
}
}
if (declared == 0)
throwError(11); // err: ident undeclared
if (symTable[identIndex].kind == varsym)
emit(3, currRegister, currLexLevel - symTable[identIndex].level, symTable[identIndex].addr - 1);
else if (symTable[identIndex].kind == constsym)
emit(1, currRegister, 0, symTable[identIndex].val);
currRegister++;
token();
}
else if (currToken.type == procsym)
{
emit(1, currRegister, 0, currToken.value);
currRegister++;
token();
}
else if (currToken.type == lparentsym)
{
token();
expression();
if (currToken.type != rparentsym)
throwError(22); // err: right parenthesis missing!
token();
}
else
throwError(23); // err: preceding factor can't begin with this symbol
}
// Retrieves token from lexeme list (input file).
void token()
{
char *tempToken = malloc(sizeof(char) * (MAX_IDENT_LENGTH + 1));
char *tempID = malloc(sizeof(char) * (MAX_IDENT_LENGTH + 1));
char *tempVal = malloc(sizeof(char) * (MAX_IDENT_LENGTH + 1));
fscanf(input, "%s", tempToken);
currToken.type = atoi(tempToken);
if (currToken.type == 2)
{
fscanf(input, "%s", tempID);
strcpy(currToken.ident, tempID);
}
if (currToken.type == 3)
{
fscanf(input, "%s", tempVal);
currToken.value = atoi(tempVal);
}
free(tempToken);
free(tempID);
free(tempVal);
}
// Write to code array, which will be entered into the output file after hitting "end".
void emit(int op, int r, int l, int m)
{
code[cx].op = op;
code[cx].r = r;
code[cx].l = l;
code[cx].m = m;
cx++;
}
void putInTable(int kind, char *name, int val, int addr)
{
symTable[symbolIndex].kind = kind;
strcpy(symTable[symbolIndex].name, name);
symTable[symbolIndex].val = val;
symTable[symbolIndex].addr = addr;
symbolIndex++;
}