-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.c
More file actions
1445 lines (1393 loc) · 40.4 KB
/
parser.c
File metadata and controls
1445 lines (1393 loc) · 40.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
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
GROUP 22 :
ID : 2021B3A70995P | Name : Dhruv Ravi Krishnan
ID : 2021B3A70981P | Name : Anirudh Anand
ID : 2021B3A71738P | Name : Akshit Phophaliya
ID : 2022A7PS1182P | Name : Arnav Dham
ID : 2022A7TS0154P | Name : Shaurya Jain
ID : 2022A7PS0187P | Name : Darsh Rathi
*/
#include "parserDef.h"
#include <time.h>
extern GRAMMAR grammar_glob;
extern ProdRule ParseTable[NUM_NONTERMS][NUM_TERMS + 1];
// Providing functionality of :
// 1. Linked List of Elements,
// 2. Set of Linked Lists (Nested Linked Lists: used for production rules)
// 3. Stack (using Linked List)
LL_Set createNewList_LL()
{
LL_Set list = malloc(sizeof(ll_ll));
list->head = NULL;
list->count = 0;
return list;
}
NODE_LL createNewNode_LL(LL_ELE data)
{
NODE_LL new_node = malloc(sizeof(node_LL));
new_node->next = NULL;
new_node->item = data;
return new_node;
}
void insertNode_LLLast(NODE_LL node, LL_Set list)
{
if (!node || !list)
return;
if (list->head == NULL)
list->head = node;
else
{
NODE_LL temp = list->head;
while (temp->next)
temp = temp->next;
temp->next = node;
}
node->next = NULL;
list->count++;
}
LL_ELE createNewList_Ele()
{
LL_ELE list = malloc(sizeof(ll_ele));
list->head = NULL;
list->count = 0;
return list;
}
NODE_ELE createNewNode_Ele(Elements data)
{
NODE_ELE new_node = malloc(sizeof(node_Ele));
new_node->next = NULL;
new_node->item = data;
return new_node;
}
void insertNode_EleFirst(NODE_ELE node, LL_ELE list)
{
node->next = list->head;
list->head = node;
list->count++;
}
void insertNode_EleLast(NODE_ELE node, LL_ELE list)
{
if (!node || !list)
return;
if (list->head == NULL)
list->head = node;
else
{
NODE_ELE temp = list->head;
while (temp->next)
temp = temp->next;
temp->next = node;
}
node->next = NULL;
list->count++;
}
void freeNode_Ele(NODE_ELE node) { free(node); }
void removeNode_EleFirst(LL_ELE list)
{
if (!list->head)
return;
NODE_ELE temp = list->head->next;
NODE_ELE rem = list->head;
list->head = temp;
rem->next = NULL;
list->count--;
freeNode_Ele(rem);
}
Stack *createStack()
{
Stack *st = (Stack *)malloc(sizeof(Stack));
st->list = createNewList_Ele();
return st;
}
void push(Stack *st, Elements element)
{
NODE_ELE node = createNewNode_Ele(element);
insertNode_EleFirst(node, st->list);
}
Elements top(Stack *st) { return ((st->list)->head)->item; }
void pop(Stack *st)
{
if (!st || st->list->count <= 1 || !st->list)
{
return;
}
else
{
removeNode_EleFirst(st->list);
}
}
// Populating Parse Table for terminals according to synchronizing array
void syncPopulateParseTable(FIRSTANDFOLLOW Fnf)
{
// Terminals, at which a new line starts, from where we can resume parsing
Elements syncArr[] = {TKN_ID, TKN_FUNID, TKN_RUID, TKN_WHILE, TKN_UNION, TKN_DEFINETYPE,
TKN_TYPE, TKN_MAIN, TKN_IF, TKN_READ, TKN_WRITE, TKN_RETURN,
TKN_CALL, TKN_RECORD, TKN_THEN, TKN_ELSE};
for (int i = 0; i < NUM_NONTERMS; i++)
{
NODE_ELE ptr = Fnf->followSet[i]->head;
// Configuring Parse Table for Terminals which belong to the respective Non-Terminal's Follow Set
while (ptr != NULL)
{
if (ParseTable[i][ptr->item - NUM_NONTERMS].LHS == TKN_ERROR)
ParseTable[i][ptr->item - NUM_NONTERMS].count_rhs = -1;
ptr = ptr->next;
}
// Configuring Parse Table for Terminals at which new line begins
for (int j = 0; j < 16; j++)
{
if (ParseTable[i][syncArr[j] - NUM_NONTERMS].LHS == TKN_ERROR)
ParseTable[i][syncArr[j] - NUM_NONTERMS].count_rhs = -1;
}
}
}
// Converting Elements enum to according string
char *TKToString[] = {"program",
"mainFunction",
"otherFunctions",
"function",
"input_par",
"output_par",
"parameter_list",
"dataType",
"primitiveDatatype",
"constructedDatatype",
"remaining_list",
"stmts",
"typeDefinitions",
"actualOrRedefined",
"typeDefinition",
"fieldDefinitions",
"fieldDefinition",
"fieldType",
"moreFields",
"declarations",
"declaration",
"global_or_not",
"otherStmts",
"stmt",
"assignmentStmt",
"singleOrRecId",
"option_single_constructed",
"oneExpansion",
"moreExpansions",
"funCallStmt",
"outputParameters",
"inputParameters",
"iterativeStmt",
"conditionalStmt",
"elsePart",
"ioStmt",
"arithmeticExpression",
"expPrime",
"term",
"termPrime",
"factor",
"highPrecedenceOperator",
"lowPrecedenceOperators",
"booleanExpression",
"var",
"logicalOp",
"relationalOp",
"returnStmt",
"optionalReturn",
"idList",
"more_ids",
"definetypestmt",
"A",
"TK_ERROR",
"TK_ID",
"TK_NUM",
"TK_END",
"TK_WHILE",
"TK_UNION",
"TK_ENDUNION",
"TK_DEFINETYPE",
"TK_AS",
"TK_TYPE",
"TK_MAIN",
"TK_ASSIGNOP",
"TK_COMMENT",
"TK_FIELDID",
"TK_GLOBAL",
"TK_PARAMETER",
"TK_MUL",
"TK_DIV",
"TK_CALL",
"TK_RECORD",
"TK_GT",
"TK_GE",
"TK_LIST",
"TK_SQL",
"TK_SQR",
"TK_INPUT",
"TK_ENDIF",
"TK_READ",
"TK_WRITE",
"TK_RETURN",
"TK_OUTPUT",
"TK_ENDRECORD",
"TK_ELSE",
"TK_AND",
"TK_OR",
"TK_NOT",
"TK_LT",
"TK_LE",
"TK_EQ",
"TK_INT",
"TK_REAL",
"TK_COMMA",
"TK_SEM",
"TK_COLON",
"TK_DOT",
"TK_ENDWHILE",
"TK_OP",
"TK_CL",
"TK_IF",
"TK_THEN",
"TK_PLUS",
"TK_MINUS",
"TK_RNUM",
"TK_FUNID",
"TK_RUID",
"TK_WITH",
"TK_PARAMETERS",
"TK_NE",
"TK_EPSILON",
"TK_DOLLAR"};
// Converting Elements enum to according string
Elements stringToTK(char *str)
{
if (strcmp(str, "TK_ERROR") == 0)
{
return TKN_ERROR;
}
else if (strcmp(str, "TK_ASSIGNOP") == 0)
{
return TKN_ASSIGNOP;
}
else if (strcmp(str, "TK_COMMENT") == 0)
{
return TKN_COMMENT;
}
else if (strcmp(str, "TK_FIELDID") == 0)
{
return TKN_FIELDID;
}
else if (strcmp(str, "TK_ID") == 0)
{
return TKN_ID;
}
else if (strcmp(str, "TK_NUM") == 0)
{
return TKN_NUM;
}
else if (strcmp(str, "TK_RNUM") == 0)
{
return TKN_RNUM;
}
else if (strcmp(str, "TK_FUNID") == 0)
{
return TKN_FUNID;
}
else if (strcmp(str, "TK_RUID") == 0)
{
return TKN_RUID;
}
else if (strcmp(str, "TK_WITH") == 0)
{
return TKN_WITH;
}
else if (strcmp(str, "TK_PARAMETERS") == 0)
{
return TKN_PARAMETERS;
}
else if (strcmp(str, "TK_END") == 0)
{
return TKN_END;
}
else if (strcmp(str, "TK_WHILE") == 0)
{
return TKN_WHILE;
}
else if (strcmp(str, "TK_UNION") == 0)
{
return TKN_UNION;
}
else if (strcmp(str, "TK_ENDUNION") == 0)
{
return TKN_ENDUNION;
}
else if (strcmp(str, "TK_DEFINETYPE") == 0)
{
return TKN_DEFINETYPE;
}
else if (strcmp(str, "TK_AS") == 0)
{
return TKN_AS;
}
else if (strcmp(str, "TK_TYPE") == 0)
{
return TKN_TYPE;
}
else if (strcmp(str, "TK_MAIN") == 0)
{
return TKN_MAIN;
}
else if (strcmp(str, "TK_GLOBAL") == 0)
{
return TKN_GLOBAL;
}
else if (strcmp(str, "TK_PARAMETER") == 0)
{
return TKN_PARAMETER;
}
else if (strcmp(str, "TK_LIST") == 0)
{
return TKN_LIST;
}
else if (strcmp(str, "TK_SQL") == 0)
{
return TKN_SQL;
}
else if (strcmp(str, "TK_SQR") == 0)
{
return TKN_SQR;
}
else if (strcmp(str, "TK_INPUT") == 0)
{
return TKN_INPUT;
}
else if (strcmp(str, "TK_OUTPUT") == 0)
{
return TKN_OUTPUT;
}
else if (strcmp(str, "TK_INT") == 0)
{
return TKN_INT;
}
else if (strcmp(str, "TK_REAL") == 0)
{
return TKN_REAL;
}
else if (strcmp(str, "TK_COMMA") == 0)
{
return TKN_COMMA;
}
else if (strcmp(str, "TK_SEM") == 0)
{
return TKN_SEM;
}
else if (strcmp(str, "TK_COLON") == 0)
{
return TKN_COLON;
}
else if (strcmp(str, "TK_DOT") == 0)
{
return TKN_DOT;
}
else if (strcmp(str, "TK_ENDWHILE") == 0)
{
return TKN_ENDWHILE;
}
else if (strcmp(str, "TK_OP") == 0)
{
return TKN_OP;
}
else if (strcmp(str, "TK_CL") == 0)
{
return TKN_CL;
}
else if (strcmp(str, "TK_IF") == 0)
{
return TKN_IF;
}
else if (strcmp(str, "TK_THEN") == 0)
{
return TKN_THEN;
}
else if (strcmp(str, "TK_ENDIF") == 0)
{
return TKN_ENDIF;
}
else if (strcmp(str, "TK_READ") == 0)
{
return TKN_READ;
}
else if (strcmp(str, "TK_WRITE") == 0)
{
return TKN_WRITE;
}
else if (strcmp(str, "TK_RETURN") == 0)
{
return TKN_RETURN;
}
else if (strcmp(str, "TK_PLUS") == 0)
{
return TKN_PLUS;
}
else if (strcmp(str, "TK_MINUS") == 0)
{
return TKN_MINUS;
}
else if (strcmp(str, "TK_MUL") == 0)
{
return TKN_MUL;
}
else if (strcmp(str, "TK_DIV") == 0)
{
return TKN_DIV;
}
else if (strcmp(str, "TK_CALL") == 0)
{
return TKN_CALL;
}
else if (strcmp(str, "TK_RECORD") == 0)
{
return TKN_RECORD;
}
else if (strcmp(str, "TK_ENDRECORD") == 0)
{
return TKN_ENDRECORD;
}
else if (strcmp(str, "TK_ELSE") == 0)
{
return TKN_ELSE;
}
else if (strcmp(str, "TK_AND") == 0)
{
return TKN_AND;
}
else if (strcmp(str, "TK_OR") == 0)
{
return TKN_OR;
}
else if (strcmp(str, "TK_NOT") == 0)
{
return TKN_NOT;
}
else if (strcmp(str, "TK_LT") == 0)
{
return TKN_LT;
}
else if (strcmp(str, "TK_LE") == 0)
{
return TKN_LE;
}
else if (strcmp(str, "TK_EQ") == 0)
{
return TKN_EQ;
}
else if (strcmp(str, "TK_GT") == 0)
{
return TKN_GT;
}
else if (strcmp(str, "TK_GE") == 0)
{
return TKN_GE;
}
else if (strcmp(str, "TK_NE") == 0)
{
return TKN_NE;
}
else if (strcmp(str, "TK_EPSILON") == 0)
{
return TKN_EPSILON;
}
else if (strcmp(str, "TK_DOLLAR") == 0)
{
return TKN_DOLLAR;
}
else if (strcmp(str, "program") == 0)
{
return program;
}
else if (strcmp(str, "mainFunction") == 0)
{
return mainFunction;
}
else if (strcmp(str, "otherFunctions") == 0)
{
return otherFunctions;
}
else if (strcmp(str, "function") == 0)
{
return function;
}
else if (strcmp(str, "input_par") == 0)
{
return input_par;
}
else if (strcmp(str, "output_par") == 0)
{
return output_par;
}
else if (strcmp(str, "parameter_list") == 0)
{
return parameter_list;
}
else if (strcmp(str, "dataType") == 0)
{
return dataType;
}
else if (strcmp(str, "primitiveDatatype") == 0)
{
return primitiveDatatype;
}
else if (strcmp(str, "constructedDatatype") == 0)
{
return constructedDatatype;
}
else if (strcmp(str, "remaining_list") == 0)
{
return remaining_list;
}
else if (strcmp(str, "stmts") == 0)
{
return stmts;
}
else if (strcmp(str, "typeDefinitions") == 0)
{
return typeDefinitions;
}
else if (strcmp(str, "actualOrRedefined") == 0)
{
return actualOrRedefined;
}
else if (strcmp(str, "typeDefinition") == 0)
{
return typeDefinition;
}
else if (strcmp(str, "fieldDefinitions") == 0)
{
return fieldDefinitions;
}
else if (strcmp(str, "fieldDefinition") == 0)
{
return fieldDefinition;
}
else if (strcmp(str, "fieldType") == 0)
{
return fieldType;
}
else if (strcmp(str, "moreFields") == 0)
{
return moreFields;
}
else if (strcmp(str, "declarations") == 0)
{
return declarations;
}
else if (strcmp(str, "declaration") == 0)
{
return declaration;
}
else if (strcmp(str, "global_or_not") == 0)
{
return global_or_not;
}
else if (strcmp(str, "otherStmts") == 0)
{
return otherStmts;
}
else if (strcmp(str, "stmt") == 0)
{
return stmt;
}
else if (strcmp(str, "stmt") == 0)
{
return stmt;
}
else if (strcmp(str, "assignmentStmt") == 0)
{
return assignmentStmt;
}
else if (strcmp(str, "singleOrRecId") == 0)
{
return singleOrRecId;
}
else if (strcmp(str, "option_single_constructed") == 0)
{
return option_single_constructed;
}
else if (strcmp(str, "oneExpansion") == 0)
{
return oneExpansion;
}
else if (strcmp(str, "moreExpansions") == 0)
{
return moreExpansions;
}
else if (strcmp(str, "funCallStmt") == 0)
{
return funCallStmt;
}
else if (strcmp(str, "outputParameters") == 0)
{
return outputParameters;
}
else if (strcmp(str, "inputParameters") == 0)
{
return inputParameters;
}
else if (strcmp(str, "iterativeStmt") == 0)
{
return iterativeStmt;
}
else if (strcmp(str, "conditionalStmt") == 0)
{
return conditionalStmt;
}
else if (strcmp(str, "elsePart") == 0)
{
return elsePart;
}
else if (strcmp(str, "ioStmt") == 0)
{
return ioStmt;
}
else if (strcmp(str, "arithmeticExpression") == 0)
{
return arithmeticExpression;
}
else if (strcmp(str, "expPrime") == 0)
{
return expPrime;
}
else if (strcmp(str, "term") == 0)
{
return term;
}
else if (strcmp(str, "termPrime") == 0)
{
return termPrime;
}
else if (strcmp(str, "factor") == 0)
{
return factor;
}
else if (strcmp(str, "highPrecedenceOperator") == 0)
{
return highPrecedenceOperator;
}
else if (strcmp(str, "lowPrecedenceOperators") == 0)
{
return lowPrecedenceOperators;
}
else if (strcmp(str, "booleanExpression") == 0)
{
return booleanExpression;
}
else if (strcmp(str, "var") == 0)
{
return var;
}
else if (strcmp(str, "logicalOp") == 0)
{
return logicalOp;
}
else if (strcmp(str, "relationalOp") == 0)
{
return relationalOp;
}
else if (strcmp(str, "returnStmt") == 0)
{
return returnStmt;
}
else if (strcmp(str, "optionalReturn") == 0)
{
return optionalReturn;
}
else if (strcmp(str, "idList") == 0)
{
return idList;
}
else if (strcmp(str, "more_ids") == 0)
{
return more_ids;
}
else if (strcmp(str, "definetypestmt") == 0)
{
return definetypestmt;
}
else if (strcmp(str, "A") == 0)
{
return A;
}
return -1;
}
// Parsing grammar.txt to convert into Grammar, which is an array of Set of Linked Lists
void parseFileGrammar(char *filename)
{
FILE *fp = fopen(filename, "r");
if (!fp)
{
printf("Error in opening file\n");
return;
}
for (int i = 0; i < NUM_NONTERMS; i++)
{
grammar_glob->rules[i] = createNewList_LL();
}
char buff[1024];
while (fgets(buff, 1024, fp) != NULL)
{
char delim1[] = " ";
char delim2[] = "\n";
char *fullLine = strtok(buff, delim2);
char *firstptr = strtok(fullLine, delim1);
int LHS_NonTerm = stringToTK(firstptr);
firstptr = strtok(NULL, delim1);
firstptr = strtok(NULL, delim1);
LL_ELE curr = createNewList_Ele();
while (firstptr)
{
if (firstptr[0] == ';')
{
insertNode_LLLast(createNewNode_LL(curr),
grammar_glob->rules[LHS_NonTerm]);
curr = createNewList_Ele();
firstptr = strtok(NULL, delim1);
continue;
}
NODE_ELE one = createNewNode_Ele(stringToTK(firstptr));
insertNode_EleLast(one, curr);
firstptr = strtok(NULL, delim1);
}
insertNode_LLLast(createNewNode_LL(curr),
grammar_glob->rules[LHS_NonTerm]);
}
fclose(fp);
return;
}
// Checking epsilon in a Linked List of terminals
int epsilonInFirst(LL_ELE first)
{
NODE_ELE ptr = first->head;
while (ptr != NULL)
{
if (ptr->item == TKN_EPSILON)
return 1;
ptr = ptr->next;
}
return 0;
}
// Computing First Sets of all Elements based on Grammar
void computeFirst(FIRSTANDFOLLOW firstAndFollowSet)
{
/*
1. If X is a terminal, then FIRST(X) = {X}.
2. If X is a nonterminal and X -> Y1 Y2 ... Yk is a production for some k >=
1, then place a in FIRST(X) if for some i, a is in FIRST(Yi), and eps is in
all of eps. If eps is in FIRST(Y ) FIRST(Y1 ); : : : ; FIRST(Yi,1 );
that is, Y1 ... Yi,1 )
j
for all j = 1; 2; : : : ; k, then add eps to FIRST(X ). For example,
everything in FIRST(Y1 ) is surely in FIRST(X ). If Y1 does not derive eps,
then we add eps, then we add FIRST(Y ), and nothing more to FIRST(X ), but
if Y1 )
2
so on.
3. If X -> eps is a production, then add eps to FIRST(X).
*/
int change = 1;
Elements lhs = TKN_ERROR;
for (; lhs < NUM_ELEMENTS; lhs++)
{
insertNode_EleLast(createNewNode_Ele(lhs),
firstAndFollowSet->firstSet[lhs]);
}
while (change)
{
change = 0;
lhs = program;
for (; lhs < NUM_NONTERMS; lhs++)
{
NODE_ELE firstLhsHead = firstAndFollowSet->firstSet[lhs]->head;
NODE_LL currRHS = grammar_glob->rules[lhs]->head;
if (currRHS->item->head->item == TKN_EPSILON &&
!epsilonInFirst(firstAndFollowSet->firstSet[lhs]))
{
insertNode_EleLast(createNewNode_Ele(TKN_EPSILON),
firstAndFollowSet->firstSet[lhs]);
currRHS = currRHS->next;
continue;
}
while (currRHS != NULL)
{
NODE_ELE currTerm = currRHS->item->head;
while (currTerm != NULL)
{
NODE_ELE firstTerm =
firstAndFollowSet->firstSet[currTerm->item]->head;
while (firstTerm != NULL)
{
if (firstTerm->item == TKN_EPSILON)
{
firstTerm = firstTerm->next;
continue;
}
NODE_ELE tempLhs = firstLhsHead;
int visited = 0;
while (tempLhs != NULL)
{
if (tempLhs->item == firstTerm->item)
visited = 1;
tempLhs = tempLhs->next;
}
if (!visited)
{
insertNode_EleLast(
createNewNode_Ele(firstTerm->item),
firstAndFollowSet->firstSet[lhs]);
change = 1;
}
firstTerm = firstTerm->next;
}
if (currTerm->next == NULL &&
epsilonInFirst(
firstAndFollowSet->firstSet[currTerm->item]) &&
!epsilonInFirst(firstAndFollowSet->firstSet[lhs]))
insertNode_EleLast(createNewNode_Ele(TKN_EPSILON),
firstAndFollowSet->firstSet[lhs]);
if (!epsilonInFirst(
firstAndFollowSet->firstSet[currTerm->item]))
break;
currTerm = currTerm->next;
}
currRHS = currRHS->next;
}
}
}
}
// Computing Follow Sets of all Elements based on Grammar and First sets
void computeFollow(FIRSTANDFOLLOW firstAndFollowSet)
{
/*
1. Place $ in FOLLOW(S), where S is the start symbol, and $ is the input
right endmarker.
2. If there is a production A->aBb , then everything in FIRST(b) except eps
is in FOLLOW(B).
3. If there is a production A->aB , or a production A->aBb , where FIRST(b)
contains eps, then everything in FOLLOW(A) is in FOLLOW(B).
*/
insertNode_EleLast(createNewNode_Ele(stringToTK("TK_DOLLAR")),
firstAndFollowSet->followSet[0]);
int change = 1;
while (change)
{
change = 0;
int lhs = 0;
for (; lhs < NUM_NONTERMS; lhs++)
{
NODE_LL currRHS = grammar_glob->rules[lhs]->head;
while (currRHS != NULL)
{
NODE_ELE currTerm = currRHS->item->head;
NODE_ELE nextTerm = currTerm;
while (currTerm != NULL)
{
if (currTerm->item >= NUM_NONTERMS)
{
currTerm = currTerm->next;
nextTerm = currTerm;
continue;
}
nextTerm = nextTerm->next;
if (nextTerm == NULL)
{
NODE_ELE lhsPtr =
firstAndFollowSet->followSet[lhs]->head;
while (lhsPtr != NULL)
{
Elements term = lhsPtr->item;
NODE_ELE bPtr =
firstAndFollowSet->followSet[currTerm->item]
->head;
int visited = 0;
while (bPtr != NULL)
{
if (bPtr->item == term)
visited = 1;
bPtr = bPtr->next;
}
if (!visited)
{
insertNode_EleLast(
createNewNode_Ele(term),
firstAndFollowSet
->followSet[currTerm->item]);
change = 1;
}
lhsPtr = lhsPtr->next;
}
}
else
{
NODE_ELE betaPtr =
firstAndFollowSet->firstSet[nextTerm->item]->head;
while (betaPtr != NULL)
{
Elements term = betaPtr->item;
if (term == TKN_EPSILON)
{
betaPtr = betaPtr->next;
continue;
}
NODE_ELE bPtr =
firstAndFollowSet->followSet[currTerm->item]
->head;
int visited = 0;
while (bPtr != NULL)
{
if (bPtr->item == term)
visited = 1;
bPtr = bPtr->next;
}
if (!visited)
{
insertNode_EleLast(
createNewNode_Ele(term),
firstAndFollowSet
->followSet[currTerm->item]);
change = 1;
}
betaPtr = betaPtr->next;
}
if (epsilonInFirst(
firstAndFollowSet->firstSet[nextTerm->item]))
continue;
if (epsilonInFirst(
firstAndFollowSet->firstSet[nextTerm->item]) &&
(nextTerm->next == NULL))
{
NODE_ELE lhsPtr =
firstAndFollowSet->followSet[lhs]->head;
while (lhsPtr != NULL)
{
Elements term = lhsPtr->item;
NODE_ELE bPtr =
firstAndFollowSet->followSet[currTerm->item]
->head;
int visited = 0;
while (bPtr != NULL)
{
if (bPtr->item == term)
visited = 1;