Skip to content

Commit e7616f1

Browse files
authored
Code
1 parent ef53d1d commit e7616f1

File tree

1 file changed

+186
-0
lines changed

1 file changed

+186
-0
lines changed

CYK.c

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <stdbool.h>
4+
5+
bool checkNTR(char ch){
6+
char big[] = {'S','A','B','C','D','E','F','G','H'};
7+
for(int i=0; i<strlen(big); i++){
8+
if(ch==big[i])
9+
return true;
10+
}
11+
return false;
12+
}
13+
14+
bool checkTR(char ch){
15+
char small[] = {'a','b','c','d','e'};
16+
for(int i=0; i<strlen(small); i++){
17+
if(ch==small[i])
18+
return true;
19+
}
20+
return false;
21+
}
22+
23+
bool checkPR(char ch[]){
24+
if(strlen(ch)==1){
25+
if(checkTR(ch[0])){
26+
return true;
27+
}
28+
} else if(strlen(ch)==2){
29+
if(checkNTR(ch[0]) && checkNTR(ch[1])){
30+
return true;
31+
}
32+
}
33+
return false;
34+
}
35+
36+
bool checkEle(char El[], char ch){
37+
for(int i=0; i<strlen(El); i++){
38+
if(ch==El[i]){
39+
return true;
40+
}
41+
}
42+
return false;
43+
}
44+
45+
int main(){
46+
47+
printf("DEMO Input for Production : S->AB, S->e, A->a, B->b :\n\n");
48+
printf("No. of production rules = 4\n\nEnter generating symbols:\n\nSymbol Count\nS 2\nA 1\nB 1\n\nEnter Production rules :\n\nNon- Terminal --> Production\nS --> AB\nS --> e\nA --> a\nB --> b\n\n\n");
49+
50+
51+
int n_pr=0, n_pr2=0, pr_cnt=0;
52+
53+
printf("Allowed Symbols: 'S','A','B','C','D','E','F','G','H' \n");
54+
printf("Allowed terminals: 'a','b','c','d' and 'e':epsilon/lambda\n");
55+
printf("\n\nNo. of production rules = ");
56+
scanf("%d",&n_pr);
57+
char ntr[n_pr], pr[n_pr][3];
58+
int ntr_cnt[n_pr];
59+
60+
printf("\nEnter generating symbols:\n\nSymbol Count\n");
61+
fflush(stdin);
62+
int i1=0;
63+
while(n_pr2<n_pr){
64+
scanf("%c %d",&ntr[i1],&ntr_cnt[i1]);
65+
fflush(stdin);
66+
67+
if(!checkNTR(ntr[i1])){
68+
printf("Invalid Input. Please choose from 'Allowed Symbols' and try again.\n\nExiting...\n");
69+
return -1;
70+
}
71+
n_pr2 += ntr_cnt[i1];
72+
i1++;
73+
}
74+
75+
if(n_pr != n_pr2){
76+
printf("Invalid Input for 'No. of production rules'. Please try again.\n\nExiting...\n");
77+
return -1;
78+
}
79+
80+
printf("\n\nEnter Production rules : \n");
81+
printf("\nNon- Terminal --> Production\n");
82+
fflush(stdin);
83+
for(int i=0; i<i1; i++){
84+
for(int j=0; j<ntr_cnt[i]; j++) {
85+
printf("%c --> ",ntr[i]);
86+
pr_cnt += (i+j);
87+
fflush(stdin);
88+
pr[pr_cnt][1]=pr[pr_cnt][2]='\0';
89+
gets(pr[pr_cnt]);
90+
fflush(stdin);
91+
if(!checkPR(pr[pr_cnt])){
92+
printf("Invalid Input. Please write correct CNF and try again.\n\nExiting...\n");
93+
return -1;
94+
}
95+
}
96+
}
97+
98+
pr_cnt = 0;
99+
for(int i=0; i<i1; i++){
100+
for(int j=0; j<ntr_cnt[i]; j++) {
101+
pr_cnt += (i+j);
102+
printf("\n%c --> %s",ntr[i],pr[pr_cnt]);
103+
}
104+
}
105+
106+
fflush(stdin);
107+
int n=0;
108+
printf("\n\nInput String: ");
109+
char s1[10];
110+
gets(s1);
111+
fflush(stdin);
112+
n = strlen(s1);
113+
char st[n];
114+
strcpy(st,s1);
115+
char tb[n][n][10]; // Table
116+
117+
if(n==0){
118+
printf("Empty String!\n");
119+
return -1;
120+
}
121+
122+
123+
for(int i=0; i<n; i++){
124+
for(int j=0; j<n; j++){
125+
for(int k=0; k<10; k++)
126+
tb[i][j][k]='\0';
127+
}
128+
}
129+
130+
for(int i=0;i<n;i++){
131+
pr_cnt = 0;
132+
int k2 = 0;
133+
for(int i2=0; i2<i1; i2++){
134+
for(int j2=0; j2<ntr_cnt[i2]; j2++) {
135+
pr_cnt += (i2+j2);
136+
if(checkTR(pr[pr_cnt][0]) && pr[pr_cnt][0]==st[i]){
137+
tb[i][i][k2]=ntr[i2];
138+
k2+=1;
139+
}
140+
}
141+
}
142+
}
143+
144+
for(int l=1; l<=(n-1); l++){
145+
for(int i=0, j=0; i<=(n-l-1); i++){
146+
j = i+l;
147+
148+
for(int k=i; k<=(j-1); k++){
149+
pr_cnt = 0;
150+
int k2 = 0;
151+
152+
for(int i2=0; i2<i1; i2++){
153+
for(int j2=0; j2<ntr_cnt[i2]; j2++) {
154+
155+
pr_cnt += (i2+j2);
156+
if(checkNTR(pr[pr_cnt][0])){
157+
if(checkEle(tb[i][k],pr[pr_cnt][0]) && checkEle(tb[k+1][j],pr[pr_cnt][1])){
158+
tb[i][j][k2]=ntr[i2];
159+
k2+=1;
160+
}
161+
}
162+
}
163+
}
164+
}
165+
}
166+
}
167+
168+
169+
printf("\n\n\nTable:\n\n");
170+
171+
for(int i=0; i<n; i++){
172+
printf("|");
173+
for(int j=0; j<n; j++){
174+
printf("%s |",tb[i][j]);
175+
}
176+
printf("\n");
177+
}
178+
179+
if(checkEle(tb[0][n-1],'S')){
180+
printf("\n\nThis String is Accepted!\n\n");
181+
} else{
182+
printf("\n\nThis String is Rejected!\n\n");
183+
}
184+
185+
return 0;
186+
}

0 commit comments

Comments
 (0)