diff --git a/parser.src/ComputingTasks.cc b/parser.src/ComputingTasks.cc index 172078c9f..13a8fb5cb 100644 --- a/parser.src/ComputingTasks.cc +++ b/parser.src/ComputingTasks.cc @@ -127,7 +127,9 @@ RamseyPolicyStatement::checkPass(ModFileStructure &mod_file_struct) { mod_file_struct.stoch_simul_or_similar_present = true; - // Fill in option_order of mod_file_struct + /* Fill in option_order of mod_file_struct + Since ramsey policy needs one further order of derivation (for example, for 1st order + approximation, it needs 2nd derivatives), we add 1 to the order declared by user */ OptionsList::num_options_type::const_iterator it = options_list.num_options.find("order"); if (it != options_list.num_options.end()) mod_file_struct.order_option = atoi(it->second.c_str()) + 1; diff --git a/parser.src/DataTree.cc b/parser.src/DataTree.cc index 1e21ba09e..d887eb974 100644 --- a/parser.src/DataTree.cc +++ b/parser.src/DataTree.cc @@ -333,11 +333,11 @@ DataTree::AddLocalParameter(const string &name, NodeID value) throw (LocalParame int id = symbol_table.getID(name); // Throw an exception if symbol already declared - map::iterator it = local_parameters_table.find(id); - if (it != local_parameters_table.end()) + map::iterator it = local_variables_table.find(id); + if (it != local_variables_table.end()) throw LocalParameterException(name); - local_parameters_table[id] = value; + local_variables_table[id] = value; } NodeID diff --git a/parser.src/DynareBison.cc b/parser.src/DynareBison.cc index c7d6c71c1..f7275d4a9 100644 --- a/parser.src/DynareBison.cc +++ b/parser.src/DynareBison.cc @@ -889,7 +889,7 @@ namespace yy case 159: #line 421 "DynareBison.yy" - {driver.declare_and_init_local_parameter((yysemantic_stack_[(5) - (2)].string_val), (yysemantic_stack_[(5) - (4)].node_val));;} + {driver.declare_and_init_model_local_variable((yysemantic_stack_[(5) - (2)].string_val), (yysemantic_stack_[(5) - (4)].node_val));;} break; case 160: diff --git a/parser.src/DynareBison.yy b/parser.src/DynareBison.yy index ceee068c8..bb561d86f 100644 --- a/parser.src/DynareBison.yy +++ b/parser.src/DynareBison.yy @@ -418,7 +418,7 @@ cutoff ; pound_expression: '#' NAME EQUAL hand_side ';' - {driver.declare_and_init_local_parameter($2, $4);} + {driver.declare_and_init_model_local_variable($2, $4);} model_var : NAME diff --git a/parser.src/DynareFlex.ll b/parser.src/DynareFlex.ll index 5218d277b..e14a2986d 100644 --- a/parser.src/DynareFlex.ll +++ b/parser.src/DynareFlex.ll @@ -262,11 +262,12 @@ int sigma_e = 0; return token::INT_NUMBER; } - /* an instruction starting with a recognized symbol is passed as NAME, + /* an instruction starting with a recognized symbol (which is not a modfile local variable) + is passed as NAME, otherwise it is a native statement until the end of the line */ [A-Za-z_][A-Za-z0-9_]* { - if (driver.exists_symbol(yytext)) + if (driver.symbol_exists_and_is_not_modfile_local_variable(yytext)) { BEGIN DYNARE_STATEMENT; yylval->string_val = new string(yytext); diff --git a/parser.src/ExprNode.cc b/parser.src/ExprNode.cc index 4f0c20a8d..9b808e657 100644 --- a/parser.src/ExprNode.cc +++ b/parser.src/ExprNode.cc @@ -162,9 +162,12 @@ VariableNode::VariableNode(DataTree &datatree_arg, int symb_id_arg, Type type_ar case eParameter: // All derivatives are null, do nothing break; - case eLocalParameter: + case eModelLocalVariable: // Non null derivatives are those of the value of the local parameter - non_null_derivatives = datatree.local_parameters_table[symb_id]->non_null_derivatives; + non_null_derivatives = datatree.local_variables_table[symb_id]->non_null_derivatives; + break; + case eModFileLocalVariable: + // Such a variable is never derived break; } } @@ -184,8 +187,11 @@ VariableNode::computeDerivative(int varID) return datatree.Zero; case eParameter: return datatree.Zero; - case eLocalParameter: - return datatree.local_parameters_table[symb_id]->getDerivative(varID); + case eModelLocalVariable: + return datatree.local_variables_table[symb_id]->getDerivative(varID); + case eModFileLocalVariable: + cerr << "ModFileLocalVariable is not derivable" << endl; + exit(-1); } cerr << "Impossible case!" << endl; exit(-1); @@ -216,8 +222,9 @@ VariableNode::writeOutput(ostream &output, ExprNodeOutputType output_type, output << "params" << LPAR(output_type) << symb_id + OFFSET(output_type) << RPAR(output_type); break; - case eLocalParameter: - output << datatree.symbol_table.getNameByID(eLocalParameter, symb_id); + case eModelLocalVariable: + case eModFileLocalVariable: + output << datatree.symbol_table.getNameByID(type, symb_id); break; case eEndogenous: diff --git a/parser.src/ModelTree.cc b/parser.src/ModelTree.cc index 59a0534f0..f133cd099 100644 --- a/parser.src/ModelTree.cc +++ b/parser.src/ModelTree.cc @@ -164,14 +164,14 @@ ModelTree::writeTemporaryTerms(ostream &output, ExprNodeOutputType output_type) } void -ModelTree::writeLocalParameters(ostream &output, ExprNodeOutputType output_type) const +ModelTree::writeModelLocalVariables(ostream &output, ExprNodeOutputType output_type) const { - for(map::const_iterator it = local_parameters_table.begin(); - it != local_parameters_table.end(); it++) + for(map::const_iterator it = local_variables_table.begin(); + it != local_variables_table.end(); it++) { int id = it->first; NodeID value = it->second; - output << symbol_table.getNameByID(eLocalParameter, id) << " = "; + output << symbol_table.getNameByID(eModelLocalVariable, id) << " = "; // Use an empty set for the temporary terms value->writeOutput(output, output_type, temporary_terms_type()); output << ";" << endl; @@ -692,7 +692,7 @@ ModelTree::writeStaticModel(ostream &StaticOutput) const ExprNodeOutputType output_type = (mode == eDLLMode ? oCStaticModel : oMatlabStaticModel); - writeLocalParameters(model_output, output_type); + writeModelLocalVariables(model_output, output_type); writeTemporaryTerms(model_output, output_type); @@ -973,7 +973,7 @@ ModelTree::writeSparseDLLDynamicCFileAndBinFile(const string &dynamic_basename, } mDynamicModelFile << "//#define DEBUG\n"; - writeLocalParameters(mDynamicModelFile, oCDynamicModelSparseDLL); + writeModelLocalVariables(mDynamicModelFile, oCDynamicModelSparseDLL); writeModelEquationsOrdered(mDynamicModelFile, block_triangular.ModelBlock); @@ -1521,7 +1521,7 @@ ModelTree::writeDynamicModel(ostream &DynamicOutput) const ExprNodeOutputType output_type = (mode == eStandardMode ? oMatlabDynamicModel : oCDynamicModel); - writeLocalParameters(model_output, output_type); + writeModelLocalVariables(model_output, output_type); writeTemporaryTerms(model_output, output_type); diff --git a/parser.src/ParsingDriver.cc b/parser.src/ParsingDriver.cc index 5a2f7fe06..d4510ea1e 100644 --- a/parser.src/ParsingDriver.cc +++ b/parser.src/ParsingDriver.cc @@ -10,9 +10,12 @@ ParsingDriver::~ParsingDriver() } bool -ParsingDriver::exists_symbol(const char *s) +ParsingDriver::symbol_exists_and_is_not_modfile_local_variable(const char *s) { - return mod_file->symbol_table.Exist(s); + if (!mod_file->symbol_table.Exist(s)) + return false; + + return(mod_file->symbol_table.getType(s) != eModFileLocalVariable); } void @@ -120,6 +123,10 @@ ParsingDriver::add_model_variable(string *name) NodeID id = model_tree->AddVariable(*name); Type type = mod_file->symbol_table.getType(*name); + + if (type == eModFileLocalVariable) + error("Variable " + *name + " not allowed inside model declaration. Its scope is only outside model."); + if ((type == eEndogenous) && (model_tree->mode == eSparseDLLMode)) { int ID = mod_file->symbol_table.getID(*name); @@ -137,6 +144,9 @@ ParsingDriver::add_model_variable(string *name, string *olag) Type type = mod_file->symbol_table.getType(*name); int lag = atoi(olag->c_str()); + if (type == eModFileLocalVariable) + error("Variable " + *name + " not allowed inside model declaration. Its scope is only outside model."); + if ((type == eExogenous) && lag != 0) { cout << "Warning: exogenous variable " @@ -156,7 +166,10 @@ ParsingDriver::add_model_variable(string *name, string *olag) NodeID ParsingDriver::add_expression_variable(string *name) { - check_symbol_existence(*name); + // If symbol doesn't exist, then declare it as a mod file local variable + if (!mod_file->symbol_table.Exist(*name)) + mod_file->symbol_table.AddSymbolDeclar(*name, eModFileLocalVariable, *name); + NodeID id = data_tree->AddVariable(*name); delete name; @@ -985,9 +998,9 @@ ParsingDriver::add_model_equal_with_zero_rhs(NodeID arg) } void -ParsingDriver::declare_and_init_local_parameter(string *name, NodeID rhs) +ParsingDriver::declare_and_init_model_local_variable(string *name, NodeID rhs) { - mod_file->symbol_table.AddSymbolDeclar(*name, eLocalParameter, *name); + mod_file->symbol_table.AddSymbolDeclar(*name, eModelLocalVariable, *name); try { model_tree->AddLocalParameter(*name, rhs); diff --git a/parser.src/SymbolTable.cc b/parser.src/SymbolTable.cc index cd4b18604..a8f409559 100644 --- a/parser.src/SymbolTable.cc +++ b/parser.src/SymbolTable.cc @@ -13,7 +13,8 @@ using namespace std; SymbolTable::SymbolTable() : endo_nbr(0), exo_nbr(0), exo_det_nbr(0), parameter_nbr(0), - local_parameter_nbr(0), recur_nbr(0) + model_local_variable_nbr(0), modfile_local_variable_nbr(0), + recur_nbr(0) { name_table.resize(20); tex_name_table.resize(20); @@ -43,14 +44,15 @@ int SymbolTable::AddSymbol(string name,Type type, string tex_name) case eRecursiveVariable: symboltable[name].id = recur_nbr; return recur_nbr++; - case eLocalParameter: - symboltable[name].id = local_parameter_nbr; - return local_parameter_nbr++; - default: - // should never happen - return -1; + case eModelLocalVariable: + symboltable[name].id = model_local_variable_nbr; + return model_local_variable_nbr++; + case eModFileLocalVariable: + symboltable[name].id = modfile_local_variable_nbr; + return modfile_local_variable_nbr++; } - + // should never happen + return -1; } int SymbolTable::AddSymbolDeclar(string name,Type type, string tex_name) diff --git a/parser.src/include/DataTree.hh b/parser.src/include/DataTree.hh index a986592ee..79ccab7d9 100644 --- a/parser.src/include/DataTree.hh +++ b/parser.src/include/DataTree.hh @@ -31,8 +31,8 @@ protected: //! A counter for filling ExprNode's idx field int node_counter; - //! Stores local parameters value - map local_parameters_table; + //! Stores local variables value + map local_variables_table; typedef map num_const_node_map_type; num_const_node_map_type num_const_node_map; diff --git a/parser.src/include/ModelTree.hh b/parser.src/include/ModelTree.hh index 6ae019207..6456b9586 100644 --- a/parser.src/include/ModelTree.hh +++ b/parser.src/include/ModelTree.hh @@ -69,9 +69,9 @@ private: void computeTemporaryTermsOrdered(int order, Model_Block *ModelBlock); //! Writes temporary terms void writeTemporaryTerms(ostream &output, ExprNodeOutputType output_type) const; - //! Writes local parameters + //! Writes model local variables /*! No temporary term is used in the output, so that local parameters declarations can be safely put before temporary terms declaration in the output files */ - void writeLocalParameters(ostream &output, ExprNodeOutputType output_type) const; + void writeModelLocalVariables(ostream &output, ExprNodeOutputType output_type) const; //! Writes model equations void writeModelEquations(ostream &output, ExprNodeOutputType output_type) const; //! Writes the static model equations and its derivatives diff --git a/parser.src/include/ParsingDriver.hh b/parser.src/include/ParsingDriver.hh index 8752babee..91e10c5f5 100644 --- a/parser.src/include/ParsingDriver.hh +++ b/parser.src/include/ParsingDriver.hh @@ -147,8 +147,8 @@ public: exit(-1); } - //! Check if a given symbol exists in the parsing context - bool exists_symbol(const char *s); + //! Check if a given symbol exists in the parsing context, and is not a mod file local variable + bool symbol_exists_and_is_not_modfile_local_variable(const char *s); //! Sets mode of ModelTree class to use C output void use_dll(); //! Sets mode of ModelTree class to block decompose the model and triggers the creation of the incidence matrix @@ -166,7 +166,7 @@ public: //! Declares a parameter by adding it to SymbolTable void declare_parameter(string *name, string *tex_name = new string); //! Declares and initializes a local parameter - void declare_and_init_local_parameter(string *name, NodeID rhs); + void declare_and_init_model_local_variable(string *name, NodeID rhs); //! Adds a constant to DataTree NodeID add_constant(string *constant); //! Adds a model variable to ModelTree and VariableTable diff --git a/parser.src/include/SymbolTable.hh b/parser.src/include/SymbolTable.hh index c19b21340..3f5c745bd 100644 --- a/parser.src/include/SymbolTable.hh +++ b/parser.src/include/SymbolTable.hh @@ -55,8 +55,10 @@ public : int exo_det_nbr; //! Number of declared parameters int parameter_nbr; - //! Number of declared local parameters - int local_parameter_nbr; + //! Number of declared model local variables + int model_local_variable_nbr; + //! Number of declared modfile local variables + int modfile_local_variable_nbr; //! Number of declared recursive variables int recur_nbr; /*! Pointer to error function of parser class */ diff --git a/parser.src/include/SymbolTableTypes.hh b/parser.src/include/SymbolTableTypes.hh index 0444b0cac..ccaa23cee 100644 --- a/parser.src/include/SymbolTableTypes.hh +++ b/parser.src/include/SymbolTableTypes.hh @@ -9,7 +9,8 @@ enum Type eExogenousDet = 2, //!< Exogenous deterministic (new) eRecursiveVariable = 3, //!< Recursive variable (reserved for future use) eParameter = 4, //!< Parameter - eLocalParameter = 10, //!< Parameter local to a model + eModelLocalVariable = 10, //!< Local variable whose scope is model (pound expression) + eModFileLocalVariable = 11 //!< Local variable whose scope is mod file (model excluded) }; //! Symbol reference flag enum