def option(tree, automata): symbol = tree.data start = nfa.State(len(automata.states), len(automata.states)) automata.states.append(start) if tree.left.data in OPERATORS: st1, fn1 = t_handler(tree.left, automata) else: st1, fn1 = single(tree.left, automata) if tree.right.data in OPERATORS: st2, fn2 = t_handler(tree.right, automata) else: st2, fn2 = single(tree.right, automata) end = nfa.State(len(automata.states), len(automata.states)) automata.states.append(end) start.transitions.append(nfa.Transition(EPSILON, st1.id)) start.transitions.append(nfa.Transition(EPSILON, st2.id)) fn1.transitions.append(nfa.Transition(EPSILON, end.id)) fn2.transitions.append(nfa.Transition(EPSILON, end.id)) return start, end
def create(inicial, final, table, exp): auto_direct = automata.Automata(exp) first = automata.State(inicial, len(auto_direct.states)) auto_direct.states.append(first) if final[-1] in first.id: first.accept = True symbols = [] for symbol in exp: if symbol not in OPERATORS and symbol not in symbols and symbol != EPSILON: symbols.append(symbol) for state in auto_direct.states: for symbol in symbols: temp = [] for pos in state.id: if pos.data == symbol: tos = table[pos] for t in tos: if t not in temp: temp.append(t) if dfa.check(auto_direct, temp) and temp != []: new_state = automata.State(temp, len(auto_direct.states)) if final[-1] in temp: new_state.accept = True auto_direct.states.append(new_state) state.transitions.append( automata.Transition(symbol, auto_direct.states[-1].id2)) elif temp != []: selected = eval.select(auto_direct, temp) if selected: state.transitions.append( automata.Transition(symbol, selected.id2)) else: print("No existe nodo con ", temp, " de id") return auto_direct
def single(tree, automata): symbol = tree.data first = nfa.State(len(automata.states), len(automata.states)) automata.states.append(first) second = nfa.State(len(automata.states), len(automata.states)) automata.states.append(second) first.transitions.append(nfa.Transition(symbol, second.id)) return first, second
def to_dfa(automata, regex): dfa = nfa.Automata(automata.id) print(dfa.id) actual = [0] # simbolos del lenguaje symbols = [] for symbol in regex: if symbol not in OPERATORS and symbol not in symbols and symbol != EPSILON: symbols.append(symbol) # Metodo de conjuntos new_state = nfa.State(eval.cerradura(automata, actual), len(dfa.states)) dfa.states.append(new_state) if check_aceptacion(automata, new_state.id): new_state.accept = True for state in dfa.states: # Busqueda de los movimientos con los simbolos del lenguaje for symbol in symbols: c_epsilon = [] temp = [] for num in state.id: for transition in automata.states[num].transitions: #print("de ", automata.states[num].id, " a ", transition.to, " con ", transition.symbol) if transition.symbol == symbol: temp.append(transition.to) # Cerradura epsilon del movimiento de los estados con # los simbolos c_epsilon = eval.cerradura(automata, temp) if check(dfa, c_epsilon) and c_epsilon != []: new_s = nfa.State(c_epsilon, len(dfa.states)) dfa.states.append(new_s) if check_aceptacion(automata, c_epsilon): new_s.accept = True state.transitions.append( nfa.Transition(symbol, dfa.states[-1].id2)) elif c_epsilon != []: selected = eval.select(dfa, c_epsilon) if selected: state.transitions.append( nfa.Transition(symbol, selected.id2)) else: print("No existe nodo con ", c_epsilon, " de id") return dfa