Exemple #1
0
def parse_nfa(lines):
    def parse_pair(key, f, s):
        k, v = s.split(":")
        v = v.strip()
        assert(k == key)
        return f(v)

    def filter_and_strip(xs):
        return [s.strip() for s in xs if len(s.strip()) > 0]

    assert(lines[0].strip() == "NFA")
    alphabet = parse_pair(
        "Alphabet", lambda x: filter_and_strip(x.split(";")), lines[1])
    states = parse_pair(
        "States", lambda x: filter_and_strip(x.split(";")), lines[2])
    init = parse_pair("Init", lambda x: x.strip(), lines[3])
    final = parse_pair(
        "Final", lambda x: filter_and_strip(x.split(";")), lines[4])
    parse_pair("Transitions", lambda x: x.strip(), lines[5])

    states = dict((s, State(s)) for s in states)
    init = states[init]
    final = list(states[s] for s in final)
    transitions = []
    for line in lines[6:]:
        if line.strip() == "END":
            break
        q, label, q1 = line.split(";")
        t = Transition(states[q.strip()], states[q1.strip()], label.strip())
        transitions.append(t)
    return Epsilon_NFA(list(states.values()), transitions, alphabet, init, final)
Exemple #2
0
 def to_epsilon_nfa(self, alphabet):
     n = self.r.to_epsilon_nfa(alphabet)
     ts = n.transitions
     ts |= set(Transition(s, n.start_state, None) for s in n.final_states)
     start = State()
     ts.add(Transition(start, n.start_state, None))
     return Epsilon_NFA(n.states | set([start]), ts, alphabet, start, n.final_states | set([start]))
Exemple #3
0
 def to_epsilon_nfa(self, alphabet):
     n1 = self.r1.to_epsilon_nfa(alphabet)
     n2 = self.r2.to_epsilon_nfa(alphabet)
     transitions = n1.transitions | n2.transitions
     transitions |= set(Transition(s, n2.start_state, None)
                        for s in n1.final_states)
     return Epsilon_NFA(n1.states | n2.states, transitions, alphabet, n1.start_state, n2.final_states)
Exemple #4
0
 def to_epsilon_nfa(self, alphabet):
     n1 = self.r1.to_epsilon_nfa(alphabet)
     n2 = self.r2.to_epsilon_nfa(alphabet)
     start = State()
     transitions = n1.transitions | n2.transitions
     transitions.add(Transition(start, n1.start_state, None))
     transitions.add(Transition(start, n2.start_state, None))
     states = n1.states | n2.states
     states.add(start)
     return Epsilon_NFA(states, transitions, alphabet, start, n1.final_states | n2.final_states)
Exemple #5
0
 def to_epsilon_nfa(self, alphabet):
     start = State()
     return Epsilon_NFA(set([start]), set(), alphabet, start, set([start]))
Exemple #6
0
    def to_epsilon_nfa(self, alphabet):
        start = State()
        end = State()
        t = Transition(start, end, self.c)

        return Epsilon_NFA(set([start, end]), set([t]), alphabet, start, set([t.end]))