예제 #1
0
        public Sentencia Statement()
        {
            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();
                    sPrint.Expr = Expr();
                    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();
                    sIf.Condicion = Expr();
                    if (currentToken.Tipo == TipoToken.TK_CLOSEPAR)
                    {
                        currentToken = lex.NextToken();
                        sIf.Cierto = CompoundStatement();
                        sIf.Falso = ELSE();
                        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();
                    sWhile.Condicion = Expr();
                    if (currentToken.Tipo == TipoToken.TK_CLOSEPAR)
                    {
                        currentToken = lex.NextToken();
                        sWhile.S = CompoundStatement();
                        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();
                sDo.S = CompoundStatement();
                if (currentToken.Tipo == TipoToken.TK_WHILE)
                {
                    currentToken = lex.NextToken();
                    if (currentToken.Tipo == TipoToken.TK_OPENPAR)
                    {
                        currentToken = lex.NextToken();
                        sDo.Condicion = Expr();
                        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)
                    {
                        sFor.Tip = Type();
                    }
                    if (currentToken.Tipo == TipoToken.TK_ID)
                    {
                        sFor.Var.id = currentToken.Lexema;
                        currentToken = lex.NextToken();
                        if (currentToken.Tipo == TipoToken.TK_ASSIGN)
                        {
                            currentToken = lex.NextToken();
                            sFor.Inicio = Expr();
                            if (currentToken.Tipo == TipoToken.TK_FINSENTENCIA)
                            {
                                currentToken = lex.NextToken();
                                sFor.Condicion = Expr();
                                if (currentToken.Tipo == TipoToken.TK_FINSENTENCIA)
                                {
                                    currentToken = lex.NextToken();
                                    sFor.Iteracion = Expr();
                                    if (currentToken.Tipo == TipoToken.TK_CLOSEPAR)
                                    {
                                        currentToken = lex.NextToken();
                                        sFor.S = CompoundStatement();
                                        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();
                sReturn.Expr = Expr();
                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 == TipoToken.TK_ID)
            {
                #region Id
                Sentencia S = new Sentencia();
                Variable Id = new Variable();
                Id.id = currentToken.Lexema;
                currentToken = lex.NextToken();

                S = StatementP(Id);
                if (currentToken.Tipo == TipoToken.TK_FINSENTENCIA)
                {
                    currentToken = lex.NextToken();
                    return S;
                }
                else
                {
                    throw new Exception("Error Sintactico - Se esperaba simbolo ;");
                }

                #endregion
            }
            else 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)
            {
                #region Declaraciones
                Sentencia S = new Sentencia();
                S = Declaration();
                return S;
                #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();
                sSwitch.Var = Expr();

                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();

                sSwitch.Casos = Cases();

                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();
                sSwitch.sdefault = StatementList();

                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_CLASS)
            {
                return Clases();
            }
            return null;
        }
예제 #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 Statement()
        {
            if (this.currentToken.Tipo == Lexico.TipoToken.TK_PRINT)
            {
                this.currentToken = lex.NextToken();
                if (currentToken.Tipo != Lexico.TipoToken.TK_OPENPAR)
                    throw new Exception("Se esperaba un (");
                S_Print sprint = new S_Print();
                sprint.Expr = Expression();

                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;

            }
            else if (this.currentToken.Tipo == Lexico.TipoToken.TK_READ)
            {
                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;

            }
            else if (this.currentToken.Tipo == Lexico.TipoToken.TK_IF)
            {
                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;

            }
            else if (this.currentToken.Tipo == Lexico.TipoToken.TK_WHILE)
            {
                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;
            }
            else if (this.currentToken.Tipo == Lexico.TipoToken.TK_DO)
            {
                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");

            }
            else if (this.currentToken.Tipo == Lexico.TipoToken.TK_FOR)
            {
                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;
            }
            else if (this.currentToken.Tipo == Lexico.TipoToken.TK_RETURN)
            {
                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;
            }
            else if (this.currentToken.Tipo == Lexico.TipoToken.TK_BREAK)
            {
                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;
            }
            else if (this.currentToken.Tipo == Lexico.TipoToken.TK_SWITCH)
            {
                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;

            }
            else if (currentToken.Tipo == Lexico.TipoToken.TK_ID)
            {
                Sentencia S = new Sentencia();
                Variable Id = new Variable();
                Id.id= currentToken.Lexema;
                currentToken = lex.NextToken();
                S=StatementP(Id);
                if (currentToken.Tipo != Lexico.TipoToken.TK_FINSENTENCIA)
                    throw new Exception("Se esperaba el simbolo ;");
                currentToken = lex.NextToken();
                return S;
            }

            else if (currentToken.Tipo == Lexico.TipoToken.TK_PUBLIC || currentToken.Tipo == Lexico.TipoToken.TK_PRIVATE)
            {
                currentToken = lex.NextToken();

                Sentencia s = new Sentencia();
                s=Declaration();
                return s;

            }
            else if (currentToken.Tipo == Lexico.TipoToken.TK_INT || currentToken.Tipo == Lexico.TipoToken.TK_FLOAT || currentToken.Tipo == Lexico.TipoToken.TK_CHAR ||
                    currentToken.Tipo == Lexico.TipoToken.TK_TRUE || currentToken.Tipo == Lexico.TipoToken.TK_FALSE)
            {

                Sentencia s = new Sentencia();
                s = Declaration();
                return s;
            }
            return null;
        }
예제 #4
0
        Sentencia CompoundStatement()
        {
            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();

                    case TipoToken.TK_ID:
                        return parseAssignOrCall();

                    #region read/print/break/continue/return
                    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;
                            }
                        }

                    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; }
        }
예제 #5
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.Tip = new Entero();
                    sfor.S = CompoundStatement();
                    sfor.Tip = new Entero();
                    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;
            }
        }