-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdfa2.py
More file actions
44 lines (39 loc) · 1.04 KB
/
Copy pathdfa2.py
File metadata and controls
44 lines (39 loc) · 1.04 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
# DFA 2 (alphabet {a,b})
def dfa2(word):
state = 0 # start at q0
for ch in word:
if state == 0: # q0
if ch == "a":
state = 1
elif ch == "b":
state = 2
elif state == 1: # q1
if ch == "a":
state = 0
elif ch == "b":
state = 3
elif state == 2: # q2
if ch == "a":
state = 3
elif ch == "b":
state = 0
elif state == 3: # q3
if ch == "a":
state = 2
elif ch == "b":
state = 1
if state == 0 or state == 3:
return True
else:
return False
# MAIN PROGRAM
while True:
print("\n--- DFA 2 SIMULATOR ---")
s = input("Enter string of a and b (or type exit): ")
if s.lower() == "exit":
print("Program ended.")
break
if dfa2(s):
print("ACCEPTED by DFA 2")
else:
print("REJECTED by DFA 2")