Beispiel #1
0
def createDFA(Autom):
    #Inicializando variables
    count_states = 0
    new_states = []
    # Primer e-closure
    inicial = e_closure(Autom, Autom.initial_state)
    new_states.append(inicial)
    Aut = Automata()
    Aut.set_inicial([0])
    Symbols = Autom.symbols
    if ("3" in Symbols):
        Symbols.remove("3")
    Symbols.sort()
    Aut.set_symbols(Symbols)
    while count_states < len(new_states):
        for sim in Symbols:
            #print("simbolo: ", sim)
            temp = e_closure(Autom, move(Autom, new_states[count_states], sim))
            if (temp != []):
                if (temp in new_states):
                    #print("igual", temp, new_states)
                    pos = new_states.index(temp)
                    #print(pos)
                    Aut.add_transitions([count_states, pos, sim])
                else:
                    #print("diferente", temp, new_states)
                    new_states.append(temp)
                    #print(count_states, len(new_states)-1)
                    Aut.add_transitions(
                        [count_states, len(new_states) - 1, sim])
        count_states += 1

    for st in new_states:
        if Autom.final_states[0] in st:
            pos = new_states.index(st)
            Aut.add_finalState(pos)

    states = list(range(0, count_states))
    Aut.set_states(states)

    return Aut
Beispiel #2
0
def D_DFA(arbol):
    complete_tree = copy.copy(arbol)

    # Tabla base con fistpos y lastpos de cada nodo importante y 3
    pos_nodes = 0
    pos = 0
    for node in complete_tree:
        if (node != "*" and node != "|" and node != "."):
            if (node == "3"):
                complete_tree[pos] = [pos, node, [], []]
            else:
                complete_tree[pos] = [
                    pos, node, pos_nodes, [pos_nodes], [pos_nodes]
                ]
            pos_nodes += 1
        else:
            complete_tree[pos] = [pos, node]
        pos += 1

    # Firstpos y lastpos de cada nodo
    pos = 0
    for node in complete_tree:
        if (node[1] == "*" or node[1] == "." or node[1] == "|"):
            complete_tree[pos] = [
                pos, complete_tree[pos][1],
                firstpos(complete_tree, pos),
                lastpos(complete_tree, pos)
            ]
        pos += 1

    # Followpos
    for node in complete_tree:
        followpos(complete_tree, node[0])

    print("******** Table ALL************")
    print("id\tSymbol\tPos n\tfirstpos\tlastpos\tfollowpos")
    for a in complete_tree:
        if (len(a) == 6):
            print(a[0], "\t", a[1], "\t", a[2], "\t\t", a[3], "\t", a[4], "\t",
                  a[5])
        elif (len(a) == 5):
            print(a[0], "\t", a[1], "\t", a[2], "\t\t", a[3], "\t", a[4], "\t",
                  " --")
        elif (len(a) == 4):
            print(a[0], "\t", a[1], "\t", a[2], "\t\t", a[3], "\t", "--", "\t",
                  " --")
        else:
            print(a[0], "\t", a[1], "\t", a[2], "\t\t", "--", "\t", "--", "\t",
                  " --")

    print("******** Table DFA************")
    print("Symbol\tPos n\tfirstpos\tlastpos\tfollowpos\t")
    table = []
    symbols = []
    for a in complete_tree:
        if (isinstance(a[2], int)):
            if (a[1] != "#"):
                if a[1] not in symbols:
                    symbols.append(a[1])
            table.append(a)

    for a in table:
        if (len(a) == 6):
            print(a[1], "\t", a[2], "\t", a[3], "\t", a[4], "\t", a[5], "\t")
        else:
            print(a[1], "\t", a[2], "\t", a[3], "\t", a[4], "\t", " --")

    # Creando el nuevo automata
    Aut = Automata()
    states = []
    if (len(complete_tree[len(complete_tree) - 1]) == 4):
        initial = complete_tree[len(complete_tree) - 1][2]
    else:
        initial = complete_tree[len(complete_tree) - 1][4]

    states.append(initial)
    Aut.set_inicial(initial)
    Aut.set_symbols(symbols)
    print("Inicial", initial)
    count_states = 0
    while count_states < len(states):
        for sim in symbols:
            temp_state = []
            for pos in states[count_states]:
                for a in table:
                    if (a[2] == pos):
                        if (sim == a[1]):
                            temp_state = joinList(temp_state, a[5])
            if (temp_state not in states):
                states.append(temp_state)
                Aut.add_transitions([count_states, len(states) - 1, sim])
            else:
                pos = states.index(temp_state)
                Aut.add_transitions([count_states, pos, sim])
        count_states += 1

    for st in states:
        for a in table:
            if (a[1] == "#"):
                if a[2] in st:
                    pos = states.index(st)
                    Aut.add_finalState(pos)

    all_states = list(range(0, count_states))
    Aut.set_states(all_states)

    return Aut
