-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNFA_to_DFA.cpp
217 lines (193 loc) · 6.04 KB
/
NFA_to_DFA.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
#include <bits/stdc++.h>
using namespace std;
#define N 100
int nfa_numberOfStates;
char nfa_states[N];
char nfa_starting_state;
int nfa_numberOfFinalStates;
char nfa_final_states[N];
string nfa_transitions[N][N]; // indexing(states) according to the order of nfa_states
char dfa_starting_state;
string dfa_states[N];
string dfa_transitions[N][N];
int number_of_dfa_states = 0;
set <string> done;
queue <string> to_be_done;
int findIndex(char c){
for(int i=0; i<nfa_numberOfStates; i++){
if(c==nfa_states[i])
return i;
}
return -1;
}
void generateDFA(){
dfa_starting_state = nfa_starting_state;
dfa_states[0] = dfa_starting_state;
number_of_dfa_states++;
done.insert(dfa_states[0]);
int index = findIndex(dfa_starting_state);
dfa_transitions[0][0] = nfa_transitions[index][0];
dfa_transitions[0][1] = nfa_transitions[index][1];
to_be_done.push(nfa_transitions[index][0]);
to_be_done.push(nfa_transitions[index][1]);
int dfa_state_index = 1;
bool flag = true;
while(!to_be_done.empty()){
string temp = to_be_done.front();
to_be_done.pop();
if(done.count(temp))
flag = false;
else
flag = true;
if(flag){
done.insert(temp);
dfa_states[dfa_state_index] = temp;
number_of_dfa_states++;
string zero_temp, one_temp;
for(int i=0; i<temp.size(); i++){
char c = temp[i];
if(c != ' '){
int index = findIndex(c);
zero_temp += nfa_transitions[index][0];
one_temp += nfa_transitions[index][1];
}
}
// geting unique elements
set<char> temp;
for(int i=0; i<zero_temp.size(); i++){
if(zero_temp[i]!= ' ')
temp.insert(zero_temp[i]);
}
zero_temp.clear();
set<char>:: iterator it;
for( it = temp.begin(); it!=temp.end(); ++it){
zero_temp += *it;
zero_temp += ' ';
}
temp.clear();
for(int i=0; i<one_temp.size(); i++){
if(one_temp[i]!= ' ')
temp.insert(one_temp[i]);
}
one_temp.clear();
set<char>:: iterator it1;
for( it1 = temp.begin(); it1!=temp.end(); ++it1){
one_temp += *it1;
one_temp += ' ';
}
zero_temp = zero_temp.substr(0, zero_temp.size()-1);
one_temp = one_temp.substr(0, one_temp.size()-1);
dfa_transitions[dfa_state_index][0] = zero_temp;
dfa_transitions[dfa_state_index][1] = one_temp;
dfa_state_index++;
to_be_done.push(zero_temp);
to_be_done.push(one_temp);
}
}
}
void printDFA(){
for(int i=0; i<number_of_dfa_states; i++){
string t = dfa_states[i];
if(t.length()==0)
dfa_states[i] = "phi";
}
cout<<endl<<"--------DFA-------"<<endl;
cout<<"DFA statring state: "<<dfa_starting_state<<endl;
cout<<"DFA final states: ";
for(int i=0; i<number_of_dfa_states; i++){
string temp = dfa_states[i];
bool flag = false;
for(int j=0; j<temp.size(); j++){
for(int k=0; k<nfa_numberOfFinalStates; k++){
if(nfa_final_states[k]==temp[j]){
flag = true;
}
}
}
if(flag){
if(temp.size()==0)
cout<<"phi"<<" ";
else cout<<temp<<" ";
}
}
cout<<endl<<"-----------DFA Transitions----------"<<endl;
cout<<"states\t\tusing 0\t\tusing 1"<<endl;
for(int i=0; i<number_of_dfa_states; i++){
cout<<dfa_states[i]<<"\t\t";
string s = dfa_transitions[i][0];
if(s.length()==0)
cout<<"phi\t\t";
else
cout<<s<<"\t\t";
string str = dfa_transitions[i][1];
if(str.length()==0)
cout<<"phi\t\t"<<endl;
else
cout<<str<<"\t\t"<<endl;
}
}
int main(void){
freopen("nfaToDfa.txt", "r", stdin);
//freopen("nfa.txt", "r", stdin);
cout<<"Enter number of states of NFA: ";
cin>>nfa_numberOfStates;
cout<<endl;
// taking states
cout<<"Enter the states of NFA: ";
for(int i=0; i<nfa_numberOfStates; i++){
cin>>nfa_states[i];
}
cout<<endl;
// taking starting state
cout<<"Enter the starting state of NFA: ";
cin>>nfa_starting_state;
cout<<endl;
// taking number of final state
cout<<"Enter the number of final states of NFA: ";
cin>>nfa_numberOfFinalStates;
cout<<endl;
// taking final states
cout<<"Enter the final states of NFA: ";
for(int i=0; i<nfa_numberOfFinalStates; i++){
cin>>nfa_final_states[i];
}
cout<<endl;
// transitions of nfa
cout<<"Enter the transitions: "<<endl;
for(int i=0; i<nfa_numberOfStates; i++){
cout<<nfa_states[i]<<" using 0 goes to: ";
cout<<"(enter the states (enter 'x' to stop))";
char c;
string s;
cin>>c;
while(c!='x'){
s += c;
s += ' ';
cin>>c;
}
s = s.substr(0, s.size()-1); // to remove the last whitespace
nfa_transitions[i][0] = s;
cout<<endl;
cout<<nfa_states[i]<<" using 1 goes to: ";
cout<<"(enter the states (enter 'x' to stop))";
char ch;
string t;
cin>>ch;
while(ch!='x'){
t += ch;
t += ' ';
cin>>ch;
}
t = t.substr(0, t.size()-1); // to remove the last whitespace
nfa_transitions[i][1] = t;
cout<<endl;
}
// cout<<"-----------NFA----------"<<endl;
// for(int i=0; i<nfa_numberOfStates; i++){
// cout<<nfa_states[i]<<" using 0 goes to: "<<nfa_transitions[i][0]<<endl;
// cout<<nfa_states[i]<<" using 1 goes to: "<<nfa_transitions[i][1]<<endl;
// }
generateDFA();
printDFA();
return 0;
}