-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.h
More file actions
524 lines (455 loc) · 10.4 KB
/
parser.h
File metadata and controls
524 lines (455 loc) · 10.4 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
/* Compiler Builder 16
Kenneth Mitchell
Georg Anemogiannis
David Israwi Yordi
Tyler Chauhan
*/
#include "header.h"
/* Symbol table */
symbol symbol_table[MAX_SYMBOL_TABLE_SIZE];
instruction code[MAX_CODE_LENGTH];
int symTableLoc = 0;
/* Current Token */
token currentTok;
int tokenNumber = 0;
/* Functions */
void program();
void block();
void statement();
void condition();
void expression();
void term();
void factor();
void getToken();
void errorMessage(int x);
void emit(int op, int l, int m);
symbol * get_symbol(char *name);
void put_symbol(int kind, char* name, int val, int level, int addr);
int cx = 0;
int level = 0;
/* program ::= block "." */
void program()
{
getToken(); //retrieve the token
block(); //
if(currentTok.type != periodsym)
{
errorMessage(9);
}
//halt command
emit(9, 0, 2);
}
/* block ::= const-declaration var-declaration statement */
void block()
{
int ctemp = cx;
int num_vars = 0;
emit(7, 0, 0);
if (currentTok.type == constsym)
{
char id[13];
do {
//setting constant name
getToken();
if (currentTok.type != identsym)
errorMessage(4);
strcpy(id, currentTok.value);
//checking if it has an equal sign
getToken();
if (currentTok.type == becomessym)
errorMessage(1);
if (currentTok.type != eqsym)
errorMessage(3);
//get value
getToken();
if (currentTok.type != numbersym)
errorMessage(2);
//create symbol
put_symbol(1, id, atoi(currentTok.value), level, 0);
getToken();
} while(currentTok.type == commasym);
if (currentTok.type != semicolonsym)
errorMessage(5);
getToken();
}
if (currentTok.type == varsym)
{
num_vars = 0;
do {
getToken();
if (currentTok.type != identsym)
errorMessage(4);
//adding variable
num_vars++;
put_symbol(2, currentTok.value, 0, level, 3 + num_vars);
getToken();
} while(currentTok.type == commasym);
if (currentTok.type != semicolonsym)
errorMessage(5);
getToken();
}
while (currentTok.type == procsym)
{
getToken();
if (currentTok.type != identsym)
errorMessage(4);
put_symbol(3, currentTok.value, 0, level, cx);
level++;
getToken();
if (currentTok.type != semicolonsym)
errorMessage(5);
getToken();
block();
emit(2, 0, 0);
//code[temp].m = cx;
level--;
if (currentTok.type != semicolonsym)
errorMessage(55);
getToken();
}
code[ctemp].m = cx;
//INC = instCode[6]
emit(6, 0, 4 + num_vars);
statement();
}
/* statement ::= [ ident “:=” expression
| “begin” statement { “;” statement } “end”
| “if” condition “then” statement ]
*/
void statement()
{
if (currentTok.type == identsym)
{
symbol* sym = get_symbol(currentTok.value);
getToken();
if (currentTok.type != becomessym)
errorMessage(13);
getToken();
expression();
emit(4, level - sym->level, sym->addr);
}
else if (currentTok.type == callsym)
{
getToken();
if (currentTok.type != identsym)
errorMessage(14);
symbol* sym = get_symbol(currentTok.value);
emit(5, level - sym->level, sym->addr);
getToken();
}
else if (currentTok.type == beginsym)
{
getToken();
statement();
while (currentTok.type == semicolonsym)
{
getToken();
statement();
}
if (currentTok.type != endsym)
errorMessage(8);
getToken();
}
else if (currentTok.type == ifsym)
{
getToken();
condition();
if (currentTok.type != thensym)
errorMessage(16);
getToken();
int ctemp = cx;
emit(8, 0, 0);
statement();
code[ctemp].m = cx;
if (currentTok.type == elsesym) {
code[ctemp].m = cx + 1;
ctemp = cx;
emit(7, 0, 0);
getToken();
statement();
code[ctemp].m = cx;
}
}
else if (currentTok.type == whilesym)
{
int cx1, cx2;
cx1 = cx;
getToken();
condition();
cx2 = cx;
emit(8, 0, 0);
if (currentTok.type != dosym)
errorMessage(18);
getToken();
statement();
int cxtemp = cx;
emit(7, 0, cx1);
code[cx2].m = cx;
}
else if (currentTok.type == readsym)
{
getToken();
if (currentTok.type != identsym)
errorMessage(14);
symbol* sym = get_symbol(currentTok.value);
//read input
emit(9, 0, 1);
if (currentTok.type == identsym)
emit(4, 0, sym->addr);
else
emit(1, 0, atoi(currentTok.value));
getToken();
}
else if (currentTok.type == writesym)
{
getToken();
if (currentTok.type != identsym && currentTok.type != numbersym)
errorMessage(14);
symbol* sym = get_symbol(currentTok.value);
if (sym->kind == 2)
emit(3, level - sym->level, sym->addr);
else
emit(1, 0, sym->val);
//write output
emit(9, 0, 0);
getToken();
}
}
/*condition ::= “odd” expression
| expression rel-op expression */
void condition()
{
if (currentTok.type == oddsym)
{
getToken();
expression();
}
else
{
expression();
if (currentTok.type != leqsym && currentTok.type != neqsym && currentTok.type != lessym && currentTok.type != geqsym && currentTok.type != gtrsym && currentTok.type != eqsym)
errorMessage(20);
token curr = currentTok;
getToken();
expression();
switch (curr.type)
{
case leqsym: emit(2, 0, 11);
break;
case neqsym: emit(2, 0, 9);
break;
case lessym: emit(2, 0, 10);
break;
case geqsym: emit(2, 0, 13);
break;
case gtrsym: emit(2, 0, 12);
break;
case eqsym: emit(2, 0, 8);
break;
default: errorMessage(26);
}
}
}
/* expression ::= [ “+” | “-” ] term { ( “+” | “-” ) term} */
void expression()
{
int addop;
if (currentTok.type == plussym || currentTok.type == minussym)
{
addop = currentTok.type;
getToken();
term();
if (addop == minussym)
emit(2, 0, 1);
}
else
term();
while (currentTok.type == plussym || currentTok.type == minussym)
{
addop = currentTok.type;
getToken();
term();
if (addop == plussym)
emit(2, 0, 2);
else
emit(2, 0, 3);
}
}
/* term ::= factor { ( “*” | “/” ) factor } */
void term()
{
int mulop;
factor();
while(currentTok.type == multsym || currentTok.type == slashsym) {
mulop = currentTok.type;
getToken();
factor();
if (mulop == multsym)
emit(2, 0, 4);
else
emit(2, 0, 5);
}
}
/* factor ::= ident | number | “(” expression “)”*/
void factor()
{
if (currentTok.type == identsym)
{
symbol* sym = get_symbol(currentTok.value);
if (sym->kind == 1)
emit(1, 0, sym->val);
else
emit(3, level - sym->level, sym->addr);
getToken();
}
else if (currentTok.type == numbersym)
{
emit(1, 0, atoi(currentTok.value));
getToken();
}
else if (currentTok.type == lparentsym)
{
getToken();
expression();
if (currentTok.type != rparentsym)
errorMessage(22);
getToken();
}
else
errorMessage(23);
}
/* function to get the current token */
void getToken()
{
currentTok = tokens[tokenNumber++];
}
/* Error messages for the tiny PL/0 Parser */
void errorMessage(int x)
{
switch(x)
{
case 0:
printf("No errors, program is syntactically correct.\n");
break;
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 a =.\n");
break;
case 4:
printf("const, var, 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("An expression cannot begin with this symbol.\n");
break;
case 25:
printf("This number is too large.\n");
break;
default:
printf("Error. There was a problem with the code.\n");
}
exit(x);
}
void emit(int op, int l, int m)
{
if(cx > MAX_CODE_LENGTH)
{
errorMessage(25);
}
else
{
code[cx].op = op;
code[cx].l = l;
code[cx].m = m;
cx++;
//printf("%d %d %d %d\n", op, l, m, cx);
}
}
symbol* get_symbol(char *name)
{
int i;
for(i = symTableLoc - 1; i >= 0; i--)
{
if(strcmp(name, symbol_table[i].name) == 0)
{
return &symbol_table[i];
}
}
return NULL;
}
void put_symbol(int kind, char* name, int val, int level, int addr)
{
symbol_table[symTableLoc].kind = kind;
strcpy(symbol_table[symTableLoc].name, name);
symbol_table[symTableLoc].val = val;
symbol_table[symTableLoc].level = level;
symbol_table[symTableLoc].addr = addr;
symTableLoc++;
}
void writeToFile(FILE *output)
{
int i;
for (i = 0; i < cx; i++)
fprintf(output, "%d %d %d\n", code[i].op, code[i].l, code[i].m);
}