Beispiel #1
0
 private void analitic()
 {
     string[] p;
     for (int i = 0; i < elemento.Count(); i++)
     {
         analisis anl = new analisis();
         p = elemento[i].Split('#');
         anl.modificar(Convert.ToInt32(p[1]), p[0], p[2]);
         analisis.Add(anl);
     }
 }
Beispiel #2
0
        public string[] opigualdad = { "==", "!=" };             //11
        private void analizarLista()
        {
            int      i;
            string   p;
            analisis anl = new analisis();

            for (i = 0; i < elemento.Count(); i++)
            {
                p = elemento[i];
                bool id = true;
                foreach (char l in p)
                {
                    if (!Char.IsLetterOrDigit(l))
                    {
                        id = false;
                        break;
                    }
                }
                if (id)
                {
                    p           = p + "#0#identificador";
                    elemento[i] = p;
                }
                else if (p == "if")
                {
                    p           = p + "#19#if";
                    elemento[i] = p;
                }
                else if (p == "else")
                {
                    p           = p + "#22#else";
                    elemento[i] = p;
                }
                else if (p == "while")
                {
                    p           = p + "#20#while";
                    elemento[i] = p;
                }
                else if (p == "return")
                {
                    p           = p + "#21#return";
                    elemento[i] = p;
                }
                else if (p == "$")
                {
                    p           = p + "#2#$";
                    elemento[i] = p;
                }
                else if (p == "=")
                {
                    p           = p + "#18#=";
                    elemento[i] = p;
                }
                else if (p == "}")
                {
                    p           = p + "#17#}";
                    elemento[i] = p;
                }
                else if (p == "{")
                {
                    p           = p + "#16#{";
                    elemento[i] = p;
                }
                else if (p == ")")
                {
                    p           = p + "#15#)";
                    elemento[i] = p;
                }
                else if (p == "(")
                {
                    p           = p + "#14#(";
                    elemento[i] = p;
                }
                else if (p == ",")
                {
                    p           = p + "#13#,";
                    elemento[i] = p;
                }
                else if (p == ";")
                {
                    p           = p + "#12#if";
                    elemento[i] = p;
                }
                else if (opigualdad.Contains(p))
                {
                    p           = p + "#11#opIgualdad";
                    elemento[i] = p;
                }
                else if (p == "!")
                {
                    p           = p + "#10#opNot";
                    elemento[i] = p;
                }
                else if (p == "&&")
                {
                    p           = p + "#9#opAnd";
                    elemento[i] = p;
                }
                else if (p == "||")
                {
                    p           = p + "#8#opOr";
                    elemento[i] = p;
                }
                else if (oprelac.Contains(p))
                {
                    p           = p + "#7#opRelac";
                    elemento[i] = p;
                }
                else if (opmul.Contains(p))
                {
                    p           = p + "#6#opMul";
                    elemento[i] = p;
                }
                else if (opsum.Contains(p))
                {
                    p           = p + "#1#opSum";
                    elemento[i] = p;
                }
                else if (tipo.Contains(p))
                {
                    p           = p + "#4#tipo";
                    elemento[i] = p;
                }
                else
                {
                    p           = p + "#-1#error";
                    elemento[i] = p;
                }
                int   numero;
                float numero2;
                bool  result  = false;
                bool  result2 = false;
                try
                {
                    result  = int.TryParse(p, out numero);
                    result2 = float.TryParse(p, out numero2);
                }
                catch { };
                if (result2)
                {
                    elemento[i] = p + "#2#real";
                }
                if (result)
                {
                    elemento[i] = p + "#19#entero";
                }
            }
            analitic();
        }
Beispiel #3
0
        public void ejemplo_3()
        {
            int [,] tabla = new int[3, 3] {
                { 2, 0, 1 }, { 0, -1, 0 }, { 0, -2, 0 }
            };
            Stack <ElementoPila> pila = new Stack <ElementoPila>();
            int      fila = 0, columna = 0, accion = 0;
            bool     aceptación = false;
            Lexico   lexico     = new Lexico("a$");
            analisis entrada    = new analisis();

            entrada.modificar(0, "$", "$");
            ElementoPila t = new Terminal(entrada.id, entrada.palabra, 0);
            ElementoPila aux;

            pila.Push(t);

            List <analisis> elemento = lexico.leerCadena();

            aux     = pila.Peek();
            fila    = aux.accion;
            columna = elemento[0].id;
            accion  = tabla[fila, columna];
            ElementoPila nt = new NoTerminal(entrada.id, entrada.palabra, accion);

            mostrar2(pila);
            Console.WriteLine("entrada: " + elemento[1].palabra);
            Console.WriteLine("accion: " + nt.accion);
            pila.Push(nt);
            if (accion == 2)
            {
                aux     = pila.Peek();
                fila    = aux.accion;
                columna = elemento[1].id;
                accion  = tabla[fila, columna];
                pila.Push(new NoTerminal(elemento[1].id, elemento[1].palabra, accion));
                mostrar2(pila);
                Console.WriteLine("entrada: " + elemento[1].palabra);
                Console.WriteLine("accion: " + accion);
            }
            if (accion == -2)
            {
                pila.Pop();
                aux     = pila.Peek();
                fila    = aux.accion;
                columna = 2;
                accion  = tabla[fila, columna];
                //transicion
                pila.Push(new Estado(2, "E1", accion));
                mostrar2(pila);
                Console.WriteLine("entrada: " + elemento[1].palabra);
                Console.WriteLine("accion: " + accion);
                aux     = pila.Peek();
                fila    = aux.accion;
                columna = elemento[1].id;
                accion  = tabla[fila, columna];
                if (accion == -1)
                {
                    mostrar2(pila);
                    Console.WriteLine("entrada: " + elemento[1].palabra);
                    Console.WriteLine("accion: " + accion);
                    aceptación = accion == -1;
                    if (aceptación)
                    {
                        Console.WriteLine("aceptacion");
                    }
                }
            }
        }