48 lines
1.3 KiB
Python
48 lines
1.3 KiB
Python
import sys
|
|
|
|
|
|
def read_fsa_description(file_path):
|
|
transitions = {}
|
|
final_states = set()
|
|
|
|
with open(file_path, 'r', encoding='utf-8') as file:
|
|
for line in file:
|
|
line = line.rstrip()
|
|
if not line:
|
|
break
|
|
|
|
parts = line.split('\t')
|
|
if len(parts) == 3:
|
|
state_from, state_to, symbol = parts[0], parts[1], parts[2]
|
|
if state_from in transitions:
|
|
transitions[state_from].append((state_to, symbol))
|
|
else:
|
|
transitions[state_from] = [(state_to, symbol)]
|
|
|
|
if len(parts)==1:
|
|
line = line.rstrip()
|
|
final_states.add(line)
|
|
|
|
return transitions, final_states
|
|
|
|
def generate_paths(transitions, current_state, current_path, final_states):
|
|
paths = []
|
|
if current_state in final_states:
|
|
paths.append("".join(current_path))
|
|
|
|
if current_state in transitions:
|
|
for next_state, symbol in transitions[current_state]:
|
|
paths.extend(generate_paths(transitions, next_state, current_path + [symbol], final_states))
|
|
|
|
return paths
|
|
|
|
|
|
fsa_description_file = sys.argv[1]#'medium.in'
|
|
transitions, final_states = read_fsa_description(fsa_description_file)
|
|
paths = generate_paths(transitions, '0', [], final_states)
|
|
|
|
for path in sorted(paths):
|
|
print(path)
|
|
|
|
|