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