예제 #1
0
        public Sentencia DeclarationP(Sentencia C)
        {
            if (currentToken.Tipo == TipoToken.TK_COMA)
            {
                currentToken = lex.NextToken();
                Campos CP = new Campos();
                if (currentToken.Tipo == TipoToken.TK_ID)
                {
                    Variable Var = new Variable();
                    Var.id = currentToken.Lexema;
                    currentToken = lex.NextToken();
                    C.sig = DeclarationP(CP);

                    return C;
                }
                else
                {
                    throw new Exception("Error Sintactico - Se esperaba un identificador");
                }
            }
            else if (currentToken.Tipo == TipoToken.TK_ASSIGN)
            {
                return AssignDeclaration(C);

            }
            else if (currentToken.Tipo == TipoToken.TK_OPENPAR)
            {
                currentToken = lex.NextToken();
                S_Functions sFunctions = new S_Functions();
                sFunctions.Retorno = ((Campos)C).Tip;
                sFunctions.var.id = ((Campos)C).Var.id;
                sFunctions.Campo = ParameterList();
                if (currentToken.Tipo == TipoToken.TK_CLOSEPAR)
                {
                    currentToken = lex.NextToken();
                    sFunctions.S = CompoundStatement();
                    return sFunctions;
                }
                else
                {
                    throw new Exception("Error Sintactico - Se esperaba simbolo )");
                }
            }
            else if (currentToken.Tipo == TipoToken.TK_FINSENTENCIA)
            {
                currentToken = lex.NextToken();
                return C;
            }
            else
            {
                throw new Exception("Error Sintactico - Se esperaba un fin sentencia");
            }
        }
예제 #2
0
        Sentencia Statement()
        {
            try
            {
                switch (currentToken.Tipo)
                {
                    case TipoToken.TK_IF:
                        currentToken = lex.NextToken();
                        return parseIf();

                    case TipoToken.TK_FOR:
                        currentToken = lex.NextToken();
                        return parseFor();

                    case TipoToken.TK_WHILE:
                        currentToken = lex.NextToken();
                        return parseWhile();

                    case TipoToken.TK_CASE:
                        currentToken = lex.NextToken();
                        return parseSwitch();

                    case TipoToken.TK_REPEAT:
                        currentToken = lex.NextToken();
                        return parseDo();

                    #region variable
                    case TipoToken.TK_VAR:
                        {
                            currentToken = lex.NextToken();
                            Declaracion ret = VariableDeclarationList();
                            if (currentToken.Tipo != TipoToken.TK_END)
                                throw new Exception("Se esperaba end.");
                            else
                            {
                                currentToken = lex.NextToken();
                                return ret;
                            }
                        }
                    #endregion

                    case TipoToken.TK_ID:
                        return parseAssignOrCall();

                    #region read/print
                    case TipoToken.TK_PRINT:
                        {
                            currentToken = lex.NextToken();
                            S_Print ret = new S_Print();
                            ret.Expr = Expr();
                            return ret;
                        }
                    case TipoToken.TK_READ:
                        {
                            currentToken = lex.NextToken();
                            if (currentToken.Tipo != TipoToken.TK_ID)
                                throw new Exception("Se esperaba identificador.");
                            else
                            {
                                S_Read ret = new S_Read();
                                ret.var = new Variable(currentToken.Lexema, AccessList(currentToken.Lexema));
                                return ret;
                            }
                        }
                    #endregion

                    #region Procedure
                    case TipoToken.TK_VOID:
                        {
                            currentToken = lex.NextToken();
                            if (currentToken.Tipo != TipoToken.TK_ID)
                                throw new Exception("Se esperaba ID");
                            else
                            {
                                S_Functions ret = new S_Functions();
                                ret.Retorno = new Voids();
                                ret.Var = currentToken.Lexema;
                                currentToken = lex.NextToken();
                                if (currentToken.Tipo != TipoToken.TK_OPENPAR)
                                    throw new Exception("Se esperaba \"(\"");
                                else
                                {
                                    currentToken = lex.NextToken();
                                    ret.Campo = VariableDeclarationList();
                                    if (currentToken.Tipo != TipoToken.TK_CLOSEPAR)
                                        throw new Exception("Se esperaba\")\"");
                                    else
                                    {
                                        currentToken = lex.NextToken();
                                        ret.S = CodeBlock();
                                        return ret;
                                    }
                                }
                            }
                        }
                    #endregion

                    #region Function
                    case TipoToken.TK_FUNCTION:
                        {
                            currentToken = lex.NextToken();
                            if (currentToken.Tipo != TipoToken.TK_ID)
                                throw new Exception("Se esperaba ID");
                            else
                            {
                                S_Functions ret = new S_Functions();
                                ret.Var = currentToken.Lexema;
                                currentToken = lex.NextToken();
                                if (currentToken.Tipo != TipoToken.TK_OPENPAR)
                                    throw new Exception("Se esperaba \"(\"");
                                else
                                {
                                    currentToken = lex.NextToken();
                                    ret.Campo = VariableDeclarationList();
                                    if (currentToken.Tipo != TipoToken.TK_CLOSEPAR)
                                        throw new Exception("Se esperaba\")\"");
                                    else
                                    {
                                        currentToken = lex.NextToken();
                                        if (currentToken.Tipo != TipoToken.TK_DOSPUNTOS)
                                            throw new Exception("Se esperaba\":\"");
                                        else
                                        {
                                            currentToken = lex.NextToken();
                                            ret.Retorno = ParseType();
                                            ret.S = CodeBlock();
                                            return ret;
                                        }
                                    }
                                }
                            }
                        }
                    #endregion

                    #region type
                    case TipoToken.TK_TYPE:
                        {
                            currentToken = lex.NextToken();
                            TypeDef ret = TypeDeclarationList();
                            if (currentToken.Tipo != TipoToken.TK_END)
                                throw new Exception("Se esperaba end.");
                            else
                            {
                                currentToken = lex.NextToken();
                                return ret;
                            }
                        }
                    #endregion

                    #region break/continue/return
                    case TipoToken.TK_BREAK:
                        currentToken = lex.NextToken();
                        return new S_Break();

                    case TipoToken.TK_CONTINUE:
                        currentToken = lex.NextToken();
                        return new S_Continue();

                    case TipoToken.TK_RETURN:
                        {
                            currentToken = lex.NextToken();
                            S_Return ret = new S_Return();
                            ret.Expr = Expr();
                            return ret;
                        }
                    #endregion
                    default:
                        throw new Exception("Sentencia no reconocida.");
                }
            }
            catch (Exception ex) { throw ex; }
        }
