Commit feec2020 authored by sebastien's avatar sebastien
Browse files

v4 parser: merged Fehrat's changes


git-svn-id: https://www.dynare.org/svn/dynare/dynare_v4@1190 ac1d8469-bf42-47a9-8791-bf33cf982152
parent 4cf72812
This diff is collapsed.
......@@ -60,6 +60,33 @@ SimulStatement::writeOutput(ostream &output, const string &basename) const
output << "simul(oo_.dr);\n";
}
SimulSparseStatement::SimulSparseStatement(const OptionsList &options_list_arg) :
options_list(options_list_arg)
{
}
void
SimulSparseStatement::checkPass(ModFileStructure &mod_file_struct)
{
mod_file_struct.simul_present = true;
}
void
SimulSparseStatement::writeOutput(ostream &output, const string &basename) const
{
options_list.writeOutput(output);
output << "if ~ options_.initval_file\n";
output << " make_y_;\n";
output << " make_ex_;\n";
output << "end\n";
output << "disp('compiling...');\n";
if (compiler == 0)
output << "mex " << filename << "_dynamic.c;\n";
else
output << "mex " << filename << "_dynamic.cc;\n";
output << "oo_.endo_simul=" << filename << "_dynamic;\n";
}
StochSimulStatement::StochSimulStatement(const TmpSymbolTable &tmp_symbol_table_arg,
const OptionsList &options_list_arg) :
tmp_symbol_table(tmp_symbol_table_arg),
......@@ -205,6 +232,16 @@ PeriodsStatement::writeOutput(ostream &output, const string &basename) const
output << "options_.simul = 1;" << endl;
}
CutoffStatement::CutoffStatement(int cutoff_arg) : cutoff(cutoff_arg)
{
}
void
CutoffStatement::writeOutput(ostream &output, const string &basename) const
{
output << "options_.cutoff = " << cutoff << ";" << endl;
}
DsampleStatement::DsampleStatement(int val1_arg) : val1(val1_arg), val2(-1)
{
}
......
......@@ -5,8 +5,10 @@
DataTree::DataTree(SymbolTable &symbol_table_arg, NumericalConstants &num_constants_arg) :
symbol_table(symbol_table_arg),
num_constants(num_constants_arg),
node_counter(0),
variable_table(symbol_table_arg),
offset(1)
offset(1),
compiler(LCC_COMPILE)
{
Zero = AddNumConstant("0.0");
One = AddNumConstant("1.0");
......
This diff is collapsed.
......@@ -40,19 +40,19 @@ typedef pair<int, Type> ExpObj;
%token AR AUTOCORR
%token BAYESIAN_IRF BETA_PDF
%token CALIB CALIB_VAR CHECK CONF_SIG CONSTANT CORR COVAR
%token CALIB CALIB_VAR CHECK CONF_SIG CONSTANT CORR COVAR CUTOFF
%token DATAFILE DR_ALGO DROP DSAMPLE DYNASAVE DYNATYPE
%token END ENDVAL EQUAL ESTIMATION ESTIMATED_PARAMS ESTIMATED_PARAMS_BOUNDS ESTIMATED_PARAMS_INIT
%token FILTER_STEP_AHEAD FILTERED_VARS FIRST_OBS
%token FILENAME FILTER_STEP_AHEAD FILTERED_VARS FIRST_OBS
%token <string_val> FLOAT_NUMBER
%token FORECAST
%token GAMMA_PDF GRAPH
%token GAMMA_PDF GCC_COMPILER GRAPH
%token HISTVAL HP_FILTER HP_NGRID
%token INITVAL
%token <string_val> INT_NUMBER
%token INV_GAMMA_PDF IRF
%token KALMAN_ALGO KALMAN_TOL
%token LAPLACE LIK_ALGO LIK_INIT LINEAR LOAD_MH_FILE LOGLINEAR
%token LAPLACE LCC_COMPILER LIK_ALGO LIK_INIT LINEAR LOAD_MH_FILE LOGLINEAR
%token MH_DROP MH_INIT_SCALE MH_JSCALE MH_MODE MH_NBLOCKS MH_REPLIC MH_RECOVER
%token MODE_CHECK MODE_COMPUTE MODE_FILE MODEL MODEL_COMPARISON MSHOCKS
%token MODEL_COMPARISON_APPROXIMATION MODIFIEDHARMONICMEAN MOMENTS_VARENDO
......@@ -62,7 +62,7 @@ typedef pair<int, Type> ExpObj;
%token PARAMETERS PERIODS PLANNER_OBJECTIVE PREFILTER PRESAMPLE PRINT PRIOR_TRUNC PRIOR_ANALYSIS POSTERIOR_ANALYSIS
%token QZ_CRITERIUM
%token RELATIVE_IRF REPLIC RPLOT
%token SHOCKS SIGMA_E SIMUL SIMUL_ALGO SIMUL_SEED SMOOTHER SOLVE_ALGO STDERR STEADY STOCH_SIMUL
%token SHOCKS SIGMA_E SIMUL SIMUL_ALGO SIMUL_SEED SMOOTHER SOLVE_ALGO SPARSE_DLL STDERR STEADY STOCH_SIMUL
%token TEX RAMSEY_POLICY PLANNER_DISCOUNT
%token <string_val> TEX_NAME
%token UNIFORM_PDF UNIT_ROOT_VARS USE_DLL
......@@ -93,6 +93,7 @@ typedef pair<int, Type> ExpObj;
statement
: declaration
| periods
| cutoff
| model
| initval
| endval
......@@ -232,6 +233,16 @@ typedef pair<int, Type> ExpObj;
}
;
cutoff
: CUTOFF FLOAT_NUMBER ';'
{
driver.cutoff($2);
}
| CUTOFF EQUAL FLOAT_NUMBER ';'
{
driver.cutoff($3);
}
;
init_param
: NAME EQUAL expression ';'
......@@ -296,7 +307,13 @@ typedef pair<int, Type> ExpObj;
initval
: INITVAL ';' initval_list END
{driver.end_initval();}
| INITVAL '(' initval_option ')' ';' initval_list END
{driver.end_initval();}
;
initval_option
: FILENAME EQUAL NAME {driver.init_val_filename($3);}
;
endval
: ENDVAL ';' initval_list END
......@@ -327,6 +344,16 @@ typedef pair<int, Type> ExpObj;
: NAME '(' signed_integer ')' EQUAL expression ';'
{driver.hist_val($1, $3, $6);}
;
model_sparse_options_list : model_sparse_options_list COMMA model_sparse_options
| model_sparse_options
;
model_sparse_options :
LCC_COMPILER { driver.init_compiler(0); }
| GCC_COMPILER { driver.init_compiler(1); }
| o_cutoff
;
model
: MODEL ';' { driver.begin_model(); } equation_list END
......@@ -334,6 +361,10 @@ typedef pair<int, Type> ExpObj;
equation_list END
| MODEL '(' USE_DLL ')' ';' { driver.begin_model(); driver.use_dll(); }
equation_list END
| MODEL '(' SPARSE_DLL COMMA model_sparse_options_list ')' { driver.sparse_dll(); driver.begin_model(); } ';'
equation_list END
| MODEL '(' SPARSE_DLL ')' { driver.sparse_dll(); driver.begin_model(); } ';'
equation_list END
;
equation_list
......@@ -529,9 +560,9 @@ typedef pair<int, Type> ExpObj;
simul
: SIMUL ';'
{driver.simul();}
{driver.simulate();}
| SIMUL '(' simul_options_list ')' ';'
{driver.simul();}
{driver.simulate();}
;
simul_options_list: simul_options_list COMMA simul_options
......@@ -572,6 +603,7 @@ typedef pair<int, Type> ExpObj;
| o_hp_filter
| o_hp_ngrid
| o_periods
| o_cutoff
| o_simul
| o_simul_seed
| o_qz_criterium
......@@ -1091,6 +1123,7 @@ typedef pair<int, Type> ExpObj;
o_hp_filter: HP_FILTER EQUAL INT_NUMBER {driver.option_num("hp_filter", $3);};
o_hp_ngrid: HP_NGRID EQUAL INT_NUMBER {driver.option_num("hp_ngrid", $3);};
o_periods: PERIODS EQUAL INT_NUMBER {driver.option_num("periods", $3); driver.option_num("simul", "1");};
o_cutoff: CUTOFF EQUAL FLOAT_NUMBER {driver.option_num("cutoff", $3);}
o_simul: SIMUL {driver.option_num("simul", "1");};
o_simul_seed: SIMUL_SEED EQUAL INT_NUMBER { driver.option_num("simul_seed", $3)};
o_qz_criterium: QZ_CRITERIUM EQUAL INT_NUMBER { driver.option_num("qz_criterium", $3)}
......
......@@ -57,6 +57,7 @@ int sigma_e = 0;
<INITIAL>varexo_det {BEGIN DYNARE_STATEMENT; return token::VAREXO_DET;}
<INITIAL>parameters {BEGIN DYNARE_STATEMENT; return token::PARAMETERS;}
<INITIAL>periods {BEGIN DYNARE_STATEMENT; return token::PERIODS;}
<INITIAL>cutoff {BEGIN DYNARE_STATEMENT; return token::CUTOFF;}
<INITIAL>estimation {BEGIN DYNARE_STATEMENT; return token::ESTIMATION;}
<INITIAL>prior_analysis {BEGIN DYNARE_STATEMENT; return token::PRIOR_ANALYSIS;}
<INITIAL>posterior_analysis {BEGIN DYNARE_STATEMENT; return token::POSTERIOR_ANALYSIS;}
......@@ -148,6 +149,7 @@ int sigma_e = 0;
<DYNARE_STATEMENT>nocorr {return token::NOCORR;}
<DYNARE_STATEMENT>optim {return token::OPTIM;}
<DYNARE_STATEMENT>periods {return token::PERIODS;}
<DYNARE_STATEMENT>cutoff {return token::CUTOFF;}
<DYNARE_STATEMENT>model_comparison_approximation {return token::MODEL_COMPARISON;}
<DYNARE_STATEMENT>laplace {return token::LAPLACE;}
<DYNARE_STATEMENT>modifiedharmonicmean {return token::MODIFIEDHARMONICMEAN;}
......@@ -167,7 +169,7 @@ int sigma_e = 0;
<DYNARE_BLOCK>values {return token::VALUES;}
<DYNARE_BLOCK>corr {return token::CORR;}
<DYNARE_BLOCK>periods {return token::PERIODS;}
<DYNARE_BLOCK>filename {return token::FILENAME;}
<DYNARE_BLOCK>gamma_pdf {return token::GAMMA_PDF;}
<DYNARE_BLOCK>beta_pdf {return token::BETA_PDF;}
<DYNARE_BLOCK>normal_pdf {return token::NORMAL_PDF;}
......@@ -208,6 +210,9 @@ int sigma_e = 0;
<DYNARE_STATEMENT>[\'] {return yy::parser::token_type (yytext[0]);}
<DYNARE_STATEMENT,DYNARE_BLOCK>use_dll {return token::USE_DLL;}
<DYNARE_STATEMENT,DYNARE_BLOCK>sparse_dll {return token::SPARSE_DLL;}
<DYNARE_STATEMENT,DYNARE_BLOCK>gcc_compiler {return token::GCC_COMPILER;}
<DYNARE_STATEMENT,DYNARE_BLOCK>lcc_compiler {return token::LCC_COMPILER;}
<DYNARE_STATEMENT,DYNARE_BLOCK>linear {return token::LINEAR;}
<DYNARE_STATEMENT,DYNARE_BLOCK>[,] {return token::COMMA;}
<DYNARE_STATEMENT,DYNARE_BLOCK>[:] {return yy::parser::token_type (yytext[0]);}
......
......@@ -2,6 +2,8 @@
#include <iterator>
#include <algorithm>
#include <math.h>
#include "ExprNode.hh"
#include "DataTree.hh"
......@@ -52,6 +54,18 @@ ExprNode::cost(const temporary_terms_type &temporary_terms) const
return 0;
}
int
ExprNode::present_endogenous_size() const
{
return(present_endogenous.size());
}
int
ExprNode::present_endogenous_find(int var, int lag) const
{
return(present_endogenous.find(make_pair(var,lag))!=present_endogenous.end());
}
void
ExprNode::computeTemporaryTerms(map<NodeID, int> &reference_count,
temporary_terms_type &temporary_terms) const
......@@ -59,6 +73,16 @@ ExprNode::computeTemporaryTerms(map<NodeID, int> &reference_count,
// Nothing to do for a terminal node
}
void
ExprNode::computeTemporaryTerms(map<NodeID, int> &reference_count,
temporary_terms_type &temporary_terms,
map<NodeID, int> &first_occurence,
int Curr_block,
Model_Block *ModelBlock) const
{
// Nothing to do for a terminal node
}
NumConstNode::NumConstNode(DataTree &datatree_arg, int id_arg) :
ExprNode(datatree_arg),
id(id_arg)
......@@ -77,15 +101,29 @@ NumConstNode::computeDerivative(int varID)
void
NumConstNode::writeOutput(ostream &output, bool is_dynamic,
const temporary_terms_type &temporary_terms) const
const temporary_terms_type &temporary_terms, int offset) const
{
temporary_terms_type::const_iterator it = temporary_terms.find(const_cast<NumConstNode *>(this));
if (it != temporary_terms.end())
output << "T" << idx;
if (offset != 2)
output << "T" << idx;
else
output << "T" << idx << "[it_]";
else
output << datatree.num_constants.get(id);
}
void
NumConstNode::Evaluate() const
{
datatree.interprete_.Stack.push(atof(datatree.num_constants.get(id).c_str()));
}
void
NumConstNode::collectEndogenous(NodeID &Id)
{
}
VariableNode::VariableNode(DataTree &datatree_arg, int id_arg, Type type_arg) :
ExprNode(datatree_arg),
id(id_arg),
......@@ -150,13 +188,16 @@ VariableNode::computeDerivative(int varID)
void
VariableNode::writeOutput(ostream &output, bool is_dynamic,
const temporary_terms_type &temporary_terms) const
const temporary_terms_type &temporary_terms, int offset) const
{
// If node is a temporary term
temporary_terms_type::const_iterator it = temporary_terms.find(const_cast<VariableNode *>(this));
if (it != temporary_terms.end())
{
output << "T" << idx;
if (offset != 2)
output << "T" << idx;
else
output << "T" << idx << "[it_]";
return;
}
......@@ -167,7 +208,10 @@ VariableNode::writeOutput(ostream &output, bool is_dynamic,
switch(type)
{
case eParameter:
output << "params" << lpar << id + datatree.offset << rpar;
if (datatree.offset < 2)
output << "params" << lpar << id + datatree.offset << rpar;
else
output << "params" << lpar << id << rpar;
break;
case eLocalParameter:
output << datatree.symbol_table.getNameByID(eLocalParameter, id);
......@@ -175,17 +219,39 @@ VariableNode::writeOutput(ostream &output, bool is_dynamic,
case eEndogenous:
if (is_dynamic)
{
idx = datatree.variable_table.getPrintIndex(id) + datatree.offset;
output << "y" << lpar << idx << rpar;
if (datatree.offset < 2)
idx = datatree.variable_table.getPrintIndex(id) + datatree.offset;
else
idx = datatree.variable_table.getSymbolID(id);
if (datatree.offset == 2)
{
int l = datatree.variable_table.getLag((long int) id);
if (l > 0)
output << "y" << lpar << "(it_+" << l << ")*y_size+" << idx << rpar;
else if (l < 0)
output << "y" << lpar << "(it_" << l << ")*y_size+" << idx << rpar;
else
output << "y" << lpar << "Per_y_+" << idx << rpar;
}
else
output << "y" << lpar << idx << rpar;
}
else
{
idx = datatree.variable_table.getSymbolID(id) + datatree.offset;
if (datatree.offset < 2)
idx = datatree.variable_table.getSymbolID(id) + datatree.offset;
else
idx = datatree.variable_table.getSymbolID(id);
output << "y" << lpar << idx << rpar;
}
break;
case eExogenous:
idx = datatree.variable_table.getSymbolID(id) + datatree.offset;
if (datatree.offset < 2)
idx = datatree.variable_table.getSymbolID(id) + datatree.offset;
else
idx = datatree.variable_table.getSymbolID(id);
if (is_dynamic)
{
int lag = datatree.variable_table.getLag(id);
......@@ -195,30 +261,40 @@ VariableNode::writeOutput(ostream &output, bool is_dynamic,
else
output << "x" << lpar << "it_, " << idx << rpar;
else
if (lag != 0)
if (lag == 0)
output << "x" << lpar << "it_+" << idx << "*nb_row_x" << rpar;
else if (lag > 0)
output << "x" << lpar << "it_+" << lag << "+" << idx << "*nb_row_x" << rpar;
else
output << "x" << lpar << "it_+" << idx << "*nb_row_x" << rpar;
output << "x" << lpar << "it_" << lag << "+" << idx << "*nb_row_x" << rpar;
}
else
output << "x" << lpar << idx << rpar;
break;
case eExogenousDet:
idx = datatree.variable_table.getSymbolID(id) + datatree.symbol_table.exo_nbr
+ datatree.offset;
if (datatree.offset < 2)
idx = datatree.variable_table.getSymbolID(id) + datatree.symbol_table.exo_nbr
+ datatree.offset;
else
idx = datatree.variable_table.getSymbolID(id) + datatree.symbol_table.exo_nbr;
if (is_dynamic)
{
int lag = datatree.variable_table.getLag(id);
if (datatree.offset == 1)
if (lag != 0)
output << "x" << lpar << "it_ + " << lag << ", " << idx << rpar;
if (lag > 0)
output << "x" << lpar << "it_ +" << lag << ", " << idx << rpar;
else if (lag < 0)
output << "x" << lpar << "it_ " << lag << ", " << idx << rpar;
else
output << "x" << lpar << "it_, " << idx << rpar;
else
if (lag != 0)
output << "x" << lpar << "it_ + " << lag << "+" << idx << "*nb_row_xd" << rpar;
else
if (lag == 0)
output << "x" << lpar << "it_+" << idx << "*nb_row_xd" << rpar;
else if (lag < 0)
output << "x" << lpar << "it_ " << lag << "+" << idx << "*nb_row_xd" << rpar;
else
output << "x" << lpar << "it_ +" << lag << "+" << idx << "*nb_row_xd" << rpar;
}
else
output << "x" << lpar << idx << rpar;
......@@ -235,6 +311,26 @@ VariableNode::writeOutput(ostream &output, bool is_dynamic,
}
}
void
VariableNode::Evaluate() const
{
if (type == eParameter)
datatree.interprete_.Stack.push(datatree.interprete_.GetDataValue(id, type));
else
datatree.interprete_.Stack.push(datatree.interprete_.GetDataValue(datatree.variable_table.getSymbolID(id), type));
}
void
VariableNode::collectEndogenous(NodeID &Id)
{
int idx;
if (type == eEndogenous)
{
idx = datatree.variable_table.getSymbolID(id);
Id->present_endogenous.insert(make_pair(idx, datatree.variable_table.getLag((long int) id)));
}
}
UnaryOpNode::UnaryOpNode(DataTree &datatree_arg, UnaryOpcode op_code_arg, const NodeID arg_arg) :
ExprNode(datatree_arg),
arg(arg_arg),
......@@ -327,7 +423,7 @@ UnaryOpNode::cost(const temporary_terms_type &temporary_terms) const
int cost = arg->cost(temporary_terms);
if (datatree.offset)
if (datatree.offset == 1)
// Cost for Matlab files
switch(op_code)
{
......@@ -422,15 +518,44 @@ UnaryOpNode::computeTemporaryTerms(map<NodeID, int> &reference_count,
}
}
void
UnaryOpNode::computeTemporaryTerms(map<NodeID, int> &reference_count,
temporary_terms_type &temporary_terms,
map<NodeID, int> &first_occurence,
int Curr_block,
Model_Block *ModelBlock) const
{
NodeID this2 = const_cast<UnaryOpNode *>(this);
map<NodeID, int>::iterator it = reference_count.find(this2);
if (it == reference_count.end())
{
reference_count[this2] = 1;
first_occurence[this2] = Curr_block;
arg->computeTemporaryTerms(reference_count, temporary_terms, first_occurence, Curr_block, ModelBlock);
}
else
{
reference_count[this2]++;
if (reference_count[this2] * cost(temporary_terms) > datatree.min_cost)
{
temporary_terms.insert(this2);
ModelBlock->Block_List[first_occurence[this2]].Temporary_terms->insert(this2);
}
}
}
void
UnaryOpNode::writeOutput(ostream &output, bool is_dynamic,
const temporary_terms_type &temporary_terms) const
const temporary_terms_type &temporary_terms, int offset) const
{
// If node is a temporary term
temporary_terms_type::const_iterator it = temporary_terms.find(const_cast<UnaryOpNode *>(this));
if (it != temporary_terms.end())
{
output << "T" << idx;
if (offset != 2)
output << "T" << idx;
else
output << "T" << idx << "[it_]";
return;
}
......@@ -507,7 +632,7 @@ UnaryOpNode::writeOutput(ostream &output, bool is_dynamic,
}
// Write argument
arg->writeOutput(output, is_dynamic, temporary_terms);
arg->writeOutput(output, is_dynamic, temporary_terms, offset);
if (close_parenthesis)
output << ")";
......@@ -517,6 +642,75 @@ UnaryOpNode::writeOutput(ostream &output, bool is_dynamic,
output << ")";
}
void
UnaryOpNode::Evaluate() const
{
this->arg->Evaluate();
datatree.interprete_.u2 = datatree.interprete_.Stack.top();
datatree.interprete_.Stack.pop();
switch(op_code)
{
case oUminus:
datatree.interprete_.u1=-datatree.interprete_.u2;
break;
case oExp:
datatree.interprete_.u1=exp(datatree.interprete_.u2);
break;
case oLog:
datatree.interprete_.u1=log(datatree.interprete_.u2);
break;
case oLog10:
datatree.interprete_.u1=log10(datatree.interprete_.u2);
break;
case oCos:
datatree.interprete_.u1=cos(datatree.interprete_.u2);
break;
case oSin:
datatree.interprete_.u1=sin(datatree.interprete_.u2);
break;
case oTan:
datatree.interprete_.u1=tan(datatree.interprete_.u2);
break;
case oAcos:
datatree.interprete_.u1=acos(datatree.interprete_.u2);
break;
case oAsin:
datatree.interprete_.u1=asin(datatree.interprete_.u2);
break;
case oAtan:
datatree.interprete_.u1=atan(datatree.interprete_.u2);
break;
case oCosh:
datatree.interprete_.u1=cosh(datatree.interprete_.u2);
break;
case oSinh:
datatree.interprete_.u1=sinh(datatree.interprete_.u2);
break;
case oTanh:
datatree.interprete_.u1=tanh(datatree.interprete_.u2);
break;
case oAcosh:
datatree.interprete_.u1=acosh(datatree.interprete_.u2);
break;
case oAsinh:
datatree.interprete_.u1=asinh(datatree.interprete_.u2);
break;
case oAtanh:
datatree.interprete_.u1=atanh(datatree.interprete_.u2);
break;
case oSqrt:
datatree.interprete_.u1=sqrt(datatree.interprete_.u2);
break;
}
datatree.interprete_.Stack.push(datatree.interprete_.u1);
}
void
UnaryOpNode::collectEndogenous(NodeID &Id)
{
arg->collectEndogenous(Id);
}
BinaryOpNode::BinaryOpNode(DataTree &datatree_arg, const NodeID arg1_arg,
BinaryOpcode op_code_arg, const NodeID arg2_arg) :
ExprNode(datatree_arg),
......@@ -606,7 +800,7 @@ BinaryOpNode::precedence(const temporary_terms_type &temporary_terms) const
case oDivide:
return 1;
case oPower:
if (datatree.offset)
if (datatree.offset == 1)
// In C, power operator is of the form pow(a, b)
return 100;
else
......@@ -627,7 +821,7 @@ BinaryOpNode::cost(const temporary_terms_type &temporary_terms) const
int cost = arg1->cost(temporary_terms);
cost += arg2->cost(temporary_terms);
if (datatree.offset)
if (datatree.offset == 1)
// Cost for Matlab files
switch(op_code)
{
......@@ -685,25 +879,88 @@ BinaryOpNode::computeTemporaryTerms(map<NodeID, int> &reference_count,
}
}