示例#1
0
def automata_concatenation(a1, a2):
    transitions = {}
    start = 0
    d1 = 0
    d2 = a1.states + d1
    u = a2.states + d2

    for src, d in a1.transitions.items():
        for symbol, dest in d.items():
            transitions[src + d1, symbol] = [state + d1 for state in dest]

    for src, d in a2.transitions.items():
        for symbol, dest in d.items():
            transitions[src + d2, symbol] = [state + d2 for state in dest]

    for z in a1.finals:
        try:
            eps_trans = transitions[z + d1, '']
        except KeyError:
            eps_trans = transitions[z + d1, ''] = []
        eps_trans.append(a2.start + d2)

    for z in a2.finals:
        try:
            eps_trans = transitions[z + d2, '']
        except KeyError:
            eps_trans = transitions[z + d2, ''] = []
        eps_trans.append(u)

    states = a1.states + a2.states + 2
    finals = {u}
    return NFA(states, finals, transitions, start)
示例#2
0
def automata_union(a1, a2):
    transitions = {}
    start = 0
    d1 = 1
    d2 = a1.states + d1
    u = a2.states + d2

    for src, d in a1.transitions.items():
        for symbol, dest in d.items():
            transitions[src + d1, symbol] = [state + d1 for state in dest]

    for src, d in a2.transitions.items():
        for symbol, dest in d.items():
            transitions[src + d2, symbol] = [state + d2 for state in dest]

    transitions[start, ''] = [a1.start + d1, a2.start + d2]

    for dx, S in zip([d1, d2], [a1.finals, a2.finals]):
        for z in S:
            try:
                eps_trans = transitions[z + dx, '']
            except KeyError:
                eps_trans = transitions[z + dx, ''] = []
            eps_trans.append(u)

    states = a1.states + a2.states + 2
    finals = {u}
    return NFA(states, finals, transitions, start)
示例#3
0
def automata_closure(a1):
    transitions = {}
    start = 0
    d1 = 1
    u = a1.states + d1

    for A, d in a1.transitions.items():
        for b, O in d.items():
            transitions[A + d1, b] = [F + d1 for F in O]
    transitions[start, ''] = [a1.start + d1, u]

    for z in a1.finals:
        try:
            X = transitions[z + d1, '']
        except KeyError:
            X = transitions[z + d1, ''] = []
        X.append(u)
        X.append(a1.start + d1)

    states = a1.states + 2
    finals = {u}
    return NFA(states, finals, transitions, start)
示例#4
0
def grammar_to_automaton(G: Grammar):
    d = {sym.Name: i for i, sym in enumerate(G.nonTerminals)}
    s = {i: sym for i, sym in enumerate(G.nonTerminals)}
    if assert_regular(G):
        states = len(G.nonTerminals)
        transitions = {}
        finals = []
        for i in range(states):
            sym = s[i]
            for production in sym.productions:
                if len(production.Right) == 1:
                    next_sym = production.Right[0]
                    if next_sym.IsTerminal:
                        try:
                            transitions[(i, next_sym.Name)].append(i)
                        except:
                            transitions[(i, next_sym.Name)] = [i]
                        finals.append(i)
                    else:
                        try:
                            transitions[(i, '')].append(d[next_sym.Name])
                        except:
                            transitions[(i, '')] = [d[next_sym.Name]]
                if len(production.Right) == 2:
                    next_sym = production.Right[0]
                    next_state = d[production.Right[1].Name]
                    try:
                        transitions[(i, next_sym.Name)].append(next_state)
                    except:
                        transitions[(i, next_sym.Name)] = [next_state]
                if len(production.Right) == 0:
                    finals.append(i)
        start = d[G.startSymbol.Name]
        return nfa_to_deterministic(
            NFA(states, finals, transitions, start=start))
    else:
        print('La Gramatica no es regular')
示例#5
0
 def evaluate(self):
     s = self.lex
     automaton = NFA(states=2, finals=[1], transitions={(0, s): [1]})
     return automaton
示例#6
0
 def evaluate(self):
     automaton = NFA(states=1, finals=[0], transitions={})
     return automaton