From 4a789f6b16c11127ea1888249b2bbd14084f6841 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juhani=20Krekel=C3=A4?= Date: Sat, 1 Jun 2019 19:36:22 +0300 Subject: [PATCH] Move prettyprint() to nfa.py --- nfa.py | 31 +++++++++++++++++++++++++++++++ nfa_to_regex.py | 33 +-------------------------------- 2 files changed, 32 insertions(+), 32 deletions(-) diff --git a/nfa.py b/nfa.py index 367d618..3589573 100644 --- a/nfa.py +++ b/nfa.py @@ -8,3 +8,34 @@ def copy_nfa(nfa): 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)) diff --git a/nfa_to_regex.py b/nfa_to_regex.py index 7d04324..4709b20 100644 --- a/nfa_to_regex.py +++ b/nfa_to_regex.py @@ -1,7 +1,7 @@ import enum from regex import lit, concat, bar, star -from nfa import NFA, copy_nfa +from nfa import NFA, copy_nfa, prettyprint def remove_states(nfa): start, accept, transitions = nfa @@ -109,37 +109,6 @@ def to_regex(nfa): 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(): nfa = NFA('start', ['0'], { 'start': {'1': lit('i'), '2': lit('d')},