-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser.c
681 lines (644 loc) · 16 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
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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
/*
************************************************************
* COMPILERS COURSE - Algonquin College
* Code version: Winter, 2022
* Author: Sohrab Najafzadeh
* Professors: Paulo Sousa / George Kriger / Abdullah Kadri
************************************************************
*/
/*
************************************************************
* File name: mainBuffer.c
* Compiler: MS Visual Studio 2022
* Author: Paulo Sousa
* Course: CST 8152 – Compilers, Lab Section: [011, 012, 013, 014]
* Assignment: A12, A22, A32.
* Date: Jan 01 2022
* Professor: Paulo Sousa
* Purpose: This file contains all functionalities from Parser.
* Function list: (...).
************************************************************
*/
/* TODO: Adjust the function header */
#ifndef COMPILERS_H_
#include "Compilers.h"
#endif
#ifndef PARSER_H_
#include "Parser.h"
#endif
/*
************************************************************
* Process Parser
***********************************************************
*/
/* TODO: This is the function to start the parser - check your program definition */
oslo_null startParser() {
lookahead = tokenizer();
if (lookahead.code != SEOF_T) {
program();
}
matchToken(SEOF_T, NO_ATTR);
printf("%s%s\n", STR_LANGNAME, ": Source file parsed");
}
/*
************************************************************
* Match Token
***********************************************************
*/
/* TODO: This is the main code for match - check your definition */
oslo_null matchToken(oslo_int tokenCode, oslo_int tokenAttribute) {
oslo_int matchFlag = 1;
switch (lookahead.code) {
case CID_T:
while (lookahead.code == tokenCode) {
printf("%s%s\n", STR_LANGNAME, ": Comment parsed");
lookahead = tokenizer();
}
break;
case KW_T:
default:
if (lookahead.code != tokenCode)
matchFlag = 0;
}
if (matchFlag && lookahead.code == SEOF_T)
return;
if (matchFlag) {
//printf("matched lookhead.code: %d\n", lookahead.code);
lookahead = tokenizer();
if (lookahead.code == ERR_T) {
printError();
lookahead = tokenizer();
syntaxErrorNumber++;
}
}
else
syncErrorHandler(tokenCode);
}
/*
************************************************************
* Syncronize Error Handler
***********************************************************
*/
/* TODO: This is the function to handler error - adjust basically datatypes */
oslo_null syncErrorHandler(oslo_int syncTokenCode) {
printError();
syntaxErrorNumber++;
while (lookahead.code != syncTokenCode) {
if (lookahead.code == SEOF_T)
exit(syntaxErrorNumber);
lookahead = tokenizer();
}
if (lookahead.code != SEOF_T)
lookahead = tokenizer();
}
/*
************************************************************
* Print Error
***********************************************************
*/
/* TODO: This is the function to error printing - adjust basically datatypes */
oslo_null printError() {
Token t = lookahead;
printf("%s%s%3d\n", STR_LANGNAME, ": Syntax error: Line:", line);
printf("***** Token code:%3d Attribute: ", t.code);
switch (t.code) {
case ERR_T:
printf("%s\n", t.attribute.errLexeme);
break;
case SEOF_T:
printf("SEOF_T\t\t%d\t\n", t.attribute.seofType);
break;
case MNID_T:
printf("%s\n", t.attribute.idLexeme);
break;
case STR_T:
printf("%s\n", bGetContent(stringLiteralTable, t.attribute.contentString));
break;
case KW_T:
printf("%s\n", keywordTable[t.attribute.codeType]);
break;
case LPR_T:
case RPR_T:
case LBR_T:
case RBR_T:
case EOS_T:
printf("NA\n");
break;
default:
printf("%s%s%d\n", STR_LANGNAME, ": Scanner error: invalid token code: ", t.code);
}
}
/*
************************************************************
* Program statement
* BNF: <program> -> PLATYPUS { <opt_statements> }
* FIRST(<program>)= {KW_T (MAIN)}.
***********************************************************
*/
oslo_null program() {
switch (lookahead.code)
{
case CID_T:
matchToken(CID_T, NO_ATTR);
switch (lookahead.code)
{
case KW_T:
if (lookahead.attribute.codeType == 2)
{
matchToken(KW_T, NO_ATTR);
case MNID_T:
if (strncmp(lookahead.attribute.idLexeme, "main", 4) == 0) {
matchToken(MNID_T, NO_ATTR);
matchToken(LPR_T, NO_ATTR);
matchToken(RPR_T, NO_ATTR);
matchToken(LBR_T, NO_ATTR);
if (lookahead.code == EOS_T)
matchToken(EOS_T, NO_ATTR);
dataSession();
codeSession();
matchToken(RBR_T, NO_ATTR);
break;
}
else {
printError();
}
}
case SEOF_T:
; // Empty
break;
default:
printError();
}
break;
case KW_T:
if (lookahead.attribute.codeType == 2)
{
matchToken(KW_T, NO_ATTR);
case MNID_T:
if (strncmp(lookahead.attribute.idLexeme, "main", 4) == 0) {
matchToken(MNID_T, NO_ATTR);
matchToken(LPR_T, NO_ATTR);
matchToken(RPR_T, NO_ATTR);
matchToken(LBR_T, NO_ATTR);
if (lookahead.code == EOS_T)
matchToken(EOS_T, NO_ATTR);
dataSession();
codeSession();
matchToken(RBR_T, NO_ATTR);
break;
}
else {
printError();
}
}
case SEOF_T:
; // Empty
break;
default:
printError();
}
printf("%s%s\n", STR_LANGNAME, ": Program parsed");
}
/*
************************************************************
* dataSession
* BNF: <dataSession> -> DATA { <opt_varlist_declarations> }
* FIRST(<program>)= {KW_T (DATA)}.
***********************************************************
*/
oslo_null dataSession() {
optVarListDeclarations();
printf("%s%s\n", STR_LANGNAME, ": Data Session parsed");
}
/*
************************************************************
* Optional Var List Declarations
* BNF: <opt_varlist_declarations> -> <varlist_declarations> | e
* FIRST(<opt_varlist_declarations>) = { e, KW_T (INT), KW_T (FLOAT), KW_T (STRING)}.
***********************************************************
*/
oslo_null optVarListDeclarations() {
switch (lookahead.code) {
case VID_T:
varListDeclarations();
break;
default:
; // Empty
}
printf("%s%s\n", STR_LANGNAME, ": Optional Variable List Declarations parsed");
}
oslo_null varListDeclarations() {
varListDeclaration();
varListDeclarationsPrime();
printf("%s%s\n", STR_LANGNAME, ": Variable List Declarations parsed");
}
oslo_null varListDeclaration() {
switch (lookahead.code) {
case VID_T:
strcpy_s(variableTable[variableCount], sizeof(variableTable[variableCount]), lookahead.attribute.idLexeme);
variableCount++;
matchToken(VID_T, NO_ATTR);
case ASS_OP_T:
matchToken(ASS_OP_T, NO_ATTR);
switch (lookahead.code)
{
case KW_T:
if (lookahead.attribute.codeType == 11) {
matchToken(KW_T, NO_ATTR);
printf("%s%s\n", STR_LANGNAME, ": Variable identifier parsed");
if (lookahead.code == EOS_T) {
matchToken(EOS_T, NO_ATTR);
}
}
break;
case LS_T:
matchToken(LS_T, NO_ATTR);
if (lookahead.code == EOS_T) {
matchToken(EOS_T, NO_ATTR);
}
break;
case STR_T:
matchToken(STR_T, NO_ATTR);
if (lookahead.code == EOS_T) {
matchToken(EOS_T, NO_ATTR);
}
default:
printError();
}
break;
default:
printError();
}
}
oslo_null varListDeclarationsPrime() {
oslo_int found = 0;
switch (lookahead.code) {
case VID_T:
for (size_t i = 0; i < variableCount; i++)
{
if (strcmp(variableTable[i],lookahead.attribute.idLexeme)==0) {
found = 1;
break;
}
}
if (!found) {
varListDeclaration();
varListDeclarationsPrime();
}
break;
default:
; //empty string
}
}
/*
************************************************************
* codeSession statement
* BNF: <codeSession> -> CODE { <opt_statements> }
* FIRST(<codeSession>)= {KW_T (CODE)}.
***********************************************************
*/
oslo_null codeSession() {
optionalStatements();
printf("%s%s\n", STR_LANGNAME, ": Code Session parsed");
}
/* TODO_205: Continue the development (all non-terminal functions) */
/*
************************************************************
* Optional statement
* BNF: <opt_statements> -> <statements> | ϵ
* FIRST(<opt_statements>) = { ϵ , IVID_T, FVID_T, SVID_T, KW_T(IF),
* KW_T(WHILE), KW_T(READ), KW_T(WRITE) }
***********************************************************
*/
oslo_null optionalStatements() {
switch (lookahead.code) {
case KW_T:
statements();
break;
case MNID_T:
statements();
break;
case VID_T:
statements();
break;
default:
; // Empty
}
printf("%s%s\n", STR_LANGNAME, ": Optional statements parsed");
}
/*
************************************************************
* Statements
* BNF: <statements> -> <statement><statementsPrime>
* FIRST(<statements>) = { IVID_T, FVID_T, SVID_T, KW_T(IF),
* KW_T(WHILE), KW_T(READ), KW_T(WRITE) }
***********************************************************
*/
oslo_null statements() {
statement();
statementsPrime();
printf("%s%s\n", STR_LANGNAME, ": Statements parsed");
}
/*
************************************************************
* Statements Prime
* BNF: <statementsPrime> <statement><statementsPrime> | ϵ
* FIRST(<statementsPrime>) = { ϵ , IVID_T, FVID_T, SVID_T,
* KW_T(IF), KW_T(WHILE), KW_T(READ), KW_T(WRITE) }
***********************************************************
*/
oslo_null statementsPrime() {
switch (lookahead.code) {
case MNID_T:
statements();
break;
case VID_T:
statements();
break;
default:
; //empty string
}
}
/*
************************************************************
* Single statement
* BNF: <statement> -> <assignment statement> | <selection statement> |
* <iteration statement> | <input statement> | <output statement>
* FIRST(<statement>) = { IVID_T, FVID_T, SVID_T, KW_T(IF), KW_T(WHILE),
* KW_T(READ), KW_T(WRITE) }
***********************************************************
*/
oslo_null statement() {
switch (lookahead.code) {
case MNID_T:
if (strncmp(lookahead.attribute.idLexeme, "print", 5) == 0) {
outputStatement();
}
break;
case VID_T:
matchToken(VID_T, NO_ATTR);
printf("%s%s\n", STR_LANGNAME, ": Variable identifier parsed");
if (lookahead.code == ASS_OP_T) {
matchToken(ASS_OP_T, NO_ATTR);
assignmentStatement();
}
else {
printError();
}
break;
case KW_T:
if (lookahead.code == KW_T) {
matchToken(KW_T, NO_ATTR);
assignmentStatement();
}
break;
default:
printError();
}
printf("%s%s\n", STR_LANGNAME, ": Statement parsed");
}
/*
************************************************************
* Assignment Statement
* BNF: <assignment statement> -> <assignment expression>
* FIRST(<assignment statement>) = { IVID_T, FVID_T, SVID_T, KW_T(IF),
* KW_T(WHILE), KW_T(READ), KW_T(WRITE) }
***********************************************************
*/
oslo_null assignmentStatement() {
assignmentExpression();
matchToken(EOS_T, NO_ATTR);
printf("%s%s\n", STR_LANGNAME, ": Assignment statement parsed");
}
/*
************************************************************
* Assignment Expression
* BNF: <assignment expression> -> <integer_variable> = <arithmetic expression>
* | <float_variable> = <arithmetic expression>
* | <string_variable>= <string expression>
* FIRST(<assignment expression>) = { IVID_T, FVID_T, SVID_T }
***********************************************************
*/
oslo_null assignmentExpression() {
switch (lookahead.code) {
case KW_T:
matchToken(KW_T, NO_ATTR);
case LPR_T:
matchToken(LPR_T, NO_ATTR);
switch (lookahead.code) {
case VID_T:
arithmeticExpression();
stringExpression();
break;
case LS_T:
arithmeticExpression();
break;
case STR_T:
stringExpression();
break;
case MNID_T:
if (strncmp(lookahead.attribute.idLexeme, "input", 5) == 0) {
matchToken(MNID_T, NO_ATTR);
matchToken(LPR_T, NO_ATTR);
matchToken(RPR_T, NO_ATTR);
}
break;
default:
printError();
}
matchToken(RPR_T, NO_ATTR);
break;
case VID_T:
arithmeticExpression();
stringExpression();
break;
case LS_T:
arithmeticExpression();
break;
case STR_T:
stringExpression();
break;
default:
printError();
}
printf("%s%s\n", STR_LANGNAME, ": Assignment expression parsed");
}
oslo_null arithmeticExpression() {
switch (lookahead.code) {
case ART_OP_T:
switch (lookahead.attribute.codeType) {
case OP_ADD:
matchToken(ART_OP_T, OP_ADD);
break;
case OP_SUB:
matchToken(ART_OP_T, OP_SUB);
break;
case OP_DIV:
matchToken(ART_OP_T, OP_DIV);
break;
case OP_MUL:
matchToken(ART_OP_T, OP_MUL);
break;
default:
printError();
}
primaryArithmeticExpression();
break;
case RPR_T:
matchToken(RPR_T, NO_ATTR);
case LPR_T:
case VID_T:
case LS_T:
additiveArithmeticExpression();
break;
case EOS_T:
break;
default:
printError();
}
printf("%s%s\n", STR_LANGNAME, ": Arithmetic expression parsed");
}
oslo_null primaryArithmeticExpression() {
switch (lookahead.code) {
case VID_T:
matchToken(VID_T, NO_ATTR);
if (lookahead.code != EOS_T)
arithmeticExpression();
break;
case LS_T:
matchToken(LS_T, NO_ATTR);
if(lookahead.code!=EOS_T)
arithmeticExpression();
break;
case LPR_T:
arithmeticExpression();
break;
default:
printError();
}
printf("%s%s\n", STR_LANGNAME, ": primary arithmetic expression parsed");
}
oslo_null additiveArithmeticExpression() {
multiplicativeArithmeticExpression();
additiveArithmeticExpressionPrime();
printf("%s%s\n", STR_LANGNAME, ": Additive arithmetic expression parsed");
}
oslo_null additiveArithmeticExpressionPrime() {
switch (lookahead.code) {
case ART_OP_T:
switch (lookahead.attribute.codeType) {
case OP_ADD:
matchToken(ART_OP_T, OP_ADD);
break;
case OP_SUB:
matchToken(ART_OP_T, OP_SUB);
break;
case OP_DIV:
matchToken(ART_OP_T, OP_DIV);
break;
case OP_MUL:
matchToken(ART_OP_T, OP_MUL);
break;
default:
;
}
}
printf("%s%s\n", STR_LANGNAME, ": additive Arithmetic Expression Prime parsed");
}
oslo_null multiplicativeArithmeticExpression() {
switch (lookahead.code) {
case LPR_T:
matchToken(LPR_T, NO_ATTR);
case VID_T:
case LS_T:
primaryArithmeticExpression();
break;
case EOS_T:
break;
default:
printError();
}
printf("%s%s\n", STR_LANGNAME, ": multiplicative Arithmetic Expression parsed");
}
oslo_null multiplicativeArithmeticExpressionPrime() {
switch (lookahead.code) {
case ART_OP_T:
switch (lookahead.attribute.codeType) {
case OP_MUL:
matchToken(ART_OP_T, OP_MUL);
break;
case OP_DIV:
matchToken(ART_OP_T, OP_DIV);
break;
default:
;
}
}
printf("%s%s\n", STR_LANGNAME, ": multiplicative Arithmetic Expression Prime parsed");
}
oslo_null stringExpression() {
primaryStringExpression();
stringExpressionPrime();
printf("%s%s\n", STR_LANGNAME, ": String expression parsed");
}
oslo_null stringExpressionPrime() {
switch (lookahead.code) {
case VID_T:
matchToken(VID_T, NO_ATTR);
break;
case STR_T:
matchToken(STR_T, NO_ATTR);
break;
default:
;
}
}
oslo_null primaryStringExpression() {
switch (lookahead.code) {
case VID_T:
matchToken(VID_T, NO_ATTR);
break;
case STR_T:
matchToken(STR_T, NO_ATTR);
break;
default:
printError();
}
printf("%s%s\n", STR_LANGNAME, ": primary string expression parsed");
}
/*
************************************************************
* Output Statement
* BNF: <output statement> -> WRITE (<output statementPrime>);
* FIRST(<output statement>) = { KW_T(WRITE) }
***********************************************************
*/
oslo_null outputStatement() {
matchToken(MNID_T, NO_ATTR);
matchToken(LPR_T, NO_ATTR);
outputVariableList();
matchToken(RPR_T, NO_ATTR);
matchToken(EOS_T, NO_ATTR);
printf("%s%s\n", STR_LANGNAME, ": Output statement parsed");
}
/*
************************************************************
* Output Variable List
* BNF: <opt_variable list> -> <variable list> | ϵ
* FIRST(<opt_variable_list>) = { IVID_T, FVID_T, SVID_T, ϵ }
***********************************************************
*/
oslo_null outputVariableList() {
switch (lookahead.code) {
case STR_T:
matchToken(STR_T, NO_ATTR);
//printf("%s\n", "OSLO: Output list (string literal) parsed");
break;
case VID_T:
matchToken(VID_T, NO_ATTR);
default:
//printf("%s\n", "OSLO: Output list (empty) parsed");
;
}
printf("%s%s\n", STR_LANGNAME, ": Output variable list parsed");
}