-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.c
More file actions
515 lines (488 loc) · 13 KB
/
parser.c
File metadata and controls
515 lines (488 loc) · 13 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "token.h"
#include "node.h"
#include "scanner.h"
#include "treePrint.h"
#include "sem.h"
//Function prototypes for every nonterminal in BNF
static node_t* program(token_t*, FILE*);
static node_t* block(token_t*, FILE*);
static node_t* vars(token_t*, FILE*);
static node_t* mvars(token_t*, FILE*);
static node_t* expr(token_t*, FILE*);
static node_t* M(token_t*, FILE*);
static node_t* T(token_t*, FILE*);
static node_t* F(token_t*, FILE*);
static node_t* R(token_t*, FILE*);
static node_t* stats(token_t*, FILE*);
static node_t* mStat(token_t*, FILE*);
static node_t* stat(token_t*, FILE*);
static node_t* in(token_t*, FILE*);
static node_t* out(token_t*, FILE*);
static node_t* iff(token_t*, FILE*);
static node_t* loop(token_t*, FILE*);
static node_t* assign(token_t*, FILE*);
static node_t* RO(token_t*, FILE*);
static void error(int, token_t*);
static node_t* getNode(char*);
static token_t* getToken(token_t*);
//----parser function----
//Creates the root node, sets the root child to the program function, and prints the parse tree if the parse is successful.
node_t* parser(FILE* file){
node_t *root;
token_t token = scanner(file);
root = program(&token, file);
if(token.tokenID == EOF_tk){
printf("Parse successful.\n");
//printParseTree(root, 0);
}
else{
error(0, &token);
}
return root;
}
/*
These are the nonterminal functions that follow the BNF. In each function, a new node is created using the getNode()
function. Based on the BNF, these functions get tokens from the scanner, set the node's children to other nonterminal
functions, and store tokens in the node if needed. If there is an error, the error function is called. A node is returned
(either NULL or the actual node) in each function.
*/
static node_t* program(token_t *token, FILE* file){
node_t* node = getNode("program");
node->child1 = vars(token, file);
node->child2 = block(token, file);
return node;
}
static node_t* block(token_t *token, FILE* file){
if(token->tokenID == KW_tk && strcmp(token->tokenInstance, "BEGIN") == 0){
node_t *node = getNode("block");
*token = scanner(file);
node->child1 = vars(token, file);
if(token->tokenID == COL_tk){
*token = scanner(file);
node->child2 = stats(token, file);
if(token->tokenID == KW_tk && strcmp(token->tokenInstance, "END") == 0){
*token = scanner(file);
return node;
}
else{
error(2, token);
return NULL;
}
}
else{
error(5, token);
return NULL;
}
}
else{
error(1, token);
return NULL;
}
}
static node_t* vars(token_t *token, FILE* file){
if(token->tokenID == KW_tk && strcmp(token->tokenInstance, "VOID") == 0){
node_t *node = getNode("vars");
*token = scanner(file);
if(token->tokenID == ID_tk){
node->token1 = getToken(token);
*token = scanner(file);
node->child1 = mvars(token, file);
return node;
}
else{
error(3, token);
return NULL;
}
}
else
return NULL;
}
static node_t* mvars(token_t *token, FILE* file){
if(token->tokenID == ID_tk){
node_t *node = getNode("mvars");
node->token1 = getToken(token);
*token = scanner(file);
node->child1 = mvars(token, file);
return node;
}
else
return NULL;
}
static node_t* expr(token_t *token, FILE* file){
node_t *node = getNode("expr");
node->child1 = M(token, file);
if(token->tokenID == PLUS_tk){
node->token1 = getToken(token);
*token = scanner(file);
node->child2 = expr(token, file);
return node;
}
else
return node;
}
static node_t* M(token_t *token, FILE* file){
node_t *node = getNode("M");
node->child1 = T(token, file);
if(token->tokenID == MIN_tk){
node->token1 = getToken(token);
*token = scanner(file);
node->child2 = M(token, file);
return node;
}
else
return node;
}
static node_t* T(token_t *token, FILE* file){
node_t *node = getNode("T");
node->child1 = F(token, file);
if(token->tokenID == MUL_tk){
node->token1 = getToken(token);
*token = scanner(file);
node->child2 = T(token, file);
return node;
}
else if(token->tokenID == DIV_tk){
node->token1 = getToken(token);
*token = scanner(file);
node->child2 = T(token, file);
return node;
}
else
return node;
}
static node_t* F(token_t *token, FILE* file){
node_t *node = getNode("F");
if(token->tokenID == MIN_tk){
node->token1 = getToken(token);
*token = scanner(file);
node->child1 = F(token, file);
return node;
}
else{
node->child1 = R(token, file);
return node;
}
}
static node_t* R(token_t *token, FILE* file){
if(token->tokenID == OPENPAR_tk){
node_t *node = getNode("R");
*token = scanner(file);
node->child1 = expr(token, file);
if(token->tokenID == CLOSEDPAR_tk){
*token = scanner(file);
return node;
}
else{
error(4, token);
return NULL;
}
}
else if(token->tokenID == ID_tk){
node_t *node = getNode("R");
node->token1 = getToken(token);
*token = scanner(file);
return node;
}
else if(token->tokenID == DIGIT_tk){
node_t *node = getNode("R");
node->token1 = getToken(token);
*token = scanner(file);
return node;
}
else{
error(8, token);
return NULL;
}
}
static node_t* stats(token_t *token, FILE* file){
node_t *node = getNode("stats");
node->child1 = stat(token, file);
if(token->tokenID == COL_tk){
*token = scanner(file);
node->child2 = mStat(token, file);
return node;
}
else{
error(5, token);
return NULL;
}
}
static node_t* mStat(token_t *token, FILE* file){
if((token->tokenID == KW_tk && strcmp(token->tokenInstance, "INPUT") == 0) || (token->tokenID == KW_tk && strcmp(token->tokenInstance, "OUTPUT") == 0) || (token->tokenID == KW_tk && strcmp(token->tokenInstance, "BEGIN") == 0) || (token->tokenID == KW_tk && strcmp(token->tokenInstance, "IF") == 0) || (token->tokenID == KW_tk && strcmp(token->tokenInstance, "FOR") == 0) || (token->tokenID == ID_tk)){
node_t *node = getNode("mStat");
node->child1 = stat(token, file);
if(token->tokenID == COL_tk){
*token = scanner(file);
node->child2 = mStat(token, file);
return node;
}
else{
error(5, token);
return NULL;
}
}
else
return NULL;
}
static node_t* stat(token_t *token, FILE* file){
node_t *node = getNode("stat");
if(token->tokenID == KW_tk && strcmp(token->tokenInstance, "INPUT") == 0){
node->child1 = in(token, file);
return node;
}
else if(token->tokenID == KW_tk && strcmp(token->tokenInstance, "OUTPUT") == 0){
node->child1 = out(token, file);
return node;
}
else if(token->tokenID == KW_tk && strcmp(token->tokenInstance, "BEGIN") == 0){
node->child1 = block(token, file);
return node;
}
else if(token->tokenID == KW_tk && strcmp(token->tokenInstance, "IF") == 0){
node->child1 = iff(token, file);
return node;
}
else if(token->tokenID == KW_tk && strcmp(token->tokenInstance, "FOR") == 0){
node->child1 = loop(token, file);
return node;
}
else if(token->tokenID == ID_tk){
node->child1 = assign(token, file);
return node;
}
else{
error(14, token);
return NULL;
}
}
static node_t* in(token_t *token, FILE* file){
if(token->tokenID == KW_tk && strcmp(token->tokenInstance, "INPUT") == 0){
node_t *node = getNode("in");
*token = scanner(file);
if(token->tokenID == ID_tk){
node->token1 = getToken(token);
*token = scanner(file);
return node;
}
else{
error(3, token);
return NULL;
}
}
else{
error(9, token);
return NULL;
}
}
static node_t* out(token_t *token, FILE* file){
if(token->tokenID == KW_tk && strcmp(token->tokenInstance, "OUTPUT") == 0){
node_t *node = getNode("out");
*token = scanner(file);
node->child1 = expr(token, file);
return node;
}
else{
error(10, token);
return NULL;
}
}
static node_t* iff(token_t *token, FILE* file){
if(token->tokenID == KW_tk && strcmp(token->tokenInstance, "IF") == 0){
node_t *node = getNode("if");
*token = scanner(file);
if(token->tokenID == OPENPAR_tk){
*token = scanner(file);
node->child1 = expr(token, file);
node->child2 = RO(token, file);
node->child3 = expr(token, file);
if(token->tokenID == CLOSEDPAR_tk){
*token = scanner(file);
node->child4 = block(token, file);
return node;
}
else{
error(4, token);
return NULL;
}
}
else{
error(6, token);
return NULL;
}
}
else{
error(11, token);
return NULL;
}
}
static node_t* loop(token_t *token, FILE* file){
if(token->tokenID == KW_tk && strcmp(token->tokenInstance, "FOR") == 0){
node_t *node = getNode("loop");
*token = scanner(file);
if(token->tokenID == OPENPAR_tk){
*token = scanner(file);
node->child1 = expr(token, file);
node->child2 = RO(token, file);
node->child3 = expr(token, file);
if(token->tokenID == CLOSEDPAR_tk){
*token = scanner(file);
node->child4 = block(token, file);
return node;
}
else{
error(4, token);
return NULL;
}
}
else{
error(6, token);
return NULL;
}
}
else{
error(12, token);
return NULL;
}
}
static node_t* assign(token_t *token, FILE* file){
if(token->tokenID == ID_tk){
node_t *node = getNode("assign");
node->token1 = getToken(token);
*token = scanner(file);
if(token->tokenID == EQ_tk){
*token = scanner(file);
node->child1 = expr(token, file);
return node;
}
else{
error(7, token);
return NULL;
}
}
else{
error(3, token);
return NULL;
}
}
static node_t* RO(token_t *token, FILE* file){
node_t *node = getNode("RO");
if(token->tokenID == GREQGR_tk){
node->token1 = getToken(token);
*token = scanner(file);
if(token->tokenID == EQ_tk){
node->token2 = getToken(token);
*token = scanner(file);
return node;
}
else
return node;
}
else if(token->tokenID == LSEQLS_tk){
node->token1 = getToken(token);
*token = scanner(file);
if(token->tokenID == EQ_tk){
node->token2 = getToken(token);
*token = scanner(file);
return node;
}
else
return node;
}
else if(token->tokenID == NOT_tk){
node->token1 = getToken(token);
*token = scanner(file);
return node;
}
else if(token->tokenID == EQ_tk){
node->token1 = getToken(token);
*token = scanner(file);
if(token->tokenID == EQ_tk){
node->token2 = getToken(token);
*token = scanner(file);
return node;
}
else{
error(7, token);
return NULL;
}
}
else{
error(13, token);
return NULL;
}
}
//Prints the errors based on the errorNumber that is passed to the function.
static void error(int errorNumber, token_t *token){
switch (errorNumber){
case 0:
printf("%s\n", "ERRORS");
exit(0);
case 1:
printf("%s%s%s%d\n", "Error: expected BEGIN, received ", token->tokenInstance, " on line number: ", token->lineNumber);
exit(0);
case 2:
printf("%s%s%s%d\n", "Error: expected END, received ", token->tokenInstance, " on line number: ", token->lineNumber);
exit(0);
case 3:
printf("%s%s%s%d\n", "Error: expected ID, received ", token->tokenInstance, " on line number: ", token->lineNumber);
exit(0);
case 4:
printf("%s%s%s%d\n", "Error: expected ), received ", token->tokenInstance, " on line number: ", token->lineNumber);
exit(0);
case 5:
printf("%s%s%s%d\n", "Error: expected :, received ", token->tokenInstance, " on line number: ", token->lineNumber);
exit(0);
case 6:
printf("%s%s%s%d\n", "Error: expected (, received ", token->tokenInstance, " on line number: ", token->lineNumber);
exit(0);
case 7:
printf("%s%s%s%d\n", "Error: expected =, received ", token->tokenInstance, " on line number: ", token->lineNumber);
exit(0);
case 8:
printf("%s%s%s%d\n", "Error: expected ( or ID or number, received ", token->tokenInstance, " on line number: ", token->lineNumber);
exit(0);
case 9:
printf("%s%s%s%d\n", "Error: expected INPUT, received ", token->tokenInstance, " on line number: ", token->lineNumber);
exit(0);
case 10:
printf("%s%s%s%d\n", "Error: expected OUTPUT, received ", token->tokenInstance, " on line number: ", token->lineNumber);
exit(0);
case 11:
printf("%s%s%s%d\n", "Error: expected IF, received ", token->tokenInstance, " on line number: ", token->lineNumber);
exit(0);
case 12:
printf("%s%s%s%d\n", "Error: expected FOR, received ", token->tokenInstance, " on line number: ", token->lineNumber);
exit(0);
case 13:
printf("%s%s%s%d\n", "Error: expected >=> or <=< or !! or =, received ", token->tokenInstance, " on line number: ", token->lineNumber);
exit(0);
case 14:
printf("%s%s%s%d\n", "Error: expected INPUT or OUTPUT or IF or FOR or Id, received ", token->tokenInstance, " on line number: ", token->lineNumber);
exit(0);
}
}
//Creates a new node. Uses the char* label that is passed as the node's label and then sets the tokens and children to NULL.
static node_t* getNode(char* label){
node_t* node;
node = (node_t*)malloc(sizeof(node_t));
node->label = label;
node->token1 = NULL;
node->token2 = NULL;
node->child1 = NULL;
node->child2 = NULL;
node->child3 = NULL;
node->child4 = NULL;
return node;
}
//Makes a copy of the token that is passed to it and returns this new copy.
static token_t* getToken(token_t *token){
token_t* newToken;
newToken = (token_t*)malloc(sizeof(token_t));
newToken->tokenInstance = token->tokenInstance;
newToken->tokenID = token->tokenID;
newToken->lineNumber = token->lineNumber;
return newToken;
}