Commit 9f33835d authored by sebastien's avatar sebastien
Browse files

v4 parser:

* implemented handling of mod file local variables (eModFileLocalVariable)
* renamed "local parameters" to model local variables (eModelLocalVariables)


git-svn-id: https://www.dynare.org/svn/dynare/dynare_v4@1253 ac1d8469-bf42-47a9-8791-bf33cf982152
parent 5ad7e3b4
...@@ -127,7 +127,9 @@ RamseyPolicyStatement::checkPass(ModFileStructure &mod_file_struct) ...@@ -127,7 +127,9 @@ RamseyPolicyStatement::checkPass(ModFileStructure &mod_file_struct)
{ {
mod_file_struct.stoch_simul_or_similar_present = true; 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"); OptionsList::num_options_type::const_iterator it = options_list.num_options.find("order");
if (it != options_list.num_options.end()) if (it != options_list.num_options.end())
mod_file_struct.order_option = atoi(it->second.c_str()) + 1; mod_file_struct.order_option = atoi(it->second.c_str()) + 1;
......
...@@ -333,11 +333,11 @@ DataTree::AddLocalParameter(const string &name, NodeID value) throw (LocalParame ...@@ -333,11 +333,11 @@ DataTree::AddLocalParameter(const string &name, NodeID value) throw (LocalParame
int id = symbol_table.getID(name); int id = symbol_table.getID(name);
// Throw an exception if symbol already declared // Throw an exception if symbol already declared
map<int, NodeID>::iterator it = local_parameters_table.find(id); map<int, NodeID>::iterator it = local_variables_table.find(id);
if (it != local_parameters_table.end()) if (it != local_variables_table.end())
throw LocalParameterException(name); throw LocalParameterException(name);
local_parameters_table[id] = value; local_variables_table[id] = value;
} }
NodeID NodeID
......
...@@ -889,7 +889,7 @@ namespace yy ...@@ -889,7 +889,7 @@ namespace yy
case 159: case 159:
#line 421 "DynareBison.yy" #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; break;
case 160: case 160:
......
...@@ -418,7 +418,7 @@ cutoff ...@@ -418,7 +418,7 @@ cutoff
; ;
pound_expression: '#' NAME EQUAL hand_side ';' 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 model_var
: NAME : NAME
......
...@@ -262,11 +262,12 @@ int sigma_e = 0; ...@@ -262,11 +262,12 @@ int sigma_e = 0;
return token::INT_NUMBER; 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 otherwise it is a native statement until the end of the line
*/ */
<INITIAL>[A-Za-z_][A-Za-z0-9_]* { <INITIAL>[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; BEGIN DYNARE_STATEMENT;
yylval->string_val = new string(yytext); yylval->string_val = new string(yytext);
......
...@@ -162,9 +162,12 @@ VariableNode::VariableNode(DataTree &datatree_arg, int symb_id_arg, Type type_ar ...@@ -162,9 +162,12 @@ VariableNode::VariableNode(DataTree &datatree_arg, int symb_id_arg, Type type_ar
case eParameter: case eParameter:
// All derivatives are null, do nothing // All derivatives are null, do nothing
break; break;
case eLocalParameter: case eModelLocalVariable:
// Non null derivatives are those of the value of the local parameter // 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; break;
} }
} }
...@@ -184,8 +187,11 @@ VariableNode::computeDerivative(int varID) ...@@ -184,8 +187,11 @@ VariableNode::computeDerivative(int varID)
return datatree.Zero; return datatree.Zero;
case eParameter: case eParameter:
return datatree.Zero; return datatree.Zero;
case eLocalParameter: case eModelLocalVariable:
return datatree.local_parameters_table[symb_id]->getDerivative(varID); return datatree.local_variables_table[symb_id]->getDerivative(varID);
case eModFileLocalVariable:
cerr << "ModFileLocalVariable is not derivable" << endl;
exit(-1);
} }
cerr << "Impossible case!" << endl; cerr << "Impossible case!" << endl;
exit(-1); exit(-1);
...@@ -216,8 +222,9 @@ VariableNode::writeOutput(ostream &output, ExprNodeOutputType output_type, ...@@ -216,8 +222,9 @@ VariableNode::writeOutput(ostream &output, ExprNodeOutputType output_type,
output << "params" << LPAR(output_type) << symb_id + OFFSET(output_type) << RPAR(output_type); output << "params" << LPAR(output_type) << symb_id + OFFSET(output_type) << RPAR(output_type);
break; break;
case eLocalParameter: case eModelLocalVariable:
output << datatree.symbol_table.getNameByID(eLocalParameter, symb_id); case eModFileLocalVariable:
output << datatree.symbol_table.getNameByID(type, symb_id);
break; break;
case eEndogenous: case eEndogenous:
......
...@@ -164,14 +164,14 @@ ModelTree::writeTemporaryTerms(ostream &output, ExprNodeOutputType output_type) ...@@ -164,14 +164,14 @@ ModelTree::writeTemporaryTerms(ostream &output, ExprNodeOutputType output_type)
} }
void void
ModelTree::writeLocalParameters(ostream &output, ExprNodeOutputType output_type) const ModelTree::writeModelLocalVariables(ostream &output, ExprNodeOutputType output_type) const
{ {
for(map<int, NodeID>::const_iterator it = local_parameters_table.begin(); for(map<int, NodeID>::const_iterator it = local_variables_table.begin();
it != local_parameters_table.end(); it++) it != local_variables_table.end(); it++)
{ {
int id = it->first; int id = it->first;
NodeID value = it->second; NodeID value = it->second;
output << symbol_table.getNameByID(eLocalParameter, id) << " = "; output << symbol_table.getNameByID(eModelLocalVariable, id) << " = ";
// Use an empty set for the temporary terms // Use an empty set for the temporary terms
value->writeOutput(output, output_type, temporary_terms_type()); value->writeOutput(output, output_type, temporary_terms_type());
output << ";" << endl; output << ";" << endl;
...@@ -692,7 +692,7 @@ ModelTree::writeStaticModel(ostream &StaticOutput) const ...@@ -692,7 +692,7 @@ ModelTree::writeStaticModel(ostream &StaticOutput) const
ExprNodeOutputType output_type = (mode == eDLLMode ? oCStaticModel : oMatlabStaticModel); ExprNodeOutputType output_type = (mode == eDLLMode ? oCStaticModel : oMatlabStaticModel);
writeLocalParameters(model_output, output_type); writeModelLocalVariables(model_output, output_type);
writeTemporaryTerms(model_output, output_type); writeTemporaryTerms(model_output, output_type);
...@@ -973,7 +973,7 @@ ModelTree::writeSparseDLLDynamicCFileAndBinFile(const string &dynamic_basename, ...@@ -973,7 +973,7 @@ ModelTree::writeSparseDLLDynamicCFileAndBinFile(const string &dynamic_basename,
} }
mDynamicModelFile << "//#define DEBUG\n"; mDynamicModelFile << "//#define DEBUG\n";
writeLocalParameters(mDynamicModelFile, oCDynamicModelSparseDLL); writeModelLocalVariables(mDynamicModelFile, oCDynamicModelSparseDLL);
writeModelEquationsOrdered(mDynamicModelFile, block_triangular.ModelBlock); writeModelEquationsOrdered(mDynamicModelFile, block_triangular.ModelBlock);
...@@ -1521,7 +1521,7 @@ ModelTree::writeDynamicModel(ostream &DynamicOutput) const ...@@ -1521,7 +1521,7 @@ ModelTree::writeDynamicModel(ostream &DynamicOutput) const
ExprNodeOutputType output_type = (mode == eStandardMode ? oMatlabDynamicModel : oCDynamicModel); ExprNodeOutputType output_type = (mode == eStandardMode ? oMatlabDynamicModel : oCDynamicModel);
writeLocalParameters(model_output, output_type); writeModelLocalVariables(model_output, output_type);
writeTemporaryTerms(model_output, output_type); writeTemporaryTerms(model_output, output_type);
......
...@@ -10,9 +10,12 @@ ParsingDriver::~ParsingDriver() ...@@ -10,9 +10,12 @@ ParsingDriver::~ParsingDriver()
} }
bool 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 void
...@@ -120,6 +123,10 @@ ParsingDriver::add_model_variable(string *name) ...@@ -120,6 +123,10 @@ ParsingDriver::add_model_variable(string *name)
NodeID id = model_tree->AddVariable(*name); NodeID id = model_tree->AddVariable(*name);
Type type = mod_file->symbol_table.getType(*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)) if ((type == eEndogenous) && (model_tree->mode == eSparseDLLMode))
{ {
int ID = mod_file->symbol_table.getID(*name); int ID = mod_file->symbol_table.getID(*name);
...@@ -137,6 +144,9 @@ ParsingDriver::add_model_variable(string *name, string *olag) ...@@ -137,6 +144,9 @@ ParsingDriver::add_model_variable(string *name, string *olag)
Type type = mod_file->symbol_table.getType(*name); Type type = mod_file->symbol_table.getType(*name);
int lag = atoi(olag->c_str()); 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) if ((type == eExogenous) && lag != 0)
{ {
cout << "Warning: exogenous variable " cout << "Warning: exogenous variable "
...@@ -156,7 +166,10 @@ ParsingDriver::add_model_variable(string *name, string *olag) ...@@ -156,7 +166,10 @@ ParsingDriver::add_model_variable(string *name, string *olag)
NodeID NodeID
ParsingDriver::add_expression_variable(string *name) 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); NodeID id = data_tree->AddVariable(*name);
delete name; delete name;
...@@ -985,9 +998,9 @@ ParsingDriver::add_model_equal_with_zero_rhs(NodeID arg) ...@@ -985,9 +998,9 @@ ParsingDriver::add_model_equal_with_zero_rhs(NodeID arg)
} }
void 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 try
{ {
model_tree->AddLocalParameter(*name, rhs); model_tree->AddLocalParameter(*name, rhs);
......
...@@ -13,7 +13,8 @@ ...@@ -13,7 +13,8 @@
using namespace std; using namespace std;
SymbolTable::SymbolTable() : endo_nbr(0), exo_nbr(0), exo_det_nbr(0), parameter_nbr(0), 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); name_table.resize(20);
tex_name_table.resize(20); tex_name_table.resize(20);
...@@ -43,14 +44,15 @@ int SymbolTable::AddSymbol(string name,Type type, string tex_name) ...@@ -43,14 +44,15 @@ int SymbolTable::AddSymbol(string name,Type type, string tex_name)
case eRecursiveVariable: case eRecursiveVariable:
symboltable[name].id = recur_nbr; symboltable[name].id = recur_nbr;
return recur_nbr++; return recur_nbr++;
case eLocalParameter: case eModelLocalVariable:
symboltable[name].id = local_parameter_nbr; symboltable[name].id = model_local_variable_nbr;
return local_parameter_nbr++; return model_local_variable_nbr++;
default: case eModFileLocalVariable:
// should never happen symboltable[name].id = modfile_local_variable_nbr;
return -1; return modfile_local_variable_nbr++;
} }
// should never happen
return -1;
} }
int SymbolTable::AddSymbolDeclar(string name,Type type, string tex_name) int SymbolTable::AddSymbolDeclar(string name,Type type, string tex_name)
......
...@@ -31,8 +31,8 @@ protected: ...@@ -31,8 +31,8 @@ protected:
//! A counter for filling ExprNode's idx field //! A counter for filling ExprNode's idx field
int node_counter; int node_counter;
//! Stores local parameters value //! Stores local variables value
map<int, NodeID> local_parameters_table; map<int, NodeID> local_variables_table;
typedef map<int, NodeID> num_const_node_map_type; typedef map<int, NodeID> num_const_node_map_type;
num_const_node_map_type num_const_node_map; num_const_node_map_type num_const_node_map;
......
...@@ -69,9 +69,9 @@ private: ...@@ -69,9 +69,9 @@ private:
void computeTemporaryTermsOrdered(int order, Model_Block *ModelBlock); void computeTemporaryTermsOrdered(int order, Model_Block *ModelBlock);
//! Writes temporary terms //! Writes temporary terms
void writeTemporaryTerms(ostream &output, ExprNodeOutputType output_type) const; 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 */ /*! 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 //! Writes model equations
void writeModelEquations(ostream &output, ExprNodeOutputType output_type) const; void writeModelEquations(ostream &output, ExprNodeOutputType output_type) const;
//! Writes the static model equations and its derivatives //! Writes the static model equations and its derivatives
......
...@@ -147,8 +147,8 @@ public: ...@@ -147,8 +147,8 @@ public:
exit(-1); exit(-1);
} }
//! Check if a given symbol exists in the parsing context //! Check if a given symbol exists in the parsing context, and is not a mod file local variable
bool exists_symbol(const char *s); bool symbol_exists_and_is_not_modfile_local_variable(const char *s);
//! Sets mode of ModelTree class to use C output //! Sets mode of ModelTree class to use C output
void use_dll(); void use_dll();
//! Sets mode of ModelTree class to block decompose the model and triggers the creation of the incidence matrix //! Sets mode of ModelTree class to block decompose the model and triggers the creation of the incidence matrix
...@@ -166,7 +166,7 @@ public: ...@@ -166,7 +166,7 @@ public:
//! Declares a parameter by adding it to SymbolTable //! Declares a parameter by adding it to SymbolTable
void declare_parameter(string *name, string *tex_name = new string); void declare_parameter(string *name, string *tex_name = new string);
//! Declares and initializes a local parameter //! 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 //! Adds a constant to DataTree
NodeID add_constant(string *constant); NodeID add_constant(string *constant);
//! Adds a model variable to ModelTree and VariableTable //! Adds a model variable to ModelTree and VariableTable
......
...@@ -55,8 +55,10 @@ public : ...@@ -55,8 +55,10 @@ public :
int exo_det_nbr; int exo_det_nbr;
//! Number of declared parameters //! Number of declared parameters
int parameter_nbr; int parameter_nbr;
//! Number of declared local parameters //! Number of declared model local variables
int local_parameter_nbr; int model_local_variable_nbr;
//! Number of declared modfile local variables
int modfile_local_variable_nbr;
//! Number of declared recursive variables //! Number of declared recursive variables
int recur_nbr; int recur_nbr;
/*! Pointer to error function of parser class */ /*! Pointer to error function of parser class */
......
...@@ -9,7 +9,8 @@ enum Type ...@@ -9,7 +9,8 @@ enum Type
eExogenousDet = 2, //!< Exogenous deterministic (new) eExogenousDet = 2, //!< Exogenous deterministic (new)
eRecursiveVariable = 3, //!< Recursive variable (reserved for future use) eRecursiveVariable = 3, //!< Recursive variable (reserved for future use)
eParameter = 4, //!< Parameter 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 //! Symbol reference flag enum
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment