-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab3.cpp
More file actions
76 lines (63 loc) · 1.84 KB
/
Lab3.cpp
File metadata and controls
76 lines (63 loc) · 1.84 KB
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
#include <iostream>
#include <string>
using namespace std;
void recognize_string(string s) {
int state = 0;
int i = 0;
char c;
while (i < s.length()) {
c = s[i];
i++;
switch (state) {
case 0:
if (c == 'a') state = 1;
else if (c == 'b') state = 2;
else state = 6;
break;
case 1:
if (c == 'a') state = 3;
else if (c == 'b') state = 4;
else state = 6;
break;
case 2:
if (c == 'a') state = 6;
else if (c == 'b') state = 2;
else state = 6;
break;
case 3:
if (c == 'a') state = 3;
else if (c == 'b') state = 2;
else state = 6;
break;
case 4:
if (c == 'a') state = 6;
else if (c == 'b') state = 5;
else state = 6;
break;
case 5:
if (c == 'a') state = 6;
else if (c == 'b') state = 2;
else state = 6;
break;
case 6:
cout << s << " is not recognized" << endl;
return;
}
}
if (state == 1 || state == 3)
cout << s << " is accepted under rule 'a*'" << endl;
else if (state == 2 || state == 4)
cout << s << " is accepted under rule 'a*b+'" << endl;
else if (state == 5)
cout << s << " is accepted under rule 'abb'" << endl;
else
cout << s << " is not recognized" << endl;
}
int main() {
string input_string;
cout << "Enter a string: ";
getline(cin, input_string);
recognize_string(input_string);
cout << "\nCompiled by : Subodh Ghimire" << endl;
return 0;
}