-
Notifications
You must be signed in to change notification settings - Fork 0
/
nfa simulation.cpp
155 lines (110 loc) · 2.71 KB
/
nfa simulation.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
/***** This program simulate NFA that accepts a string over alphabet E = {0,1} ****/
/**** Accept if an input string ends with '01' ****/
#include <iostream>
#include <string.h>
using namespace std;
char curstate[100], nextstate[100];
int state,accept;
// the nfa contain three states
char q0 = '1', q1 = '2', q2 = '3';
// insert the initial state to the current state array
void state0(char), state1(char), state2(char);
void copyToCurrent(), popCurrent();
// define the next states from current
// value of state0 is always sits at index 0,1
void state0(char a){
if(a == '0'){
//push to nextstate
nextstate[0] = q0;
nextstate[1] = q1;
}
else{
//push to next state
nextstate[0] = q0;
}
}
// determine the next state from the current input
void state1(char a){
if(a == '1'){
//push to nextstate
nextstate[2] = q2;
}
else{
//push to next state
}
}
// determine the next state from current input
void state2(char a){
if(a == '0'){
//push to nextstate
nextstate[2] = '0';
}
else{
//push to next state
nextstate[2] = '0';
}
}
void copyToCurrent(){
for(int i = 0; i< 100; i++){
curstate[i] = nextstate[i];
}
}
void popCurrent(){
for(int i = 0; i< 100; i++){
curstate[i] = '0';
}
}
int acceptInput(char inputString[]){
int len = strlen(inputString);
for (int i = 0; i < len; i++) {
// loop through the current state and determine the next state
for (int j =0; j<100; j++){
//if curstate contain state push its nextstae to nextstate list
if(curstate[j] == q0){
state0(inputString[i]);
}
if(curstate[j] == q1){
state1(inputString[i]);
}
if(curstate[j] == q2){
state2(inputString[i]);
}
}
// clear the current array
popCurrent();
// copy the next states to the current state array
copyToCurrent();
}
// output th possible final states array
cout<<endl<<"Possible Final states : [";
for (int i = 0; i <3; i++) {
cout<<curstate[i];
if(i != 2){
cout<<",";
}
}
cout<<"]"<<endl<<endl;;
//check if the final state is in the possible final states array
for (int n = 0; n < 100; n++) {
if(curstate[n] == q2){
accept = 1;
}
}
}
// MAIN FUNCTION
int main(){
char input[] = "1010110101";
curstate[0] = q0;
cout<<"This NFA Accepts a String ending with '01'"<<endl;
cout<<endl<<"List of states"<<endl;
cout<<"q0 == 1"<<endl<<"q1 == 2"<<endl<<"q2 == 3"<<endl<<"dead state == 0"<<endl;
cout<<endl<<"Input String: "<<input<<endl;
acceptInput(input);
if(accept == 1){
cout<<"String Accepted";
}
else{
cout<<"String Rejected";
}
return 0;
}