예제 #3
0
        public Sentencia DeclaracionesClase()
        {
            Declaracion Decl = new Declaracion();

            VARTYPE();
            try
            {
                Class c = new Class();
                c.Nombre = currentToken.Lexema;
                c.Campos=new T_Campos();
                Decl.Tip =c;
                currentToken = lex.NextToken();
            }
            catch (Exception ex)
            {
                throw ex;
            }

            if (currentToken.Tipo == TipoToken.TK_OPENCOR) //ARREGLO
            {
                int dim = arrayDimensions(1);
                Arreglo ArrTip = new Arreglo();
                ArrTip.Contenido = Decl.Tip;
                ArrTip.Dimensiones = dim;
                Decl.Tip = ArrTip;
                if (currentToken.Tipo == TipoToken.TK_ID)
                {
                    Decl.Var.id = currentToken.Lexema;
                    currentToken = lex.NextToken();
                    if (currentToken.Tipo == TipoToken.TK_FINSENTENCIA)
                    {
                        currentToken = lex.NextToken();
                        return Decl;
                    }
                    try
                    {
                        Decl = DeclOption(Decl);
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }

                    if (currentToken.Tipo != TipoToken.TK_FINSENTENCIA)
                    {
                        currentToken = lex.NextToken();
                        return Decl;
                    }
                    else
                    {
                        throw new Exception("Error Sintactico - Se esperaba Fin Sentencia");
                    }
                }
                else
                {
                    throw new Exception("Error Sintactico - Se esperaba un ID");
                }

            }
            else if (currentToken.Tipo == TipoToken.TK_ID)
            {
                Decl.Var.id = currentToken.Lexema;
                currentToken = lex.NextToken();
                if (currentToken.Tipo == TipoToken.TK_COMA || currentToken.Tipo == TipoToken.TK_ASSIGN)
                {
                    try
                    {
                        DeclaracionesVarias(Decl);
                        DeclOption(Decl);
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
                else if (currentToken.Tipo == TipoToken.TK_OPENPAR)
                {
                    currentToken = lex.NextToken();
                    S_Functions sFunctions = new S_Functions();
                    sFunctions.Retorno = Decl.Tip;
                    sFunctions.Var = Decl.Var.id;
                    try
                    {
                        sFunctions.Campo = ParameterList();
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }

                    if (currentToken.Tipo == TipoToken.TK_CLOSEPAR)
                    {
                        currentToken = lex.NextToken();
                        try
                        {
                            sFunctions.S = CompoundStatement();
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }

                        return sFunctions;
                    }
                    else
                    {
                        throw new Exception("Error Sintactico - Se esperaba simbolo )");
                    }
                }
                else if (currentToken.Tipo == TipoToken.TK_OPENLLAVE)
                {
                    currentToken = lex.NextToken();
                    S_Class sClass = new S_Class();
                    sClass.Var.id = Decl.Var.id;

                    try
                    {
                        sClass.CamposClase = ListaDeclaracion(sClass.CamposClase);
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }

                    if (currentToken.Tipo != TipoToken.TK_CLOSELLAVE)
                        throw new Exception("Error Sintactico - Se esperaba simbolo }");
                    currentToken = lex.NextToken();
                    return sClass;
                }
                else if (currentToken.Tipo == TipoToken.TK_FINSENTENCIA)
                {
                    currentToken = lex.NextToken();
                    return Decl;
                }
                else
                    throw new Exception("Error Sintactico - Se esperaba simbolo ;");
            }
            return Decl;
        }
예제 #4
0
        public Sentencia DeclarationP(Sentencia campos)
        {
            if (currentToken.Tipo == Lexico.TipoToken.TK_COMA)
            {
                currentToken = lex.NextToken();
                Campos c = new Campos();
                if (currentToken.Tipo != Lexico.TipoToken.TK_ID)
                    throw new Exception("Se esperaba el token ID");

                c.Var.id = currentToken.Lexema;
                currentToken = lex.NextToken();

                campos.sig=DeclarationP(c);
                return campos;
            }
            else if (currentToken.Tipo == Lexico.TipoToken.TK_ASSIGN)
            {
                S_Asignacion sasignacion = new S_Asignacion();

                sasignacion.id.id = ((Campos)campos).Var.id;
                currentToken = lex.NextToken();
                sasignacion.Valor= Expression();
                return DeclarationP(sasignacion);

            }
            else if (currentToken.Tipo == Lexico.TipoToken.TK_OPENPAR)
            {
                S_Functions sfunciones = new S_Functions();
                currentToken = lex.NextToken();
                sfunciones.Retorno = ((Campos)campos).Tip;
                sfunciones.var.id = ((Campos)campos).Var.id;
                sfunciones.Campo= ParametroList();
                if (currentToken.Tipo != Lexico.TipoToken.TK_CLOSEPAR)
                    throw new Exception("Se esperaba el token )");
                currentToken = lex.NextToken();
                sfunciones.S= CompoundStatement();

                return sfunciones;
            }
            else if (currentToken.Tipo == Lexico.TipoToken.TK_FINSENTENCIA)
            {
                currentToken = lex.NextToken();
                return campos;
            }
            else
                throw new Exception("Se esperaba el token ;");
        }
예제 #5
0
        public Sentencia DeclaracionesCPrima(Declaracion decl)
        {
            try
            {
                /*Cuando se declaran varias variables seguidas int x,t,y  */
                #region
                if (currentToken.Tipo == Lexico.TipoToken.TK_COMA)
                {
                    currentToken = lex.NextToken();
                    if (currentToken.Tipo != Lexico.TipoToken.TK_ID)
                        throw new Exception("Se esperaba un token ID");

                    Variable nombre = new Variable(currentToken.Lexema, null);
                    decl.Sig.Tip = decl.Tip;
                    decl.Sig.Var = nombre;
                    currentToken = lex.NextToken();
                    DeclCPrima(decl.Sig);
                    return decl;
                }
                #endregion
                /*Declaracion de arreglos */
                #region
                else if (currentToken.Tipo == Lexico.TipoToken.TK_OPENCOR)
                {
                    currentToken = lex.NextToken();
                    Expresiones E1 = Expression();
                    if (currentToken.Tipo != Lexico.TipoToken.TK_CLOSECOR)
                        throw new Exception("Se esperaba el token ] ");
                    currentToken = lex.NextToken();
                    Arreglo tipoArreglo = new Arreglo();
                    tipoArreglo.Contenido = decl.Tip;
                    tipoArreglo.Rangos.Add(E1);
                    DeclArreglo(tipoArreglo);
                    tipoArreglo.Dimensiones = tipoArreglo.Rangos.Count;
                    decl.Tip = tipoArreglo;
                    return decl;
                }
                #endregion
                /*Declaracion de funciones */
                #region
                else if (currentToken.Tipo == Lexico.TipoToken.TK_OPENPAR)
                {
                    currentToken = lex.NextToken();
                    Declaracion listaParams = FuncionesParams();
                    if (currentToken.Tipo != Lexico.TipoToken.TK_CLOSEPAR)
                        throw new Exception("Se esperaba el token )");
                    currentToken = lex.NextToken();

                    S_Functions declFuncion = new S_Functions();
                    declFuncion.Retorno = decl.Tip;
                    declFuncion.Var = decl.Var.id;
                    declFuncion.S = CompoundStatement();
                    if (listaParams != null)
                        declFuncion.Campo = listaParams;
                    return declFuncion;
                }
                #endregion
                /*Declaracion de Structs */
                #region
                else if (currentToken.Tipo == Lexico.TipoToken.TK_OPENLLAVE)
                {
                    currentToken = lex.NextToken();
                    Declaracion strDec = StructDeclaration();
                    Declaracion tmp = StructDeclaration();
                    while (tmp != null)
                    {
                        strDec.Sig = tmp;
                        tmp = StructDeclaration();
                    }

                    Structs s = new Structs();
                    s.nombre = decl.Var.id;
                    s.campos = strDec;
                    if (currentToken.Tipo != Lexico.TipoToken.TK_CLOSELLAVE)
                        throw new Exception("Error sintactico se esperaba }");
                    currentToken = lex.NextToken();

                    if (currentToken.Tipo != Lexico.TipoToken.TK_FINSENTENCIA)
                        throw new Exception("Error sintactico se esperaba ;");
                    currentToken = lex.NextToken();

                    return s;
                }
                #endregion
                return decl;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }