-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCYK.cpp
163 lines (139 loc) · 3.66 KB
/
CYK.cpp
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
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define N 100
string table[N][N];
vector <pair<char,string>> production;
string input_string;
vector<string> get_curtesian_products(int i, int m, int n, int j){
vector<string> prod;
string a = table[i][m];
string b = table[n][j];
//null check
if(a.length()==0 || b.length()==0)
return prod;
for(int i=0; i<a.length(); i++){
for(int j=0; j<b.length(); j++){
string temp_prod = "";
temp_prod += a[i];
temp_prod += b[j];
prod.push_back(temp_prod);
}
}
return prod;
}
set<char> find_prod(string str){
set<char> t;
for(int i=0; i<production.size(); i++){
if(production[i].second == str){
t.insert(production[i].first);
}
}
return t;
}
void fill_table_index(int i, int j){
vector <vector <string>> curtesian_products;
for(int m=i; m<=j-1; m++){
curtesian_products.push_back(get_curtesian_products(i,m,m+1,j));
}
//now take the acceptable products
set<char> s;
set<char> temp_s;
for(int i=0; i<curtesian_products.size(); i++){
for(int j=0; j<curtesian_products[i].size(); j++){
string str = curtesian_products[i][j];
temp_s = find_prod(str);
for(auto itr: temp_s){
s.insert(itr);
}
}
}
string temp_string = "";
for(auto itr: s){
temp_string += itr;
}
table[i][j] = temp_string;
}
void make_table(int n){
// first make the base (eg. X11, X22, .... , Xnn)
for(int i=1; i<=n; i++){
string terminal = "";
terminal += input_string[i-1];;
string temp = "";
for(int i=0; i<production.size(); i++){
if(production[i].second == terminal){
temp += production[i].first;
}
}
table[i][i] = temp;
temp.clear();
terminal.clear();
}
// then others
for(int i=2; i<=n; i++){
int j = 1;
for(int k=i; k<=n; k++){
fill_table_index(j,k);
j++;
}
}
}
void print_table(int n){
cout<<"\nThe table is:\n";
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
string s = table[i][j];
if(s.length() == 0)
cout<<"null"<<" ";
else
cout<<s<<" ";
}
cout<<endl;
}
}
void check_acceptance(char s, int n){
string str = table[1][n];
bool flag = false;
for(int i=0; i<str.length(); i++){
if(str[i] == s)
flag = true;
}
if(flag)
cout<<"\nAccepted!\n";
else
cout<<"\nRejected!\n";
}
int main(void){
freopen("cyk.txt", "r", stdin);
cout<<"Enter Productions in CNF(enter xxx to stop): \n";
string str;
cin>>str;
while(str != "xxx"){
char left = str[0];
string temp_str;
for(int i=2; i<str.length(); i++){
if(str[i]=='|'){
production.push_back(make_pair(left,temp_str));
temp_str.clear();
}
else
temp_str += str[i];
}
production.push_back(make_pair(left,temp_str));
temp_str.clear();
cin>>str;
}
//print productions
// for(int i=0; i<production.size(); i++){
// cout<<production[i].first<< " "<<production[i].second<<endl;
// }
cout<<"\nEnter input string: ";
cin>>input_string;
int n = input_string.length();
make_table(n);
// all is okay just table valo vabe print korte hobe
print_table(n);
char start = production[0].first;
check_acceptance(start, n);
return 0;
}