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
time-shift
sebastien 2007-04-30 12:09:05 +00:00
parent 5ad7e3b4a1
commit 9f33835d6a
14 changed files with 72 additions and 44 deletions

View File

@ -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;

View File

@ -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<int, NodeID>::iterator it = local_parameters_table.find(id);
if (it != local_parameters_table.end())
map<int, NodeID>::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

View File

@ -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:

View File

@ -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

View File

@ -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
*/
<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;
yylval->string_val = new string(yytext);

View File

@ -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:

View File

@ -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<int, NodeID>::const_iterator it = local_parameters_table.begin();
it != local_parameters_table.end(); it++)
for(map<int, NodeID>::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);

View File

@ -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);

View File

@ -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)

View File

@ -31,8 +31,8 @@ protected:
//! A counter for filling ExprNode's idx field
int node_counter;
//! Stores local parameters value
map<int, NodeID> local_parameters_table;
//! Stores local variables value
map<int, NodeID> local_variables_table;
typedef map<int, NodeID> num_const_node_map_type;
num_const_node_map_type num_const_node_map;

View File

@ -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

View File

@ -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

View File

@ -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 */

View File

@ -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