v4 parser: set up # statements
git-svn-id: https://www.dynare.org/svn/dynare/dynare_v4@702 ac1d8469-bf42-47a9-8791-bf33cf982152time-shift
parent
bb10c1d82e
commit
91f8b693fc
|
@ -20,7 +20,7 @@
|
||||||
#endif
|
#endif
|
||||||
%}
|
%}
|
||||||
/* %pure_parser */
|
/* %pure_parser */
|
||||||
|
%token POUND_SIGN
|
||||||
%token AR AUTOCORR
|
%token AR AUTOCORR
|
||||||
%token BAYESIAN_IRF BETA_PDF
|
%token BAYESIAN_IRF BETA_PDF
|
||||||
%token CALIB CALIB_VAR CHECK CONF_SIG CORR COVAR
|
%token CALIB CALIB_VAR CHECK CONF_SIG CORR COVAR
|
||||||
|
@ -291,17 +291,21 @@
|
||||||
;
|
;
|
||||||
|
|
||||||
model
|
model
|
||||||
: MODEL ';' equation_list END
|
: MODEL {_parser->initialize_model();} ';' equation_list END
|
||||||
{_parser->check_model();}
|
|
||||||
| MODEL '(' LINEAR ')' ';' {_parser->option_num("linear","1");} equation_list END
|
|
||||||
{_parser->check_model();}
|
|
||||||
| MODEL '(' USE_DLL ')' ';' {_parser->use_dll();} equation_list END
|
|
||||||
{_parser->check_model();}
|
{_parser->check_model();}
|
||||||
|
| MODEL '(' LINEAR ')' ';' {_parser->option_num("linear","1");
|
||||||
|
_parser->initialize_model();}
|
||||||
|
equation_list END {_parser->check_model();}
|
||||||
|
| MODEL '(' USE_DLL ')' ';' {_parser->use_dll();
|
||||||
|
_parser->initialize_model();}
|
||||||
|
equation_list END {_parser->check_model();}
|
||||||
;
|
;
|
||||||
|
|
||||||
equation_list
|
equation_list
|
||||||
: equation_list equation
|
: equation_list equation
|
||||||
|
| equation_list pound_expression
|
||||||
| equation
|
| equation
|
||||||
|
| pound_expression
|
||||||
;
|
;
|
||||||
|
|
||||||
equation
|
equation
|
||||||
|
@ -356,6 +360,11 @@
|
||||||
{$$ = _parser->add_sqrt($3);}
|
{$$ = _parser->add_sqrt($3);}
|
||||||
;
|
;
|
||||||
|
|
||||||
|
pound_expression: POUND_SIGN NAME
|
||||||
|
{$$ = _parser->add_local_parameter($2);}
|
||||||
|
EQUAL hand_side ';'
|
||||||
|
{$$ = _parser->init_local_parameter($3,$5);}
|
||||||
|
|
||||||
model_var
|
model_var
|
||||||
: NAME
|
: NAME
|
||||||
{$$ = _parser->add_variable($1);}
|
{$$ = _parser->add_variable($1);}
|
||||||
|
@ -774,6 +783,7 @@
|
||||||
| o_xls_sheet
|
| o_xls_sheet
|
||||||
| o_xls_range
|
| o_xls_range
|
||||||
| o_filter_step_ahead
|
| o_filter_step_ahead
|
||||||
|
| o_solve_algo
|
||||||
;
|
;
|
||||||
|
|
||||||
list_optim_option
|
list_optim_option
|
||||||
|
|
|
@ -240,6 +240,7 @@ int sigma_e = 0;
|
||||||
_scanner->do_as_is(yytext);
|
_scanner->do_as_is(yytext);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
<DYNARE_BLOCK># {return POUND_SIGN;}
|
||||||
<INITIAL>. {BEGIN NATIVE; _scanner->do_as_is(yytext);}
|
<INITIAL>. {BEGIN NATIVE; _scanner->do_as_is(yytext);}
|
||||||
|
|
||||||
/* NATIVE Block */
|
/* NATIVE Block */
|
||||||
|
|
|
@ -57,6 +57,13 @@ dynare::Objects* dynare::parser::add_parameter(Objects* obj, Objects* tex_name)
|
||||||
obj->type = eParameter;
|
obj->type = eParameter;
|
||||||
return (obj);
|
return (obj);
|
||||||
}
|
}
|
||||||
|
dynare::Objects* dynare::parser::add_local_parameter(Objects* obj)
|
||||||
|
{
|
||||||
|
obj->ID = (NodeID) symbol_table.AddSymbolDeclar(obj->symbol,eLocalParameter, obj->symbol);
|
||||||
|
obj->type = eLocalParameter;
|
||||||
|
NodeID id = model_tree.AddTerminal(obj->symbol);
|
||||||
|
return new Objects("", id, eTempResult);
|
||||||
|
}
|
||||||
dynare::Objects* dynare::parser::add_constant(Objects* obj)
|
dynare::Objects* dynare::parser::add_constant(Objects* obj)
|
||||||
{
|
{
|
||||||
obj->ID = (NodeID) num_constants.AddConstant(obj->symbol);
|
obj->ID = (NodeID) num_constants.AddConstant(obj->symbol);
|
||||||
|
@ -134,6 +141,10 @@ dynare::Objects* dynare::parser::translate_symbol(Objects* obj)
|
||||||
symbol << "M_.params(" << (int)obj->ID+1 << ")";
|
symbol << "M_.params(" << (int)obj->ID+1 << ")";
|
||||||
obj->symbol = symbol.str();
|
obj->symbol = symbol.str();
|
||||||
}
|
}
|
||||||
|
else if (obj->type == eLocalParameter)
|
||||||
|
{
|
||||||
|
symbol << obj->symbol;
|
||||||
|
}
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -213,6 +224,9 @@ void dynare::parser::hist_val(Objects* lhs, Objects* slag)
|
||||||
numerical_initialization.SetHist(lhs->symbol, lag, expression.get());
|
numerical_initialization.SetHist(lhs->symbol, lag, expression.get());
|
||||||
expression.clear();
|
expression.clear();
|
||||||
}
|
}
|
||||||
|
void dynare::parser::initialize_model(void)
|
||||||
|
{
|
||||||
|
}
|
||||||
void dynare::parser::use_dll(void)
|
void dynare::parser::use_dll(void)
|
||||||
{
|
{
|
||||||
// Seetting variable momber offset to use C outputs
|
// Seetting variable momber offset to use C outputs
|
||||||
|
@ -269,6 +283,7 @@ void dynare::parser::finish(void)
|
||||||
*output << "save('" << model_file_name << "_results', 'oo_');\n";
|
*output << "save('" << model_file_name << "_results', 'oo_');\n";
|
||||||
*output << "diary off\n";
|
*output << "diary off\n";
|
||||||
|
|
||||||
|
symbol_table.erase_local_parameters();
|
||||||
}
|
}
|
||||||
void dynare::parser::begin_initval(void)
|
void dynare::parser::begin_initval(void)
|
||||||
{
|
{
|
||||||
|
@ -601,6 +616,12 @@ dynare::Objects* dynare::parser::add_equal(Objects* arg1, Objects* arg2)
|
||||||
return new Objects("", id, eTempResult);
|
return new Objects("", id, eTempResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dynare::Objects* dynare::parser::init_local_parameter(Objects* arg1, Objects* arg2)
|
||||||
|
{
|
||||||
|
NodeID id = model_tree.AddEqual(arg1->ID, arg2->ID);
|
||||||
|
return new Objects("", id, eTempResult);
|
||||||
|
}
|
||||||
|
|
||||||
dynare::Objects* dynare::parser::add_plus(Objects* arg1, Objects* arg2)
|
dynare::Objects* dynare::parser::add_plus(Objects* arg1, Objects* arg2)
|
||||||
{
|
{
|
||||||
NodeID id = model_tree.AddPlus(arg1->ID, arg2->ID);
|
NodeID id = model_tree.AddPlus(arg1->ID, arg2->ID);
|
||||||
|
|
|
@ -16,6 +16,7 @@ int ModelParameters::exo_det_nbr = 0;
|
||||||
int ModelParameters::endo_nbr = 0;
|
int ModelParameters::endo_nbr = 0;
|
||||||
int ModelParameters::var_endo_nbr = 0;
|
int ModelParameters::var_endo_nbr = 0;
|
||||||
int ModelParameters::parameter_nbr = 0;
|
int ModelParameters::parameter_nbr = 0;
|
||||||
|
int ModelParameters::local_parameter_nbr = 0;
|
||||||
int ModelParameters::lagged_nbr = 0;
|
int ModelParameters::lagged_nbr = 0;
|
||||||
int ModelParameters::static_nbr = 0;
|
int ModelParameters::static_nbr = 0;
|
||||||
int ModelParameters::forward_nbr = 0;
|
int ModelParameters::forward_nbr = 0;
|
||||||
|
|
|
@ -642,6 +642,9 @@ inline NodeID ModelTree::DeriveArgument(NodeID iArg, Type iType, int iVarID)
|
||||||
case eParameter :
|
case eParameter :
|
||||||
return Zero;
|
return Zero;
|
||||||
break;
|
break;
|
||||||
|
case eLocalParameter :
|
||||||
|
return Zero;
|
||||||
|
break;
|
||||||
case eUNDEF :
|
case eUNDEF :
|
||||||
return NullID;
|
return NullID;
|
||||||
break;
|
break;
|
||||||
|
@ -690,12 +693,18 @@ string ModelTree::setStaticModel(void)
|
||||||
{
|
{
|
||||||
if ((*tree_it)->op_code == EQUAL)
|
if ((*tree_it)->op_code == EQUAL)
|
||||||
{
|
{
|
||||||
if (lEquationNBR < ModelParameters::eq_nbr)
|
if ((*tree_it)->id1->type1 == eLocalParameter)
|
||||||
|
{
|
||||||
|
model_output << getExpression((*tree_it)->id1, eStaticEquations, lEquationNBR);
|
||||||
|
model_output << " = ";
|
||||||
|
model_output << getExpression((*tree_it)->id2, eStaticEquations, lEquationNBR) << ";" << endl;
|
||||||
|
}
|
||||||
|
else if (lEquationNBR < ModelParameters::eq_nbr)
|
||||||
{
|
{
|
||||||
model_output << "lhs =";
|
model_output << "lhs =";
|
||||||
model_output << getExpression(((*tree_it)->id1), eStaticEquations, lEquationNBR) << ";" << endl;
|
model_output << getExpression((*tree_it)->id1, eStaticEquations, lEquationNBR) << ";" << endl;
|
||||||
model_output << "rhs =";
|
model_output << "rhs =";
|
||||||
model_output << getExpression(((*tree_it)->id2), eStaticEquations, lEquationNBR) << ";" << endl;
|
model_output << getExpression((*tree_it)->id2, eStaticEquations, lEquationNBR) << ";" << endl;
|
||||||
model_output << "residual" << lpar << lEquationNBR+1 << rpar << "= lhs-rhs;" << endl;
|
model_output << "residual" << lpar << lEquationNBR+1 << rpar << "= lhs-rhs;" << endl;
|
||||||
lEquationNBR++;
|
lEquationNBR++;
|
||||||
}
|
}
|
||||||
|
@ -830,7 +839,13 @@ string ModelTree::setDynamicModel(void)
|
||||||
{
|
{
|
||||||
if ((*tree_it)->op_code == EQUAL)
|
if ((*tree_it)->op_code == EQUAL)
|
||||||
{
|
{
|
||||||
if (lEquationNBR < ModelParameters::eq_nbr)
|
if ((*tree_it)->id1->type1 == eLocalParameter)
|
||||||
|
{
|
||||||
|
model_output << getExpression((*tree_it)->id1, eStaticEquations, lEquationNBR);
|
||||||
|
model_output << " = ";
|
||||||
|
model_output << getExpression((*tree_it)->id2, eStaticEquations, lEquationNBR) << ";" << endl;
|
||||||
|
}
|
||||||
|
else if (lEquationNBR < ModelParameters::eq_nbr)
|
||||||
{
|
{
|
||||||
model_output << "lhs =";
|
model_output << "lhs =";
|
||||||
model_output << getExpression(((*tree_it)->id1), eDynamicEquations, lEquationNBR) << ";" << endl;
|
model_output << getExpression(((*tree_it)->id1), eDynamicEquations, lEquationNBR) << ";" << endl;
|
||||||
|
@ -1246,6 +1261,10 @@ inline string ModelTree::getArgument(NodeID id, Type type, EquationType iEquatio
|
||||||
{
|
{
|
||||||
argument << param_name << lpar << (int)id+offset << rpar;
|
argument << param_name << lpar << (int)id+offset << rpar;
|
||||||
}
|
}
|
||||||
|
else if (type == eLocalParameter)
|
||||||
|
{
|
||||||
|
argument << SymbolTable::getNameByID(eLocalParameter,(int)id);
|
||||||
|
}
|
||||||
else if (type == eNumericalConstant)
|
else if (type == eNumericalConstant)
|
||||||
{
|
{
|
||||||
argument << NumericalConstants::get((int) id);
|
argument << NumericalConstants::get((int) id);
|
||||||
|
|
|
@ -48,6 +48,28 @@ void NumericalInitialization::SetConstant (string name, string expression)
|
||||||
|
|
||||||
}
|
}
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
|
void NumericalInitialization::SetLocalConstant (string name, string expression)
|
||||||
|
{
|
||||||
|
|
||||||
|
//Testing if symbol exists
|
||||||
|
if (SymbolTable::Exist(name))
|
||||||
|
{
|
||||||
|
if (SymbolTable::getType(name) != eLocalParameter)
|
||||||
|
{
|
||||||
|
string msg = "Using existing symbol " + name +
|
||||||
|
" as local parameter name";
|
||||||
|
(* error) (msg.c_str());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
SymbolTable::AddSymbolDeclar(name,eLocalParameter,name);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Writing expression
|
||||||
|
*output << name << " = " << expression << ";\n";
|
||||||
|
}
|
||||||
|
//------------------------------------------------------------------------------
|
||||||
void NumericalInitialization::BeginInitval (void)
|
void NumericalInitialization::BeginInitval (void)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
|
@ -19,8 +19,8 @@ void (* SymbolTable::error) (const char* ) = NULL;
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
SymbolTable::SymbolTable()
|
SymbolTable::SymbolTable()
|
||||||
{
|
{
|
||||||
name_table.resize(10);
|
name_table.resize(20);
|
||||||
tex_name_table.resize(10);
|
tex_name_table.resize(20);
|
||||||
}
|
}
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
SymbolTable::~SymbolTable()
|
SymbolTable::~SymbolTable()
|
||||||
|
@ -57,6 +57,10 @@ int SymbolTable::AddSymbol(string name,Type type, string tex_name)
|
||||||
symboltable[name].id = ModelParameters::recur_nbr;
|
symboltable[name].id = ModelParameters::recur_nbr;
|
||||||
ModelParameters::recur_nbr++;
|
ModelParameters::recur_nbr++;
|
||||||
return ModelParameters::recur_nbr-1;
|
return ModelParameters::recur_nbr-1;
|
||||||
|
case eLocalParameter:
|
||||||
|
symboltable[name].id = ModelParameters::local_parameter_nbr;
|
||||||
|
ModelParameters::local_parameter_nbr++;
|
||||||
|
return ModelParameters::local_parameter_nbr-1;
|
||||||
default:
|
default:
|
||||||
// should never happen
|
// should never happen
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -266,8 +270,18 @@ string SymbolTable::get()
|
||||||
output << "M_.param_nbr = " << ModelParameters::parameter_nbr << ";\n";
|
output << "M_.param_nbr = " << ModelParameters::parameter_nbr << ";\n";
|
||||||
return output.str();
|
return output.str();
|
||||||
}
|
}
|
||||||
|
//------------------------------------------------------------------------------
|
||||||
|
void SymbolTable::erase_local_parameters(void)
|
||||||
|
{
|
||||||
|
std::map<std::string, Symbol, std::less<std::string> >::iterator i = symboltable.begin();
|
||||||
|
for(; i != symboltable.end(); ++i)
|
||||||
|
{
|
||||||
|
if ((i->second).type == eLocalParameter)
|
||||||
|
{
|
||||||
|
symboltable.erase(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
/*
|
/*
|
||||||
ostream & operator << (ostream & output, Symbol s)
|
ostream & operator << (ostream & output, Symbol s)
|
||||||
|
|
|
@ -120,6 +120,8 @@ namespace dynare
|
||||||
void check_model(void);
|
void check_model(void);
|
||||||
/*! Executes final instructions */
|
/*! Executes final instructions */
|
||||||
void finish(void);
|
void finish(void);
|
||||||
|
/*! Initializes the model */
|
||||||
|
void initialize_model(void);
|
||||||
/*! Sets variable offset of ModelTree class to use C output */
|
/*! Sets variable offset of ModelTree class to use C output */
|
||||||
void use_dll(void);
|
void use_dll(void);
|
||||||
/*! Adds an endogenous variable to SymbolTable*/
|
/*! Adds an endogenous variable to SymbolTable*/
|
||||||
|
@ -130,6 +132,8 @@ namespace dynare
|
||||||
Objects* add_exogenous_det(Objects* name, Objects* tex_name = new Objects("",NULL, eUNDEF));
|
Objects* add_exogenous_det(Objects* name, Objects* tex_name = new Objects("",NULL, eUNDEF));
|
||||||
/*! Adds a parameter to SymbolTable*/
|
/*! Adds a parameter to SymbolTable*/
|
||||||
Objects* add_parameter(Objects* name, Objects* tex_name = new Objects("",NULL, eUNDEF));
|
Objects* add_parameter(Objects* name, Objects* tex_name = new Objects("",NULL, eUNDEF));
|
||||||
|
Objects* add_local_parameter(Objects* name);
|
||||||
|
Objects* init_local_parameter(Objects*, Objects*);
|
||||||
/*! Adds a constant to NumericalConstants */
|
/*! Adds a constant to NumericalConstants */
|
||||||
Objects* add_constant(Objects* obj);
|
Objects* add_constant(Objects* obj);
|
||||||
/*! Adds a constant to ModelTree */
|
/*! Adds a constant to ModelTree */
|
||||||
|
|
|
@ -34,6 +34,8 @@ class ModelParameters
|
||||||
static int var_endo_nbr;
|
static int var_endo_nbr;
|
||||||
/*! Number of parameters */
|
/*! Number of parameters */
|
||||||
static int parameter_nbr;
|
static int parameter_nbr;
|
||||||
|
/*! Number of local parameters */
|
||||||
|
static int local_parameter_nbr;
|
||||||
/*! Number of lagged variables */
|
/*! Number of lagged variables */
|
||||||
static int lagged_nbr;
|
static int lagged_nbr;
|
||||||
/*! Number of static variables */
|
/*! Number of static variables */
|
||||||
|
|
|
@ -39,6 +39,7 @@ class NumericalInitialization
|
||||||
- in C++, evaluate expression and set value for Name in Parameters_Table\n
|
- in C++, evaluate expression and set value for Name in Parameters_Table\n
|
||||||
*/
|
*/
|
||||||
void SetConstant(std::string name,std::string expression);
|
void SetConstant(std::string name,std::string expression);
|
||||||
|
void SetLocalConstant(std::string name,std::string expression);
|
||||||
/*!
|
/*!
|
||||||
\par Description
|
\par Description
|
||||||
Initializes an initval block to set initial values for variables
|
Initializes an initval block to set initial values for variables
|
||||||
|
|
|
@ -19,6 +19,7 @@
|
||||||
\class SymbolTable
|
\class SymbolTable
|
||||||
\brief This class keeps track of symbols
|
\brief This class keeps track of symbols
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class SymbolTable
|
class SymbolTable
|
||||||
{
|
{
|
||||||
|
|
||||||
|
@ -85,6 +86,7 @@ class SymbolTable
|
||||||
static std::string get();
|
static std::string get();
|
||||||
/*! Checks if symbols are used in model equations, removes unused symbol */
|
/*! Checks if symbols are used in model equations, removes unused symbol */
|
||||||
void SymbolTable::clean();
|
void SymbolTable::clean();
|
||||||
|
void SymbolTable::erase_local_parameters();
|
||||||
//void PrintSymbolTable(void);
|
//void PrintSymbolTable(void);
|
||||||
};
|
};
|
||||||
inline bool SymbolTable::Exist(std::string name)
|
inline bool SymbolTable::Exist(std::string name)
|
||||||
|
|
|
@ -17,7 +17,7 @@ enum Type
|
||||||
eExogenousDet = 2, //!< Exogenousous deterministic (new)
|
eExogenousDet = 2, //!< Exogenousous 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 = 41, //!< Parameter local to a model
|
eLocalParameter = 10, //!< Parameter local to a model
|
||||||
eLoopIndex = 5, //!< Loop index
|
eLoopIndex = 5, //!< Loop index
|
||||||
eTempResult = 6, //!< Temporary result, used only in Expression class
|
eTempResult = 6, //!< Temporary result, used only in Expression class
|
||||||
eNumericalConstant = 7, //!< Numerical constant, used only in Expression class
|
eNumericalConstant = 7, //!< Numerical constant, used only in Expression class
|
||||||
|
|
Loading…
Reference in New Issue