Ejemplo n.º 1
0
        public ExpresionRegular buscarExp(string id)
        {
            int i = 0;

            while (i < expresiones.Count)
            {
                ExpresionRegular exp = (ExpresionRegular)expresiones[i];
                if (exp.getId().Equals(id))
                {
                    return(exp);
                }
                i++;
            }
            return(null);
        }
Ejemplo n.º 2
0
        public void analizar(RichTextBox texto, ArrayList conjuntos, ArrayList expresiones, ArrayList cadenas)
        {
            Token     token;
            ArrayList tokens = this.lexico.analizar(texto);

            for (i = 0; i < tokens.Count; i++)
            {
                token = (Token)tokens[i];
                if (token.getTipo() == Token.TIPO.IDENTIFICADOR)
                {
                    Token tokensig = (Token)tokens[i + 1];
                    switch (tokensig.getTipo())
                    {
                    case Token.TIPO.FLECHA:
                        tokensig = (Token)tokens[i + 2];
                        if (tokensig.getTipo() == Token.TIPO.CONJ)
                        {
                            conjuntos.Add(new Conjunto(token.getLexema(), tokensig.getLexema()));
                            i += 2;
                        }
                        else
                        {
                            i += 2;
                            Stack <Object []> lista = new Stack <Object[]>();
                            while (tokensig.getTipo() != Token.TIPO.PUNTOCOMA)
                            {
                                switch (tokensig.getTipo())
                                {
                                case Token.TIPO.CERRADURAKLEENE:
                                case Token.TIPO.CERRADURAPOSITIVA:
                                case Token.TIPO.CONCATENACION:
                                case Token.TIPO.OR:
                                case Token.TIPO.INTERROGACION:
                                case Token.TIPO.ENTER:
                                case Token.TIPO.TAB:
                                case Token.TIPO.COMILLA:
                                case Token.TIPO.APOSTROFE:
                                    lista.Push(new object[2] {
                                        tokensig.getLexema(), tokensig.getTipo()
                                    });
                                    break;

                                case Token.TIPO.TODO:
                                    int l = tokensig.getLexema().Length;
                                    lista.Push(new object[2] {
                                        tokensig.getLexema().Substring(2, l - 4), tokensig.getTipo()
                                    });
                                    break;

                                case Token.TIPO.CADENA:
                                    int m = tokensig.getLexema().Length;
                                    lista.Push(new object[2] {
                                        tokensig.getLexema().Substring(1, m - 2), tokensig.getTipo()
                                    });
                                    break;

                                case Token.TIPO.LLAVEABRE:
                                    tokensig = (Token)tokens[i + 1];
                                    if (tokensig.getTipo() == Token.TIPO.IDENTIFICADOR)
                                    {
                                        Conjunto conj;
                                        int      c = 0;
                                        while (c < conjuntos.Count)
                                        {
                                            conj = (Conjunto)conjuntos[c];
                                            if (tokensig.getLexema().Equals(conj.nombre))
                                            {
                                                lista.Push(new object[2] {
                                                    conj.conjunto, Token.TIPO.CONJ
                                                });
                                                break;
                                            }
                                            c++;
                                        }
                                    }
                                    i += 2;
                                    break;
                                }
                                i++;
                                tokensig = (Token)tokens[i];
                            }
                            ExpresionRegular exp = new ExpresionRegular(token.getLexema());
                            exp.setAFN(p.crearAFN(lista, exp.simbolos));
                            expresiones.Add(exp);
                        }

                        break;

                    case Token.TIPO.DOSPUNTOS:
                        tokensig = (Token)tokens[i + 2];
                        if (tokensig.getTipo() == Token.TIPO.CADENA)
                        {
                            string cad = tokensig.getLexema();
                            cadenas.Add(new CadenaAValidar(token.getLexema(), cad.Substring(1, cad.Length - 2)));
                            i += 2;
                        }
                        break;
                    }
                }
            }
        }
