Commit 91f8b693 authored by michel's avatar michel
Browse files

v4 parser: set up # statements

git-svn-id: https://www.dynare.org/svn/dynare/dynare_v4@702 ac1d8469-bf42-47a9-8791-bf33cf982152
parent bb10c1d8
......@@ -20,7 +20,7 @@
#endif
%}
/* %pure_parser */
%token POUND_SIGN
%token AR AUTOCORR
%token BAYESIAN_IRF BETA_PDF
%token CALIB CALIB_VAR CHECK CONF_SIG CORR COVAR
......@@ -291,17 +291,21 @@
;
model
: 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
: MODEL {_parser->initialize_model();} ';' equation_list END
{_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
| equation_list pound_expression
| equation
| pound_expression
;
equation
......@@ -356,6 +360,11 @@
{$$ = _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
: NAME
{$$ = _parser->add_variable($1);}
......@@ -774,6 +783,7 @@
| o_xls_sheet
| o_xls_range
| o_filter_step_ahead
| o_solve_algo
;
list_optim_option
......
......@@ -240,6 +240,7 @@ int sigma_e = 0;
_scanner->do_as_is(yytext);
}
}
<DYNARE_BLOCK># {return POUND_SIGN;}
<INITIAL>. {BEGIN NATIVE; _scanner->do_as_is(yytext);}
/* NATIVE Block */
......
......@@ -57,6 +57,13 @@ dynare::Objects* dynare::parser::add_parameter(Objects* obj, Objects* tex_name)
obj->type = eParameter;
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)
{
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 << ")";
obj->symbol = symbol.str();
}
else if (obj->type == eLocalParameter)
{
symbol << obj->symbol;
}
return obj;
}
......@@ -213,6 +224,9 @@ void dynare::parser::hist_val(Objects* lhs, Objects* slag)
numerical_initialization.SetHist(lhs->symbol, lag, expression.get());
expression.clear();
}
void dynare::parser::initialize_model(void)
{
}
void dynare::parser::use_dll(void)
{
// Seetting variable momber offset to use C outputs
......@@ -268,7 +282,8 @@ void dynare::parser::finish(void)
*output << "save('" << model_file_name << "_results', 'oo_');\n";
*output << "diary off\n";
symbol_table.erase_local_parameters();
}
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);
}
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)
{
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::var_endo_nbr = 0;
int ModelParameters::parameter_nbr = 0;
int ModelParameters::local_parameter_nbr = 0;
int ModelParameters::lagged_nbr = 0;
int ModelParameters::static_nbr = 0;
int ModelParameters::forward_nbr = 0;
......
......@@ -642,6 +642,9 @@ inline NodeID ModelTree::DeriveArgument(NodeID iArg, Type iType, int iVarID)
case eParameter :
return Zero;
break;
case eLocalParameter :
return Zero;
break;
case eUNDEF :
return NullID;
break;
......@@ -690,12 +693,18 @@ string ModelTree::setStaticModel(void)
{
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 << getExpression(((*tree_it)->id1), eStaticEquations, lEquationNBR) << ";" << endl;
model_output << getExpression((*tree_it)->id1, eStaticEquations, lEquationNBR) << ";" << endl;
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;
lEquationNBR++;
}
......@@ -830,7 +839,13 @@ string ModelTree::setDynamicModel(void)
{
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 << 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;
}
else if (type == eLocalParameter)
{
argument << SymbolTable::getNameByID(eLocalParameter,(int)id);
}
else if (type == eNumericalConstant)
{
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)
{
......
......@@ -19,8 +19,8 @@ void (* SymbolTable::error) (const char* ) = NULL;
//------------------------------------------------------------------------------
SymbolTable::SymbolTable()
{
name_table.resize(10);
tex_name_table.resize(10);
name_table.resize(20);
tex_name_table.resize(20);
}
//------------------------------------------------------------------------------
SymbolTable::~SymbolTable()
......@@ -57,6 +57,10 @@ int SymbolTable::AddSymbol(string name,Type type, string tex_name)
symboltable[name].id = ModelParameters::recur_nbr;
ModelParameters::recur_nbr++;
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:
// should never happen
return -1;
......@@ -266,8 +270,18 @@ string SymbolTable::get()
output << "M_.param_nbr = " << ModelParameters::parameter_nbr << ";\n";
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)
......
......@@ -120,6 +120,8 @@ namespace dynare
void check_model(void);
/*! Executes final instructions */
void finish(void);
/*! Initializes the model */
void initialize_model(void);
/*! Sets variable offset of ModelTree class to use C output */
void use_dll(void);
/*! 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));
/*! Adds a parameter to SymbolTable*/
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 */
Objects* add_constant(Objects* obj);
/*! Adds a constant to ModelTree */
......
......@@ -34,6 +34,8 @@ class ModelParameters
static int var_endo_nbr;
/*! Number of parameters */
static int parameter_nbr;
/*! Number of local parameters */
static int local_parameter_nbr;
/*! Number of lagged variables */
static int lagged_nbr;
/*! Number of static variables */
......
......@@ -39,6 +39,7 @@ class NumericalInitialization
- in C++, evaluate expression and set value for Name in Parameters_Table\n
*/
void SetConstant(std::string name,std::string expression);
void SetLocalConstant(std::string name,std::string expression);
/*!
\par Description
Initializes an initval block to set initial values for variables
......
......@@ -19,112 +19,114 @@
\class SymbolTable
\brief This class keeps track of symbols
*/
class SymbolTable
{
private :
static std::ostringstream output;
/*! Adds symbol into symbol table
\param name a string.
\param type a Type struct.
\par Description
- warning if symbol is already set with same type \n
- error if symbol is already set with different type\n
- set Name and Type\n
- increase corresponding counter in ModelParameters class\n
*/
/*! Pointer to current parssing line to set by class parser*/
//int *current_line;
static int AddSymbol(std::string name,Type type, std::string tex_name);
protected :
/*! Symbol table map */
static std::map<std::string, Symbol, std::less<std::string> > symboltable;
/*! Symbol name table indexed by type and ID */
static std::vector< std::vector<std::string> > name_table;
static std::vector< std::vector<std::string> > tex_name_table;
protected :
/*! Changes type of a symbol */
static void ResetType(std::string name,Type new_type);
public :
/*! Constructor */
SymbolTable();
/*! Destructor*/
~SymbolTable();
/*! Adds a symbol apearing in declaration
- warning if symbol is already set with same type
- error if symbol is already set with different type
- set name, type
- increase corresponding counter in ModelParameters
*/
/*! Pointer to error function of parser class */
static void (* error) (const char* m);
static int AddSymbolDeclar(std::string name,Type type, std::string tex_name);
/*! Adds symbol range */
static void AddSymbolRange(std::string name,int nbr,Type type, std::string tex_name);
/*! Adds a lag to field lags */
static void AddLag(std::string name,int lag);
/*! Tests if symbol exists in symbol table
\return true if exists, false outherwise
*/
/*! Sets a symbol as referenced */
static void SetReferenced(std::string name);
/*! Return eReferenced if symbol is referenced eNotReferenced otherwise*/
static Reference isReferenced(std::string name);
/*! Sets a symbol as out of scoop */
static void SetOutOfScoop(std::string name);
inline static bool Exist(std::string name);
/*! Gets name by type and ID */
inline static std::string getNameByID(Type type,int id);
/*! Gets tex name by type and ID */
inline static std::string getTexNameByID(Type type,int id);
/*! Gets type by name */
inline static Type getType(std::string name);
/*! Gets ID by name */
inline static int getID(std::string name);
/*! Gets output string of this class */
static std::string get();
/*! Checks if symbols are used in model equations, removes unused symbol */
void SymbolTable::clean();
//void PrintSymbolTable(void);
private :
static std::ostringstream output;
/*! Adds symbol into symbol table
\param name a string.
\param type a Type struct.
\par Description
- warning if symbol is already set with same type \n
- error if symbol is already set with different type\n
- set Name and Type\n
- increase corresponding counter in ModelParameters class\n
*/
/*! Pointer to current parssing line to set by class parser*/
//int *current_line;
static int AddSymbol(std::string name,Type type, std::string tex_name);
protected :
/*! Symbol table map */
static std::map<std::string, Symbol, std::less<std::string> > symboltable;
/*! Symbol name table indexed by type and ID */
static std::vector< std::vector<std::string> > name_table;
static std::vector< std::vector<std::string> > tex_name_table;
protected :
/*! Changes type of a symbol */
static void ResetType(std::string name,Type new_type);
public :
/*! Constructor */
SymbolTable();
/*! Destructor*/
~SymbolTable();
/*! Adds a symbol apearing in declaration
- warning if symbol is already set with same type
- error if symbol is already set with different type
- set name, type
- increase corresponding counter in ModelParameters
*/
/*! Pointer to error function of parser class */
static void (* error) (const char* m);
static int AddSymbolDeclar(std::string name,Type type, std::string tex_name);
/*! Adds symbol range */
static void AddSymbolRange(std::string name,int nbr,Type type, std::string tex_name);
/*! Adds a lag to field lags */
static void AddLag(std::string name,int lag);
/*! Tests if symbol exists in symbol table
\return true if exists, false outherwise
*/
/*! Sets a symbol as referenced */
static void SetReferenced(std::string name);
/*! Return eReferenced if symbol is referenced eNotReferenced otherwise*/
static Reference isReferenced(std::string name);
/*! Sets a symbol as out of scoop */
static void SetOutOfScoop(std::string name);
inline static bool Exist(std::string name);
/*! Gets name by type and ID */
inline static std::string getNameByID(Type type,int id);
/*! Gets tex name by type and ID */
inline static std::string getTexNameByID(Type type,int id);
/*! Gets type by name */
inline static Type getType(std::string name);
/*! Gets ID by name */
inline static int getID(std::string name);
/*! Gets output string of this class */
static std::string get();
/*! Checks if symbols are used in model equations, removes unused symbol */
void SymbolTable::clean();
void SymbolTable::erase_local_parameters();
//void PrintSymbolTable(void);
};
inline bool SymbolTable::Exist(std::string name)
{
std::map<std::string, Symbol, std::less<std::string> >::iterator iter;
iter = symboltable.find(name);
//Testing if symbol exists
if (iter == symboltable.end()) return false;
else return true;
std::map<std::string, Symbol, std::less<std::string> >::iterator iter;
iter = symboltable.find(name);
//Testing if symbol exists
if (iter == symboltable.end()) return false;
else return true;
}
//------------------------------------------------------------------------------
inline std::string SymbolTable::getNameByID(Type type,int id)
{
if (id >= 0 && (int)name_table[type].size() > id)
return(name_table[type][id]);
else return "";
return(name_table[type][id]);
else return "";
}
//------------------------------------------------------------------------------
inline std::string SymbolTable::getTexNameByID(Type type,int id)
{
if (id >= 0 && (int)tex_name_table[type].size() > id)
return(tex_name_table[type][id]);
else return "";
else return "";
}
//------------------------------------------------------------------------------
inline Type SymbolTable::getType(std::string name)
{
if (Exist(name))
return(symboltable[name].type);
else
return eUNDEF;
if (Exist(name))
return(symboltable[name].type);
else
return eUNDEF;
}
//------------------------------------------------------------------------------
inline int SymbolTable::getID(std::string name)
{
if (Exist(name))
return(symboltable[name].id);
else
return -1;
if (Exist(name))
return(symboltable[name].id);
else
return -1;
}
//------------------------------------------------------------------------------
#endif
......@@ -17,7 +17,7 @@ enum Type
eExogenousDet = 2, //!< Exogenousous deterministic (new)
eRecursiveVariable = 3, //!< Recursive variable (reserved for future use)
eParameter = 4, //!< Parameter
eLocalParameter = 41, //!< Parameter local to a model
eLocalParameter = 10, //!< Parameter local to a model
eLoopIndex = 5, //!< Loop index
eTempResult = 6, //!< Temporary result, used only in Expression class
eNumericalConstant = 7, //!< Numerical constant, used only in Expression class
......
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