Exemple #1
0
        public void llenarLista(Exp_Reg listaToken)
        {
            lista      = new List <TKER>();
            terminales = new List <String>();
            Token[] tokens = listaToken.getExpresion().ToArray();
            for (int i = 0; i < tokens.Count(); i++)
            {
                if (!tokens[i].getLexema().Equals("{") && !tokens[i].getLexema().Equals("}"))
                {
                    if (tokens[i].getToken().Equals("sPunto") || tokens[i].getToken().Equals("sOr"))
                    {
                        lista.Add(new TKER(tokens[i].getToken(), tokens[i].getLexema(), false, false, true));
                    }
                    else if (tokens[i].getToken().Equals("sPor") || tokens[i].getToken().Equals("sMas") || tokens[i].getToken().Equals("sInterrogacion"))
                    {
                        lista.Add(new TKER(tokens[i].getToken(), tokens[i].getLexema(), false, false, false));
                    }
                    else
                    {
                        lista.Add(new TKER(tokens[i].getToken(), tokens[i].getLexema(), true, false, false));
                        Boolean existe = false;

                        if (terminales.Count() > 0)
                        {
                            for (int j = 0; j < terminales.Count(); j++)
                            {
                                if (tokens[i].getLexema().Equals(terminales[j]))
                                {
                                    existe = true;
                                    break;
                                }
                            }
                            if (!existe)
                            {
                                terminales.Add(tokens[i].getLexema());
                            }
                        }
                        else
                        {
                            terminales.Add(tokens[i].getLexema());
                        }
                    }
                }
            }
        }
        public void Analizar(String cadenaEntrada)
        {
            listaToken      = new LinkedList <Token>();
            listaError      = new LinkedList <Error>();
            listaER         = new List <Exp_Reg>();
            listaConj       = new List <ListConj>();
            fila            = 1;
            columna         = 0;
            estado          = 0;
            cadenaAcumulada = "";

            conj = false;
            expR = false;

            cadenaEntrada = cadenaEntrada + "#";
            char[] c = cadenaEntrada.ToCharArray();

            for (int i = 0; i < cadenaEntrada.Length; i++)
            {
                columna++;
                switch (estado)
                {
                case 0:
                    if (c[i] == ' ' || c[i] == '\n' || c[i] == '\r' || c[i] == '\t')
                    {
                        if (c[i] == '\n')
                        {
                            fila++;
                            columna = 0;
                        }
                        estado = 0;
                    }
                    else if (((int)c[i] >= 97 && (int)c[i] <= 122) || ((int)c[i] >= 65 && (int)c[i] <= 90))
                    {
                        //letra
                        cadenaAcumulada += c[i];
                        estado           = 1;
                    }
                    else if ((int)c[i] >= 48 && (int)c[i] <= 57)
                    {
                        //numero
                        cadenaAcumulada += c[i];
                        estado           = 2;
                    }
                    else if (c[i] == '"')
                    {
                        //cadena
                        //cadenaAcumulada += c[i];
                        estado = 3;
                    }

                    else if (c[i] == ':')
                    {
                        cadenaAcumulada += c[i];
                        agregarToken(Token.tipo.sDosPuntos, c[i], c[i + 1]);
                    }
                    else if (c[i] == ';')
                    {
                        expR             = false;
                        conj             = false;
                        cadenaAcumulada += c[i];
                        agregarToken(Token.tipo.sPuntoyComa, c[i], c[i + 1]);
                    }
                    else if (c[i] == '{')
                    {
                        cadenaAcumulada += c[i];
                        agregarToken(Token.tipo.sLlaveIzq, c[i], c[i + 1]);
                    }
                    else if (c[i] == '}')
                    {
                        cadenaAcumulada += c[i];
                        agregarToken(Token.tipo.sLlaveDer, c[i], c[i + 1]);
                    }
                    else if (c[i] == '*')
                    {
                        cadenaAcumulada += c[i];
                        agregarToken(Token.tipo.sPor, c[i], c[i + 1]);
                    }
                    else if (c[i] == ',')
                    {
                        cadenaAcumulada += c[i];
                        agregarToken(Token.tipo.sComa, c[i], c[i + 1]);
                    }
                    else if (c[i] == '+')
                    {
                        cadenaAcumulada += c[i];
                        agregarToken(Token.tipo.sMas, c[i], c[i + 1]);
                    }
                    else if (c[i] == '>')
                    {
                        cadenaAcumulada += c[i];
                        if (c[i - 1] == '-')
                        {
                            expR = true;

                            if (!conj)
                            {
                                // listaExprecion = new LinkedList<ExprecionRegular>();
                                Token[] lista = listaToken.ToArray();
                                expresion = new Exp_Reg();
                                expresion.setNombre(lista[lista.Count() - 2].getLexema());
                                listaER.Add(expresion);
                            }
                            else
                            {
                                Token[] lista = listaToken.ToArray();
                                conjunto = new ListConj();
                                conjunto.setNombre(lista[lista.Count() - 2].getLexema());
                                listaConj.Add(conjunto);
                            }
                        }

                        agregarToken(Token.tipo.sMayor, c[i], c[i + 1]);
                    }
                    else if (c[i] == '<')
                    {
                        estado = 6;
                    }
                    else if (c[i] == '-')
                    {
                        cadenaAcumulada += c[i];
                        agregarToken(Token.tipo.sMenos, c[i], c[i + 1]);
                    }
                    else if (c[i] == '|')
                    {
                        cadenaAcumulada += c[i];
                        agregarToken(Token.tipo.sOr, c[i], c[i + 1]);
                    }
                    else if (c[i] == '.')
                    {
                        cadenaAcumulada += c[i];
                        agregarToken(Token.tipo.sPunto, c[i], c[i + 1]);
                    }
                    else if (c[i] == '~')
                    {
                        cadenaAcumulada += c[i];
                        agregarToken(Token.tipo.sVirgulilla, c[i], c[i + 1]);
                    }
                    else if (c[i] == '%')
                    {
                        cadenaAcumulada += c[i];
                        agregarToken(Token.tipo.sPorcentaje, c[i], c[i + 1]);
                    }
                    else if (c[i] == '?')
                    {
                        cadenaAcumulada += c[i];
                        agregarToken(Token.tipo.sInterrogacion, c[i], c[i + 1]);
                    }
                    else if (c[i] == '/')
                    {
                        estado = 4;
                    }
                    else
                    {
                        if (c[i] == '#' && i == cadenaEntrada.Length - 1)
                        {
                            Console.WriteLine("final de la cadena");
                            mostrarListaExp();
                            mostrarListaConj();

                            //  generarAFN();
                        }
                        else
                        {
                            cadenaAcumulada += c[i];
                            Console.WriteLine("Error léxico con: " + cadenaAcumulada);
                            agregarError();
                            estado          = 0;
                            cadenaAcumulada = "";
                        }
                    }
                    break;

                case 1:

                    if ((((int)c[i] >= 97 && (int)c[i] <= 122) || ((int)c[i] >= 65 && (int)c[i] <= 90)) || ((int)c[i] >= 48 && (int)c[i] <= 57))
                    {
                        //letra-num
                        cadenaAcumulada += c[i];
                        estado           = 1;
                    }
                    else
                    {
                        if ("CONJ".Equals(cadenaAcumulada))
                        {
                            conj = true;
                            agregarToken(Token.tipo.prConj, c[i - 1], c[i]);
                            i--;
                            columna--;
                        }
                        else
                        {
                            agregarToken(Token.tipo.identificador, c[i - 1], c[i]);
                            i--;
                            columna--;
                        }
                    }
                    break;

                case 2:
                    if (((int)c[i] >= 48 && (int)c[i] <= 57) || c[i] == '.')
                    {
                        //numero
                        cadenaAcumulada += c[i];
                        estado           = 2;
                    }
                    else
                    {
                        //cadenaAcumulada += c[i];
                        agregarToken(Token.tipo.numero, c[i - 1], c[i]);
                        i--;
                        columna--;
                    }
                    break;

                case 3:
                    if (c[i] == '"')
                    {
                        agregarToken(Token.tipo.cadena, c[i], c[i + 1]);
                    }
                    else
                    {
                        cadenaAcumulada += c[i];
                        estado           = 3;
                    }

                    break;

                case 4:
                    if (c[i] == '/')
                    {
                        estado = 5;
                    }
                    else
                    {
                        cadenaAcumulada += c[i];
                        Console.WriteLine("Error léxico con: " + "/");
                        agregarError();
                        estado          = 0;
                        cadenaAcumulada = "";
                    }

                    break;

                case 5:
                    if (c[i] == '\n')
                    {
                        agregarToken(Token.tipo.comentario, c[i], c[i + 1]);
                    }
                    else
                    {
                        cadenaAcumulada += c[i];
                        estado           = 5;
                    }

                    break;

                case 6:

                    if (c[i] == '!')
                    {
                        estado = 7;
                    }
                    else
                    {
                        cadenaAcumulada += c[i];
                        Console.WriteLine("Error léxico con: <");
                        agregarError();
                        estado          = 0;
                        cadenaAcumulada = "";
                    }

                    break;

                case 7:

                    if (c[i] == '!' && c[i + 1] == '>')
                    {
                        agregarToken(Token.tipo.comentarioML, c[i], c[i + 1]);
                    }
                    else
                    {
                        cadenaAcumulada += c[i];
                        estado           = 7;
                    }

                    break;
                }
            }
        }