Ejemplo n.º 3
0
        public string validarLexema(CadenaAValidar cadena)
        {
            ExpresionRegular exp    = buscarExp(cadena.getId());
            string           valida = "Cadena Valida";
            int    estado           = 0;
            int    edoant           = 0;
            string tok = "";
            char   c;
            string aux;
            string cad = cadena.getCadena() + "¿";

            for (int i = 0; i < cad.Length; i++)
            {
                c = cad.ElementAt(i);
                int j = 0;
                if (c == '¿' && i == cad.Length - 1)
                {
                    if (exp.afd.getEstado(estado).isAceptable())
                    {
                        cadena.tokens.Add(new Token(0, i, tok, Token.TIPO.CADENA));
                    }
                    else
                    {
                        cadena.errores.Add(new Token(0, i, tok, Token.TIPO.DESCONOCIDO));
                    }
                    tok = "";
                    if (i < cad.Length - 1 && estado != 0)
                    {
                        i--;
                    }
                    estado = 0;
                }
                else
                {
                    bool val = false;
                    while (j < exp.afd.getEstado(estado).getMovimiento().Count)
                    {
                        Movimiento mov = exp.afd.getEstado(estado).getMov(j);
                        if (mov.getTipo() == Token.TIPO.TODO)
                        {
                            if (cad.Length >= mov.getId().Length)
                            {
                                int tam = cad.Length - i + 1;
                                if (tam >= mov.getId().Length)
                                {
                                    aux = cad.Substring(i, mov.getId().Length);
                                    if (aux.Equals(mov.getId()))
                                    {
                                        edoant = estado;
                                        estado = mov.getMov().getId();
                                        i     += mov.getId().Length - 1;
                                        tok   += aux;
                                        val    = true;
                                        break;
                                    }
                                }
                            }
                        }
                        else if (mov.getTipo() == Token.TIPO.CONJ)
                        {
                            if (mov.getId().Length == 1)
                            {
                                if (c.ToString() == mov.getId())
                                {
                                    edoant = estado;
                                    estado = mov.getMov().getId();
                                    tok   += c;
                                    val    = true;
                                    break;
                                }
                            }
                            else if (mov.getId().Length == 2)
                            {
                                bool encontrado = false;
                                if (mov.getId().Equals("\\n"))
                                {
                                    if (c == '\n')
                                    {
                                        encontrado = true;
                                    }
                                    else if (mov.getId().Equals("\\t"))
                                    {
                                        if (c == '\t')
                                        {
                                            encontrado = true;
                                        }
                                        else if (mov.getId().Equals("\\\'"))
                                        {
                                            if (c == '\'')
                                            {
                                                encontrado = true;
                                            }
                                            else if (mov.getId().Equals("\\\""))
                                            {
                                                if (c == '"')
                                                {
                                                    encontrado = true;
                                                }
                                            }
                                        }
                                    }
                                }
                                if (encontrado)
                                {
                                    tok   += c;
                                    val    = true;
                                    edoant = estado;
                                    estado = mov.getMov().getId();
                                    break;
                                }
                            }
                            else if (mov.getId().ElementAt(1) == '~')
                            {
                                if (c >= mov.getId().ElementAt(0) && c <= mov.getId().ElementAt(2))
                                {
                                    edoant = estado;
                                    val    = true;
                                    tok   += c;
                                    estado = mov.getMov().getId();
                                    break;
                                }
                            }
                            else if (mov.getId().ElementAt(1) == ',')
                            {
                                int z = 0;
                                while (z < mov.getId().Length)
                                {
                                    if (c == mov.getId().ElementAt(z))
                                    {
                                        edoant = estado;
                                        tok   += c;
                                        val    = true;
                                        estado = mov.getMov().getId();
                                        break;
                                    }
                                    z += 2;
                                }
                                if (z < mov.getId().Length)
                                {
                                    break;
                                }
                            }
                        }
                        else if (mov.getTipo() == Token.TIPO.CADENA)
                        {
                            if (c.ToString() == mov.getId())
                            {
                                tok   += c;
                                edoant = estado;
                                val    = true;
                                estado = mov.getMov().getId();
                                break;
                            }
                        }
                        else if (mov.getTipo() == Token.TIPO.APOSTROFE)
                        {
                            if (c == '\'')
                            {
                                tok   += c;
                                val    = true;
                                edoant = estado;
                                estado = mov.getMov().getId();
                                break;
                            }
                        }
                        else if (mov.getTipo() == Token.TIPO.COMILLA)
                        {
                            if (c == '"')
                            {
                                tok   += c;
                                edoant = estado;
                                val    = true;
                                estado = mov.getMov().getId();
                                break;
                            }
                        }
                        else if (mov.getTipo() == Token.TIPO.ENTER)
                        {
                            if (c == '\n')
                            {
                                tok   += c;
                                edoant = estado;
                                val    = true;
                                estado = mov.getMov().getId();
                                break;
                            }
                        }
                        else if (mov.getTipo() == Token.TIPO.TAB)
                        {
                            if (c == '\t')
                            {
                                tok   += c;
                                val    = true;
                                edoant = estado;
                                estado = mov.getMov().getId();
                                break;
                            }
                        }
                        j++;
                    }
                    if (j >= exp.afd.getEstado(edoant).getMovimiento().Count || !val)
                    {
                        if (exp.afd.getEstado(estado).isAceptable())
                        {
                            cadena.tokens.Add(new Token(0, i, tok, Token.TIPO.CADENA));
                        }
                        else
                        {
                            if (tok.Equals(""))
                            {
                                tok = c.ToString();
                            }
                            cadena.errores.Add(new Token(0, i, tok, Token.TIPO.DESCONOCIDO));
                        }
                        tok = "";
                        if (i < cad.Length - 1 && estado != 0)
                        {
                            i--;
                        }
                        estado = 0;
                    }
                }
            }
            if (cadena.errores.Count != 0)
            {
                valida = "Cadena No Valida";
            }
            return(valida);
        }