Exemplo n.º 1
0
        public Sentencia Statement()
        {
            try
            {
                //Para Declaraciones
                if (currentToken.Tipo == Lexico.TipoToken.TK_INT || currentToken.Tipo == Lexico.TipoToken.TK_FLOAT || currentToken.Tipo == Lexico.TipoToken.TK_STRUCT ||
                   currentToken.Tipo == Lexico.TipoToken.TK_CHAR || currentToken.Tipo == Lexico.TipoToken.TK_VOID || currentToken.Tipo == Lexico.TipoToken.TK_STRING ||
                    currentToken.Tipo == Lexico.TipoToken.TK_BOOL)
                {
                    return DeclaracionesC();
                }
                else if (this.currentToken.Tipo == Lexico.TipoToken.TK_PRINT)
                {
                    #region
                    this.currentToken = lex.NextToken();
                    if (currentToken.Tipo != Lexico.TipoToken.TK_OPENPAR)
                        throw new Exception("Se esperaba un (");
                    this.currentToken = lex.NextToken();
                    S_Print sprint = new S_Print();
                    sprint.Expr = Expression();

                    //this.currentToken = lex.NextToken();
                    if (currentToken.Tipo != Lexico.TipoToken.TK_CLOSEPAR)
                        throw new Exception("Se esperaba un )");
                    this.currentToken = lex.NextToken();
                    if (currentToken.Tipo != Lexico.TipoToken.TK_FINSENTENCIA)
                        throw new Exception("Se esperaba un ;");
                    this.currentToken = lex.NextToken();
                    return sprint;
                    #endregion
                }
                else if (this.currentToken.Tipo == Lexico.TipoToken.TK_READ)
                {
                    #region
                    this.currentToken = lex.NextToken();
                    if (currentToken.Tipo != Lexico.TipoToken.TK_OPENPAR)
                        throw new Exception("Se esperaba un (");
                    this.currentToken = lex.NextToken();

                    if (currentToken.Tipo != Lexico.TipoToken.TK_ID)
                        throw new Exception("Se esperaba un ID");

                    S_Read sread = new S_Read();
                    sread.var.id = currentToken.Lexema;
                    this.currentToken = lex.NextToken();
                    if (currentToken.Tipo != Lexico.TipoToken.TK_CLOSEPAR)
                        throw new Exception("Se esperaba un )");
                    this.currentToken = lex.NextToken();
                    if (currentToken.Tipo != Lexico.TipoToken.TK_FINSENTENCIA)
                        throw new Exception("Se esperaba un ;");
                    this.currentToken = lex.NextToken();
                    return sread;
                    #endregion
                }
                else if (this.currentToken.Tipo == Lexico.TipoToken.TK_IF)
                {
                    #region
                    S_If sif = new S_If();
                    this.currentToken = lex.NextToken();
                    if (this.currentToken.Tipo != Lexico.TipoToken.TK_OPENPAR)
                        throw new Exception("Se esperaba un (");

                    this.currentToken = lex.NextToken();
                    sif.Condicion = Expression();

                    if (this.currentToken.Tipo != Lexico.TipoToken.TK_CLOSEPAR)
                        throw new Exception("Se esperaba un )");

                    this.currentToken = lex.NextToken();
                    sif.Cierto = CompoundStatement();
                    sif.Falso = Else();
                    return sif;
                    #endregion
                }
                else if (this.currentToken.Tipo == Lexico.TipoToken.TK_WHILE)
                {
                    #region
                    S_While swhile = new S_While();
                    this.currentToken = lex.NextToken();
                    if (this.currentToken.Tipo != Lexico.TipoToken.TK_OPENPAR)
                        throw new Exception("Se esperaba un (");

                    this.currentToken = lex.NextToken();
                    swhile.Condicion = Expression();

                    if (this.currentToken.Tipo != Lexico.TipoToken.TK_CLOSEPAR)
                        throw new Exception("Se esperaba un )");

                    this.currentToken = lex.NextToken();
                    swhile.S = CompoundStatement();
                    return swhile;
                    #endregion
                }
                else if (this.currentToken.Tipo == Lexico.TipoToken.TK_DO)
                {
                    #region
                    S_Do sdo = new S_Do();
                    this.currentToken = lex.NextToken();
                    sdo.S = CompoundStatement();

                    if (this.currentToken.Tipo == Lexico.TipoToken.TK_WHILE)
                    {
                        this.currentToken = lex.NextToken();
                        if (this.currentToken.Tipo != Lexico.TipoToken.TK_OPENPAR)
                            throw new Exception("Se esperaba un (");

                        this.currentToken = lex.NextToken();
                        sdo.Condicion = Expression();

                        if (this.currentToken.Tipo != Lexico.TipoToken.TK_CLOSEPAR)
                            throw new Exception("Se esperaba un )");

                        this.currentToken = lex.NextToken();
                        if (this.currentToken.Tipo != Lexico.TipoToken.TK_FINSENTENCIA)
                            throw new Exception("Se esperaba un ;");
                        this.currentToken = lex.NextToken();
                        return sdo;
                    }
                    else
                        throw new Exception("Se esperaba la palabra reservada WHILE");
                    #endregion
                }
                else if (this.currentToken.Tipo == Lexico.TipoToken.TK_FOR)
                {
                    #region
                    S_For sfor = new S_For();
                    this.currentToken = lex.NextToken();
                    if (this.currentToken.Tipo != Lexico.TipoToken.TK_OPENPAR)
                        throw new Exception("Se esperaba un (");

                    this.currentToken = lex.NextToken();
                    if (this.currentToken.Tipo != Lexico.TipoToken.TK_ID)
                        throw new Exception("Se esperaba un ID");

                    sfor.Var.id = this.currentToken.Lexema;
                    this.currentToken = lex.NextToken();
                    if (this.currentToken.Tipo != Lexico.TipoToken.TK_ASSIGN)
                        throw new Exception("Se esperaba el simbolo =");

                    this.currentToken = lex.NextToken();
                    sfor.Inicio = Expression();
                    if (this.currentToken.Tipo != Lexico.TipoToken.TK_FINSENTENCIA)
                        throw new Exception("Se esperaba un ;");

                    this.currentToken = lex.NextToken();
                    sfor.Condicion = Expression();
                    if (this.currentToken.Tipo != Lexico.TipoToken.TK_FINSENTENCIA)
                        throw new Exception("Se esperaba un ;");

                    this.currentToken = lex.NextToken();
                    sfor.Iteracion = Expression();
                    if (this.currentToken.Tipo != Lexico.TipoToken.TK_CLOSEPAR)
                        throw new Exception("Se esperaba un )");

                    this.currentToken = lex.NextToken();
                    sfor.S = CompoundStatement();
                    return sfor;
                    #endregion
                }
                else if (this.currentToken.Tipo == Lexico.TipoToken.TK_RETURN)
                {
                    #region
                    S_Return sreturn = new S_Return();
                    this.currentToken = lex.NextToken();
                    sreturn.Expr = Expression();
                    if (this.currentToken.Tipo != Lexico.TipoToken.TK_FINSENTENCIA)
                        throw new Exception("Se esperaba un ;");

                    this.currentToken = lex.NextToken();
                    return sreturn;
                    #endregion
                }
                else if (this.currentToken.Tipo == Lexico.TipoToken.TK_BREAK)
                {
                    #region
                    S_Break sbreak = new S_Break();
                    this.currentToken = lex.NextToken();
                    if (this.currentToken.Tipo != Lexico.TipoToken.TK_FINSENTENCIA)
                        throw new Exception("Se esperaba un ;");

                    this.currentToken = lex.NextToken();
                    return sbreak;
                    #endregion
                }
                else if (this.currentToken.Tipo == Lexico.TipoToken.TK_SWITCH)
                {
                    #region
                    S_Switch sSwitch = new S_Switch();
                    this.currentToken = lex.NextToken();
                    if (this.currentToken.Tipo != Lexico.TipoToken.TK_OPENPAR)
                        throw new Exception("Se esperaba un (");
                    this.currentToken = lex.NextToken();
                    sSwitch.Var = Expression();

                    if (this.currentToken.Tipo != Lexico.TipoToken.TK_CLOSEPAR)
                        throw new Exception("Se esperaba una )");

                    this.currentToken = lex.NextToken();

                    if (this.currentToken.Tipo != Lexico.TipoToken.TK_OPENLLAVE)
                        throw new Exception("Se esperaba una {");

                    this.currentToken = lex.NextToken();
                    sSwitch.Casos = Cases();

                    if (this.currentToken.Tipo != Lexico.TipoToken.TK_DEFAULT)
                        throw new Exception("Se esperaba la palabra reservada DEFAULT");

                    this.currentToken = lex.NextToken();
                    if (this.currentToken.Tipo != Lexico.TipoToken.TK_DOSPUNTOS)
                        throw new Exception("Se esperaba el simbolo :");
                    this.currentToken = lex.NextToken();
                    sSwitch.sdefault = StatementList();

                    if (this.currentToken.Tipo != Lexico.TipoToken.TK_CLOSELLAVE)
                        throw new Exception("Se esperaba una }");

                    this.currentToken = lex.NextToken();
                    return sSwitch;
                    #endregion
                }
                else if (this.currentToken.Tipo == Lexico.TipoToken.TK_ID)
                {
                    #region
                    Sentencia s = SentenciaAssign_LlamaFun();
                    if (currentToken.Tipo != Lexico.TipoToken.TK_FINSENTENCIA)
                        throw new Exception("Se esperaba un token ;");
                    this.currentToken = lex.NextToken();
                    return s;
                    #endregion
                }
                return null;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemplo n.º 2
0
 S_For parseFor()
 {
     S_For ret = new S_For();
     ret.Tip = new Entero();
     if (currentToken.Tipo != TipoToken.TK_ID)
         throw new Exception("Se esperaba identificador.");
     else
     {
         ret.Var = new Variable(currentToken.Lexema, null);
         currentToken = lex.NextToken();
         if (currentToken.Tipo != TipoToken.TK_ASSIGN)
             throw new Exception("Se esperaba el operador de asignacion");
         else
         {
             currentToken = lex.NextToken();
             try
             {
                 ret.Inicio = Expr();
             }
             catch (Exception ex) { throw ex; }
             switch (currentToken.Tipo)
             {
                 case TipoToken.TK_TO:
                     currentToken = lex.NextToken();
                     MenorQue cond;
                     try
                     {
                         cond = new MenorQue(ret.Var, Expr());
                     }
                     catch (Exception ex) { throw ex; }
                     ExpMasMas it = new ExpMasMas();
                     it.ID = ret.Var;
                     ret.Condicion = cond;
                     ret.Iteracion = it;
                     break;
                 case TipoToken.TK_DOWNTO:
                     currentToken = lex.NextToken();
                     MayorQue cond1;
                     try
                     {
                         cond1 = new MayorQue(ret.Var, Expr());
                     }
                     catch (Exception ex) { throw ex; }
                     ExpMenosMenos it1 = new ExpMenosMenos();
                     it1.ID = ret.Var;
                     ret.Condicion = cond1;
                     ret.Iteracion = it1;
                     break;
                 default:
                     throw new Exception("Se esperaba to/Downto");
             }
             if (currentToken.Tipo != TipoToken.TK_DO)
                 throw new Exception("Se esperaba do.");
             else
             {
                 currentToken = lex.NextToken();
                 try
                 {
                     ret.S = CodeBlock();
                 }
                 catch (Exception ex) { throw ex; }
                 return ret;
             }
         }
     }
 }
Exemplo n.º 3
0
        public Sentencia Statement()
        {
            if (currentToken.Tipo == TipoToken.TK_CHAR || currentToken.Tipo == TipoToken.TK_BOOL || currentToken.Tipo == TipoToken.TK_STRING || currentToken.Tipo == TipoToken.TK_FLOAT ||
                currentToken.Tipo == TipoToken.TK_INT || currentToken.Tipo == TipoToken.TK_PRIVATE || currentToken.Tipo == TipoToken.TK_PUBLIC || currentToken.Tipo == TipoToken.TK_CLASS || currentToken.Tipo == TipoToken.TK_VOID)
            {
                try
                {
                    return Declaraciones();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            else if (currentToken.Tipo == TipoToken.TK_PRINT)
            {
                #region Print
                currentToken = lex.NextToken();
                if (currentToken.Tipo == TipoToken.TK_OPENPAR)
                {
                    currentToken = lex.NextToken();
                    S_Print sPrint = new S_Print();
                    try
                    {
                        sPrint.Expr = Expr();
                    }
                    catch (Exception ex)
                    {

                        throw ex;
                    }

                    if (currentToken.Tipo == TipoToken.TK_CLOSEPAR)
                    {
                        currentToken = lex.NextToken();
                        if (currentToken.Tipo == TipoToken.TK_FINSENTENCIA)
                        {
                            currentToken = lex.NextToken();
                            return sPrint;
                        }
                        else
                        {
                            throw new Exception("Error Sintactico - Se esperaba simbolo ;");
                        }
                    }
                    else
                    {
                        throw new Exception("Error Sintactico - Se esperaba simbolo )");
                    }
                }
                else
                {
                    throw new Exception("Error Sintactico - Se esperaba simbolo (");
                }
                #endregion
            }
            else if (currentToken.Tipo == TipoToken.TK_IF)
            {
                #region If
                currentToken = lex.NextToken();
                if (currentToken.Tipo == TipoToken.TK_OPENPAR)
                {
                    currentToken = lex.NextToken();
                    S_If sIf = new S_If();
                    try
                    {
                        sIf.Condicion = Expr();
                    }
                    catch (Exception ex)
                    {

                        throw ex;
                    }
                    if (currentToken.Tipo == TipoToken.TK_CLOSEPAR)
                    {
                        currentToken = lex.NextToken();
                        try
                        {
                            sIf.Cierto = CompoundStatement();
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }
                        try
                        {
                            sIf.Falso = ELSE();
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }

                        return sIf;
                    }
                    else
                    {
                        throw new Exception("Error Sintactico - Se esperaba simbolo )");
                    }
                }
                else
                {
                    throw new Exception("Error Sintactico - Se esperaba simbolo (");
                }
                #endregion
            }
            else if (currentToken.Tipo == TipoToken.TK_WHILE)
            {
                #region While
                currentToken = lex.NextToken();
                if (currentToken.Tipo == TipoToken.TK_OPENPAR)
                {
                    currentToken = lex.NextToken();
                    S_While sWhile = new S_While();
                    try
                    {
                        sWhile.Condicion = Expr();
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }

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

                        return sWhile;
                    }
                    else
                    {
                        throw new Exception("Error Sintactico - Se esperaba simbolo )");
                    }
                }
                else
                {
                    throw new Exception("Error Sintactico - Se esperaba simbolo (");
                }
                #endregion
            }
            else if (currentToken.Tipo == TipoToken.TK_DO)
            {
                #region Do
                currentToken = lex.NextToken();
                S_Do sDo = new S_Do();
                try
                {
                    sDo.S = CompoundStatement();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                if (currentToken.Tipo == TipoToken.TK_WHILE)
                {
                    currentToken = lex.NextToken();
                    if (currentToken.Tipo == TipoToken.TK_OPENPAR)
                    {
                        currentToken = lex.NextToken();
                        try
                        {
                            sDo.Condicion = Expr();
                        }
                        catch (Exception ex)
                        {

                            throw ex;
                        }
                        if (currentToken.Tipo == TipoToken.TK_CLOSEPAR)
                        {
                            currentToken = lex.NextToken();
                            if (currentToken.Tipo == TipoToken.TK_FINSENTENCIA)
                            {
                                currentToken = lex.NextToken();
                                return sDo;
                            }
                            else
                            {
                                throw new Exception("Error Sintactico - Se esperaba simbolo ;");
                            }
                        }
                        else
                        {
                            throw new Exception("Error Sintactico - Se esperaba simbolo )");
                        }
                    }
                    else
                    {
                        throw new Exception("Error Sintactico - Se esperaba simbolo (");
                    }
                }
                else
                {
                    throw new Exception("Error Sintactico - Se esperaba palabra fin de ciclo While");
                }

                #endregion
            }
            else if (currentToken.Tipo == TipoToken.TK_FOR)
            {
                #region For
                currentToken = lex.NextToken();
                if (currentToken.Tipo == TipoToken.TK_OPENPAR)
                {
                    currentToken = lex.NextToken();
                    S_For sFor = new S_For();
                    if (currentToken.Tipo == TipoToken.TK_CHAR || currentToken.Tipo == TipoToken.TK_FLOAT || currentToken.Tipo == TipoToken.TK_INT)
                    {
                        try
                        {
                            sFor.Tip = Type();
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }
                    }
                    if (currentToken.Tipo == TipoToken.TK_ID)
                    {
                        sFor.Var.id = currentToken.Lexema;
                        currentToken = lex.NextToken();
                        if (currentToken.Tipo == TipoToken.TK_ASSIGN)
                        {
                            currentToken = lex.NextToken();
                            try
                            {
                                sFor.Inicio = Expr();
                            }
                            catch (Exception ex)
                            {
                                throw ex;
                            }

                            if (currentToken.Tipo == TipoToken.TK_FINSENTENCIA)
                            {
                                currentToken = lex.NextToken();
                                try
                                {
                                    sFor.Condicion = Expr();
                                }
                                catch (Exception ex)
                                {
                                    throw ex;
                                }

                                if (currentToken.Tipo == TipoToken.TK_FINSENTENCIA)
                                {
                                    currentToken = lex.NextToken();
                                    try
                                    {
                                        Expresiones e = Expr();
                                        sFor.Iteracion = Expr();
                                    }
                                    catch (Exception ex)
                                    {

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

                                        return sFor;
                                    }
                                    else
                                    {
                                        throw new Exception("Error Sintactico - Se esperaba simbolo )");
                                    }

                                }
                                else
                                {
                                    throw new Exception("Error Sintactico - Se esperaba simbolo ;");
                                }
                            }
                            else
                            {
                                throw new Exception("Error Sintactico - Se esperaba simbolo ;");
                            }
                        }
                        else
                        {
                            throw new Exception("Error Sintactico - Se esperaba simbolo asignacion =");
                        }
                    }
                    else
                    {
                        throw new Exception("Error Sintactico - Se esperaba un Identificador");
                    }
                }
                else
                {
                    throw new Exception("Error Sintactico - Se esperaba simbolo (");
                }
                #endregion
            }
            else if (currentToken.Tipo == TipoToken.TK_RETURN)
            {
                #region Return
                currentToken = lex.NextToken();
                S_Return sReturn = new S_Return();
                try
                {
                    sReturn.Expr = Expr();
                }
                catch (Exception ex)
                {
                    throw ex;
                }

                if (currentToken.Tipo == TipoToken.TK_FINSENTENCIA)
                {
                    currentToken = lex.NextToken();
                    return sReturn;
                }
                else
                {
                    throw new Exception("Error Sintactico - Se esperaba simbolo ;");
                }
                #endregion
            }
            else if (currentToken.Tipo == TipoToken.TK_BREAK)
            {
                #region Break
                currentToken = lex.NextToken();
                if (currentToken.Tipo == TipoToken.TK_FINSENTENCIA)
                {
                    S_Break sBreak = new S_Break();
                    currentToken = lex.NextToken();
                    return sBreak;
                }
                else
                {
                    throw new Exception("Error Sintactico - Se esperaba simbolo ;");
                }
                #endregion
            }
            else if (currentToken.Tipo == Lexico.TipoToken.TK_SWITCH)
            {
                #region Switch
                currentToken = lex.NextToken();
                if (currentToken.Tipo != Lexico.TipoToken.TK_OPENPAR)
                    throw new Exception("Error Sintactico - Se esperaba un (");
                currentToken = lex.NextToken();
                S_Switch sSwitch = new S_Switch();
                try
                {
                    sSwitch.Var = Expr();
                }
                catch (Exception ex)
                {
                    throw ex;
                }

                if (currentToken.Tipo != Lexico.TipoToken.TK_CLOSEPAR)
                    throw new Exception("Error Sintactico - Se esperaba una )");

                currentToken = lex.NextToken();

                if (currentToken.Tipo != Lexico.TipoToken.TK_OPENLLAVE)
                    throw new Exception("Error Sintactico - Se esperaba una {");

                currentToken = lex.NextToken();

                try
                {
                    sSwitch.Casos = Cases();
                }
                catch (Exception ex)
                {
                    throw ex;
                }

                if (currentToken.Tipo != Lexico.TipoToken.TK_DEFAULT)
                    throw new Exception("Error Sintactico - Se esperaba la palabra reservada DEFAULT");

                currentToken = lex.NextToken();
                if (currentToken.Tipo != Lexico.TipoToken.TK_DOSPUNTOS)
                    throw new Exception("Error Sintactico - Se esperaba el simbolo :");
                currentToken = lex.NextToken();

                try
                {
                    sSwitch.sdefault = StatementList();
                }
                catch (Exception ex)
                {
                    throw ex;
                }

                if (currentToken.Tipo != Lexico.TipoToken.TK_CLOSELLAVE)
                    throw new Exception("Error Sintactico - Se esperaba una }");

                currentToken = lex.NextToken();
                return sSwitch;
                #endregion
            }
            else if (currentToken.Tipo == TipoToken.TK_ID)
            {
                try
                {
                    return SentenciaASSIGN_LLAMFUNC();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            return null;
        }