/* * public void thompson() * { * * Stack<Object> thompson_stack = new Stack<Object>(); * * foreach (string token in regex) * { * * switch (token) * { * * * case "|": * * Automaton a1_or = (Automaton)thompson_stack.Pop(); * Automaton a2_or = (Automaton)thompson_stack.Pop(); * Automaton or = this.or(a1_or, a2_or); * thompson_stack.Push(or); * * * this.afn = or; * * break; * * case ".": * * Automaton a1_and = (Automaton)thompson_stack.Pop(); * Automaton a2_and = (Automaton)thompson_stack.Pop(); * Automaton and = this.and(a1_and, a2_and); * thompson_stack.Push(and); * this.afn = and; * break; * * case "*": * * Automaton a1_kleene = (Automaton)thompson_stack.Pop(); * Automaton kleene = cerradura_kleene(a1_kleene); * thompson_stack.Push(kleene); * this.afn = kleene; * * break; * * case "+": * * Automaton c1 = (Automaton)thompson_stack.Pop(); * Automaton c4 = (Automaton)this.cerradura_positiva(c1); * Automaton c2 = (Automaton)this.cerradura_kleene(c1); * Automaton c3 = this.and(c4, c2); * thompson_stack.Push(c3); * this.afn = c3; * * break; * * case "?": * * Automaton interrogation_simbol = new Automaton(); * Automaton a1_inte = this.simbol(Automaton.EPSILON); * Automaton a2_inte = (Automaton)thompson_stack.Pop(); * interrogation_simbol = this.or(a1_inte, a2_inte); * thompson_stack.Push(interrogation_simbol); * this.afn = interrogation_simbol; * break; * * default: * Automaton simbol = this.simbol(token); * thompson_stack.Push(simbol); * this.afn = simbol; * this.alphabet.AddLast(token); * break; * } * * * } * * this.afn.setType("AFN"); * thompson_stack.Clear(); * Graphviz g = new Graphviz(); * g.graph_automaton(generarDOT(id + " : AFN", this.afn)); * AFD afd = new AFD(this.afn, this.alphabet, id); * afd.construir_afd(); * * // transicionesE(); * * * }*/ public Automaton simbol(Object t_simbol) { //Definir un nuevo automata Automaton afn = new Automaton(); Estado begin = new Estado(0); Estado end = new Estado(1); //Establecer las transiciones para el estado inicial Transition tran = new Transition(begin, end, t_simbol); begin.setTransitions(tran); //Agregar los estados a la lista de estados del automata afn.addEstados(begin); afn.addEstados(end); //Definir el estado inicial y final del automata afn.setBegin(begin); afn.addEstadosAceptacion(end); return(afn); }
public void construir_afd() { Estado inicio = new Estado(0); afd.addEstados(inicio); afd.setBegin(inicio); afd.setAlphabet(alphabet); HashSet <HashSet <Estado> > cerraduras = new HashSet <HashSet <Estado> >(); Queue <HashSet <Estado> > pila = new Queue <HashSet <Estado> >(); int identificador = 0; LinkedList <Estado> lista_inicial = new LinkedList <Estado>(); lista_inicial.AddLast(afn.getBegin()); HashSet <Estado> cerradura_inicial = e_cerradura(lista_inicial); foreach (Estado final in afn.getEstadosAceptacion()) { if (cerradura_inicial.Contains(final)) { afd.addEstadosAceptacion(inicio); } } pila.Enqueue(cerradura_inicial); while (pila.Count() > 0) { HashSet <Estado> aux = pila.Dequeue(); foreach (string id in alphabet) { LinkedList <Estado> res_mueve = mueve(aux, id); HashSet <Estado> res_cerradura = new HashSet <Estado>(); res_cerradura = e_cerradura(res_mueve); Estado anterior = afd.getEstados().ElementAt(identificador); if (res_cerradura.Count() > 0) { if (existe_estado(cerraduras, res_cerradura)) { LinkedList <Estado> estados_actuales = afd.getEstados(); Estado actual = anterior; Estado siguiente = estados_actuales.ElementAt(posicion_estado(cerraduras, res_cerradura) + 1); actual.setTransitions(new Transition(actual, siguiente, id)); } else { cerraduras.Add(res_cerradura); pila.Enqueue(res_cerradura); Estado agregar = new Estado(cerraduras.Count()); anterior.setTransitions(new Transition(anterior, agregar, id)); afd.addEstados(agregar); foreach (Estado e in afn.getEstadosAceptacion()) { if (res_cerradura.Contains(e)) { if (!afd.getEstadosAceptacion().Contains(agregar)) { afd.addEstadosAceptacion(agregar); } } } } } } identificador++; } afd.setId(id); Form1.automatons.AddLast(afd); afd.setType("AFD"); Graphviz g = new Graphviz(); g.graph_automaton2(generarDOT(id + " : AFD", afd)); }