int Anasin::Condicion(){ // 17) Condicion -> Expresion relational Expresion if(!Expresion()) return 0; if(!ParseToken(Lexema("Relational",""),false)) return 0; if(!Expresion()) return 0; return 1; }
ExpressionNode* Parser::Factors() { if(currenttoken->type == Id) return Variable(); else if(currenttoken->type == OpenParenthesis){ ConsumeToken(); ExpressionNode * value = Expresion(); if(currenttoken->type != CloseParenthesis) throw invalid_argument("Se esperaba ). Row:"+to_string(currenttoken->row)+"Column:"+to_string(currenttoken->column)); ConsumeToken(); return value; } else if(IsLiteral(currenttoken)) { return Literal(); } else if(currenttoken->type == Verdadero) { ConsumeToken(); return new ConstantBooleanNode(true); } else if(currenttoken->type == Falso) { ConsumeToken(); return new ConstantBooleanNode(false); } }
vector<ExpressionNode*> Parser::ExpressionListNotNull() { if(currenttoken->type == CloseBracket) throw invalid_argument("Se esperaban dimensiones. Row:"+to_string(currenttoken->row)+"Column:"+to_string(currenttoken->column)); ExpressionNode* expression=Expresion(); vector<ExpressionNode*> list=ExpresionListNotNullPrime(); list.insert(list.begin(),expression); return list; }
ExpressionNode *Parser::ConditionalExpresion(ExpressionNode * param) { if(currenttoken->type == Or) { ConsumeToken(); ExpressionNode* expression=Expresion(); return new OrNode(param,expression); } else if(currenttoken->type == And) { ConsumeToken(); ExpressionNode* expression=Expresion(); return new AndNode(param,expression); } else { return param; } }
int Anasin::Operacion(){ Lexema nextLexema = this->_symTab->lookAheadLexema(); Lexema nextnextLexema = this->_symTab->lookAheadLexema(1); if(nextLexema._tipo == "Id"){ if(nextnextLexema._tipo == "Agrupation" && nextnextLexema._valor == "["){ // 26) Operacion -> Aarr=Expresion; if(!Aarr()) return 0; if(!ParseToken(Lexema("Asignment",""),false)) return 0; if(!Expresion()) return 0; if(!ParseToken(Lexema("Punctuation",";"),true)) return 0; }else{ // 25) Operacion -> id=Expresion; if(!ParseToken(Lexema("Id",""),false)) return 0; if(!ParseToken(Lexema("Asignment",""),false)) return 0; if(!Expresion()) return 0; if(!ParseToken(Lexema("Punctuation",";"),true)) return 0; } }else{ std::cout << "Error sintáctico, se esperaba : Id y se obtuvo: " << nextLexema._tipo << " ("<< nextLexema._valor <<")["<< this->_symTab->getOffset() <<"]" << std::endl; return 0; } return 1; }
vector<ExpressionNode*> Parser::ExpresionListNull() { if(currenttoken->type != CloseParenthesis) { ExpressionNode* expression=Expresion(); vector<ExpressionNode*> list=ExpresionListNotNullPrime(); list.insert(list.begin(),expression); return list; } else{ vector<ExpressionNode*> list; return list; } }
vector<ExpressionNode*> Parser::ExpresionListNotNullPrime() { if(currenttoken->type == Coma) { ConsumeToken(); ExpressionNode* expression=Expresion(); vector<ExpressionNode*> list=ExpresionListNotNullPrime(); list.insert(list.begin(),expression); return list; } else { vector<ExpressionNode*> list; return list; } }
int Anasin::Operador(){ Lexema nextLexema = this->_symTab->lookAheadLexema(); if(nextLexema._tipo == "Arithmetic"){ // 27) Operador -> arithmetic Expresion if(!ParseToken(Lexema("Arithmetic",""),false)) return 0; if(!Expresion()) return 0; }else if(nextLexema._tipo == "Relational" || (nextLexema._tipo == "Agrupation" && nextLexema._valor == ")") || (nextLexema._tipo == "Punctuation" && nextLexema._valor == ";")){ // 28) Operador -> € // No hago nada }else{ std::cout << "Error sintáctico, se esperaba : Arithmetic, Relational, ) o ; y se obtuvo: " << nextLexema._tipo << " ("<< nextLexema._valor <<")["<< this->_symTab->getOffset() <<"]" << std::endl; return 0; } return 1; }
StatementNode* Parser::Instruction() { if(currenttoken->type == Si) { ConsumeToken(); ExpressionNode* condition=Expresion(); if(currenttoken->type != Entonces) throw invalid_argument("Se esperaba palabra reservada entonces. Row:"+to_string(currenttoken->row)+"Column:"+to_string(currenttoken->column)); ConsumeToken(); vector<StatementNode*> codeTrue=InstructionList(); vector<StatementNode*> codeFalse=SiInstructionPrime(); return new SiNode(condition,codeTrue,codeFalse); } else if(currenttoken->type == Para) { ConsumeToken(); VariableNode * id=Variable(); if(currenttoken->type != ValueAllocation) throw invalid_argument("Se esperaba <-. Row:"+to_string(currenttoken->row)+"Column:"+to_string(currenttoken->column)); ConsumeToken(); ExpressionNode* value=Expresion(); if(currenttoken->type != Hasta) throw invalid_argument("Se esperaba palabra reservada hasta. Row:"+to_string(currenttoken->row)+"Column:"+to_string(currenttoken->column)); ConsumeToken(); ExpressionNode* condition = Expresion(); if(currenttoken->type != Haga) throw invalid_argument("Se esperaba palabra reservada haga. Row:"+to_string(currenttoken->row)+"Column:"+to_string(currenttoken->column)); ConsumeToken(); vector<StatementNode*> code =InstructionList(); if(currenttoken->type != Fin) throw invalid_argument("Se esperaba palabra reservada fin. Row:"+to_string(currenttoken->row)+"Column:"+to_string(currenttoken->column)); ConsumeToken(); if(currenttoken->type != Para) throw invalid_argument("Se esperaba palabra reservada para. Row:"+to_string(currenttoken->row)+"Column:"+to_string(currenttoken->column)); ConsumeToken(); return new ParaNode(new AssignmentNode(id,value),condition,code); } else if(currenttoken->type == Id) { VariableNode* id=Variable(); if(currenttoken->type != ValueAllocation) throw invalid_argument("Se esperaba <-. Row:"+to_string(currenttoken->row)+"Column:"+to_string(currenttoken->column)); ConsumeToken(); ExpressionNode* expression=Expresion(); return new AssignmentNode(id,expression); } else if(currenttoken->type == Escriba) { ConsumeToken(); vector<ExpressionNode*> exprList=ExpressionListNotNull(); return new EscribaNode(exprList); } else if(currenttoken->type == Mientras) { ConsumeToken(); ExpressionNode * condition=Expresion(); if(currenttoken->type != Haga) throw invalid_argument("Se esperaba palabra reservada haga. Row:"+to_string(currenttoken->row)+"Column:"+to_string(currenttoken->column)); ConsumeToken(); vector<StatementNode*>code=InstructionList(); if(currenttoken->type != Fin) throw invalid_argument("Se esperaba palabra reservada fin. Row:"+to_string(currenttoken->row)+"Column:"+to_string(currenttoken->column)); ConsumeToken(); if(currenttoken->type != Mientras) throw invalid_argument("Se esperaba palabra reservada mientras. Row:"+to_string(currenttoken->row)+"Column:"+to_string(currenttoken->column)); ConsumeToken(); return new MientrasNode(condition,code); } else if(currenttoken->type == Repita) { ConsumeToken(); vector<StatementNode*> code=InstructionList(); if(currenttoken->type != Hasta) throw invalid_argument("Se esperaba palabra reservada hasta. Row:"+to_string(currenttoken->row)+"Column:"+to_string(currenttoken->column)); ConsumeToken(); ExpressionNode*condition=Expresion(); return new RepitaNode(code,condition); } else if(currenttoken->type == Caso) { ConsumeToken(); VariableNode * id=Variable(); vector<CasoLineNode*> cases=CasoList(); vector<StatementNode*> defaultcase=CasoSino(); if(currenttoken->type != Fin) throw invalid_argument("Se esperaba palabra reservada fin. Row:"+to_string(currenttoken->row)+"Column:"+to_string(currenttoken->column)); ConsumeToken(); if(currenttoken->type != Caso) throw invalid_argument("Se esperaba palabra reservada caso. Row:"+to_string(currenttoken->row)+"Column:"+to_string(currenttoken->column)); ConsumeToken(); return new CasoNode(id,cases,defaultcase); } else if(currenttoken->type == Abrir) { ConsumeToken(); string archiveId=Archive(); if(currenttoken->type != Como) throw invalid_argument("Se esperaba palabra reservada como. Row:"+to_string(currenttoken->row)+"Column:"+to_string(currenttoken->column)); ConsumeToken(); VariableNode* archiveType=Variable(); vector<ModeNode*> modes=ModeList(); return new AbrirArchivoNode(archiveId,archiveType,modes); } else if(currenttoken->type == Cerrar) { ConsumeToken(); VariableNode* id= Variable(); return new CerrarArchivoNode(id); } else if(currenttoken->type == Leer) { ConsumeToken(); VariableNode* archive=Variable(); if(currenttoken->type != Coma) throw invalid_argument("Se esperaba ,. Row:"+to_string(currenttoken->row)+"Column:"+to_string(currenttoken->column)); ConsumeToken(); VariableNode* data=Variable(); return new LeerArchivoNode(archive,data); } else if(currenttoken->type == Escribir) { ConsumeToken(); VariableNode* archive=Variable(); if(currenttoken->type != Coma) throw invalid_argument("Se esperaba ,. Row:"+to_string(currenttoken->row)+"Column:"+to_string(currenttoken->column)); ConsumeToken(); VariableNode* data=Variable(); return new EscribirArchivoNode(archive,data); } else if(currenttoken->type == Llamar) { ConsumeToken(); if(currenttoken->type != Id) throw invalid_argument("Se esperaba un id. Row:"+to_string(currenttoken->row)+"Column:"+to_string(currenttoken->column)); string id=currenttoken->lexeme; ConsumeToken(); vector<ExpressionNode*> parameters=Parameters(); return new LlamarNode(id,parameters); } else if(currenttoken->type == Retorne) { ConsumeToken(); ExpressionNode* id=Expresion(); return new RetorneNode(id); } }