-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.c
560 lines (493 loc) · 13.2 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
/* Austin Hester
CS 4280 sp18
C.Z. Janikow */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "parser.h"
#include "tree.h"
#include "treehelper.h"
#include "node.h"
#include "scanner.h"
#include "wordlist.h"
#include "token.h"
#include "first.h"
#include "pop.h"
static token_t* tk = NULL;
static int error = 0;
static int level = 0;
// return num tokens
node_t*
parser(node_t* root, token_t** tklist, wordlist_t* filter, int* n)
{
// build token list from filtered source
int i;
while (1)
{
token_t* t = scanner(filter);
if (t == (token_t*)NULL)
return (node_t*)NULL;
//displaytoken(t);
tklist[i] = t;
i++;
if (isEOFtoken(t))
break;
}
// set numtokens
*n = i;
// deep copy token list
//copytokenlist(tc, tklist, i);
displaytokens(tklist, i);
// add the initial <program> nonterminal
tk = customtoken("<>", "<program>", 0);
root = NULL;
root = insert(root, tk, level++);
// start parsing
tk = (token_t*) pop((void**) tklist);
program(root, tklist);
return root;
}
void
program(node_t* root, token_t** tklist) {
const char* FUNC = "program";
// Does it start properly?
if (strcmp(tk->id, "programTK") == 0) {
insert(root, tk, level);
tk = (token_t*) pop((void**) tklist);
// vars nonterminal
if (strcmp(tk->id, "varTK") == 0) {
nonterminal(&vars, "<vars>", root, tklist);
}
// block nonterminal
if (strcmp(tk->id, "startTK") == 0) {
nonterminal(&block, "<block>", root, tklist);
} else {
printerror(FUNC);
return;
}
} else {
printerror(FUNC);
return;
}
// insert EOFtk
if (strcmp(tk->id, "EOFTK") == 0) {
insert(root, tk, level);
} else {
printerror(FUNC);
return;
}
if (!error) {
printf("PARSER\n");
printf("OK\n");
}
return;
}
void
block(node_t* root, token_t** tklist) {
const char* FUNC = "block";
// Does it start properly?
if (strcmp(tk->id, "startTK") == 0) {
insert(root, tk, level);
tk = (token_t*) pop((void**) tklist);
// if there are vars
if (strcmp(tk->id, "varTK") == 0) {
nonterminal(&vars, "<vars>", root, tklist);
}
// if there are stats
if (first_stat(tk->id)) {
nonterminal(&stats, "<stats>", root, tklist);
}
} else {
printerror(FUNC);
return;
}
// Does it stop properly?
if (strcmp(tk->id, "stopTK") == 0) {
insert(root, tk, level);
tk = (token_t*) pop((void**) tklist);
} else {
printerror(FUNC);
return;
}
}
void
vars(node_t* root, token_t** tklist) {
const char* FUNC = "vars";
// start a vars
if (strcmp(tk->id, "varTK") == 0) {
insert(root, tk, level);
tk = (token_t*) pop((void**) tklist);
// if vars is empty
} else return;
// check identifier
if (strcmp(tk->id, "idTK") == 0) {
insert(root, tk, level);
tk = (token_t*) pop((void**) tklist);
} else {
printerror(FUNC);
return;
}
// check = or .
if (strcmp(tk->id, "=TK") == 0) {
// it is being defined
insert(root, tk, level);
tk = (token_t*) pop((void**) tklist);
} else if (strcmp(tk->id, ".TK") == 0) {
// it is being declared
insert(root, tk, level);
tk = (token_t*) pop((void**) tklist);
return;
} else {
printerror(FUNC);
return;
}
// check expression
if (first_expr(tk->id)) {
// set it to an expr
nonterminal(&expr, "<expr>", root, tklist);
} else {
printerror(FUNC);
return;
}
// we have more vars
if (first_mvars(tk->id)) {
nonterminal(&mvars, "<mvars>", root, tklist);
}
return;
}
void
mvars(node_t* root, token_t** tklist) {
const char* FUNC = "mvars";
// If this var line is over
if (strcmp(tk->id, ".TK") == 0) {
insert(root, tk, level);
tk = (token_t*) pop((void**) tklist);
} else if (strcmp(tk->id, ":TK") == 0) {
// if we will declare more
insert(root, tk, level);
tk = (token_t*) pop((void**) tklist);
if (strcmp(tk->id, "idTK") == 0) {
insert(root, tk, level);
tk = (token_t*) pop((void**) tklist);
if (first_mvars(tk->id)) {
nonterminal(&mvars, "<mvars>", root, tklist);
return;
} else {
printerror(FUNC);
return;
}
} else {
printerror(FUNC);
return;
}
}
// if there are more vars
if (strcmp(tk->id, "varTK") == 0) {
nonterminal(&vars, "<vars>", root, tklist);
return;
}
return;
}
void
expr(node_t* root, token_t** tklist) {
// Check M
nonterminal(&M, "<M>", root, tklist);
if (first_xhelp(tk->id)) {
// We need help
nonterminal(&xhelp, "<xhelp>", root, tklist);
return;
} else return;
}
// this is not necessary to separate right now,
// but it should help later
void
xhelp(node_t* root, token_t** tklist) {
if (first_xhelp(tk->id)) {
insert(root, tk, level);
tk = (token_t*) pop((void**) tklist);
nonterminal(&expr, "<expr>", root, tklist);
}
return;
}
void
M(node_t* root, token_t** tklist) {
const char* FUNC = "M";
// M is a wonderful nonterminal
if (first_R(tk->id)) {
nonterminal(&R, "<R>", root, tklist);
return;
} else if (first_M(tk->id)) {
insert(root, tk, level);
tk = (token_t*) pop((void**) tklist);
nonterminal(&M, "<M>", root, tklist);
return;
} else {
printerror(FUNC);
return;
}
}
void
R(node_t* root, token_t** tklist) {
const char* FUNC = "R";
// So is R
if (strcmp(tk->id, "(TK") == 0) {
insert(root, tk, level);
tk = (token_t*) pop((void**) tklist);
nonterminal(&expr, "<expr>", root, tklist);
if (strcmp(tk->id, ")TK") == 0) {
insert(root, tk, level);
tk = (token_t*) pop((void**) tklist);
return;
} else {
printerror(FUNC);
return;
}
} else if (strcmp(tk->id, "idTK") == 0) {
insert(root, tk, level);
tk = (token_t*) pop((void**) tklist);
return;
} else if (strcmp(tk->id, "intTK") == 0) {
insert(root, tk, level);
tk = (token_t*) pop((void**) tklist);
return;
} else {
printerror(FUNC);
return;
}
}
void
stats(node_t* root, token_t** tklist) {
nonterminal(&stat, "<stat>", root, tklist);
if (first_stat(tk->id)) {
// there are more stats
nonterminal(&mstat, "<mstat>", root, tklist);
}
return;
}
void
mstat(node_t* root, token_t** tklist) {
if (first_stat(tk->id)) {
// wow many stats holy cow
nonterminal(&stat, "<stat>", root, tklist);
if (first_stat(tk->id)) {
// there are more stats
nonterminal(&mstat, "<mstat>", root, tklist);
}
return;
} else return;
}
void
stat(node_t* root, token_t** tklist) {
const char* FUNC = "stat";
// Now, which stat will it be?
if (strcmp(tk->id, "readTK") == 0) {
nonterminal(&in, "<in>", root, tklist);
return;
} else if (strcmp(tk->id, "printTK") == 0) {
nonterminal(&out, "<out>", root, tklist);
return;
} else if (strcmp(tk->id, "startTK") == 0) {
nonterminal(&block, "<block>", root, tklist);
return;
} else if (strcmp(tk->id, "iffTK") == 0) {
nonterminal(&iffandloop, "<iff>", root, tklist);
return;
} else if (strcmp(tk->id, "iterTK") == 0) {
nonterminal(&iffandloop, "<iter>", root, tklist);
return;
} else if (strcmp(tk->id, "letTK") == 0) {
nonterminal(&assign, "<assign>", root, tklist);
return;
} else {
printerror(FUNC);
return;
}
}
void
in(node_t* root, token_t** tklist) {
const char* FUNC = "in";
// We read in a value
if (strcmp(tk->id, "readTK") == 0) {
insert(root, tk, level);
tk = (token_t*) pop((void**) tklist);
} else {
printerror(FUNC);
return;
}
// Set it to this identifier
if (strcmp(tk->id, "idTK") == 0) {
insert(root, tk, level);
tk = (token_t*) pop((void**) tklist);
} else {
printerror(FUNC);
return;
}
// and .
if (strcmp(tk->id, ".TK") == 0) {
insert(root, tk, level);
tk = (token_t*) pop((void**) tklist);
return;
} else {
printerror(FUNC);
return;
}
}
void
out(node_t* root, token_t** tklist) {
const char* FUNC = "out";
// print something
if (strcmp(tk->id, "printTK") == 0) {
insert(root, tk, level);
tk = (token_t*) pop((void**) tklist);
} else {
printerror(FUNC);
return;
}
// this something
nonterminal(&expr, "<expr>", root, tklist);
if (strcmp(tk->id, ".TK") == 0) {
insert(root, tk, level);
tk = (token_t*) pop((void**) tklist);
return;
} else {
printerror(FUNC);
return;
}
}
void
assign(node_t* root, token_t** tklist) {
const char* FUNC = "assign";
// set a variable to an expr
if (strcmp(tk->id, "letTK") == 0) {
insert(root, tk, level);
tk = (token_t*) pop((void**) tklist);
} else {
printerror(FUNC);
return;
}
// check identifier
if (strcmp(tk->id, "idTK") == 0) {
insert(root, tk, level);
tk = (token_t*) pop((void**) tklist);
} else {
printerror(FUNC);
return;
}
// check =
if (strcmp(tk->id, "=TK") == 0) {
insert(root, tk, level);
tk = (token_t*) pop((void**) tklist);
} else {
printerror(FUNC);
return;
}
nonterminal(&expr, "<expr>", root, tklist);
if (strcmp(tk->id, ".TK") == 0) {
insert(root, tk, level);
tk = (token_t*) pop((void**) tklist);
return;
} else {
printerror(FUNC);
return;
}
}
void
iffandloop(node_t* root, token_t** tklist) {
const char* FUNC = "iffandloop";
// I combined iff and iter because they are the same
insert(root, tk, level);
tk = (token_t*) pop((void**) tklist);
// open (
if (strcmp(tk->id, "(TK") == 0) {
insert(root, tk, level);
tk = (token_t*) pop((void**) tklist);
} else {
printerror(FUNC);
return;
}
// what to evaluate
nonterminal(&evaluate, "<evaluate>", root, tklist);
// close )
if (strcmp(tk->id, ")TK") == 0) {
insert(root, tk, level);
tk = (token_t*) pop((void**) tklist);
} else {
printerror(FUNC);
return;
}
// what happens when <evaluate> is true
nonterminal(&stat, "<stat>", root, tklist);
return;
}
void
evaluate(node_t* root, token_t** tklist)
{
nonterminal(&expr, "<expr>", root, tklist);
nonterminal(&RO, "<RO>", root, tklist);
nonterminal(&expr, "<expr>", root, tklist);
return;
}
// "Relational Operator"
void
RO(node_t* root, token_t** tklist) {
const char* FUNC = "RO";
// I deal with these using backtracking/ memoization
// instead of lookahead. Check trimline.c
// This is implemented in the preprocesser
// we don't need to lookahead anymore than we do
if (strcmp(tk->id, "<TK") == 0) {
insert(root, tk, level);
tk = (token_t*) pop((void**) tklist);
return;
} else if (strcmp(tk->id, "<<TK") == 0) {
insert(root, tk, level);
tk = (token_t*) pop((void**) tklist);
return;
} else if (strcmp(tk->id, ">TK") == 0) {
insert(root, tk, level);
tk = (token_t*) pop((void**) tklist);
return;
} else if (strcmp(tk->id, ">>TK") == 0) {
insert(root, tk, level);
tk = (token_t*) pop((void**) tklist);
return;
} else if (strcmp(tk->id, "=TK") == 0) {
insert(root, tk, level);
tk = (token_t*) pop((void**) tklist);
return;
} else if (strcmp(tk->id, "==TK") == 0) {
insert(root, tk, level);
tk = (token_t*) pop((void**) tklist);
return;
} else {
printerror(FUNC);
return;
}
}
// A very helpful little function
// It takes in a function pointer,
// the name of the nonterminal, and the usual
void
nonterminal(void (*nont)(node_t*,token_t**), char* name,
node_t* root, token_t** tklist)
{
token_t* tmp = (token_t*) malloc(sizeof(token_t));
// Save what level we are on
int lvl = level;
tmp = customtoken("<>", name, 0);
// insert the nonterminal into the parse tree
root = insert(root, tmp, level++);
// now deal with its children
nont(root->children[root->num_children-1], tklist);
// done with children, back to original level
level = lvl;
}
void
printerror(const char* callingFunction){
// set error flag and tell them where it happened
error = 1;
fprintf(stderr, "Error parsing %s @ line %d.\t",
tk->instance, tk->line_num);
fprintf(stderr, "[[ in function %s ]]\n", callingFunction);
return;
}