-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
115 lines (92 loc) · 3.45 KB
/
main.py
File metadata and controls
115 lines (92 loc) · 3.45 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
109
110
111
112
113
114
115
class Node:
def __init__(self, name:str, adjacency: list[str], transition: list[list[str]], is_acceptor: bool):
#INFO: Name is just used as an identifier
self.name = name
#INFO: Used to keep track whether this is an acceptor state
self.is_acceptor = is_acceptor
self.create_edge(adjacency, transition)
def create_edge(self, adjacency, transition):
edge_list = []
#INFO: Each node contains a list of its edges
for i in range(len(adjacency)):
edge_list.append(Edge(adjacency[i],transition[i]))
self.edge = edge_list
def __str__(self):
print("State", self.name, "| Acceptor", self.is_acceptor)
for edge in self.edge:
print(f' {self.name} --> {edge}')
return ""
class Edge:
def __init__(self, destination: list[str], transition: list[list[str]]):
#INFO: The destination is the node that we point to
self.destination = destination
self.transition = transition
def __str__(self):
return f'Destination: {self.destination}, Transition: {self.transition}'
class Graph:
def __init__(self, valid_char: list[str]):
self.nodes = []
#INFO: Valid Char is used to delcare what
# characters are valid in the language
self.valid_char = valid_char
def add(self, node):
self.nodes.append(node)
def get_node(self, input_node: str) -> Node:
for node in self.nodes:
if input_node == node.name:
return node
return None
def __str__(self):
for node in self.nodes:
print(node)
return ''
def validate(self, string: str) -> bool:
#INFO: The first item in the node list of the graph is the entry point of the DFA
state = self.nodes[0]
#INFO: Iterate through each character
for char in string:
valid_transition = False
if char in self.valid_char:
#INFO: Iterates through each edge that the node has
for edge in state.edge:
#INFO: Checking for transitions
if char in edge.transition:
valid_transition = True
state = self.get_node(edge.destination)
break
if not valid_transition:
print('Trap State')
return False
else:
print(f'"{char}" is not a valid character.')
return False
print(f'Changed State: {state.name}')
if state.is_acceptor:
print('Valid')
return state.is_acceptor
else:
print('Trap State')
return state.is_acceptor
'''
Aidan Jimenez
3/2/25
DFA INITIALIZATION:
In this main function I declare a DFA that has the language
that contains chars (a,b) and has to start and end with b
'''
if __name__ == "__main__":
#INFO: Setting up DFA/Graph
#Graph input validates the valid chars for the language
dfa = Graph(["a", "b"])
# q0 State
q0 = Node('q0',['q1'], [['b']], False)
dfa.add(q0)
# q1 State
q1 = Node('q1',['q1', 'q2'], [['b'],['a']], True)
dfa.add(q1)
# q2 State
q2 = Node('q2',['q2', 'q1'], [['a'],['b']], False)
dfa.add(q2)
string = str(input("Input the string to validate: "))
# Check if Valid/Traverse DFA with input string.
dfa.validate(string)