-
Notifications
You must be signed in to change notification settings - Fork 0
/
contextFreeGrammar.cpp
239 lines (149 loc) · 3.94 KB
/
contextFreeGrammar.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
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
#include <bits/stdc++.h>
using namespace std;
bool isPossible(string input);
int prefixZeroCount(string input);
void formatIt();
int countSubZero(string input);
int main()
{
string inputWord;
cout<<"Input the word here: \n";
cin >> inputWord;
vector <string> transitionS = {"A1B00"};
vector <string> transitionA = {"0A","epsilon"};
vector <string> transitionB = {"0B","1B","epsilon"};
if(!isPossible(inputWord))
cout<<"This word is not contained in the given context free grammar.\n";
else
{
cout<<"\t\t\t"<<transitionS[0]<<endl;
formatIt();
int preZeroCnt = prefixZeroCount(inputWord);
int subZeroCnt = 0;
string outputTemp = "";
for(int i = 1; i <= preZeroCnt; i++)
{
outputTemp += "0";
cout << "\t\t\t"<<outputTemp + transitionS[0] <<endl;
formatIt();
}
cout<<"\t\t\t"<<outputTemp + "epsilon1B"<<endl;
formatIt();
cout <<"\t\t\t"<<outputTemp + "1B"<<endl;
formatIt();
string temp = outputTemp + "1";
transitionS.push_back(temp);
int idx = 0;
for( ; idx < inputWord.size() and inputWord[idx] != '1'; idx++);
idx++;
while(idx < inputWord.size())
{
if(inputWord[idx] == '1')
{
transitionS[1] += "1";
}else
{
transitionS[1] += "0";
}
cout <<"\t\t\t" << transitionS[1]+"B"<<endl;
formatIt();
idx++;
}
cout << "\t\t\t"<<inputWord + transitionB[2]<<endl;
formatIt();
cout<<"\t\t\t"<<inputWord <<endl;
formatIt();
cout <<"\t\t\tWe have arrived at the destination!\n\n";
return 0;
}
}
/*
cout <<"Now let us see the rightmost derivation: \n\n\n";
transitionS.push_back("A1");
cout<<"\t\t\t S\n";
formatIt();
cout<<"\t\t\t A1B\n";
formatIt();
idx = 0;
for( ; idx < inputWord.size() and inputWord[idx] != '1'; idx++);
idx++;
while(idx < inputWord.size())
{
if(inputWord[idx] == '1')
{
transitionS[2] += "1";
}else
{
transitionS[2] += "0";
}
cout <<"\t\t\t" << transitionS[2]+"B"<<endl;
formatIt();
idx++;
}
cout << "\t\t\t"<<transitionS[2] + transitionB[2]<<endl;
formatIt();
cout << "\t\t\t"<<transitionS[2] <<endl;
formatIt();
outputTemp = "";
for(int i = 1; i <= preZeroCnt; i++)
{
outputTemp += "0";
cout << "\t\t\t"<<outputTemp + transitionS[2] <<endl;
formatIt();
}
cout<<"\t\t\t"<<inputWord <<endl;
formatIt();
cout <<"\t\t\tWe have arrived at the destination!\n\n";
}*/
//return 0;
//}
bool isPossible(string input)
{
for(auto chars: input)
{
if(chars != '1' and chars != '0')
{
return false;
}
}
int zeroSuf = 0;
bool f = false;
for(auto chars: input)
{
if(chars == '1')
f = true;
}
if(input.size() >=2)
{
if(f == true and input[input.size() - 1] == '0' and input[input.size() - 2] == '0')
return true;
}
return false;
}
int prefixZeroCount(string input)
{
int cnt = 0;
for(int i = 0; i < input.size(); i++)
{
if(input[i] == '1')
return cnt;
cnt++;
}
return cnt;
}
void formatIt()
{
for(int i = 0; i < 3; i++)
cout <<"\t\t\t |\n";
}
int countSubZero(string input)
{
int idx = 0;
for( ; idx < input.size() and input[idx] != '1'; idx++);
idx++;
int ans = 0;
for( ; idx < input.size(); idx++)
if(input[idx] == '0')
ans++;
return ans;
}