-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDFA2.cpp
More file actions
109 lines (92 loc) · 1.35 KB
/
DFA2.cpp
File metadata and controls
109 lines (92 loc) · 1.35 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
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
// C program to implement DFS that accepts L = { a^n b^m ; (n)mod 2=0, m>=1 }
#include <stdio.h>
#include <string.h>
int dfa = 0;
// This function is for the first state (Q0) of DFA
void start(char c)
{
if (c == 'a') {
dfa = 1;
}
else if (c == 'b') {
dfa = 3;
}
else {
dfa = -1;
}
}
// This function is for the first state (Q1) of DFA
void state1(char c)
{
if (c == 'a') {
dfa = 2;
}
else if (c == 'b') {
dfa = 4;
}
else {
dfa = -1;
}
}
// This function is for the second state (Q2) of DFA
void state2(char c)
{
if (c == 'b') {
dfa = 3;
}
else if (c == 'a') {
dfa = 1;
}
else {
dfa = -1;
}
}
// This function is for the third state (Q3)of DFA
void state3(char c)
{
if (c == 'b') {
dfa = 3;
}
else if (c == 'a') {
dfa = 4;
}
else {
dfa = -1;
}
}
// This function is for the fourth state (Q4) of DFA
void state4(char c)
{
dfa = -1;
}
int isAccepted(char str[])
{
int i, len = strlen(str);
for (i = 0; i < len; i++) {
if (dfa == 0)
start(str[i]);
else if (dfa == 1)
state1(str[i]);
else if (dfa == 2)
state2(str[i]);
else if (dfa == 3)
state3(str[i]);
else if (dfa == 4)
state4(str[i]);
else
return 0;
}
if (dfa == 3)
return 1;
else
return 0;
}
int main()
{
char str[] = "aaaaaabbbb";
if (isAccepted(str))
printf("ACCEPTED\n");
else
printf("NOT ACCEPTED\n");
return 0;
}