def literal(literal: Literal): nfa = NFA(Alphabet({Automata.EPSILON})) start = nfa.addState() final = nfa.addState() nfa.start = {start} nfa.final = {final} for label in literal.alphabet: for start in nfa.start: for final in nfa.final: nfa.addTransition(start, final, label) return nfa
def star(nfa: NFA): new_start = nfa.addState() new_end = nfa.addState() for start in nfa.start: nfa.addTransition(new_start, start, NFA.EPSILON) for final in nfa.final: nfa.addTransition(final, new_end, NFA.EPSILON) for start in nfa.start: for final in nfa.final: nfa.addTransition(final, start, NFA.EPSILON) nfa.addTransition(new_start, new_end, NFA.EPSILON) nfa.start = [new_start] nfa.final = [new_end] return nfa
def union(nfa1: NFA, nfa2: NFA): nfa = NFA(Alphabet({Automata.EPSILON})) equiv_states = {} for state in nfa1.states: equiv_states[state] = nfa.addState() for state in nfa2.states: equiv_states[state] = nfa.addState() for state in nfa1.states: for label in nfa1.alphabet: for dest in nfa1.transitions[state][label]: nfa.addTransition(equiv_states[state], equiv_states[dest], label) for state in nfa2.states: for label in nfa2.alphabet: for dest in nfa2.transitions[state][label]: nfa.addTransition(equiv_states[state], equiv_states[dest], label) new_start = nfa.addState() new_end = nfa.addState() for start in nfa1.start: nfa.addTransition(new_start, equiv_states[start], NFA.EPSILON) for start in nfa2.start: nfa.addTransition(new_start, equiv_states[start], NFA.EPSILON) for final in nfa1.final: nfa.addTransition(equiv_states[final], new_end, NFA.EPSILON) for final in nfa2.final: nfa.addTransition(equiv_states[final], new_end, NFA.EPSILON) nfa.start = {new_start} nfa.final = {new_end} return nfa
def emptyWord(): nfa = NFA(Alphabet()) nfa.start = {nfa.addState()} nfa.final = nfa.start.copy() return nfa