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);
        }
        public void tabla_transiciones()
        {
            Automaton aut = automatons.ElementAt(contador);

            dataGridView1.Columns.Add("Estados", "Estados");
            foreach (string s in aut.getAlphabet())
            {
                dataGridView1.Columns.Add(s, s);
            }
            foreach (Estado e in aut.getEstados())
            {
                dataGridView1.Rows.Add(e.getId(), e.getId());
            }

            for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
            {
                for (int j = 1; j < dataGridView1.Columns.Count; j++)
                {
                    Estado e = aut.getEstados().ElementAt(i);
                    foreach (Transition t in e.getTransitions())
                    {
                        if (t.getSimbol().ToString().Equals(dataGridView1.Columns[j].HeaderText))
                        {
                            dataGridView1.Rows[i].Cells[j].Value = t.getEnd().getId().ToString();
                        }
                    }
                }
            }
        }
Esempio n. 3
0
        public HashSet <Estado> e_cerradura(LinkedList <Estado> estados)
        {
            HashSet <Estado> resultado = new HashSet <Estado>();

            foreach (Estado estado in estados)
            {
                Stack <Estado> pila   = new Stack <Estado>();
                Estado         actual = estado;
                pila.Push(actual);
                while (pila.Count() > 0)
                {
                    actual = pila.Pop();
                    foreach (Transition t in actual.getTransitions())
                    {
                        if ((string)t.getSimbol() == Automaton.EPSILON && !resultado.Contains(t.getEnd()))
                        {
                            resultado.Add(t.getEnd());
                            pila.Push(t.getEnd());
                        }
                    }
                }
                if (!resultado.Contains(estado))
                {
                    resultado.Add(estado);
                }
            }

            return(resultado);
        }
        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);
        }
        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);
        }
        public bool analisis_lex(Lexema lex)
        {
            if (automatons.Count() > 0)
            {
                if (lex.getLex().Count() > 0)
                {
                    try
                    {
                        Queue <Estado> lex_stack = new Queue <Estado>();
                        Estado         aux;
                        foreach (Automaton aut in automatons)
                        {
                            if (aut.getId().Equals(lex.getId()))
                            {
                                aux = aut.getBegin();
                                lex_stack.Enqueue(aux);

                                int i = 0;



                                for (i = 0; i < lex.getLex().Count(); i++)
                                {
                                    Estado actual   = lex_stack.Dequeue();
                                    char   char_aux = lex.getLex().ElementAt(i);
                                    foreach (Transition t in actual.getTransitions())
                                    {
                                        int n = t.getSimbol().ToString().Count();

                                        if (n > 1)
                                        {
                                            if (t.getSimbol().ToString().Equals("\\n"))
                                            {
                                                char ch = '\n';
                                                if (ch == char_aux)
                                                {
                                                    lex_stack.Enqueue(t.getEnd());
                                                    break;
                                                }
                                            }
                                            else if (t.getSimbol().ToString().Equals("\\t"))
                                            {
                                                char ch = '\t';
                                                if (ch == char_aux)
                                                {
                                                    lex_stack.Enqueue(t.getEnd());
                                                    break;
                                                }
                                            }
                                            else if (t.getSimbol().ToString().Equals("\""))
                                            {
                                                char ch = '\"';
                                                if (ch == char_aux)
                                                {
                                                    lex_stack.Enqueue(t.getEnd());
                                                    break;
                                                }
                                            }
                                            else if (t.getSimbol().ToString().Equals("\\'"))
                                            {
                                                char ch = '\'';
                                                if (ch == char_aux)
                                                {
                                                    lex_stack.Enqueue(t.getEnd());
                                                    break;
                                                }
                                            }

                                            if (isConjunto((string)t.getSimbol()))
                                            {
                                                foreach (Conjunto c in pa.GetConjuntos())
                                                {
                                                    if (t.getSimbol().ToString() == c.getId())
                                                    {
                                                        string s = "";
                                                        s += char_aux;
                                                        if (c.getValores().Contains(s))
                                                        {
                                                            lex_stack.Enqueue(t.getEnd());
                                                            break;
                                                        }
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                int n_tran = t.getSimbol().ToString().Count();
                                                int n_lex  = lex.getLex().Count();
                                                if (n_lex > n_tran)
                                                {
                                                    string aux_lex = "";
                                                    int    j       = 0;
                                                    int    aux_i   = i;
                                                    for (j = 0; j < n; j++)
                                                    {
                                                        char s = lex.getLex().ElementAt(i);
                                                        aux_lex += s;
                                                        i++;
                                                    }
                                                    if (t.getSimbol().ToString() == aux_lex)
                                                    {
                                                        lex_stack.Enqueue(t.getEnd());
                                                        i = aux_i + (n - 1);
                                                        j = 0;
                                                        break;
                                                    }
                                                    else
                                                    {
                                                        i = aux_i;
                                                        break;
                                                    }
                                                }
                                            }
                                        }
                                        else
                                        {
                                            if (isConjunto((string)t.getSimbol()))
                                            {
                                                foreach (Conjunto c in pa.GetConjuntos())
                                                {
                                                    if (t.getSimbol().ToString() == c.getId())
                                                    {
                                                        string s = "";
                                                        s += char_aux;
                                                        if (c.getValores().Contains(s))
                                                        {
                                                            lex_stack.Enqueue(t.getEnd());
                                                            break;
                                                        }
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                string s = "";
                                                s += char_aux;
                                                if (t.getSimbol().ToString() == s)
                                                {
                                                    lex_stack.Enqueue(t.getEnd());
                                                    break;
                                                }
                                            }
                                        }
                                    }

                                    if (i == lex.getLex().Count() - 1)
                                    {
                                        try
                                        {
                                            Estado final = lex_stack.Dequeue();
                                            if (aut.getEstadosAceptacion().Contains(final))
                                            {
                                                return(true);
                                            }
                                        }
                                        catch (Exception e)
                                        {
                                            return(false);
                                        }
                                    }
                                }
                            }
                        }
                        return(false);
                    }
                    catch (Exception e)
                    {
                        return(false);
                    }
                }
            }

            return(false);
        }