-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdfa.py
133 lines (92 loc) · 3.98 KB
/
dfa.py
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
class Utility:
@staticmethod
def show_error(message):
print("Output: Your string is not valid")
print("Reason: " + message)
@staticmethod
def get_input(message):
while True:
input_string = input(message)
if len(input_string) == 0:
print("Input can not be empty")
else:
return input_string
class DFAValidateString:
@staticmethod
def main():
message = "Enter valid input symbols (∑) (comma-separated): "
valid_input_string = Utility.get_input(message)
valid_inputs = set(valid_input_string.split(","))
message = "Enter states (Q) (comma-separated): "
state_string = Utility.get_input(message)
states = set(state_string.split(","))
message = "Enter starting state (q0): "
starting_state = Utility.get_input(message)
message = "Enter all final states (F) (comma-separated): "
final_state_string = Utility.get_input(message)
final_states = set(final_state_string.split(","))
transitions = {}
for state in states:
transitions[state] = {}
for valid_input in valid_inputs:
message = f"Enter transition (δ) for state '{state}' having input '{valid_input}': "
next_state = Utility.get_input(message)
transitions[state][valid_input] = next_state
dfa = DFA(starting_state, final_states, transitions)
while True:
message = "Enter string to be validated: "
input_sequence = Utility.get_input(message)
dfa.process_input_sequence(input_sequence)
choice = input("Do you want to validate another string? (y/n): ")
if choice.lower() == "n":
break
class DFA:
def __init__(self, starting_state, final_states, transitions):
self.starting_state = starting_state
self.final_states = final_states
self.transitions = transitions
def process_input_sequence(self, input_sequence):
current_state = self.starting_state
traversal_path = [current_state]
for input_symbol in input_sequence:
current_state_transition = self.transitions.get(current_state, {})
if input_symbol in current_state_transition:
next_state = current_state_transition.get(input_symbol)
traversal_path.append(f"{input_symbol} -> {next_state}")
current_state = next_state
else:
Utility.show_error(f"Invalid transition: State '{current_state}' with input '{input_symbol}'")
return
if current_state in self.final_states:
print("Output: Your string is valid")
else:
Utility.show_error(f"Your string ends at state '{current_state}' which is not a final state")
print("Traversal Path: " + " - ".join(traversal_path))
if __name__ == "__main__":
DFAValidateString.main()
''' Algorithm Overview: DFA String Validation
Initialization:
Get the set of valid input symbols (∑).
Get the set of states (Q).
Get the starting state (q0).
Get the set of final states (F).
Initialize an empty dictionary for transitions.
Transition Table Construction:
For each state in Q:
For each valid input symbol in ∑:
Get the next state for the given input symbol.
Populate the transition table.
DFA Object Initialization:
Create a DFA object with the starting state, final states, and transitions.
String Validation:
For each character in the input string:
Check if there is a transition for the current state and the input symbol.
If yes, update the current state.
If no, show an error indicating an invalid input symbol.
Result Analysis:
After processing the entire string:
If the final state is in the set of final states, the string is valid.
If not, show an error indicating an invalid final state.
Traversal Path Recording:
Record the traversal path, showing each transition from state to state.
'''