40 lines
1.2 KiB
Python
40 lines
1.2 KiB
Python
def run_automaton(file_path):
|
|
transitions = {}
|
|
|
|
# Чтение информации из файла
|
|
with open(file_path, 'r') as file:
|
|
lines = file.readlines()
|
|
|
|
for line in lines:
|
|
if line.strip(): # Пропускаем пустые строки
|
|
parts = line.split()
|
|
state_from = int(parts[0])
|
|
state_to = int(parts[1])
|
|
|
|
if len(parts) == 3:
|
|
symbol = parts[2]
|
|
transitions.setdefault(state_from, []).append((symbol, state_to))
|
|
else:
|
|
transitions[state_from] = []
|
|
|
|
current_state = 0
|
|
word = ""
|
|
|
|
while current_state in transitions:
|
|
possible_transitions = transitions[current_state]
|
|
if not possible_transitions:
|
|
break
|
|
|
|
transition_symbol, next_state = possible_transitions[0]
|
|
word += transition_symbol
|
|
current_state = next_state
|
|
|
|
print(f"Достигнуто конечное состояние: {current_state}")
|
|
print(f"Полученное слово: {word}")
|
|
|
|
# Указываем путь к файлу
|
|
file_path = "small.in"
|
|
|
|
# Запускаем автомат
|
|
run_automaton(file_path)
|