private Automata obtenerAFN_caracteres(String cadena)
 {
     //Armar un And con todos los caracteres de la cadena
     if (cadena.Length == 1)
     {
         //Solo es 1
         Nodo       start = new Nodo();
         Nodo       end   = new Nodo();
         Transicion trans = new Transicion(cadena, start, end);
         start.transiciones.Add(trans);
         return(new Automata(start, end));
     }
     else
     {
         Nodo start = new Nodo();
         Nodo end   = new Nodo();
         for (int i = 0; i < cadena.Length; i++)
         {
             if (i == 0)
             {
                 Transicion trans = new Transicion(cadena[i].ToString(), start, end);
                 start.transiciones.Add(trans);
             }
             else
             {
                 Nodo       nuevo_end = new Nodo();
                 Transicion trans     = new Transicion(cadena[i].ToString(), end, nuevo_end);
                 end.transiciones.Add(trans);
                 end = nuevo_end;
             }
         }
         return(new Automata(start, end));
     }
 }
Esempio n. 2
0
        public Automata getAFND_sin_expandir()
        {
            //Si tengo 1 simbolo
            if (this.lista.Count == 1)
            {
                Nodo start = new Nodo();
                Nodo end   = new Nodo();
                foreach (String item in this.lista.Values)
                {
                    Transicion trans = new Transicion(item + "", start, end);
                    start.transiciones.Add(trans);
                }
                return(new Automata(start, end));
                //return start;
            }
            else
            {
                if (this.tipo == 1)
                {
                    //    Nodo start = new Nodo();
                    //    Nodo end = new Nodo();
                    //    Transicion trans = new Transicion("["+sinicio+"-"+sfinal+"]", start, end);
                    //    start.transiciones.Add(trans);
                    //    return start;
                    Nodo       start_t1 = new Nodo();
                    Nodo       end_t1   = new Nodo();
                    Transicion trans_t1 = new Transicion("[" + sinicio + "~" + sfinal + "]", start_t1, end_t1);
                    start_t1.transiciones.Add(trans_t1);

                    //Limpiar los simbolos
                    this.lista = new Dictionary <String, String>();
                    this.lista.Add("[" + sinicio + "~" + sfinal + "]", "[" + sinicio + "~" + sfinal + "]");
                    return(new Automata(start_t1, end_t1));
                }

                //start = nodo de donde derivan todos
                //end = nodo en donde llegan todos
                //s1 = nodo de start de simbolo
                //s2 = nodo de end de simbolo
                //start [epsilon] s1 [SIMBOLO] s2 [epsilon] end
                Nodo start = new Nodo();
                Nodo end   = new Nodo();

                foreach (String item in this.lista.Values)
                {
                    Nodo       s1    = new Nodo();
                    Nodo       s2    = new Nodo();
                    Transicion e1    = new Transicion("ε", start, s1);
                    Transicion e2    = new Transicion("ε", s2, end);
                    Transicion trans = new Transicion(item + "", s1, s2);
                    start.transiciones.Add(e1);
                    s1.transiciones.Add(trans);
                    s2.transiciones.Add(e2);
                }
                return(new Automata(start, end));
                //return start;
            }
        }
        private Automata obtenerAFN_muchos(ParseTreeNode h0, ParseTreeNode h1)
        {
            Automata sub_afn = this.obtenerAFN(h0);

            Nodo start = new Nodo();
            Nodo end   = new Nodo();

            Automata reto = new Automata(start, end);

            Transicion e1 = new Transicion("ε", start, sub_afn.start);
            Transicion e2 = new Transicion("ε", sub_afn.end, sub_afn.start);
            Transicion e3 = new Transicion("ε", sub_afn.end, end);

            start.transiciones.Add(e1);
            sub_afn.end.transiciones.Add(e2);
            sub_afn.end.transiciones.Add(e3);

            return(reto);
        }
