Move prettyprint() to nfa.py
This commit is contained in:
parent
46aab39ee6
commit
4a789f6b16
31
nfa.py
31
nfa.py
|
@ -8,3 +8,34 @@ def copy_nfa(nfa):
|
||||||
transitions_copy[from_state] = nfa.transitions[from_state].copy()
|
transitions_copy[from_state] = nfa.transitions[from_state].copy()
|
||||||
|
|
||||||
return NFA(nfa.start, nfa.accept, transitions_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))
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import enum
|
import enum
|
||||||
|
|
||||||
from regex import lit, concat, bar, star
|
from regex import lit, concat, bar, star
|
||||||
from nfa import NFA, copy_nfa
|
from nfa import NFA, copy_nfa, prettyprint
|
||||||
|
|
||||||
def remove_states(nfa):
|
def remove_states(nfa):
|
||||||
start, accept, transitions = nfa
|
start, accept, transitions = nfa
|
||||||
|
@ -109,37 +109,6 @@ def to_regex(nfa):
|
||||||
|
|
||||||
return processed.transitions[_.start][_.end]
|
return processed.transitions[_.start][_.end]
|
||||||
|
|
||||||
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))
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
nfa = NFA('start', ['0'], {
|
nfa = NFA('start', ['0'], {
|
||||||
'start': {'1': lit('i'), '2': lit('d')},
|
'start': {'1': lit('i'), '2': lit('d')},
|
||||||
|
|
Loading…
Reference in New Issue