コード例 #1
0
        public void thompson2()
        {
            LinkedList <string> alphabet       = new LinkedList <string>();
            Stack <Object>      thompson_stack = new Stack <Object>();

            foreach (Token token in token_regex)
            {
                switch (token.getTipo())
                {
                case Token.Tipo.OPERADOR_OR:

                    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 Token.Tipo.OPERADOR_CONCAT:

                    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 Token.Tipo.OPERADOR_KLEENE:

                    Automaton a1_kleene = (Automaton)thompson_stack.Pop();
                    Automaton kleene    = cerradura_kleene(a1_kleene);
                    thompson_stack.Push(kleene);
                    this.afn = kleene;

                    break;

                case Token.Tipo.OPERADOR_POSITIVA:

                    Automaton c1 = (Automaton)thompson_stack.Pop();
                    Automaton c4 = (Automaton)this.cerradura_positiva(c1);
                    Automaton c2 = (Automaton)this.cerradura_kleene(c1);
                    Automaton c3 = this.and(c2, c4);
                    thompson_stack.Push(c3);
                    this.afn = c3;

                    break;

                case Token.Tipo.OPERADOR_INTE:

                    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.Getval());
                    thompson_stack.Push(simbol);
                    this.afn = simbol;
                    if (!alphabet.Contains(token.Getval()))
                    {
                        alphabet.AddLast(token.Getval());
                    }

                    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, alphabet, id);

            afd.construir_afd();
        }
コード例 #2
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));
        }