42 lines
940 B
Python
42 lines
940 B
Python
from collections import namedtuple
|
|
|
|
NFA = namedtuple('NFA', ['start', 'accept', 'transitions'])
|
|
|
|
def copy_nfa(nfa):
|
|
transitions_copy = {}
|
|
for from_state in nfa.transitions:
|
|
transitions_copy[from_state] = nfa.transitions[from_state].copy()
|
|
|
|
return NFA(nfa.start, nfa.accept, transitions_copy)
|
|
|
|
def prettyprint(nfa):
|
|
def process_state(state):
|
|
nonlocal start, accept
|
|
|
|
t = ''
|
|
if state == start:
|
|
# Bold
|
|
t += '\x1b[1m'
|
|
if state in accept:
|
|
# Green
|
|
t += '\x1b[32m'
|
|
|
|
if t != '':
|
|
return t + str(state) + '\x1b[0m'
|
|
else:
|
|
return str(state)
|
|
|
|
start, accept, transitions = nfa
|
|
states = transitions.keys()
|
|
|
|
print('\t' + '\t'.join(map(process_state, states)))
|
|
for from_state in states:
|
|
t = []
|
|
for to_state in states:
|
|
if to_state in transitions[from_state]:
|
|
t.append(str(transitions[from_state][to_state]))
|
|
else:
|
|
t.append('\x1b[90m-\x1b[0m')
|
|
|
|
print(process_state(from_state) + '\t' + '\t'.join(t))
|