-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrammar.c
941 lines (867 loc) · 25 KB
/
grammar.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
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
// GROUP 39
// AKANKSHYA MISHRA 2016A7PS0026P
// NARAPAREDDY BHAVANA 2016A7PS0034P
// KARABEE BATTA 2016A7PS0052P
// AASTHA KATARIA 2016A7PS0062P
#include <stdlib.h>
#include <stdio.h>
#include "lexer.h"
#include "grammarDef.h"
g_node_head* create_g_node_head(NON_TERMINAL nt){
g_node_head *head = (g_node_head*)malloc(sizeof(g_node_head));
head->non_terminal = nt;
head->next = NULL;
return head;
}
node_head_follow* create_head_follow(NON_TERMINAL nt_index)
{
node_head_follow* head=(node_head_follow*) malloc(sizeof(node_head_follow));
head->is_visited=false;
head->head=NULL;
return head;
}
node_head_first* create_head_first(NON_TERMINAL nt_index)
{
node_head_first* head=(node_head_first*) malloc(sizeof(node_head_first));
head->has_eps=false;
head->head=NULL;
return head;
}
g_node* create_g_node(bool is_term, int value){
g_node *g = (g_node*)malloc(sizeof(g_node));
g->is_term = is_term;
if(is_term) g->elem.terminal = (TOKEN)value;
else g->elem.nonterminal = (NON_TERMINAL)value;
g->next = NULL;
g->prev = NULL;
return g;
}
node* create_node(TOKEN tkname, int rule_no){
node *n = (node*)malloc(sizeof(node));
n->tokenName = tkname;
n->rule_no_index = rule_no;
n->next = NULL;
return n;
}
g_node* first_gnode(g_node_head* h,int no,bool is_term)
{
g_node* pnext;
pnext = h->next = create_g_node(is_term, no);
pnext->prev=NULL;
pnext->next=NULL;
return pnext;
}
g_node* dl_nodes(g_node* pnext,int no,bool is_term)
{
g_node* ptr;
ptr=pnext->next=create_g_node(is_term,no);
ptr->prev=pnext;
ptr->next=NULL;
return ptr;
}
void print_gnode(g_node* ptr)
{
if(ptr->is_term)
{
printf( " %s ",TerminalString( ptr->elem.terminal));
}
else
printf("%s ", nonTerminalStringTable[ ptr->elem.nonterminal]->nonterminal);
}
void print_grule(g_node_head* head)
{
printf("Rule %d: %s -->",head->rule_no,nonTerminalStringTable [head->non_terminal]->nonterminal);
g_node* temp=head->next;
while(temp!=NULL){
print_gnode(temp);
temp=temp->next;
}
printf("\n");
}
void populateGrammar(){
grammar = (g_node_head**)malloc(sizeof(g_node_head*)*NO_OF_GRAMMAR_RULES); //array of g_node_head pointers
g_node *ptr, *pnext;
//Rule 0
ptr = NULL;
grammar[0]=create_g_node_head(program);
pnext=first_gnode(grammar[0],otherFunctions,0);
pnext=dl_nodes(pnext,mainFunction,0);
// print_gnode(pnext);
//Rule 1
grammar[1]=create_g_node_head(mainFunction);
pnext=first_gnode(grammar[1],TK_MAIN,1);
pnext=dl_nodes(pnext,stmts,0);
pnext=dl_nodes(pnext,TK_END,1);
//Rule 2
grammar[2]=create_g_node_head(otherFunctions);
pnext=first_gnode(grammar[2],function,0);
pnext=dl_nodes(pnext,otherFunctions,0);
//Rule 3(eps)
grammar[3]=create_g_node_head(otherFunctions);
pnext = first_gnode(grammar[3],eps,1);
//Rule 4
grammar[4]=create_g_node_head(function);
pnext=first_gnode(grammar[4],TK_FUNID,1);
pnext=dl_nodes(pnext,input_par,0);
pnext=dl_nodes(pnext,output_par,0);
pnext=dl_nodes(pnext,TK_SEM,1);
pnext=dl_nodes(pnext,stmts,0);
pnext=dl_nodes(pnext,TK_END,1);
//Rule 5
grammar[5]=create_g_node_head(input_par);
pnext=first_gnode(grammar[5],TK_INPUT,1);
pnext=dl_nodes(pnext,TK_PARAMETER,1);
pnext=dl_nodes(pnext,TK_LIST,1);
pnext=dl_nodes(pnext,TK_SQL,1);
pnext=dl_nodes(pnext,parameter_list,0);
pnext=dl_nodes(pnext,TK_SQR,1);
//Rule 6
grammar[6]=create_g_node_head(output_par);
pnext=first_gnode(grammar[6], TK_OUTPUT, 1);
pnext=dl_nodes(pnext, TK_PARAMETER, 1);
pnext=dl_nodes(pnext, TK_LIST, 1);
pnext=dl_nodes(pnext, TK_SQL, 1);
pnext=dl_nodes(pnext, parameter_list, 0);
pnext=dl_nodes(pnext, TK_SQR, 1);
//Rule 7
grammar[7]=create_g_node_head(output_par);
pnext=first_gnode(grammar[7], eps, 1);
//Rule 8
grammar[8]=create_g_node_head(parameter_list);
pnext=first_gnode(grammar[8],dataType,0);
pnext=dl_nodes(pnext,TK_ID,1);
pnext=dl_nodes(pnext,remaining_list,0);
//Rule 9
grammar[9]=create_g_node_head(dataType);
pnext=first_gnode(grammar[9],primitiveDatatype,0);
//Rule 10
grammar[10]=create_g_node_head(dataType);
pnext=first_gnode(grammar[10],constructedDatatype,0);
//Rule 11
grammar[11]=create_g_node_head(primitiveDatatype);
pnext=first_gnode(grammar[11],TK_INT,1);
//Rule 12
grammar[12]=create_g_node_head(primitiveDatatype);
pnext=first_gnode(grammar[12],TK_REAL,1);
//Rule 13
grammar[13]=create_g_node_head(constructedDatatype);
pnext=first_gnode(grammar[13],TK_RECORD,1);
pnext=dl_nodes(pnext,TK_RECORDID,1);
//Rule 14
grammar[14]=create_g_node_head(remaining_list);
pnext=first_gnode(grammar[14],TK_COMMA,1);
pnext=dl_nodes(pnext,parameter_list,0);
//Rule 15(eps)
grammar[15]=create_g_node_head(remaining_list);
pnext=first_gnode(grammar[15],eps,1);
//Rule 16
grammar[16]=create_g_node_head(stmts);
pnext=first_gnode(grammar[16],typeDefinitions,0);
pnext=dl_nodes(pnext,declarations,0);
pnext=dl_nodes(pnext,otherStmts,0);
pnext=dl_nodes(pnext,returnStmt,0);
//Rule 17
grammar[17]=create_g_node_head(typeDefinitions);
pnext=first_gnode(grammar[17],typeDefinition,0);
pnext=dl_nodes(pnext,typeDefinitions,0);
//Rule 18(eps)
grammar[18]=create_g_node_head(typeDefinitions);
pnext=first_gnode(grammar[18],eps,1);
//Rule 19
grammar[19]=create_g_node_head(typeDefinition);
pnext=first_gnode(grammar[19],TK_RECORD,1);
pnext=dl_nodes(pnext,TK_RECORDID,1);
pnext=dl_nodes(pnext,fieldDefinitions,0);
pnext=dl_nodes(pnext,TK_ENDRECORD,1);
pnext=dl_nodes(pnext,TK_SEM,1);
//Rule 20
grammar[20]=create_g_node_head(fieldDefinitions);
pnext=first_gnode(grammar[20],fieldDefinition,0);
pnext=dl_nodes(pnext,fieldDefinition,0);
pnext=dl_nodes(pnext,moreFields,0);
//Rule 21
grammar[21]=create_g_node_head(fieldDefinition);
pnext=first_gnode(grammar[21],TK_TYPE,1);
pnext=dl_nodes(pnext,primitiveDatatype,0);
pnext=dl_nodes(pnext,TK_COLON,1);
pnext=dl_nodes(pnext,TK_FIELDID,1);
pnext=dl_nodes(pnext,TK_SEM,1);
//Rule 22
grammar[22]=create_g_node_head(moreFields);
pnext=first_gnode(grammar[22],fieldDefinition,0);
pnext=dl_nodes(pnext,moreFields,0);
//Rule 23(eps)
grammar[23]=create_g_node_head(moreFields);
pnext=first_gnode(grammar[23],eps,1);
//Rule 24
grammar[24]=create_g_node_head(declarations);
pnext=first_gnode(grammar[24],declaration,0);
pnext=dl_nodes(pnext,declarations,0);
//Rule 25(eps)
grammar[25]=create_g_node_head(declarations);
pnext=first_gnode(grammar[25],eps,1);
//Rule 26
grammar[26]=create_g_node_head(declaration);
pnext=first_gnode(grammar[26],TK_TYPE,1);
pnext=dl_nodes(pnext,dataType,0);
pnext=dl_nodes(pnext,TK_COLON,1);
pnext=dl_nodes(pnext,TK_ID,1);
pnext=dl_nodes(pnext,global_or_not,0);
pnext=dl_nodes(pnext,TK_SEM,1);
//Rule 27
grammar[27]=create_g_node_head(global_or_not);
pnext=first_gnode(grammar[27],TK_COLON,1);
pnext=dl_nodes(pnext,TK_GLOBAL,1);
//Rule 28(eps)
grammar[28]=create_g_node_head(global_or_not);
pnext=first_gnode(grammar[28],eps,1);
//Rule 29
grammar[29]=create_g_node_head(otherStmts);
pnext=first_gnode(grammar[29],stmt,0);
pnext=dl_nodes(pnext,otherStmts,0);
//Rule 30(eps)
grammar[30]=create_g_node_head(otherStmts);
pnext=first_gnode(grammar[30],eps,1);
//Rule 31
grammar[31]=create_g_node_head(stmt);
pnext=first_gnode(grammar[31],assignmentStmt,0);
//Rule 32
grammar[32]=create_g_node_head(stmt);
pnext=first_gnode(grammar[32],iterativeStmt,0);
//Rule 33
grammar[33]=create_g_node_head(stmt);
pnext=first_gnode(grammar[33],conditionalStmt,0);
//Rule 34
grammar[34]=create_g_node_head(stmt);
pnext=first_gnode(grammar[34],ioStmt,0);
//Rule 35
grammar[35]=create_g_node_head(stmt);
pnext=first_gnode(grammar[35],funCallStmt,0);
//Rule 36
grammar[36]=create_g_node_head(assignmentStmt);
pnext=first_gnode(grammar[36],singleOrRecId,0);
pnext=dl_nodes(pnext,TK_ASSIGNOP,1);
pnext=dl_nodes(pnext,arithmeticExpression,0);
pnext=dl_nodes(pnext,TK_SEM,1);
//Rule 37
grammar[37]=create_g_node_head(singleOrRecId);
pnext=first_gnode(grammar[37],TK_ID,1);
pnext=dl_nodes(pnext,new_24,0);
//Rule 38(eps)
grammar[38]=create_g_node_head(new_24);
pnext=first_gnode(grammar[38],eps,1);
//Rule 39
grammar[39]=create_g_node_head(new_24);
pnext=first_gnode(grammar[39],TK_DOT,1);
pnext=dl_nodes(pnext,TK_FIELDID,1);
//Rule 40
grammar[40]=create_g_node_head(funCallStmt);
pnext=first_gnode(grammar[40],outputParameters,0);
pnext=dl_nodes(pnext,TK_CALL,1);
pnext=dl_nodes(pnext,TK_FUNID,1);
pnext=dl_nodes(pnext,TK_WITH,1);
pnext=dl_nodes(pnext,TK_PARAMETERS,1);
pnext=dl_nodes(pnext,inputParameters,0);
pnext=dl_nodes(pnext,TK_SEM,1);
//Rule 41
grammar[41]=create_g_node_head(outputParameters);
pnext=first_gnode(grammar[41],TK_SQL,1);
pnext=dl_nodes(pnext,idList,0);
pnext=dl_nodes(pnext,TK_SQR,1);
pnext=dl_nodes(pnext,TK_ASSIGNOP,1);
//Rule 42(eps)
grammar[42]=create_g_node_head(outputParameters);
pnext=first_gnode(grammar[42],eps,1);
//Rule 43
grammar[43]=create_g_node_head(inputParameters);
pnext=first_gnode(grammar[43],TK_SQL,1);
pnext=dl_nodes(pnext,idList,0);
pnext=dl_nodes(pnext,TK_SQR,1);
//Rule 44
grammar[44]=create_g_node_head(iterativeStmt);
pnext=first_gnode(grammar[44],TK_WHILE,1);
pnext=dl_nodes(pnext,TK_OP,1);
pnext=dl_nodes(pnext,booleanExpression,0);
pnext=dl_nodes(pnext,TK_CL,1);
pnext=dl_nodes(pnext,stmt,0);
pnext=dl_nodes(pnext,otherStmts,0);
pnext=dl_nodes(pnext,TK_ENDWHILE,1);
//Rule 45
grammar[45]=create_g_node_head(conditionalStmt);
pnext=first_gnode(grammar[45],TK_IF,1);
pnext=dl_nodes(pnext,TK_OP,1);
pnext=dl_nodes(pnext,booleanExpression,0);
pnext=dl_nodes(pnext,TK_CL,1);
pnext=dl_nodes(pnext,TK_THEN,1);
pnext=dl_nodes(pnext,stmt,0);
pnext=dl_nodes(pnext,otherStmts,0);
pnext=dl_nodes(pnext,elsePart,0);
//Rule 46
grammar[46]=create_g_node_head(elsePart);
pnext=first_gnode(grammar[46],TK_ELSE,1);
pnext=dl_nodes(pnext,stmt,0);
pnext=dl_nodes(pnext,otherStmts,0);
pnext=dl_nodes(pnext,TK_ENDIF,1);
//Rule 47
grammar[47]=create_g_node_head(elsePart);
pnext=first_gnode(grammar[47],TK_ENDIF,1);
//Rule 48
grammar[48]=create_g_node_head(ioStmt);
pnext=first_gnode(grammar[48],TK_READ,1);
pnext=dl_nodes(pnext,TK_OP,1);
pnext=dl_nodes(pnext,singleOrRecId,0);
pnext=dl_nodes(pnext,TK_CL,1);
pnext=dl_nodes(pnext,TK_SEM,1);
//Rule 49
grammar[49]=create_g_node_head(ioStmt);
pnext=first_gnode(grammar[49],TK_WRITE,1);
pnext=dl_nodes(pnext,TK_OP,1);
pnext=dl_nodes(pnext,allVar,0);
pnext=dl_nodes(pnext,TK_CL,1);
pnext=dl_nodes(pnext,TK_SEM,1);
//Rule 50
grammar[50]=create_g_node_head(allVar);
pnext=first_gnode(grammar[50],TK_NUM,1);
//Rule 51
grammar[51]=create_g_node_head(allVar);
pnext=first_gnode(grammar[51], TK_RNUM, 1);
//Rule 52
grammar[52]=create_g_node_head(allVar);
pnext=first_gnode(grammar[52], TK_ID, 1);
pnext=dl_nodes(pnext, temp, 0);
//Rule 53
grammar[53]=create_g_node_head(arithmeticExpression);
pnext=first_gnode(grammar[53],term,0);
pnext=dl_nodes(pnext,expPrime,0);
//Rule 54
grammar[54]=create_g_node_head(expPrime);
pnext=first_gnode(grammar[54],lowPrecedenceOperators,0);
pnext=dl_nodes(pnext,term,0);
pnext=dl_nodes(pnext,expPrime,0);
//Rule 55(eps)
grammar[55]=create_g_node_head(expPrime);
pnext=first_gnode(grammar[55],eps,1);
//Rule 56
grammar[56]=create_g_node_head(term);
pnext=first_gnode(grammar[56],factor,0);
pnext=dl_nodes(pnext,termPrime,0);
//Rule 57
grammar[57]=create_g_node_head(termPrime);
pnext=first_gnode(grammar[57],highPrecedenceOperators,0);
pnext=dl_nodes(pnext,factor,0);
pnext=dl_nodes(pnext,termPrime,0);
//Rule 58(eps)
grammar[58]=create_g_node_head(termPrime);
pnext=first_gnode(grammar[58],eps,1);
//Rule 59
grammar[59]=create_g_node_head(factor);
pnext=first_gnode(grammar[59],TK_OP,1);
pnext=dl_nodes(pnext,arithmeticExpression,0);
pnext=dl_nodes(pnext,TK_CL,1);
//Rule 60
grammar[60]=create_g_node_head(factor);
pnext=first_gnode(grammar[60],allVar,0);
//Rule 61
grammar[61]=create_g_node_head(highPrecedenceOperators);
pnext=first_gnode(grammar[61],TK_MUL,1);
//Rule 62
grammar[62]=create_g_node_head(highPrecedenceOperators);
pnext=first_gnode(grammar[62],TK_DIV,1);
//Rule 63
grammar[63]=create_g_node_head(lowPrecedenceOperators);
pnext=first_gnode(grammar[63],TK_PLUS,1);
//Rule 64
grammar[64]=create_g_node_head(lowPrecedenceOperators);
pnext=first_gnode(grammar[64],TK_MINUS,1);
//Rule 65
grammar[65]=create_g_node_head(temp);
pnext=first_gnode(grammar[65],eps,1);
//Rule 66
grammar[66]=create_g_node_head(temp);
pnext=first_gnode(grammar[66],TK_DOT,1);
pnext=dl_nodes(pnext,TK_FIELDID,1);
//Rule 67
grammar[67]=create_g_node_head(booleanExpression);
pnext=first_gnode(grammar[67],TK_OP,1);
pnext=dl_nodes(pnext,booleanExpression,0);
pnext=dl_nodes(pnext,TK_CL,1);
pnext=dl_nodes(pnext,logicalOp,0);
pnext=dl_nodes(pnext,TK_OP,1);
pnext=dl_nodes(pnext,booleanExpression,0);
pnext=dl_nodes(pnext,TK_CL,1);
//Rule 68
grammar[68]=create_g_node_head(booleanExpression);
pnext=first_gnode(grammar[68],var,0);
pnext=dl_nodes(pnext,relationalOp,0);
pnext=dl_nodes(pnext,var,0);
//Rule 69
grammar[69]=create_g_node_head(booleanExpression);
pnext=first_gnode(grammar[69],TK_NOT,1);
pnext=dl_nodes(pnext, TK_OP, 1);
pnext=dl_nodes(pnext,booleanExpression,0);
pnext=dl_nodes(pnext, TK_CL, 1);
//Rule 70
grammar[70]=create_g_node_head(var);
pnext=first_gnode(grammar[70],TK_ID,1);
//Rule 71
grammar[71]=create_g_node_head(var);
pnext=first_gnode(grammar[71],TK_NUM,1);
//Rule 72
grammar[72]=create_g_node_head(var);
pnext=first_gnode(grammar[72],TK_RNUM,1);
//Rule 73
grammar[73]=create_g_node_head(logicalOp);
pnext=first_gnode(grammar[73],TK_AND,1);
//Rule 74
grammar[74]=create_g_node_head(logicalOp);
pnext=first_gnode(grammar[74],TK_OR,1);
//Rule 75
grammar[75]=create_g_node_head(relationalOp);
pnext=first_gnode(grammar[75],TK_LT,1);
//Rule 76
grammar[76]=create_g_node_head(relationalOp);
pnext=first_gnode(grammar[76],TK_LE,1);
//Rule 77
grammar[77]=create_g_node_head(relationalOp);
pnext=first_gnode(grammar[77],TK_EQ,1);
//Rule 78
grammar[78]=create_g_node_head(relationalOp);
pnext=first_gnode(grammar[78],TK_GT,1);
//Rule 79
grammar[79]=create_g_node_head(relationalOp);
pnext=first_gnode(grammar[79],TK_GE,1);
//Rule 80
grammar[80]=create_g_node_head(relationalOp);
pnext=first_gnode(grammar[80],TK_NE,1);
//Rule 81
grammar[81]=create_g_node_head(returnStmt);
pnext=first_gnode(grammar[81],TK_RETURN,1);
pnext=dl_nodes(pnext,optionalReturn,0);
pnext=dl_nodes(pnext,TK_SEM,1);
//Rule 82
grammar[82]=create_g_node_head(optionalReturn);
pnext=first_gnode(grammar[82],TK_SQL,1);
pnext=dl_nodes(pnext,idList,0);
pnext=dl_nodes(pnext,TK_SQR,1);
//Rule 83
grammar[83]=create_g_node_head(optionalReturn);
pnext=first_gnode(grammar[83],eps,1);
//Rule 84
grammar[84]=create_g_node_head(idList);
pnext=first_gnode(grammar[84],TK_ID,1);
pnext=dl_nodes(pnext,more_ids,0);
//Rule 85
grammar[85]=create_g_node_head(more_ids);
pnext=first_gnode(grammar[85],TK_COMMA,1);
pnext=dl_nodes(pnext,idList,0);
//Rule 86
grammar[86]=create_g_node_head(more_ids);
pnext=first_gnode(grammar[86],eps,1);
for(int i=0;i<NO_OF_GRAMMAR_RULES;i++)
{
grammar[i]->rule_no=i;
}
//so on
return ;
}
void add_node_to_first(node_head_first* head,TOKEN tk, int rule)
{
node* temp=create_node(tk,rule);
temp->next=head->head;
head->head=temp;
}
void add_list_to_first(node_head_first* head, node* list,int rule)
{
node* curr;
curr=list;
node* temp;
while(curr!=NULL)
{
temp=create_node(curr->tokenName,rule);
temp->next=head->head;
head->head=temp;
curr=curr->next;
}
}
void add_list_to_first_eps(node_head_first* head, node* list,int rule)
{
node* curr;
curr=list;
node* temp;
while(curr!=NULL)
{
if(curr->tokenName==eps)
{
curr=curr->next;
continue;}
temp=create_node(curr->tokenName,rule);
temp->next=head->head;
head->head=temp;
curr=curr->next;
}
}
void first (NON_TERMINAL nt);
void recurse_first(node_head_first* node_head,g_node* temp, int rule_no)
{
if(temp==NULL)
{
add_node_to_first(node_head,eps,rule_no);
}
else
{
if(!temp->is_term)
{
if(f->first[temp->elem.nonterminal]->head!=NULL)
{
if(!(f->first[temp->elem.nonterminal]->has_eps))
{
add_list_to_first(node_head,f->first[temp->elem.nonterminal]->head,rule_no);
}
else
{
add_list_to_first_eps(node_head,f->first[temp->elem.nonterminal]->head,rule_no);
recurse_first(node_head,temp->next, rule_no);
}
}
else
{
first(temp->elem.nonterminal);
if(!f->first[temp->elem.nonterminal]->has_eps)
{
add_list_to_first(node_head,f->first[temp->elem.nonterminal]->head,rule_no);
}
else
{
add_list_to_first_eps(node_head,f->first[temp->elem.nonterminal]->head,rule_no);
recurse_first(node_head,temp->next,rule_no);
}
}
}
else
{
add_node_to_first(node_head, temp->elem.terminal, rule_no);
}
}
}
void first (NON_TERMINAL nt)
{
// printf("%d\n ",nt);
if(f->first[nt]->head!=NULL)
{
return;
}
else
{
int* rule= nonTerminalStringTable[nt]->rules;
int length= nonTerminalStringTable[nt]->length;
int i;
g_node* iterator=NULL;
node* temp=NULL;
for(i=0;i<length;i++)
{
iterator =grammar[rule[i]]->next;
// while(iterator!=NULL)
{
if(iterator->is_term)
{
// printf("hello\n");
if(iterator->elem.terminal==eps)
{
f->first[nt]->has_eps=true;
}
add_node_to_first(f->first[nt],iterator->elem.terminal,rule[i]);
}
else
{
// printf("hello bitch\n");
if(f->first[iterator->elem.nonterminal]->head!=NULL)
{
if(!(f->first[iterator->elem.nonterminal]->has_eps))
{
add_list_to_first(f->first[nt],f->first[iterator->elem.nonterminal]->head,rule[i]);
}
else
{
add_list_to_first_eps(f->first[nt],f->first[iterator->elem.nonterminal]->head,rule[i]);
recurse_first(f->first[nt],iterator->next, rule[i]);
}
}
else
{
first(iterator->elem.nonterminal);
if(!f->first[iterator->elem.nonterminal]->has_eps)
{
add_list_to_first(f->first[nt],f->first[iterator->elem.nonterminal]->head,rule[i]);
}
else
{
add_list_to_first_eps(f->first[nt],f->first[iterator->elem.nonterminal]->head,rule[i]);
recurse_first(f->first[nt],iterator->next,rule[i]);
}
}
}
// iterator=iterator->next;
}
}
}
}
void add_nodetof(node_head_follow* head,TOKEN tk)
{
if(tk==eps)
return;
node* temp=head->head;
int flag=0;
node* temp_node;
while(temp!=NULL)
{
if(temp->tokenName==tk)
{
flag=1;
break;
}
else
{
temp=temp->next;
}
}
if(flag==0)
{
temp_node=create_node(tk,0);
temp_node->next=head->head;
head->head=temp_node;
}
}
void add_ftof(node_head_follow* head,node* follow)
{
node* temp=follow;
while(temp!=NULL)
{
add_nodetof(head,temp->tokenName);
temp=temp->next;
}
}
void follow(NON_TERMINAL nt_index);
void first_eps(node_head_follow* node_head,g_node* temp,g_node_head* g)
{
if(temp==NULL)
{
// if(f->follow[g->non_terminal].is_visited)
// return NULL;
// else
if(f->follow[g->non_terminal]->head==NULL)
follow(g->non_terminal);
add_ftof(node_head,f->follow[g->non_terminal]->head);
}
else
{
if(!temp->is_term)
{
if(!f->first[temp->elem.nonterminal]->has_eps)
add_ftof(node_head,f->first[temp->elem.nonterminal]->head);
else
{
//add firsts - eps
add_ftof(node_head,f->first[temp->elem.nonterminal]->head);
first_eps(node_head,temp->next,g);
// return first(temp);first
}
}
else
{
add_nodetof(node_head,temp->elem.terminal);
}
}
}
void print_follow(NON_TERMINAL nt);
void first_eps_stmt(node_head_follow* node_head,g_node* temp,g_node_head* g)
{
if(temp==NULL)
{
// if(f->follow[g->non_terminal].is_visited)
// return NULL;
// else
printf("in first_eps_stmt 1: rule-%d \n",g->rule_no);
add_ftof(node_head,f->follow[g->non_terminal]->head);
print_follow(stmt);
}
else
{
if(!f->first[temp->elem.nonterminal]->has_eps)
{
printf("in first_eps_stmt 2: rule-%d nonterm-%s\n",g->rule_no,nonTerminalStringTable[temp->elem.nonterminal]->nonterminal);
add_ftof(node_head,f->first[temp->elem.nonterminal]->head);
print_follow(stmt);
}
else
{
//add firsts - eps
printf("in first_eps_stmt 3: rule-%d nonterm-%s\n",g->rule_no,nonTerminalStringTable[temp->elem.nonterminal]->nonterminal);
add_ftof(node_head,f->first[temp->elem.nonterminal]->head);
print_follow(stmt);
if(temp->next!=NULL&&
!temp->next->is_term)
printf("%s",nonTerminalStringTable[ temp->next->elem.nonterminal]->nonterminal);
first_eps_stmt(node_head,temp->next,g);
// return first(temp);first
}
}
}
void clear_flags_follow()
{
for(int i=0; i<NO_OF_RULES; i++)
{
f->follow[i]->is_visited=false;
}
}
void follow(NON_TERMINAL nt_index)
{
if(f->follow[nt_index]->head!=NULL)
{
f->follow[nt_index]->is_visited=true;
// return f->follow[nt_index]->head;
return;
}
else
{
if(f->follow[nt_index]->is_visited)
// return f->follow[nt_index]->head;
return;
else
{
f->follow[nt_index]->is_visited=true;
g_node* temp;
// f->follow[nt_index]->nt=nt_index;
for(int i=0; i<NO_OF_GRAMMAR_RULES; i++)
{
temp=grammar[i]->next;
while(temp!=NULL)
{
if(!temp->is_term)
{
if(temp->elem.nonterminal==nt_index)
{
if(temp->next==NULL)
{
// add follow of grammar[i]->non_terminal to
//current follow
if(f->follow[grammar[i]->non_terminal]->head==NULL)
{
follow(grammar[i]->non_terminal);
}
add_ftof(f->follow[nt_index],f->follow[grammar[i]->non_terminal]->head);
}
else
{
if(temp->next->is_term)
{
add_nodetof(f->follow[nt_index],temp->next->elem.terminal);
}
else
{
if(!f->first[temp->next->elem.nonterminal]->has_eps)
{
//add first of temp->next->elem.nonterminal to the
//current follow
add_ftof(f->follow[nt_index],f->first[temp->next->elem.nonterminal]->head);
}
else
{
first_eps(f->follow[nt_index],temp->next,grammar[i]);
}
}
}
}
}
temp=temp->next;
}
}
}
}
}
nonterminal_str* create_nt_str()
{
nonterminal_str* temp=(nonterminal_str*) malloc(sizeof(nonterminal_str));
temp->rules=(int*) malloc(sizeof(int)*MAX_DIFF_RULES);
temp->nonterminal=(char*) malloc(sizeof(char)*MAX_NONTERMINAL);
temp->length=0;
return temp;
}
void populateStrTable()
{
int i;
nonTerminalStringTable=(nonterminal_str**) malloc(sizeof(nonterminal_str*)*NO_OF_RULES);
for(i=0;i<NO_OF_RULES;i++)
{
nonTerminalStringTable[i]=create_nt_str();
}
for(i=0;i<NO_OF_GRAMMAR_RULES;i++)
{
int temp=grammar[i]->non_terminal;
nonTerminalStringTable[temp]->rules[nonTerminalStringTable[temp]->length]=i;
nonTerminalStringTable[temp]->length++;
}
FILE *f;
char c;
f=fopen("nt_string.txt","r");
int j=0;
i=0;
int flag=0;
while((c=fgetc(f))!=EOF){
if((c>='a'&&c<='z')||(c>='A'&&c<='Z')||c=='_'||(c>='0'&&c<='9'))
{
if(flag==1)
{
i++;
j=0;
flag=0;
}
nonTerminalStringTable[i]->nonterminal[j++]=c;
// printf("%c",c);
}
else
{
flag=1;
nonTerminalStringTable[i]->nonterminal[j++]='\0';
// i++;
// printf(" ");
continue;
}
}
fclose(f);
}
void print_strTable_row(NON_TERMINAL nt)
{
int temp=nonTerminalStringTable[nt]->length;
printf("nt:%d--->len:%d %s ",nt,temp,nonTerminalStringTable[nt]->nonterminal);
for(int i=0;i<temp;i++)
{
printf("%d ",nonTerminalStringTable[nt]->rules[i]);
}
}
void print_strTable()
{
for(int i=0;i<NO_OF_RULES;i++)
{
print_strTable_row(i);
printf("\n");
}
}
void print_first(NON_TERMINAL nt)
{
// printf("hello\n");
node* temp=f->first[nt]->head;
while(temp!=NULL)
{
printf("token: %s(%d) , rule no:%d \n", TerminalString(temp->tokenName),temp->tokenName+1,temp->rule_no_index);
temp=temp->next;
}
}
void print_follow(NON_TERMINAL nt)
{
node* temp=f->follow[nt]->head;
while(temp!=NULL)
{
printf("token: %s(%d) , rule no:%d \n",TerminalString(temp->tokenName),temp->tokenName+1,temp->rule_no_index);
temp=temp->next;
}
}