Ejemplo n.º 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)
Ejemplo n.º 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]))
Ejemplo n.º 3
0
def superstring_dfa(s):
    alphabet = set(s)
    states = dict((s[:i], State("_" + s[:i])) for i in range(0, len(s) + 1))
    transitions = set(
        Transition(states[s[:i]], states[get_longest_suffix(s, s[:i] + a)], a)
        for a in alphabet for i in range(len(s)))
    transitions |= set(Transition(states[s], states[s], a) for a in alphabet)
    return DFA(set(states.values()), transitions, alphabet, states[""],
               set([states[s]]))
Ejemplo n.º 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)
Ejemplo n.º 5
0
 def to_epsilon_nfa(self, alphabet):
     start = State()
     return Epsilon_NFA(set([start]), set(), alphabet, start, set([start]))
Ejemplo n.º 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]))