-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.c
More file actions
817 lines (740 loc) · 23.4 KB
/
Copy pathparser.c
File metadata and controls
817 lines (740 loc) · 23.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
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
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
/*
Group No: 26
Authors:
Nithin Benny Myppan - 2016A7PS0014P
Adhitya Mamallan - 2016A7PS0028P
Swarup N - 2016A7PS0080P
Naveen Unnikrishnan - 2016A7PS0111P
*/
#include <string.h>
#include "parser.h"
#include "lexer.h"
char* terminalMap[] = { // Maps enum values to corresponding terminal/token names.
"TK_ASSIGNOP",
"TK_COMMENT",
"TK_FIELDID",
"TK_ID",
"TK_NUM",
"TK_RNUM",
"TK_FUNID",
"TK_RECORDID",
"TK_WITH",
"TK_PARAMETERS",
"TK_END",
"TK_WHILE",
"TK_TYPE",
"TK_MAIN",
"TK_GLOBAL",
"TK_PARAMETER",
"TK_LIST",
"TK_SQL",
"TK_SQR",
"TK_INPUT",
"TK_OUTPUT",
"TK_INT",
"TK_REAL",
"TK_COMMA",
"TK_SEM",
"TK_COLON",
"TK_DOT",
"TK_ENDWHILE",
"TK_OP",
"TK_CL",
"TK_IF",
"TK_THEN",
"TK_ENDIF",
"TK_READ",
"TK_WRITE",
"TK_RETURN",
"TK_PLUS",
"TK_MINUS",
"TK_MUL",
"TK_DIV",
"TK_CALL",
"TK_RECORD",
"TK_ENDRECORD",
"TK_ELSE",
"TK_AND",
"TK_OR",
"TK_NOT",
"TK_LT",
"TK_LE",
"TK_EQ",
"TK_GT",
"TK_GE",
"TK_NE",
"TK_ERROR",
"e",
"$"
};
char* nonTerminalMap[] = { // Maps enum values to corresponding non-terminal names.
"program",
"otherFunctions",
"mainFunction",
"stmts",
"stmt",
"function",
"input_par",
"output_par",
"parameter_list",
"dataType",
"remaining_list",
"primitiveDatatype",
"constructedDatatype",
"typeDefinitions",
"typeDefinition",
"declarations",
"declaration",
"otherStmts",
"returnStmt",
"fieldDefinitions",
"fieldDefinition",
"moreFields",
"global_or_not",
"assignmentStmt",
"iterativeStmt",
"conditionalStmt",
"ioStmt",
"funCallStmt",
"singleOrRecId",
"arithmeticExpression",
"new_24",
"outputParameters",
"inputParameters",
"idList",
"booleanExpression",
"elsePart",
"allVar",
"allVar_1",
"var",
"term",
"expPrime",
"lowPrecedenceOperators",
"factor",
"termPrime",
"highPrecedenceOperators",
"all",
"temp",
"logicalOp",
"relationalOp",
"optionalReturn",
"more_ids"
};
Grammar* loadGrammar(char* inputFileName){
/* Description: Loads grammar from txt file */
/* Arguments: File name of grammar file */
/* Return Type: Pointer to grammar structure */
int ruleNo = 1;
bool alternateRule = false;
FILE* inputFile = fopen(inputFileName, "r");
if(inputFile == NULL){ // Handler if an error is thrown while opening a file
fprintf(stderr, "Error opening file\n");
return NULL;
}
bool isFileEnd = false;
Grammar* grammar = (Grammar*)malloc(sizeof(Grammar)); // Allocate memory to hold all the grammar rules
grammar->ruleCount = 0;
grammar->rules = (Rules**)malloc(sizeof(Rules*)*NON_TERMINAL_COUNT);
for(int i = 0; i < NON_TERMINAL_COUNT; i++){
grammar->rules[i] = (Rules*)malloc(sizeof(Rules));
grammar->rules[i]->head = NULL;
grammar->rules[i]->ruleCount = 0;
}
char ch;
NonTerminal nonTerm;
while(1){
if(isFileEnd == true)
return grammar;
if(!alternateRule){
while((ch = fgetc(inputFile))!='<'){
if(ch == EOF){
return grammar;
}
}
char* nonTerminalSymbol = (char*)malloc(sizeof(char)*SYMBOL_SIZE); //Load a new rule from then input file
int i = 0;
while((ch = fgetc(inputFile))!='>'){
nonTerminalSymbol[i++] = ch;
}
nonTerminalSymbol[i] = '\0';
nonTerm = find(nonTerminalSymbol,false); //Loading the LHS of the rule
}
alternateRule = false;
Rule* newRule = (Rule*)malloc(sizeof(Rule));
newRule->ruleNo = ruleNo;
ruleNo = ruleNo+1;
newRule->next = NULL;
newRule->symbols = NULL;
SymbolList* list = (SymbolList*)malloc(sizeof(SymbolList));
list->length = 0;
list->head = NULL;
SymbolNode* currentNode = list->head;
while(1){
if(isFileEnd == true)
break;
ch = fgetc(inputFile);
while(ch == ' ' || ch == '\t' || ch == '=' || ch == '>'){
ch = fgetc(inputFile);
}
if(ch == '\n')
break;
else if(ch == '|'){
alternateRule = true;
break;
}
else if(ch == EOF){
isFileEnd = true;
break;
}
else if(ch == '<'){
char* nonTerminalSymbol = (char*)malloc(sizeof(char)*SYMBOL_SIZE);
int i = 0;
while((ch = fgetc(inputFile)) != '>'){
nonTerminalSymbol[i++] = ch;
}
nonTerminalSymbol[i] = '\0';
SymbolNode* symbolNode = makeSymbolNode(find(nonTerminalSymbol,false), false);
currentNode = addToRule(list, symbolNode, currentNode);
}
else if(ch == 'e' || ch >= 'A' && ch <= 'Z' || ch == '_'){
char* terminalSymbol = (char*)malloc(sizeof(char)*SYMBOL_SIZE);
int i = 0;
terminalSymbol[i++] = ch;
ch = fgetc(inputFile);
while(ch == '_' || ch >= 'A' && ch <= 'Z'){
terminalSymbol[i++] = ch;
ch = fgetc(inputFile);
}
terminalSymbol[i] = '\0';
SymbolNode* symbolNode = makeSymbolNode(find(terminalSymbol,true), true);
currentNode = addToRule(list, symbolNode, currentNode);
if(ch == '\n' || ch == EOF){
if(ch == EOF)
isFileEnd = true;
break;
}
if(ch == '|'){
alternateRule = true;
break;
}
}
}
newRule->symbols = list;
newRule->next = grammar->rules[nonTerm]->head;
grammar->rules[nonTerm]->head = newRule;
grammar->rules[nonTerm]->ruleCount++;
grammar->ruleCount++;
}
}
SymbolNode* makeSymbolNode(int index, bool isTerminal){
/* Description: Make linked list node from terminal/non-terminal details */
/* Arguments: Enum index of terminal/non-terminal, boolean specifiying if terminal or non-terminal */
/* Return Type: Pointer to linked list node */
SymbolNode* symbolNode = (SymbolNode*)malloc(sizeof(SymbolNode));
Symbol symbol;
if(isTerminal==1){
symbol.nonterm = (NonTerminal)index;
}
else{
symbol.term = (Token_type)index;
}
symbolNode->symbol = symbol;
symbolNode->isTerminal = isTerminal;
symbolNode->next=NULL;
return symbolNode;
}
int find(char* str, bool isTerminal){
/* Description: Get enum index from terminal/non-terminal map */
/* Arguments: Terminal/non-terminal string and boolean specifying whether terminal or non-terminal */
/* Return Type: Enum value */
if(isTerminal == 0){
for(int i=0;i<NON_TERMINAL_COUNT;i++){
if(strcmp(str,nonTerminalMap[i])==0)
return i;
}
}
else{
for(int i=0;i<TERMINAL_COUNT;i++){
if(strcmp(str,terminalMap[i])==0)
return i;
}
}
}
SymbolNode* addToRule(SymbolList* list, SymbolNode* symbolNode, SymbolNode* currentNode){
/* Description: Adds terminal/non-terminal to the rule linked list */
/* Arguments: Rule linked list, node to be added, current node */
/* Return Type: Updated current node pointer */
if(currentNode != NULL){
currentNode->next = symbolNode;
currentNode = currentNode->next;
}
else{
symbolNode->next = list->head;
list->head = symbolNode;
currentNode = list->head;
}
list->length++;
return currentNode;
}
void printGrammar(Grammar* grammar){
/* Description: Prints the grammar stored in the grammar data structure */
/* Arguments: Grammar data structure */
/* Return Type: Void */
printf("\n\nGrammar:\n\n\n\n");
for(int i=0;i<NON_TERMINAL_COUNT;i++){
printf("%d. <%s> ===> ",(i+1), nonTerminalMap[i]);
Rules* rules = grammar->rules[i];
Rule* temp = rules->head;
for(int j = 0;j< rules->ruleCount;j++){
if(j!=0)
printf("| ");
SymbolList* symbols = temp->symbols;
SymbolNode* temp2 = symbols->head;
for(int k=0;k<symbols->length;k++){
if(temp2->isTerminal==1)
printf("%s", terminalMap[temp2->symbol.term]);
else
printf("<%s>", nonTerminalMap[temp2->symbol.nonterm]);
printf(" ");
temp2 = temp2->next;
}
temp = temp->next;
}
printf("\n");
}
}
FirstAndFollow* getFirstAndFollowSets(Grammar* grammar){
/* Description: Compute first and follow sets for the given grammar */
/* Arguments: Grammar stored in grammar structure */
/* Return Type: Pointer to first and follow set structure */
FirstAndFollow* ffSets = (FirstAndFollow*)malloc(sizeof(FirstAndFollow));
ffSets->first = (bool**)malloc(NON_TERMINAL_COUNT*sizeof(bool*));
ffSets->follow = (bool**)malloc(NON_TERMINAL_COUNT*sizeof(bool*));
for(int i=0; i<NON_TERMINAL_COUNT; ++i){
ffSets->first[i] = (bool*)malloc(TERMINAL_COUNT*sizeof(bool));
ffSets->follow[i] = (bool*)malloc(TERMINAL_COUNT*sizeof(bool));
for(int j=0; j<TERMINAL_COUNT; ++j){
ffSets->first[i][j] = false;
ffSets->follow[i][j] = false;
}
}
for(int i=0; i < NON_TERMINAL_COUNT; ++i){
computeFirstSet(grammar,(NonTerminal)i, ffSets->first);
}
computeFollowSet(grammar,ffSets);
return ffSets;
}
void computeFirstSet(Grammar* grammar, NonTerminal nonTerminal, bool** first){
/* Description: Computes for given non-terminal from given grammar */
/* Arguments: Grammar, non-terminal, and first set table */
/* Return Type: void */
Rules* rules = grammar->rules[nonTerminal];
Rule* temp1 = rules->head;
for(int j = 0;j< rules->ruleCount;j++){
SymbolList* symbols = temp1->symbols;
SymbolNode* temp2 = symbols->head;
int k;
for(k=0; k < symbols->length; k++){
if(temp2->isTerminal == false){
if(nonTerminal!=temp2->symbol.nonterm){
computeFirstSet(grammar, temp2->symbol.nonterm,first);
setUnion(first[nonTerminal], first[temp2->symbol.nonterm]);
}
if(!first[temp2->symbol.nonterm][EPS]){
break;
}
}
else{
addToSet(first[nonTerminal],temp2->symbol.term);
break;
}
temp2 = temp2->next;
}
if(symbols->length == k){
addToSet(first[nonTerminal],EPS);
}
temp1 = temp1->next;
}
}
void computeFollowSet(Grammar* grammar, FirstAndFollow* sets){
/* Description: Compute follow set for given grammar */
/* Arguments: Grammar and first and follow set structure */
/* Return Type: void */
bool hasChanged = true;
addToSet(sets->follow[program],DOLLAR);
while(hasChanged){
hasChanged = computeFollowUtil(grammar,sets->first,sets->follow);
}
}
bool computeFollowUtil(Grammar* grammar, bool** first, bool** follow){
/* Description: Do one pass for follow set computation */
/* Arguments: Grammar, first set table, and follow set table */
/* Return Type: boolean specifying if follow set changed in this pass */
bool hasChanged = false;
for(int i=0; i<NON_TERMINAL_COUNT; ++i){
Rules* rules = grammar->rules[i];
Rule* temp1 = rules->head;
for(int j = 0; j< rules->ruleCount; ++j){
SymbolList* symbols = temp1->symbols;
SymbolNode* temp2 = symbols->head;
for(int k = 0; k < symbols->length; ++k){
if(temp2->isTerminal == false){
SymbolNode* temp3 = temp2->next;
while(temp3 != NULL){
if(temp3->isTerminal == false){
hasChanged = hasChanged | setUnion(follow[temp2->symbol.nonterm],first[temp3->symbol.nonterm]);
if(!first[temp3->symbol.nonterm][EPS])
break;
}
else{
if(!follow[temp2->symbol.nonterm][ temp3->symbol.term]){
hasChanged = true;
addToSet(follow[temp2->symbol.nonterm], temp3->symbol.term);
}
break;
}
temp3 = temp3->next;
}
if(temp3 == NULL){
hasChanged = hasChanged | setUnion(follow[temp2->symbol.nonterm],follow[i]);
}
}
temp2 = temp2->next;
}
temp1 = temp1->next;
}
}
return hasChanged;
}
void addToSet(bool* set, int index){
/* Description: Add terminal at index in the enum to the set */
/* Arguments: The set (one row of first/follow set table) and index of terminal */
/* Return Type: void */
set[index] = true;
}
bool setUnion(bool* a, bool* b){
/* Description: Union of two sets */
/* Arguments: Two sets */
/* Return Type: boolean specifying if the first set changed */
bool hasChanged = false;
for(int i = 0; i < TERMINAL_COUNT; ++i){
if(i == EPS)
continue;
if(a[i] != (a[i]|b[i])){
hasChanged = true;
a[i] = a[i] | b[i];
}
}
return hasChanged;
}
void printFirstAndFollow(FirstAndFollow* sets){
/* Description: Print first and follow sets */
/* Arguments: First and follow set structure */
/* Return Type: void */
printf("\n\nFirst Set:\n\n");
for(int i=0; i < NON_TERMINAL_COUNT; ++i){
printf("%d. %s => ",(i+1),nonTerminalMap[i]);
printf("( ");
for(int i=0;i< TERMINAL_COUNT; ++i){
if(sets->first[i]){
printf("%s ",terminalMap[i]);
}
}
printf(")\n");
}
printf("\n\nFollow Set:\n\n");
for(int i=0;i<NON_TERMINAL_COUNT;i++){
printf("%d. %s => ",(i+1),nonTerminalMap[i]);
printf("( ");
for(int i=0;i< TERMINAL_COUNT; ++i){
if(sets->follow[i]){
printf("%s ",terminalMap[i]);
}
}
printf(")\n");
}
}
ParsingTable makeNewParseTable(){
/* Description: Make new empty parse table */
/* Arguments: void */
/* Return Type: ParsingTable */
ParsingTable table = (ParsingTable)malloc(sizeof(Rule**)*NON_TERMINAL_COUNT);
for(int i=0; i<NON_TERMINAL_COUNT; ++i){
table[i] = (Rule**)malloc(sizeof(Rule*)*TERMINAL_COUNT);
}
for(int i=0; i<NON_TERMINAL_COUNT; ++i){
for(int j=0; j<TERMINAL_COUNT; ++j){
table[i][j] = NULL;
}
}
return table;
}
void createParseTable(Grammar* grammar, FirstAndFollow* sets, ParsingTable table){
/* Description: Construct parse table from grammar and first and follow sets */
/* Arguments: Grammar, first and follow set structure, and parsing table */
/* Return Type: void */
for(int i=0; i<NON_TERMINAL_COUNT; ++i){
Rules* rules = grammar->rules[i];
Rule* tempRule = rules->head;
int nrules = rules->ruleCount;
for(int j=0; j<nrules; ++j){
SymbolList* ruleSymbols = tempRule->symbols;
SymbolNode* tempSymbol = ruleSymbols->head;
bool endHere = true;
int nsymbols = ruleSymbols->length;
for(int k=0; k<nsymbols; ++k){
if(tempSymbol->isTerminal){
if(tempSymbol->symbol.term != EPS){
if(table[i][tempSymbol->symbol.term]!=NULL && tempRule!=table[i][tempSymbol->symbol.term]){
printf("ERROR: More than one rule indexed into the parse table at %s : %s\n",nonTerminalMap[i],terminalMap[tempSymbol->symbol.term]);
}
table[i][tempSymbol->symbol.term] = tempRule;
endHere = true;
}
else{
for(int p=0; p<TERMINAL_COUNT; ++p){
if(sets->follow[i][p]){
if(table[i][p]!=NULL && tempRule!=table[i][p]){
printf("ERROR: More than one rule indexed into the parse table at %s : %s\n", nonTerminalMap[i], terminalMap[p]);
}
table[i][p] = tempRule;
}
}
endHere = false;
}
}
else{
for(int p=0; p<TERMINAL_COUNT; ++p){
if(sets->first[tempSymbol->symbol.nonterm][p]){
if(p!=EPS){
if(table[i][p]!=NULL && table[i][p]!=tempRule){
printf("ERROR: More than one rule indexed into the parse table at %s : %s\n",nonTerminalMap[i],terminalMap[p]);
}
table[i][p] = tempRule;
endHere = true;
}
else
endHere = false;
}
}
}
tempSymbol = tempSymbol->next;
if(endHere) break;
else continue;
}
tempRule = tempRule->next;
}
}
}
void printParsingTable(ParsingTable table){
/* Description: Prints parsing table */
/* Arguments: Parsing table */
/* Return Type: void */
printf("\n\nPrinting Parsing Table:\n\n");
for(int i=0; i<NON_TERMINAL_COUNT; ++i){
printf("%2d. %25s : ",(i+1),nonTerminalMap[i]);
for(int j=0; j<TERMINAL_COUNT; ++j){
if(table[i][j]!=NULL) printf("1 ");
else printf("0 ");
printf(" ");
}
printf("\n\n");
}
}
ParseTree parseInputSourceCode(char *testcaseFile, ParsingTable table, FirstAndFollow* ff, int* parseErrors){
/* Description: Parse source code from file using parse table and first-follow sets and construct parse tree */
/* Arguments: Source code file name, parsing table, first and follow sets, and integer to detect parse errors */
/* Return Type: Parse tree */
printf("\n");
FILE* fp = lexer_initialisation(testcaseFile);
if(fp == NULL){
printf("\nFile not opened for parsing\n");
return NULL;
}
Stack* stack = initializeStack();
TreeNode* bottomMarker = makeNode(true,DOLLAR,NULL);
push(stack,bottomMarker);
ParseTree tree = makeNode(false,program,NULL);
push(stack,tree);
Lexical_Unit* lu = getNextToken(&fp);
Token_type lookahead;
if(lu == NULL){
printf("\n\nInput File Empty\n\n");
return NULL;
}
bool hasError = false;
while(true){
if(lu == NULL){
if(bottomOfStack(stack) && !hasError){
printf("\n\n########## Source code is syntactically CORRECT ##########\n\n");
}
else{
*parseErrors = 1;
printf("\n\n########## Source code is syntactically WRONG ##########\n\n");
}
break;
}
lookahead = lu->token;
if(lookahead == TK_COMMENT){
lu = getNextToken(&fp);
continue;
}
else if(lookahead == TK_ERROR){
hasError = true;
*parseErrors=1;
lu = getNextToken(&fp);
continue;
}
if(bottomOfStack(stack)){
*parseErrors = 1;
printf("\n\n########## Source code is syntactically WRONG ##########\n\n");
break;
}
StackNode* stackNode = pop(stack);
TreeNode* treeNode = stackNode->parseTreeNode;
if(!(treeNode->content->isTerminal)){
Rule* ruleToBeUsed = table[treeNode->content->type.nonterm][lookahead];
bool recovered = false;
if(ruleToBeUsed != NULL){
addChildren(treeNode, ruleToBeUsed);
pushChildrenToStack(stack,treeNode);
}
else{
*parseErrors = 1;
fprintf(stderr,"Line %d\t| Syntax error: The token for lexeme %s does not match at line %d\n",lu->line_no,lu->lexeme,lu->line_no);
StackNode* recStackNode = top(stack);
TreeNode* recNode = recStackNode->parseTreeNode;
while(lu != NULL){
lookahead = lu->token;
if(ff->first[recNode->content->type.nonterm][lookahead]){
recovered = true;
push(stack,recNode);
break;
}
else if(ff->follow[recNode->content->type.nonterm][lookahead]){
recovered = true;
break;
}
else
lu = getNextToken(&fp);
}
if(recovered)
continue;
}
}
else{
if(lookahead == treeNode->content->type.term){
treeNode->lu = lu;
lu = getNextToken(&fp);
continue;
}
else{
hasError = true;
*parseErrors = 1;
fprintf(stderr,"Line %d\t| Syntax error: The token for lexeme %s does not match at line %d\n",lu->line_no,lu->lexeme,lu->line_no);
while(lu!=NULL){
if(lookahead == treeNode->content->type.term){
push(stack, treeNode);
break;
}
else if(treeNode->content->isTerminal){
if(lookahead == treeNode->content->type.term)
break;
}
else{
Rule* ruleToBeUsed = table[treeNode->content->type.nonterm][lookahead];
if(ruleToBeUsed!=NULL){
break;
}
}
if(lu->token == TK_SEM){
lu = getNextToken(&fp);
lookahead = lu->token;
break;
}
lu = getNextToken(&fp);
lookahead = lu->token;
}
continue;
}
}
}
if(fp!=NULL)
fclose(fp);
return tree;
}
void printParseTree(ParseTree tree){
/* Description: Print parse tree to file */
/* Arguments: Parse tree and output file name */
/* Return Type: void */
FILE* fp1 = stdout;
fprintf(fp1,"\n######################################### Parse Tree #########################################\n\n");
fprintf(fp1,"%-25s %-10s %-15s %-15s %-30s %-5s %s\n\n\n", "Lexeme","LineNo","TokenName","ValueIfNumber","ParentNodeSymbol","IsLeafNode","NodeSymbol");
printParseTree_util(tree,&fp1);
fprintf(fp1,"\n######################################### End Of Parse Tree #########################################\n\n");
}
void printParseTree_util(TreeNode* node, FILE** fp1){
/* Description: Utility function for printing parse tree to file */
/* Arguments: Node of parse tree, file pointer to output file */
/* Return Type: void */
if(node == NULL){
return;
}
char* blank = "----";
char* no = "No";
char* yes = "Yes";
char* root = "Root";
Children* children = node->children;
if(children != NULL){
TreeNode* temp = children->head;
printParseTree_util(temp,fp1);
printNode(fp1,node, 0, blank,no,yes,root);
temp = temp->next;
while(temp!=NULL){
printParseTree_util(temp, fp1);
temp = temp->next;
}
}
else{
printNode(fp1,node, 1, blank,no,yes,root);
}
}
void printNode(FILE** fp1, TreeNode* node, bool isLeaf, char* empty, char* no, char* yes, char* root){
/* Description: Print information about one node of parse tree into file */
/* Arguments: Output file pointer, node of tree, details of node */
/* Return Type: void */
char* error = "<Error>" ;
if(isLeaf){
if(!(node->content->isTerminal)){
if(node->parent != NULL){
fprintf(*fp1,"%-25s %-10s %-15s %-15s %-30s %-5s <%s>\n", empty,empty,empty,empty,nonTerminalMap[node->parent->content->type.nonterm],yes,nonTerminalMap[node->content->type.nonterm]);
}
else{
fprintf(*fp1,"%-25s %-10s %-15s %-15s %-30s %-5s <%s>\n", empty,empty,empty,empty,root,yes,nonTerminalMap[node->content->type.nonterm]);
}
}
else if(node->content->type.term==EPS){
fprintf(*fp1,"%-25s %-10s %-15s %-15s %-30s %-5s %s\n", empty,empty,empty,empty,nonTerminalMap[node->parent->content->type.nonterm],yes,terminalMap[node->content->type.term]);
}
else if(node->lu == NULL){
fprintf(*fp1,"%-25s %-10s %-15s %-15s %-30s %-5s %s\n", error, error, error, empty, nonTerminalMap[node->parent->content->type.nonterm], yes, terminalMap[node->content->type.term]);
}
else if(node->lu->token == TK_NUM){
fprintf(*fp1,"%-25s %-10d %-15s %-15d %-30s %-5s %s\n", node->lu->lexeme, node->lu->line_no, terminalMap[node->lu->token], node->lu->val->integer, nonTerminalMap[node->parent->content->type.nonterm], yes, terminalMap[node->content->type.term]);
}
else if(node->lu->token == TK_RNUM){
fprintf(*fp1,"%-25s %-10d %-15s %-15f %-30s %-5s %s\n", node->lu->lexeme, node->lu->line_no, terminalMap[node->lu->token], node->lu->val->real, nonTerminalMap[node->parent->content->type.nonterm], yes, terminalMap[node->content->type.term]);
}
else{
fprintf(*fp1,"%-25s %-10d %-15s %-15s %-30s %-5s %s\n", node->lu->lexeme, node->lu->line_no, terminalMap[node->lu->token], empty, nonTerminalMap[node->parent->content->type.nonterm], yes, terminalMap[node->content->type.term]);
}
}
else{
if(node->parent != NULL){
fprintf(*fp1,"%-25s %-10s %-15s %-15s %-30s %-5s <%s>\n", empty,empty,empty,empty,nonTerminalMap[node->parent->content->type.nonterm],no,nonTerminalMap[node->content->type.nonterm]);
}
else{
fprintf(*fp1,"%-25s %-10s %-15s %-15s %-30s %-5s <%s>\n", empty,empty,empty,empty,root,no,nonTerminalMap[node->content->type.nonterm]);
}
}
}