-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimulator.py
57 lines (42 loc) · 1.95 KB
/
Simulator.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
from FA import FA
def read_file(file_name):
"""
Reads the content of a file and constructs a Finite Automaton (FA) based on the file content.
Parameters:
file_name (str): The name of the file to be read.
Returns:
FA: An instance of the FA class representing the Finite Automaton constructed from the file content.
"""
with open(file_name, 'r') as f:
alphabet = f.readline().strip() # Read the alphabet from the first line of the file
num_states = int(f.readline().strip()) # Read the number of states
states = []
for i in range(num_states):
states.append(f.readline().strip()) # Read each state and add it to the list of states
start_state = f.readline().strip() # Read the start state
num_accept_states = int(f.readline().strip()) # Read the number of accept states
accept_states = []
for i in range(num_accept_states):
accept_states.append(f.readline().strip()) # Read each accept state and add it to the list of accept states
num_transitions = int(f.readline().strip()) # Read the number of transitions
transitions = []
for i in range(num_transitions):
transition = f.readline().strip().split(',') # Read each transition and split it into components
transitions.append(
(transition[0], transition[1], transition[2])) # Add the transition to the list of transitions
return FA(alphabet, states, start_state, accept_states, transitions)
file = input('Enter the name of the file: ')
file = 'definitions/' + file
file2 = 'inputs.txt'
with open(file2, 'r') as f:
strings = f.readlines()
strings = [s.strip() for s in strings]
count = 0
for string in strings:
fa = read_file(file)
if fa.accept(string):
print('Accepted the string:', string)
count += 1
else:
print('Rejected the string:', string)
print('Accepted', count, '/', len(strings))