Exemplo n.º 1
0
        public Automaton or(Automaton left, Automaton right)
        {
            Automaton afn = new Automaton();


            Estado actual_begin = new Estado(0);

            actual_begin.setTransitions(new Transition(actual_begin, right.getBegin(), Automaton.EPSILON));

            afn.addEstados(actual_begin);
            afn.setBegin(actual_begin);
            int i = 0;

            for (i = 0; i < left.getEstados().Count(); i++)
            {
                Estado aux = left.getEstados().ElementAt(i);
                aux.setId(i + 1);
                afn.addEstados(aux);
            }

            for (int j = 0; j < right.getEstados().Count(); j++)
            {
                Estado aux = right.getEstados().ElementAt(j);
                aux.setId(i + 1);
                afn.addEstados(aux);
                i++;
            }

            Estado actual_end = new Estado(left.getEstados().Count() + right.getEstados().Count() + 1);

            afn.addEstados(actual_end);
            afn.addEstadosAceptacion(actual_end);


            Estado last_begin = left.getBegin();
            LinkedList <Estado> last_end_left  = left.getEstadosAceptacion();
            LinkedList <Estado> last_end_right = right.getEstadosAceptacion();

            actual_begin.getTransitions().AddLast(new Transition(actual_begin, last_begin, Automaton.EPSILON));


            for (int k = 0; k < last_end_left.Count(); k++)
            {
                last_end_left.ElementAt(k).getTransitions().AddLast(new Transition(last_end_left.ElementAt(k), actual_end, Automaton.EPSILON));
            }

            for (int k = 0; k < last_end_left.Count(); k++)
            {
                last_end_right.ElementAt(k).getTransitions().AddLast(new Transition(last_end_right.ElementAt(k), actual_end, Automaton.EPSILON));
            }

            return(afn);
        }
Exemplo n.º 2
0
        public Automaton cerradura_kleene(Automaton afn)
        {
            //Definir un nuevo automata con su estado inicial
            Automaton kleene = new Automaton();

            /*Se marca el estado inicial como "actual" debido a  que el automata recibido por parametro tambien
             * posee sus estados inicial y final
             */
            Estado actual_begin = new Estado(0);

            kleene.addEstados(actual_begin);
            kleene.setBegin(actual_begin);


            //Actualizar los indicadores de estado del afn recibido
            for (int i = 0; i < afn.getEstados().Count(); i++)
            {
                Estado aux = afn.getEstados().ElementAt(i);
                for (int j = 0; j < aux.getTransitions().Count(); j++)
                {
                    aux.getTransitions().ElementAt(j).getBegin().setId(aux.getTransitions().ElementAt(j).getBegin().getId() + 1);
                }
                aux.setId(i + 1);
                kleene.addEstados(aux);
            }



            Estado actual_end = new Estado(afn.getEstados().Count() + 1);

            kleene.addEstados(actual_end);
            kleene.addEstadosAceptacion(actual_end);

            Estado last_begin = afn.getBegin();

            LinkedList <Estado> last_end = afn.getEstadosAceptacion();

            actual_begin.getTransitions().AddLast(new Transition(actual_begin, last_begin, Automaton.EPSILON));
            actual_begin.getTransitions().AddLast(new Transition(actual_begin, actual_end, Automaton.EPSILON));


            for (int i = 0; i < last_end.Count(); i++)
            {
                last_end.ElementAt(i).getTransitions().AddLast(new Transition(last_end.ElementAt(i), last_begin, Automaton.EPSILON));
                last_end.ElementAt(i).getTransitions().AddLast(new Transition(last_end.ElementAt(i), actual_end, Automaton.EPSILON));
            }

            return(kleene);
        }
Exemplo n.º 3
0
        public Automaton and(Automaton left, Automaton right)
        {
            Automaton afn = new Automaton();
            int       i   = 0;

            for (i = 0; i < right.getEstados().Count(); i++)
            {
                Estado aux = right.getEstados().ElementAt(i);
                aux.setId(i);

                if (i == 0)
                {
                    afn.setBegin(aux);
                }

                if (i == right.getEstados().Count() - 1)
                {
                    for (int j = 0; j < right.getEstadosAceptacion().Count(); j++)
                    {
                        //aux.setTransitions(new Transition((Estado)right.getEstadosAceptacion().ElementAt(j), left.getBegin(), Automaton.EPSILON));
                        foreach (Transition t in left.getBegin().getTransitions())
                        {
                            //t.setBegin(aux);
                            aux.setTransitions(new Transition((Estado)right.getEstadosAceptacion().ElementAt(j), t.getEnd(), t.getSimbol()));
                        }
                    }
                }
                afn.addEstados(aux);
            }


            for (int j = 1; j < left.getEstados().Count(); j++)
            {
                Estado aux = left.getEstados().ElementAt(j);
                aux.setId(i);

                if (j == left.getEstados().Count() - 1)
                {
                    afn.addEstadosAceptacion(aux);
                }
                afn.addEstados(aux);
                i++;
            }
            return(afn);
        }
Exemplo n.º 4
0
        /*
         * 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);
        }
Exemplo n.º 5
0
        public Automaton cerradura_positiva(Automaton afn_pos)
        {
            Automaton afn = new Automaton();


            int i = 0;

            //Recorrer el automata actual
            for (i = 0; i < afn_pos.getEstados().Count(); i++)
            {
                //Clonar cada estado (sin sus transiciones)
                Estado aux          = afn_pos.getEstados().ElementAt(i);
                Estado nuevo_estado = (Estado)aux.Clone();

                if (i == 0)
                {
                    afn.setBegin(nuevo_estado);
                }
                afn.addEstados(nuevo_estado);
            }


            //Recorrer el nuevo automata
            for (int j = 0; j < afn.getEstados().Count(); j++)
            {
                //Estado auxiliar del automata nuevo
                Estado e = afn.getEstados().ElementAt(j);

                //Estado auxiliar del automata actual
                Estado e2 = afn_pos.getEstados().ElementAt(j);
                foreach (Transition t in e2.getTransitions())
                {
                    //Agregar las transiciones del nuevo automata
                    Transition aux = new Transition(search(afn.getEstados(), e2.getId()), search(afn.getEstados(), t.getEnd().getId()), t.getSimbol());
                    e.setTransitions(aux);
                }
            }

            for (int j = 0; j < afn_pos.getEstadosAceptacion().Count(); j++)
            {
                Estado aux = afn_pos.getEstadosAceptacion().ElementAt(j);
                Estado n   = search(afn.getEstados(), aux.getId());
                afn.addEstadosAceptacion(n);
            }

            return(afn);
        }
Exemplo n.º 6
0
        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));
        }