Beispiel #3
0
def Thompson(tokens, countStates):
    for token in tokens:
        if (token == "*"):
            countStates += 1
            A1 = tokens[tokens.index(token) - 1]
            A1.set_final([countStates])
            A1.add_state(countStates - 1)
            A1.add_state(countStates)
            A1.set_symbols(joinList(A1.symbols, ["3"]))
            A1.sub_trans_states(1)
            A1.add_transitions(
                [A1.initial_state[0], A1.initial_state[0] + 1, "3"])
            A1.add_transitions(
                [A1.final_states[0] - 1, A1.final_states[0], "3"])
            A1.add_transitions([A1.initial_state[0], A1.final_states[0], "3"])
            A1.add_transitions(
                [A1.final_states[0] - 1, A1.initial_state[0] + 1, "3"])
            tokens.pop(tokens.index(token) - 1)
            tokens[tokens.index(token)] = A1
            countStates += 1
            #print(A1.initial_state, A1.final_states, A1.states, A1.symbols, A1.transitions)
            return tokens, countStates
        elif (token == "."):
            A1 = tokens[tokens.index(token) - 2]
            A2 = tokens[tokens.index(token) - 1]
            # Cambios en los estados del segundo autómata
            if (A1.final_states != A2.initial_state):
                A2.set_inicial(A1.final_states)
                A2.sub_finaState(-1)
                A2.sub_states(-1)
                A2.sub_trans_states(-1)
                countStates -= 1
            #print(A1.initial_state, A1.final_states, A1.states, A1.symbols, A1.transitions)
            #print(A2.initial_state, A2.final_states, A2.states, A2.symbols, A2.transitions)
            #Creación del nuevo autómata
            Aut = Automata()
            Aut.set_inicial(A1.initial_state)
            Aut.set_final(A2.final_states)
            Aut.set_states(joinList(A1.states, A2.states))
            Aut.set_symbols(joinList(A1.symbols, A2.symbols))
            Aut.set_transitions(joinList(A1.transitions, A2.transitions))
            #print(Aut.initial_state, Aut.final_states, Aut.states, Aut.symbols, Aut.transitions)
            tokens.pop(tokens.index(token) - 1)
            tokens.pop(tokens.index(token) - 1)
            tokens[tokens.index(token)] = Aut
            return tokens, countStates
        elif (token == "|"):
            countStates += 1
            A1 = tokens[tokens.index(token) - 2]
            A2 = tokens[tokens.index(token) - 1]
            Aut = Automata()
            Aut.set_inicial(A1.initial_state)
            Aut.set_final([countStates])
            Aut.set_states(joinList(A1.states, A2.states))
            Aut.add_state(countStates - 1)
            Aut.add_state(countStates)
            Aut.set_symbols(joinList(A1.symbols, A2.symbols))
            Aut.set_symbols(joinList(Aut.symbols, ["3"]))
            A1.sub_trans_states(1)
            A2.sub_trans_states(1)
            Aut.set_transitions(joinList(A1.transitions, A2.transitions))
            Aut.add_transitions(
                [Aut.initial_state[0], A1.initial_state[0] + 1, "3"])
            Aut.add_transitions(
                [Aut.initial_state[0], A2.initial_state[0] + 1, "3"])
            Aut.add_transitions(
                [A1.final_states[0] + 1, Aut.final_states[0], "3"])
            Aut.add_transitions(
                [A2.final_states[0] + 1, Aut.final_states[0], "3"])
            #Actualizar array
            tokens.pop(tokens.index(token) - 1)
            tokens.pop(tokens.index(token) - 1)
            tokens[tokens.index(token)] = Aut
            countStates += 1
            #print("Creacion", Aut.initial_state, Aut.final_states, Aut.states, Aut.symbols, Aut.transitions)
            return tokens, countStates
        elif (isinstance(token, str)):
            Aut = Automata()
            Aut.add_symbol(token)
            Aut.set_inicial([countStates])
            countStates += 1
            Aut.set_final([countStates])
            countStates += 1
            Aut.set_states([Aut.initial_state[0], Aut.final_states[0]])
            Aut.add_transitions(
                [Aut.initial_state[0], Aut.final_states[0], token])
            tokens[tokens.index(token)] = Aut
            #print("Creacion", Aut.initial_state, Aut.final_states, Aut.states, Aut.symbols, Aut.transitions)
            return tokens, countStates