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; } }
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; } }
public Sentencia DeclaracionesClase() { Declaracion Decl = new Declaracion(); VARTYPE(); try { Class c = new Class(); c.Nombre = currentToken.Lexema; c.Campos = new T_Campos(); Decl.Tip = c; currentToken = lex.NextToken(); } catch (Exception ex) { throw ex; } if (currentToken.Tipo == TipoToken.TK_OPENCOR) //ARREGLO { int dim = arrayDimensions(1); Arreglo ArrTip = new Arreglo(); ArrTip.Contenido = Decl.Tip; ArrTip.Dimensiones = dim; Decl.Tip = ArrTip; if (currentToken.Tipo == TipoToken.TK_ID) { Decl.Var.id = currentToken.Lexema; currentToken = lex.NextToken(); if (currentToken.Tipo == TipoToken.TK_FINSENTENCIA) { currentToken = lex.NextToken(); return Decl; } try { Decl = DeclOption(Decl); } catch (Exception ex) { throw ex; } if (currentToken.Tipo != TipoToken.TK_FINSENTENCIA) { currentToken = lex.NextToken(); return Decl; } else { throw new Exception("Error Sintactico - Se esperaba Fin Sentencia"); } } else { throw new Exception("Error Sintactico - Se esperaba un ID"); } } else if (currentToken.Tipo == TipoToken.TK_ID) { Decl.Var.id = currentToken.Lexema; currentToken = lex.NextToken(); if (currentToken.Tipo == TipoToken.TK_COMA || currentToken.Tipo == TipoToken.TK_ASSIGN) { try { DeclaracionesVarias(Decl); DeclOption(Decl); } catch (Exception ex) { throw ex; } } else if (currentToken.Tipo == TipoToken.TK_OPENPAR) { currentToken = lex.NextToken(); S_Functions sFunctions = new S_Functions(); sFunctions.Retorno = Decl.Tip; sFunctions.Var = Decl.Var.id; try { sFunctions.Campo = ParameterList(); } catch (Exception ex) { throw ex; } if (currentToken.Tipo == TipoToken.TK_CLOSEPAR) { currentToken = lex.NextToken(); try { sFunctions.S = CompoundStatement(); } catch (Exception ex) { throw ex; } return sFunctions; } else { throw new Exception("Error Sintactico - Se esperaba simbolo )"); } } else if (currentToken.Tipo == TipoToken.TK_OPENLLAVE) { currentToken = lex.NextToken(); S_Class sClass = new S_Class(); sClass.Var.id = Decl.Var.id; try { sClass.CamposClase = ListaDeclaracion(sClass.CamposClase); } catch (Exception ex) { throw ex; } if (currentToken.Tipo != TipoToken.TK_CLOSELLAVE) throw new Exception("Error Sintactico - Se esperaba simbolo }"); currentToken = lex.NextToken(); return sClass; } else if (currentToken.Tipo == TipoToken.TK_FINSENTENCIA) { currentToken = lex.NextToken(); return Decl; } else throw new Exception("Error Sintactico - Se esperaba simbolo ;"); } return Decl; }