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; }
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"); } }
public Expresiones ParExpr() { if (currentToken.Tipo == TipoToken.TK_ID) { Variable V = new Variable(); V.id = currentToken.Lexema; currentToken = lex.NextToken(); StatementP(V); return V; } else if (currentToken.Tipo == TipoToken.TK_INT_LIT) { LiteralEntero LE = new LiteralEntero(Convert.ToInt32(currentToken.Lexema)); currentToken = lex.NextToken(); return LE; } else if (currentToken.Tipo == TipoToken.TK_FLOAT_LIT) { LiteralFlotante LF = new LiteralFlotante(float.Parse(currentToken.Lexema)); currentToken = lex.NextToken(); return LF; } else if (currentToken.Tipo == TipoToken.TK_STRING_LIT) { LitString LS = new LitString(currentToken.Lexema); currentToken = lex.NextToken(); return LS; } else if (currentToken.Tipo == TipoToken.TK_CHAR_LIT) { LitChar LC = new LitChar(currentToken.Lexema); currentToken = lex.NextToken(); return LC; } else if (currentToken.Tipo == TipoToken.TK_TRUE) { LitBool LB = new LitBool(true); currentToken = lex.NextToken(); return LB; } else if (currentToken.Tipo == TipoToken.TK_FALSE) { LitBool LB = new LitBool(false); currentToken = lex.NextToken(); return LB; } else if (currentToken.Tipo == TipoToken.TK_OPENPAR) { currentToken = lex.NextToken(); Expresiones EX = Expr(); if (currentToken.Tipo == TipoToken.TK_CLOSEPAR) { currentToken = lex.NextToken(); return EX; } else { throw new Exception("Error Sintactico - Se esperaba simbolo )"); } } else if (currentToken.Tipo == TipoToken.TK_MASMAS || currentToken.Tipo == TipoToken.TK_MENOSMENOS) { currentToken = lex.NextToken(); } return null; }
public Sentencia StatementP(Variable id) { if (currentToken.Tipo == Lexico.TipoToken.TK_OPENPAR) { S_LlamadaFunc sfunc = new S_LlamadaFunc(); sfunc.Var=id; currentToken = lex.NextToken(); sfunc.VarList= ExpreList(); if (currentToken.Tipo == Lexico.TipoToken.TK_CLOSEPAR) { currentToken = lex.NextToken(); return sfunc; } else throw new Exception("Se esperaba el token )"); } else { Sentencia S = new Sentencia(); S=StatementP2(id); return S; } }
public Sentencia StatementP2(Variable id) { if (currentToken.Tipo == Lexico.TipoToken.TK_ASSIGN) { S_Asignacion sasign = new S_Asignacion(); sasign.Op = new Igual(); sasign.id = id; currentToken = lex.NextToken(); sasign.Valor= Expression(); return sasign; } else if(currentToken.Tipo == Lexico.TipoToken.TK_MASIGUAL) { S_Asignacion sasign = new S_Asignacion(); sasign.Op = new MasIgual(); sasign.id = id; currentToken = lex.NextToken(); sasign.Valor = Expression(); return sasign; } else if(currentToken.Tipo == Lexico.TipoToken.TK_MENOSIGUAL) { S_Asignacion sasign = new S_Asignacion(); sasign.Op = new MenosIgual(); sasign.id = id; currentToken = lex.NextToken(); sasign.Valor = Expression(); return sasign; } else if(currentToken.Tipo == Lexico.TipoToken.TK_PORIGUAL) { S_Asignacion sasign = new S_Asignacion(); sasign.Op = new PorIgual(); sasign.id = id; currentToken = lex.NextToken(); sasign.Valor = Expression(); return sasign; } else if(currentToken.Tipo == Lexico.TipoToken.TK_ENTREIGUAL) { S_Asignacion sasign = new S_Asignacion(); sasign.Op = new EntreIgual(); sasign.id = id; currentToken = lex.NextToken(); sasign.Valor = Expression(); return sasign; } else if (currentToken.Tipo == Lexico.TipoToken.TK_OPENCOR) { currentToken = lex.NextToken(); S_Asignacion assig = new S_Asignacion(); assig.id = id; assig.id.acces=Expression(); if (currentToken.Tipo != Lexico.TipoToken.TK_CLOSECOR) throw new Exception("Se esperaba el token ]"); Sentencia S = new Sentencia(); S=StatementP2(assig.id); assig.Valor = ((S_Asignacion)S).Valor; if(currentToken.Tipo != Lexico.TipoToken.TK_FINSENTENCIA) throw new Exception("Se esperaba el token ;"); currentToken = lex.NextToken(); return assig; } else if (currentToken.Tipo == Lexico.TipoToken.TK_MENOSMENOS || currentToken.Tipo == Lexico.TipoToken.TK_MASMAS) { S_Asignacion sAsignacion = new S_Asignacion(); sAsignacion.id = id; if (currentToken.Tipo == Lexico.TipoToken.TK_MASMAS) sAsignacion.Op = new MasMas(); else if (currentToken.Tipo == Lexico.TipoToken.TK_MENOSMENOS) sAsignacion.Op = new MenosMenos(); Expresiones Ex = Expression(); sAsignacion.Valor = Ex; return sAsignacion; } return null; }
public Sentencia DeclaracionesC() { try { Tipo tipo = Tipo(); if (currentToken.Tipo != Lexico.TipoToken.TK_ID) throw new Exception("Se esperaba un token ID"); Variable nombre = new Variable(currentToken.Lexema, null); Declaracion decl = new Declaracion(); decl.Tip = tipo; decl.Var = nombre; currentToken = lex.NextToken(); Sentencia s = DeclaracionesCPrima(decl); DeclOption(decl); if (s is Structs) return ((Structs)s); else if (s is S_Functions) return ((S_Functions)s); else return decl; } catch (Exception ex) { throw ex; } }
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; }
public Sentencia SentenciaAssign_LlamaFun() { try { if (currentToken.Tipo == Lexico.TipoToken.TK_ID) { Variable var = new Variable(currentToken.Lexema, null); currentToken = lex.NextToken(); if (currentToken.Tipo == Lexico.TipoToken.TK_PUNTO || currentToken.Tipo == Lexico.TipoToken.TK_OPENCOR || currentToken.Tipo == Lexico.TipoToken.TK_OPENPAR) { Access a = Accesories(var.accesor); var.accesor = a; } if (currentToken.Tipo == Lexico.TipoToken.TK_ASSIGN || currentToken.Tipo == Lexico.TipoToken.TK_MASIGUAL || currentToken.Tipo == Lexico.TipoToken.TK_MENORIGUAL || currentToken.Tipo == Lexico.TipoToken.TK_PORIGUAL || currentToken.Tipo == Lexico.TipoToken.TK_ENTREIGUAL) { S_Asignacion sAsignacion = new S_Asignacion(); if (currentToken.Tipo == Lexico.TipoToken.TK_ASSIGN) sAsignacion.Op = new Igual(); else if (currentToken.Tipo == Lexico.TipoToken.TK_MASIGUAL) sAsignacion.Op = new MasIgual(); else if (currentToken.Tipo == Lexico.TipoToken.TK_MENOSIGUAL) sAsignacion.Op = new MenosIgual(); else if (currentToken.Tipo == Lexico.TipoToken.TK_PORIGUAL) sAsignacion.Op = new PorIgual(); else if (currentToken.Tipo == Lexico.TipoToken.TK_ENTREIGUAL) sAsignacion.Op = new EntreIgual(); currentToken = lex.NextToken(); sAsignacion.id = var; sAsignacion.Valor = Expression(); return sAsignacion; } else if (currentToken.Tipo == Lexico.TipoToken.TK_ID) { Declaracion decl = new Declaracion(); Struct str = new Struct(); str.nombre = var.id; Variable vVar = new Variable(currentToken.Lexema, null); decl.Var = vVar; decl.Tip = str; currentToken = lex.NextToken(); if (currentToken.Tipo != Lexico.TipoToken.TK_FINSENTENCIA) throw new Exception("Error Sintactico --Se esperaba ;"); return decl; } else { if (currentToken.Tipo != Lexico.TipoToken.TK_FINSENTENCIA) throw new Exception("Error Sintactico --Se esperaba ;"); //if (var.accesor.Last() is AccessFunc) //{ S_LlamadaFunc sllamadafunc = new S_LlamadaFunc(); sllamadafunc.Var = var; return sllamadafunc; //} } } return null; } catch (Exception ex) { throw ex; } }
public Sentencia SentenciaASSIGN_LLAMFUNC() { if (currentToken.Tipo == TipoToken.TK_ID) { Variable var = new Variable(currentToken.Lexema, null); currentToken = lex.NextToken(); if (currentToken.Tipo == TipoToken.TK_PUNTO || currentToken.Tipo == TipoToken.TK_OPENCOR) { try { Access a=Accesories(var.accesor); var.accesor = a; } catch (Exception ex) { throw ex; } } if (currentToken.Tipo == TipoToken.TK_ASSIGN || currentToken.Tipo == TipoToken.TK_MASIGUAL || currentToken.Tipo == TipoToken.TK_MENOSIGUAL || currentToken.Tipo == TipoToken.TK_PORIGUAL || currentToken.Tipo == TipoToken.TK_ENTREIGUAL) { S_Asignacion sAssig = new S_Asignacion(); if (currentToken.Tipo == TipoToken.TK_ASSIGN) sAssig.Op = new Igual(); else if (currentToken.Tipo == TipoToken.TK_MASIGUAL) sAssig.Op = new MasIgual(); else if (currentToken.Tipo == TipoToken.TK_MENOSIGUAL) sAssig.Op = new MenosIgual(); else if (currentToken.Tipo == TipoToken.TK_PORIGUAL) sAssig.Op = new PorIgual(); else if (currentToken.Tipo == TipoToken.TK_ENTREIGUAL) sAssig.Op = new EntreIgual(); currentToken = lex.NextToken(); sAssig.id = var; try { sAssig.Valor = Expr(); } catch (Exception ex) { throw ex; } if (currentToken.Tipo != TipoToken.TK_FINSENTENCIA) throw new Exception("Error Sintactico - Se esperaba fin sentencia"); currentToken = lex.NextToken(); return sAssig; } else if (currentToken.Tipo == TipoToken.TK_ID) { Declaracion Decl = new Declaracion(); Class TipClass = new Class(); TipClass.Nombre = var.id; Variable vVar = new Variable(currentToken.Lexema, null); Decl.Var = vVar; Decl.Tip = TipClass; currentToken = lex.NextToken(); if (currentToken.Tipo != TipoToken.TK_FINSENTENCIA) throw new Exception("Error Sintactico - Se esperaba fin sentencia"); currentToken = lex.NextToken(); return Decl; } else if(currentToken.Tipo == TipoToken.TK_OPENPAR) { currentToken = lex.NextToken(); S_LlamadaFunc sLlamadaFunc = new S_LlamadaFunc(); sLlamadaFunc.Var = var; if (currentToken.Tipo == TipoToken.TK_CLOSEPAR) { currentToken = lex.NextToken(); if (currentToken.Tipo != TipoToken.TK_FINSENTENCIA) throw new Exception("Error Sintactico - Se esperaba fin sentencia"); currentToken = lex.NextToken(); return sLlamadaFunc; } else { //VERIFICAR VIENE UN LITERAL O VARIABLE Y AGREGARLO LUEGO LLAMAR EXPRLIST PARA QUE AGREGUE LO DEMAS Y VERIFICAR CLOSEPAR if (currentToken.Tipo == TipoToken.TK_ID || currentToken.Tipo == TipoToken.TK_INT_LIT || currentToken.Tipo == TipoToken.TK_FLOAT_LIT || currentToken.Tipo == TipoToken.TK_STRING_LIT || currentToken.Tipo == TipoToken.TK_CHAR_LIT) { ListaExpre listaExpre = new ListaExpre(); try { listaExpre.Ex.Add(Expr()); } catch(Exception ex) { throw ex; } if (currentToken.Tipo == TipoToken.TK_COMA) { try { sLlamadaFunc.Variables = ExprList(listaExpre); } catch(Exception ex) { throw ex; } if (currentToken.Tipo == TipoToken.TK_CLOSEPAR) { currentToken = lex.NextToken(); if (currentToken.Tipo != TipoToken.TK_FINSENTENCIA) throw new Exception("Error Sintactico - Se esperaba fin sentencia"); currentToken = lex.NextToken(); return sLlamadaFunc; } else { throw new Exception("Error Sintatico - Se esperaba simbolo )"); } } else { sLlamadaFunc.Variables = listaExpre; if (currentToken.Tipo == TipoToken.TK_CLOSEPAR) { currentToken = lex.NextToken(); if (currentToken.Tipo != TipoToken.TK_FINSENTENCIA) throw new Exception("Error Sintactico - Se esperaba fin sentencia"); currentToken = lex.NextToken(); return sLlamadaFunc; } else { throw new Exception("Error Sintatico - Se esperaba simbolo )"); } } } } } else if (currentToken.Tipo == TipoToken.TK_MASMAS) { currentToken = lex.NextToken(); s_masmas sMasmas = new s_masmas(); ExpMasMas expMasMas = new ExpMasMas(); expMasMas.ID = var; sMasmas.param = expMasMas; return sMasmas; } else if (currentToken.Tipo == TipoToken.TK_MENOSMENOS) { currentToken = lex.NextToken(); s_menosmenos sMenosmenos = new s_menosmenos(); ExpMenosMenos expMenosMenos = new ExpMenosMenos(); expMenosMenos.ID = var; sMenosmenos.param = expMenosMenos; return sMenosmenos; } else { if (currentToken.Tipo != TipoToken.TK_FINSENTENCIA) throw new Exception("Error Sintactico - Se esperaba fin sentencia"); currentToken = lex.NextToken(); if (var.accesor.Last() is AccessFunc) { S_LlamadaFunc sLlamadaFunc = new S_LlamadaFunc(); sLlamadaFunc.Var = var; return sLlamadaFunc; } } } return null; }
public Declaracion FParams() { try { Declaracion d = new Declaracion(); if (currentToken.Tipo == Lexico.TipoToken.TK_COMA) { currentToken = lex.NextToken(); //Declaracion decl = new Declaracion(); Tipo tipo = Tipo(); if (tipo == null) return d; if (currentToken.Tipo != Lexico.TipoToken.TK_ID) throw new Exception("Se esperaba el token ID"); Variable nombre = new Variable(currentToken.Lexema, null); currentToken = lex.NextToken(); d.Tip = tipo; d.Var = nombre; d.Sig = FParams(); return d; } return null; } catch(Exception ex) { throw ex; } }
/*Lista de Parametros*/ public Declaracion FuncionesParams() { try { Declaracion decl = new Declaracion(); Tipo tipo = Tipo(); if (tipo == null) return decl; if (currentToken.Tipo != Lexico.TipoToken.TK_ID) throw new Exception("Se esperaba el token ID"); Variable nombre = new Variable(currentToken.Lexema, null); nombre.flag = 'F'; currentToken = lex.NextToken(); decl.Tip = tipo; decl.Var = nombre; decl.Sig = FParams(); return decl; } catch (Exception ex) { throw ex; } }
/*Aqui se efectua la recursividad del ,id,id,id*/ public Declaracion DeclCPrima(Declaracion decl) { try { 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; } return decl; } catch (Exception ex) { throw ex; } }
public Declaracion DeclarsCstruct(Declaracion decl) { try { 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.Var = nombre; decl.Sig.Tip = decl.Tip; DeclarsCstruct(decl.Sig); return decl; } 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.Rangos.Add(E1); tipoArreglo.Contenido = decl.Tip; decl.Tip = tipoArreglo; DeclarsCstruct(decl.Sig); return decl; } return decl; } catch (Exception ex) { throw ex; } }
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; } }
public Sentencia StatementP(Variable Id) { if (currentToken.Tipo == TipoToken.TK_OPENPAR) { S_LlamadaFunc sLlamadaFunc = new S_LlamadaFunc(); sLlamadaFunc.Var = Id; currentToken = lex.NextToken(); sLlamadaFunc.VarList = ExprList(); if (currentToken.Tipo == TipoToken.TK_CLOSEPAR) { currentToken = lex.NextToken(); return sLlamadaFunc; } else { throw new Exception("Error Sintactico - Falta simbolo )"); } } else { Sentencia S =new Sentencia(); S = StatementP2(Id); return S; } }
public Expresiones Parexp() { if (currentToken.Tipo == Lexico.TipoToken.TK_OPENPAR) { currentToken = lex.NextToken(); Expression(); if (currentToken.Tipo != Lexico.TipoToken.TK_CLOSEPAR) throw new Exception("Se esperaba un )"); currentToken = lex.NextToken(); } else if (currentToken.Tipo == Lexico.TipoToken.TK_INT_LIT) { LiteralEntero lit = new LiteralEntero(Convert.ToInt32(currentToken.Lexema)); currentToken = lex.NextToken(); return lit; } else if (currentToken.Tipo == Lexico.TipoToken.TK_FLOAT_LIT) { LiteralFlotante lit = new LiteralFlotante(float.Parse(currentToken.Lexema)); currentToken = lex.NextToken(); return lit; } else if (currentToken.Tipo == Lexico.TipoToken.TK_CHAR_LIT) { LitChar lit = new LitChar(currentToken.Lexema); currentToken = lex.NextToken(); return lit; } else if (currentToken.Tipo == Lexico.TipoToken.TK_STRING_LIT) { LitString lit = new LitString(currentToken.Lexema); currentToken = lex.NextToken(); return lit; } else if (currentToken.Tipo == Lexico.TipoToken.TK_ID) { Variable v = new Variable(); v.id = currentToken.Lexema; currentToken = lex.NextToken(); StatementP(v); return v; } else if (currentToken.Tipo == Lexico.TipoToken.TK_TRUE) { LitBool lit = new LitBool(true); currentToken = lex.NextToken(); return lit; } else if (currentToken.Tipo == Lexico.TipoToken.TK_FALSE) { LitBool lit = new LitBool(false); currentToken = lex.NextToken(); return lit; } else if (currentToken.Tipo == Lexico.TipoToken.TK_MASMAS) { currentToken = lex.NextToken(); } else if (currentToken.Tipo == Lexico.TipoToken.TK_MENOSMENOS) { currentToken = lex.NextToken(); } return null; }
public Sentencia StatementP2(Variable Id) { if (currentToken.Tipo == TipoToken.TK_ASSIGN || currentToken.Tipo == TipoToken.TK_MASIGUAL || currentToken.Tipo == TipoToken.TK_MENOSIGUAL || currentToken.Tipo == TipoToken.TK_PORIGUAL || currentToken.Tipo == TipoToken.TK_ENTREIGUAL) { var Tip = currentToken.Tipo; currentToken = lex.NextToken(); if (currentToken.Tipo == TipoToken.TK_READ) { S_Read sRead = new S_Read(); sRead.var = Id; #region Read currentToken = lex.NextToken(); if (currentToken.Tipo == TipoToken.TK_OPENPAR) { currentToken = lex.NextToken(); if (currentToken.Tipo == TipoToken.TK_CLOSEPAR) { currentToken = lex.NextToken(); if (currentToken.Tipo == TipoToken.TK_FINSENTENCIA) { currentToken = lex.NextToken(); return sRead; } 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 { Expresiones E = Expr(); S_Asignacion sAsignacion = new S_Asignacion(); sAsignacion.id = Id; if (Tip == TipoToken.TK_ASSIGN) sAsignacion.Op = new Igual(); else if (Tip == TipoToken.TK_MASIGUAL) sAsignacion.Op = new MasIgual(); else if (Tip == TipoToken.TK_MENOSIGUAL) sAsignacion.Op = new MenosIgual(); else if (Tip == TipoToken.TK_PORIGUAL) sAsignacion.Op = new PorIgual(); else if (Tip == TipoToken.TK_ENTREIGUAL) sAsignacion.Op = new EntreIgual(); sAsignacion.Valor = E; return sAsignacion; } } else if (currentToken.Tipo == TipoToken.TK_OPENCOR) { currentToken = lex.NextToken(); S_Asignacion sAsignacion = new S_Asignacion(); sAsignacion.id = Id; sAsignacion.id.acces = Expr(); if (currentToken.Tipo == TipoToken.TK_CLOSECOR) { currentToken = lex.NextToken(); Sentencia S = new Sentencia(); S = StatementP2(sAsignacion.id); sAsignacion.Valor = ((S_Asignacion) S).Valor; /*if (currentToken.Tipo == TipoToken.TK_FINSENTENCIA) { currentToken = lex.NextToken(); return sAsignacion; }*/ return sAsignacion; /*else { throw new Exception("Error Sintactico - Se esperaba simbolo ;"); }*/ } else { throw new Exception("Error Sintactico - Se esperaba simbolo ]"); } } else if (currentToken.Tipo == TipoToken.TK_MASMAS || currentToken.Tipo == TipoToken.TK_MENOSMENOS) { S_Asignacion sAsignacion = new S_Asignacion(); sAsignacion.id = Id; if (currentToken.Tipo == TipoToken.TK_MASMAS) sAsignacion.Op = new MasMas(); else if (currentToken.Tipo == TipoToken.TK_MENOSMENOS) sAsignacion.Op = new MenosMenos(); Expresiones Ex = Expr(); sAsignacion.Valor = Ex; return sAsignacion; } else { return null; } }
public Declaracion StructDeclaration() { try { if (currentToken.Tipo == Lexico.TipoToken.TK_ID) { Struct t = new Struct(); t.nombre = currentToken.Lexema; currentToken = lex.NextToken(); if (currentToken.Tipo != Lexico.TipoToken.TK_ID) throw new Exception("Se esperaba el token Id"); Variable nombre = new Variable(currentToken.Lexema, null); Declaracion decl = new Declaracion(); decl.Tip = t; decl.Var = nombre; currentToken = lex.NextToken(); DeclarsCstruct(decl); if (currentToken.Tipo != Lexico.TipoToken.TK_FINSENTENCIA) throw new Exception("Se esperaba el token ;"); currentToken = lex.NextToken(); return decl; } else if (currentToken.Tipo == Lexico.TipoToken.TK_INT || currentToken.Tipo == Lexico.TipoToken.TK_FLOAT || currentToken.Tipo == Lexico.TipoToken.TK_CHAR) { Tipo t = Tipo(); if (currentToken.Tipo != Lexico.TipoToken.TK_ID) throw new Exception("Se esperaba el token Id"); Variable name = new Variable(currentToken.Lexema, null); currentToken = lex.NextToken(); Declaracion decl = new Declaracion(); decl.Tip = t; decl.Var = name; DeclarsCstruct(decl); if (currentToken.Tipo != Lexico.TipoToken.TK_FINSENTENCIA) throw new Exception("Se esperaba el token ;"); currentToken = lex.NextToken(); return decl; } return null; } catch (Exception ex) { throw ex; } }