Esempio n. 4
0
        public Automata getAFND_con_expandir()
        {
            //Si tengo 1 simbolo
            if (this.lista.Count == 1)
            {
                Nodo start = new Nodo();
                Nodo end   = new Nodo();
                foreach (String item in this.lista.Values)
                {
                    Transicion trans = new Transicion(item + "", start, end);
                    start.transiciones.Add(trans);
                }
                return(new Automata(start, end));
                //return start;
            }
            else
            {
                //start = nodo de donde derivan todos
                //end = nodo en donde llegan todos
                //s1 = nodo de start de simbolo
                //s2 = nodo de end de simbolo
                //start [epsilon] s1 [SIMBOLO] s2 [epsilon] end
                Nodo start = new Nodo();
                Nodo end   = new Nodo();

                foreach (String item in this.lista.Values)
                {
                    Nodo       s1    = new Nodo();
                    Nodo       s2    = new Nodo();
                    Transicion e1    = new Transicion("ε", start, s1);
                    Transicion e2    = new Transicion("ε", s2, end);
                    Transicion trans = new Transicion(item + "", s1, s2);
                    start.transiciones.Add(e1);
                    s1.transiciones.Add(trans);
                    s2.transiciones.Add(e2);
                }
                return(new Automata(start, end));
                //return start;
            }
        }
        private Automata obtenerAFN_or(ParseTreeNode h0, ParseTreeNode h1, ParseTreeNode h2)
        {
            Automata afn0 = this.obtenerAFN(h0);
            Automata afn1 = this.obtenerAFN(h1);

            Nodo start = new Nodo();
            Nodo end   = new Nodo();

            Automata reto = new Automata(start, end);

            Transicion start_afn0 = new Transicion("ε", start, afn0.start);
            Transicion start_afn1 = new Transicion("ε", start, afn1.start);
            Transicion afn0_end   = new Transicion("ε", afn0.end, end);
            Transicion afn1_end   = new Transicion("ε", afn1.end, end);

            start.transiciones.Add(start_afn0);
            start.transiciones.Add(start_afn1);

            afn0.end.transiciones.Add(afn0_end);
            afn1.end.transiciones.Add(afn1_end);
            return(reto);
        }
        private Automata obtenerAFN_escape(ParseTreeNode h0)
        {
            String name = "a";

            switch (h0.ChildNodes[0].Token.Text)
            {
            case "\\n":
                name = "salto";
                break;

            case "\\r":
                name = "retorno";
                break;

            case "\\t":
                name = "tab";
                break;

            case "\\'":
                name = "comilla_s";
                break;

            case "\\\"":
                name = "comilla_d";
                break;

            case "[:blanco:]":
                //or entre espacio, tab, salto, retorno
                Nodo ns1 = new Nodo();
                Nodo ne1 = new Nodo();
                Nodo ns2 = new Nodo();
                Nodo ne2 = new Nodo();
                Nodo ns3 = new Nodo();
                Nodo ne3 = new Nodo();
                Nodo ns4 = new Nodo();
                Nodo ne4 = new Nodo();

                ns1.transiciones.Add(new Transicion("espacio", ns1, ne1));
                ns2.transiciones.Add(new Transicion("tab", ns2, ne2));
                ns3.transiciones.Add(new Transicion("salto", ns3, ne3));
                ns4.transiciones.Add(new Transicion("retorno", ns4, ne4));

                Automata afn1 = new Automata(ns1, ne1);
                Automata afn2 = new Automata(ns2, ne2);
                Automata afn3 = new Automata(ns3, ne3);
                Automata afn4 = new Automata(ns4, ne4);

                Nodo start = new Nodo();
                Nodo end   = new Nodo();

                start.transiciones.Add(new Transicion("ε", start, ns1));
                start.transiciones.Add(new Transicion("ε", start, ns2));
                start.transiciones.Add(new Transicion("ε", start, ns3));
                start.transiciones.Add(new Transicion("ε", start, ns4));
                ne1.transiciones.Add(new Transicion("ε", ne1, end));
                ne2.transiciones.Add(new Transicion("ε", ne2, end));
                ne3.transiciones.Add(new Transicion("ε", ne3, end));
                ne4.transiciones.Add(new Transicion("ε", ne4, end));

                return(new Automata(start, end));

            case "[:todo:]":
                name = "todo";
                break;

            default:
                Console.WriteLine("abc");
                Nodo       n = new Nodo();
                Transicion t = new Transicion("ESCAPE", n, n);
                n.transiciones.Add(t);
                Automata afn = new Automata(n, n);
                return(afn);
            }

            Nodo       starts = new Nodo();
            Nodo       ends   = new Nodo();
            Transicion tra    = new Transicion(name, starts, ends);

            starts.transiciones.Add(tra);
            return(new Automata(starts, ends));
        }
        private Automata obtenerAFN(ParseTreeNode nodo)
        {
            if (nodo.ChildNodes.Count == 3)
            {
                //Es un and, or (|, . )
                ParseTreeNode h0 = nodo.ChildNodes[0];
                ParseTreeNode h1 = nodo.ChildNodes[1];
                ParseTreeNode h2 = nodo.ChildNodes[2];
                if (h2.Token.Text.Equals("."))   //es un and
                {
                    return(this.obtenerAFN_and(h0, h1, h2));
                }
                else //Es un or
                {
                    return(this.obtenerAFN_or(h0, h1, h2));
                }
            }
            else if (nodo.ChildNodes.Count == 2)
            {
                ParseTreeNode h0 = nodo.ChildNodes[0];
                ParseTreeNode h1 = nodo.ChildNodes[1];
                if (h1.Token.Text.Equals("*"))
                {
                    return(this.obtenerAFN_nadamuchos(h0, h1));
                }
                else if (h1.Token.Text.Equals("+"))
                {
                    return(this.obtenerAFN_muchos(h0, h1));
                }
                if (h1.Token.Text.Equals("?"))
                {
                    return(this.obtenerAFN_nada(h0, h1));
                }
            }
            else if (nodo.ChildNodes.Count == 1)
            {
                ParseTreeNode h0 = nodo.ChildNodes[0];
                if (h0.Term.Name.Equals("ESCAPE"))
                {
                    return(obtenerAFN_escape(h0));
                }
                else if (h0.Term.Name.Equals("id"))
                {
                    return(obtenerAFN_id(h0));
                }
                else if (h0.Term.Name.Equals("numero"))
                {
                    return(obtenerAFN_numero(h0));
                }
                else if (h0.Term.Name.Equals("tstring"))
                {
                    return(obtenerAFN_string(h0));
                }
                else if (h0.Term.Name.Equals("tchar"))
                {
                    return(obtenerAFN_char(h0));
                }
                else
                {
                    Nodo       n = new Nodo();
                    Transicion t = new Transicion("ELSE", n, n);
                    n.transiciones.Add(t);
                    Automata afn = new Automata(n, n);
                    return(afn);
                }
            }

            return(null);
        }