如何使DFA尊重最终状态



我为一个类编写了DFA的代码。它正确地接受或拒绝某些输入,例如字符串x=abbaaa、y=baba、z=abaaaabaab。我想解决的是,每当一个较小的字符串输入自动机(如a、ab或b(时,代码就会打印出该字符串被接受,这是不正确的。

DFA来了。代码如下:

''

state = 0
flag = False 
string = input("Introduce the string to determine if is accepted or not by the DFA: ")
separated_string= list(string)
print("The string is:", separated_string)

for i in(separated_string):
if state==0:
if i=="a":
print("From q0 to q1")
state=1
elif i=="b":
print("From q0 to q4")
state=4
elif state==1:
if i=="b":
print("From q1 to q2")
state=2
else:
print("not accepted")
flag = True
break;
elif state==2:
if i=="a":
print("From q2 to q2")
state=2
else:
print("From q2 to q3")
state=3
elif state==3:
if i=="a":
print("From q3 to q3")
state=3
else:
print("Not accepted")
flag = True
break;
elif state==4:
if i=="a":
print("From q4 to q4")
state=4
else:
print("From q4 to q5")
state = 5
elif state==5:
if i=="a":
print("From q5 to q5")
state=5
else:
print("Not accepted")
flag = True
break;
if (flag == True):
pass
else:
print("ACCEPTED ")

''

当标志为false时,您接受它。

交换语句。

if (flag):
print("ACCEPTED ")
else:
print("REJECT ")

最新更新