-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.c
More file actions
567 lines (522 loc) · 18.6 KB
/
Copy pathparser.c
File metadata and controls
567 lines (522 loc) · 18.6 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "parser.h"
#include "lex.h"
// defining global variables that will be used throughout
int list_index = 0;
int table_index = 0;
int token;
char id [12];
int num;
int numErrors = 0;
// initializing the functions that will be used throughout this program
struct symbol * parse(struct lexeme * list);
void program(struct lexeme * list, struct symbol * table);
void getToken(struct lexeme * list);
void pblock(int level, struct lexeme * list, struct symbol * table);
int const_dec(int level, struct lexeme * list, struct symbol * table);
int var_dec(int level, struct lexeme * list, struct symbol * table);
int procedure_dec(int level, struct lexeme * list, struct symbol * table);
void pstatement(int level, struct lexeme * list, struct symbol * table);
void pcondition(int level, struct lexeme * list, struct symbol * table);
void pexpression(int level, struct lexeme * list, struct symbol * table);
void pterm(int level, struct lexeme * list, struct symbol * table);
void pfactor(int level, struct lexeme * list, struct symbol * table);
void insertTable(struct symbol * table, int tx, int kind, char * name, int val, int level, int addr, int mark);
int checkIfNamePresent(int level, struct symbol * table, char * name);
void error(int error_number);
int checkIfExists(int level, int kind, struct symbol * table, char * name);
struct symbol * parse(struct lexeme * list)
{
// allocating our symbol table
struct symbol * table = malloc(500 * sizeof(struct symbol));
for (int i = 0; i < 500; i++)
{
table[i].mark = -1; // represents uninitialized
}
program(list, table);
return table;
}
void program(struct lexeme * list, struct symbol * table)
{
getToken(list);
// adding the main procedure to the symbol table
insertTable(table, table_index, 3, "main", 0, 0, 0, 0);
// block function
pblock(0, list, table);
if (token != periodsym)
{
error(9);
}
return;
}
void pblock(int level, struct lexeme * list, struct symbol * table)
{
int numSymbols = 0;
numSymbols += const_dec(level, list, table);
numSymbols += var_dec(level, list, table);
numSymbols += procedure_dec(level, list, table);
pstatement(level, list, table);
// go through the symbol table numSymbols of times and mark the last
// numSymbols num. of symbols
for (int i = table_index - 1; numSymbols >= 0; i--)
{
if (table[i].mark != 1)
{
table[i].mark = 1;
numSymbols--;
}
if (numSymbols < 1)
{
break;
}
}
return;
}
int const_dec(int level, struct lexeme * list, struct symbol * table)
{
int numConst = 0;
if(token == constsym)
{
do
{
getToken(list);
if (token != identsym)
{
error(4); // constant must be followed by indentifer
}
if(checkIfNamePresent(level, table, id) == 1)
{
error(26); // cannot assign another value to const
}
// grab the next token
getToken(list);
if (token != eqsym)
{
error(3); // identifier must be followed by an equals sign
}
getToken(list);
if (token != numbersym)
{
error(2); // = must be followed by a number
}
// no errors encountered, save to symbol table
insertTable(table, table_index, 1, id, num, level, 0, 0);
numConst++;
getToken(list);
} while (token == commasym);
if (token != semicolonsym)
{
error(17);
}
getToken(list);
return numConst;
}
return 0;
}
int var_dec(int level, struct lexeme * list, struct symbol *table)
{
int numVars = 0;
if (token == varsym)
{
do
{
numVars++;
getToken(list);
if (token != identsym)
{
error(11);
}
if (checkIfNamePresent(level, table, id) == 1)
{
error(26); // name present in table
}
insertTable(table, table_index, 2, id, 0, level, numVars + 2, 0); // num vars + size of ar (index 0)
getToken(list);
} while (token == commasym);
// if the next token after this loop isn't a semicolon, error
if (token != semicolonsym)
{
error(17); // missing semicolon
}
getToken(list);
return numVars;
}
return 0;
}
int procedure_dec(int level, struct lexeme * list, struct symbol * table)
{
int numProcedures = 0;
if (token == procsym)
{
do
{
getToken(list);
if (token != identsym)
{
error(11);
}
if (checkIfNamePresent(level, table, id) == 1)
{
error(26); // name present in table
}
insertTable(table, table_index, 3, id, 0, level, 0, 0);
numProcedures++;
getToken(list);
if (token != semicolonsym)
{
error(5);
}
getToken(list);
pblock(level + 1, list, table); // go one level deeper
if (token != semicolonsym)
{
error(5);
}
getToken(list);
} while (token == procsym);
return numProcedures;
}
return numProcedures;
}
void pstatement(int level, struct lexeme * list, struct symbol * table)
{
if (token == identsym)
{
// search symbol table working backwards to find latest unmarked var OR const with desired name
// const = 1, var = 2, proc = 3
if ((checkIfExists(level, 2, table, id) == 0) && (checkIfExists(level, 1, table, id) == 0))
{
error(19); /// --- FIX
}
getToken(list);
if (token != becomessym) // token != ':=' <-- find symbol for this
{
error(1);
}
getToken(list);
pexpression(level, list, table);
return;
}
if (token == callsym)
{
getToken(list);
// search symbol table for latest unmarked procedure with this name
// if you cant find one, error
if (checkIfExists(level, 3, table, id) == 0)
{
error(19); /// --- FIX
}
getToken(list);
return;
}
if (token == beginsym)
{
getToken(list);
pstatement(level, list, table);
while (token == semicolonsym)
{
getToken(list);
pstatement(level, list, table);
}
// SHOULD BE AT endsym
if (token != endsym)
{
error(24); // end symbol missing
}
getToken(list);
return;
}
if (token == ifsym)
{
getToken(list);
pcondition(level, list, table);
if (token != thensym)
{
error(16); // then expected
}
getToken(list);
pstatement(level, list, table);
return;
}
if (token == elsesym)
{
getToken(list);
pstatement(level, list, table);
return;
}
if (token == whilesym)
{
getToken(list);
pcondition(level, list, table);
if (token != dosym)
{
error(18); // do symbol expected
}
getToken(list);
pstatement(level, list, table);
return;
}
if (token == readsym)
{
getToken(list);
// search symbol table working backwards to find the
// latest unmarked var with the desired name, if you cant find,
// then error
if (checkIfExists(level, 2, table, id) == 0)
{
error(19); /// --- FIX
}
getToken(list);
return;
}
if (token == writesym)
{
getToken(list);
pexpression(level, list, table);
return;
}
return;
}
void pcondition(int level, struct lexeme * list, struct symbol * table)
{
if (token == oddsym)
{
getToken(list);
pexpression(level, list, table);
}
else
{
pexpression(level, list, table);
if (token != eqsym && token != neqsym && token != lessym && token != leqsym && token != gtrsym && token != geqsym)
{
error(20); // relational operation expected
}
// grab next token
getToken(list);
pexpression(level, list, table);
}
return;
}
void pexpression(int level, struct lexeme * list, struct symbol * table)
{
if (token == plussym || token == minussym)
{
getToken(list);
}
pterm(level, list, table);
while (token == plussym || token == minussym)
{
getToken(list);
pterm(level, list, table);
}
return;
}
void pterm(int level, struct lexeme * list, struct symbol * table)
{
pfactor(level, list, table);
while (token == multsym || token == slashsym)
{
getToken(list);
pfactor(level, list, table);
}
return;
}
void pfactor(int level, struct lexeme * list, struct symbol * table)
{
if (token == identsym)
{
// search through symbol table working backwards to find the latest
// unmarked var OR const with the desired name, if you can't find either, then error
if (checkIfExists(level, 2, table, id) == 0 && checkIfExists(level, 1, table, id) == 0)
{
error(19);
}
getToken(list);
}
else if (token == numbersym)
{
getToken(list);
}
else if (token == lparentsym)
{
// grab next token
getToken(list);
pexpression(level, list, table);
if (token != rparentsym)
{
error(22); // right parenthesis expected
}
// grab next token
getToken(list);
}
else
{
error(23); // preceding factor cannot be followed by this symbol
}
return;
}
void insertTable(struct symbol * table, int tx, int kind, char * name, int val, int level, int addr, int mark)
{
table[tx].kind = kind;
strcpy(table[tx].name, name);
table[tx].val = val;
table[tx].level = level;
table[tx].addr = addr;
table[tx].mark = mark; // unmarked
// increase the table index for next data entry
table_index++;
return;
}
int checkIfNamePresent(int level, struct symbol * table, char * name)
{
for (int i = table_index; i >= 0; i--)
{
if (strcmp(table[i].name, name) == 0 && table[i].mark == 0 && table[i].level == level)
{
return 1;
}
}
return 0;
}
int checkIfExists(int level, int kind, struct symbol * table, char * name)
{
for (int i = 500; i >= 0; i--)
{
if (strcmp(table[i].name, id) == 0 && table[i].mark == 0 && table[i].kind == kind)
{
return 1;
}
}
return 0;
}
void getToken(struct lexeme * list)
{
token = list[list_index].id;
if (token == 2)
{
strcpy(id, list[list_index].name);
}
else if (token == 3)
{
num = list[list_index].num;
}
list_index++;
return;
}
void error(int error_number)
{
FILE * output_file = fopen("codegenoutput.txt", "w");
numErrors++;
switch (error_number)
{
case 1:
fprintf(output_file, "\n1. Use = instead of :=.\n");
printf("\n1. Use = instead of :=.\n");
break;
case 2:
fprintf(output_file, "\n2. = must be followed by a number.\n");
printf("\n2. = must be followed by a number.\n");
break;
case 3:
fprintf(output_file, "\n3. Identifier must be followed by =.\n");
printf("\n3. Identifier must be followed by =.\n");
break;
case 4:
fprintf(output_file, "\n4. const, var, procedure must be followed by identifier.\n");
printf("\n4. const, var, procedure must be followed by identifier.\n");
break;
case 5:
fprintf(output_file, "\n5. Semicolon or comma missing.\n");
printf("\n5. Semicolon or comma missing.\n");
break;
case 6:
fprintf(output_file, "\n6. Incorrect symbol after procedure declaration.\n");
printf("\n6. Incorrect symbol after procedure declaration.\n");
break;
case 7:
fprintf(output_file, "\n7. Statement expected.\n");
printf("\n7. Statement expected.\n");
break;
case 8:
fprintf(output_file, "\n8. Incorrect symbol after statement part in block.\n");
printf("\n8. Incorrect symbol after statement part in block.\n");
break;
case 9:
fprintf(output_file, "\n9. Period expected.\n");
printf("\n9. Period expected.\n");
break;
case 10:
fprintf(output_file, "\n10. Semicolon between statements missing.\n");
printf("\n10. Semicolon between statements missing.\n");
break;
case 11:
fprintf(output_file, "\n11. Undeclared identifier.\n");
printf("\n11. Undeclared identifier.\n");
break;
case 12:
fprintf(output_file, "\n12. Assignment to constant or procedure is not allowed.\n");
printf("\n12. Assignment to constant or procedure is not allowed.\n");
break;
case 13:
fprintf(output_file, "\n13. Assignment operator expected.\n");
printf("\n13. Assignment operator expected.\n");
break;
case 14:
fprintf(output_file, "\n14. call must be followed by an identifier.\n");
printf("\n14. call must be followed by an identifier.\n");
break;
case 15:
fprintf(output_file, "\n15. Call of a constant or variable is meaningless.\n");
printf("\n15. Call of a constant or variable is meaningless.\n");
break;
case 16:
fprintf(output_file, "\n16. then expected.\n");
printf("\n16. then expected.\n");
break;
case 17:
fprintf(output_file, "\n17. Semicolon or } expected.\n");
printf("\n17. Semicolon or } expected.\n");
break;
case 18:
fprintf(output_file, "\n18. do expected.\n");
printf("\n18. do expected.\n");
break;
case 19:
fprintf(output_file, "\n19. Incorrect symbol following statement.\n");
printf("\n19. Incorrect symbol following statement.\n");
break;
case 20:
fprintf(output_file, "\n20. Relational operator expected.\n");
printf("\n20. Relational operator expected.\n");
break;
case 21:
fprintf(output_file, "\n21. Expression must not contain a procedure identifier.\n");
printf("\n21. Expression must not contain a procedure identifier.\n");
break;
case 22:
fprintf(output_file, "\n22. Right parenthesis missing.\n");
printf("\n22. Right parenthesis missing.\n");
break;
case 23:
fprintf(output_file, "\n23. The preceding factor cannot begin with this symbol.\n");
printf("\n23. The preceding factor cannot begin with this symbol.\n");
break;
case 24:
fprintf(output_file, "\n24. End symbol Expected.\n");
printf("\n24. End symbol Expected.\n");
break;
case 25:
fprintf(output_file, "\n25. This number is too large.\n");
printf("\n25. This number is too large.\n");
break;
case 26:
fprintf(output_file, "\n26. Cannot reassign constant.\n");
printf("\n26. Cannot reassign constant.\n");
break;
}
fprintf(output_file, "Compilation process terminating...\n");
fclose(output_file);
exit(1);
}