ParsingDriver.hh 14 KB
Newer Older
1
2
3
4
#ifndef _PARSING_DRIVER_HH
#define _PARSING_DRIVER_HH

#include <iostream>
sebastien's avatar
sebastien committed
5
#include <vector>
6

sebastien's avatar
sebastien committed
7
8
#include <math.h>

9
#include "ModFile.hh"
10
11
#include "TmpSymbolTable.hh"
#include "DynareBison.hh"
12
#include "ComputingTasks.hh"
sebastien's avatar
sebastien committed
13
14
15
#include "Shocks.hh"
#include "SigmaeInitialization.hh"
#include "NumericalInitialization.hh"
sebastien's avatar
sebastien committed
16
#include "ModelTree.hh"
17

18
19
using namespace std;

20
21
22
// Announce to Flex the prototype we want for lexing function, ...
#define YY_DECL                                              \
  yy::parser::token_type                                     \
23
24
25
  yylex(yy::parser::semantic_type *yylval,                   \
        yy::parser::location_type *yylloc,                   \
        ParsingDriver &driver)
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// ... and declare it for the parser's sake.
YY_DECL;

//! Drives the scanning and parsing of the .mod file, and constructs its abstract representation
/*! It is built along the guidelines given in Bison 2.3 manual. */
class ParsingDriver
{
private:
  //! Start scanning
  /*! Body defined at the end of DynareFlex.ll, for convenience reasons. */
  void scan_begin();

  //! Stop scanning
  /*! Body defined at the end of DynareFlex.ll, for convenience reasons. */
  void scan_end();

42
43
44
  //! Checks that a given symbol exists, and stops with an error message if it doesn't
  void check_symbol_existence(const string &name);

sebastien's avatar
sebastien committed
45
46
47
  //! Creates option "optim_opt" in OptionsList if it doesn't exist, else add a comma, and adds the option name
  void optim_options_helper(const string &name);

48
  //! Stores temporary symbol table
sebastien's avatar
sebastien committed
49
  TmpSymbolTable *tmp_symbol_table;
sebastien's avatar
sebastien committed
50

sebastien's avatar
sebastien committed
51
52
53
  //! The data tree in which to add expressions currently parsed
  DataTree *data_tree;

sebastien's avatar
sebastien committed
54
  //! The model tree in which to add expressions currently parsed
sebastien's avatar
sebastien committed
55
  /*! It is only a dynamic cast of data_tree pointer, and is therefore null if data_tree is not a ModelTree instance */
sebastien's avatar
sebastien committed
56
57
  ModelTree *model_tree;

sebastien's avatar
sebastien committed
58
59
60
  //! Sets data_tree and model_tree pointers
  void set_current_data_tree(DataTree *data_tree_arg);

sebastien's avatar
sebastien committed
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
  //! Stores options lists
  OptionsList options_list;
  //! Temporary storage for trend elements
  ObservationTrendsStatement::trend_elements_type trend_elements;
  //! Temporary storage for filename list of ModelComparison
  ModelComparisonStatement::filename_list_type filename_list;
  //! Temporary storage for list of EstimationParams (from estimated_params* statements)
  vector<EstimationParams> estim_params_list;
  //! Temporary storage of variances from optim_weights
  OptimWeightsStatement::var_weights_type var_weights;
  //! Temporary storage of covariances from optim_weights
  OptimWeightsStatement::covar_weights_type covar_weights;
  //! Temporary storage of variances from calib_var
  CalibVarStatement::calib_var_type calib_var;
  //! Temporary storage of covariances from calib_var
  CalibVarStatement::calib_covar_type calib_covar;
  //! Temporary storage of autocorrelations from calib_var
  CalibVarStatement::calib_ac_type calib_ac;
  //! Temporary storage for deterministic shocks
  ShocksStatement::det_shocks_type det_shocks;
  //! Temporary storage for periods of deterministic shocks
  vector<pair<int, int> > det_shocks_periods;
  //! Temporary storage for values of deterministic shocks
sebastien's avatar
sebastien committed
84
  vector<NodeID> det_shocks_values;
sebastien's avatar
sebastien committed
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
  //! Temporary storage for variances of shocks
  ShocksStatement::var_and_std_shocks_type var_shocks;
  //! Temporary storage for standard errors of shocks
  ShocksStatement::var_and_std_shocks_type std_shocks;
  //! Temporary storage for covariances of shocks
  ShocksStatement::covar_and_corr_shocks_type covar_shocks;
  //! Temporary storage for correlations of shocks
  ShocksStatement::covar_and_corr_shocks_type corr_shocks;
  //! Temporary storage for Sigma_e rows
  SigmaeStatement::row_type sigmae_row;
  //! Temporary storage for Sigma_e matrix
  SigmaeStatement::matrix_type sigmae_matrix;
  //! Temporary storage for initval/endval blocks
  InitOrEndValStatement::init_values_type init_values;
  //! Temporary storage for histval blocks
  HistValStatement::hist_values_type hist_values;
101

sebastien's avatar
sebastien committed
102
103
104
  //! Temporary storage for argument list of unknown function
  vector<NodeID> unknown_function_args;

105
106
107
108
  //! The mod file representation constructed by this ParsingDriver
  ModFile *mod_file;

public:
109
110
111
112
113
  //! Constructor
  ParsingDriver();
  //! Destructor
  virtual ~ParsingDriver();

114
115
116
117
118
119
  //! Starts parsing, and constructs the MOD file representation
  /*! \param f Name of file to parse

      The returned pointer should be deleted after use.
   */
  ModFile *parse(const string &f);
120
121

  //! Name of file being parsed
122
  string file;
123
124
125
126
127
128
129
130
131
132

  //! Trace scanning ?
  /*! If set to true before calling parse(), the flex scanner will dump a lot of debugging information. Defaults to false.
  */
  bool trace_scanning;

  //! Trace parsing ?
  /*! If set to true before calling parse(), the bison parser will dump debugging information. Defaults to false. */
  bool trace_parsing;

133
134
135
  //! Estimation parameters
  EstimationParams estim_params;

136
  //! Error handler with location
137
  void error(const yy::parser::location_type &l, const string &m);
138
  //! Error handler without location
139
  void error(const string &m);
140
141
142
143
144
145

  //! Static error handler
  /*! To be removed in the future. */
  static void error(const char *m)
  {
    extern int yylineno;
146
    cerr << "Error at line " << yylineno << ": " << m << endl;
147
148
149
    exit(-1);
  }

sebastien's avatar
sebastien committed
150
151
  //! 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);
sebastien's avatar
sebastien committed
152
  //! Sets mode of ModelTree class to use C output
153
  void use_dll();
sebastien's avatar
sebastien committed
154
  //! Sets mode of ModelTree class to block decompose the model and triggers the creation of the incidence matrix
sebastien's avatar
sebastien committed
155
156
157
158
159
  void sparse_dll();
  //! Sets the compiler type used in conjunction with SPARCE_DLL
  void init_compiler(int compiler_type);
  //! Sets the FILENAME for the initial value in initval
  void init_val_filename(string *filename);
160
161
162
163
164
165
166
167
168
  //! Declares an endogenous variable by adding it to SymbolTable
  void declare_endogenous(string *name, string *tex_name = new string);
  //! Declares an exogenous variable by adding it to SymbolTable
  void declare_exogenous(string *name, string *tex_name = new string);
  //! Declares an exogenous deterministic variable by adding it to SymbolTable
  void declare_exogenous_det(string *name, string *tex_name = new string);
  //! Declares a parameter by adding it to SymbolTable
  void declare_parameter(string *name, string *tex_name = new string);
  //! Declares and initializes a local parameter
sebastien's avatar
sebastien committed
169
  void declare_and_init_model_local_variable(string *name, NodeID rhs);
sebastien's avatar
sebastien committed
170
171
  //! Adds a constant to DataTree
  NodeID add_constant(string *constant);
172
173
174
175
176
  //! Adds a model variable to ModelTree and VariableTable
  NodeID add_model_variable(string *name);
  //! Adds a model lagged variable to ModelTree and VariableTable
  NodeID add_model_variable(string *name, string *olag);
  //! Adds an Expression's variable
sebastien's avatar
sebastien committed
177
  NodeID add_expression_variable(string *name);
sebastien's avatar
sebastien committed
178
179
  //! Adds a "periods" statement
  void periods(string *periods);
sebastien's avatar
sebastien committed
180
181
  //! Adds a "cutoff" statement
  void cutoff(string *cutoff);
sebastien's avatar
sebastien committed
182
183
184
185
  //! Adds a "dsample" statement
  void dsample(string *arg1);
  //! Adds a "dsample" statement
  void dsample(string *arg1, string *arg2);
186
  //! Writes parameter intitialisation expression
sebastien's avatar
sebastien committed
187
  void init_param(string *name, NodeID rhs);
188
  //! Writes an initval block
sebastien's avatar
sebastien committed
189
  void init_val(string *name, NodeID rhs);
190
  //! Writes an histval block
sebastien's avatar
sebastien committed
191
  void hist_val(string *name, string *lag, NodeID rhs);
192
  //! Writes end of an initval block
193
  void end_initval();
194
  //! Writes end of an endval block
195
  void end_endval();
sebastien's avatar
sebastien committed
196
197
  //! Writes end of an histval block
  void end_histval();
sebastien's avatar
sebastien committed
198
199
  //! Begin a model block
  void begin_model();
sebastien's avatar
sebastien committed
200
  //! Writes a shocks statement
201
  void end_shocks();
sebastien's avatar
sebastien committed
202
203
  //! Writes a mshocks statement
  void end_mshocks();
204
  //! Adds a deterministic chock
205
  void add_det_shock(string *var);
206
  //! Adds a std error chock
sebastien's avatar
sebastien committed
207
  void add_stderr_shock(string *var, NodeID value);
208
  //! Adds a variance chock
sebastien's avatar
sebastien committed
209
  void add_var_shock(string *var, NodeID value);
210
  //! Adds a covariance chock
sebastien's avatar
sebastien committed
211
  void add_covar_shock(string *var1, string *var2, NodeID value);
212
  //! Adds a correlated chock
sebastien's avatar
sebastien committed
213
  void add_correl_shock(string *var1, string *var2, NodeID value);
214
  //! Adds a shock period range 
215
  void add_period(string *p1, string *p2);
216
  //! Adds a shock period 
217
  void add_period(string *p1);
sebastien's avatar
sebastien committed
218
219
220
221
222
223
  //! Adds a shock value (when only a numerical constant)
  void add_value_const(string *value);
  //! Adds a shock value (when only a variable name)
  void add_value_var(string *name);
  //! Adds a shock value (when it is a complete expression)
  void add_value(NodeID value);
224
  //! Writes a Sigma_e block
225
  void do_sigma_e();
226
  //! Ends row of Sigma_e block
227
  void end_of_row();
sebastien's avatar
sebastien committed
228
229
230
231
  //! Adds a constant element to current row of Sigma_e
  void add_to_row_const(string *s);
  //! Adds an expression element to current row of Sigma_e
  void add_to_row(NodeID v);
232
  //! Write a steady command
233
  void steady();
234
  //! Sets an option to a numerical value
235
236
237
  void option_num(const string &name_option, string *opt);
  //! Sets an option to a numerical value
  void option_num(const string &name_option, const string &opt);
238
  //! Sets an option to a numerical value
239
240
241
242
243
  void option_num(const string &name_option, string *opt1, string *opt2);
  //! Sets an option to a string value
  void option_str(const string &name_option, string *opt);
  //! Sets an option to a string value
  void option_str(const string &name_option, const string &opt);
sebastien's avatar
sebastien committed
244
245
  //! Indicates that the model is linear
  void linear();
246
  //! Adds a variable to temp symbol table and sets its value
247
  void add_tmp_var(string *tmp_var1, string *tmp_var2);
248
  //! Adds a variable to temp symbol table
249
  void add_tmp_var(string *tmp_var);
250
  //! Writes a rplot() command
251
  void rplot();
252
  //! Writes a stock_simul command
253
  void stoch_simul();
sebastien's avatar
sebastien committed
254
255
256
257
  //! Determine whether to write simul command or simul_sparse command
  void simulate();
  //! Writes a simul_sparse command
  void simul_sparse();
258
  //! Writes a simul command
259
  void simul();
260
  //! Writes check command
261
  void check();
sebastien's avatar
sebastien committed
262
263
264
265
266
267
268
269
  //! Writes estimated params command
  void estimated_params();
  //! Writes estimated params init command
  void estimated_params_init();
  //! Writes estimated params bound command
  void estimated_params_bounds();
  //! Add a line in an estimated params block
  void add_estimated_params_element();
270
  //! Runs estimation process
271
  void run_estimation();
michel's avatar
michel committed
272
273
274
275
  //! Runs prior_analysis();
  void run_prior_analysis();
  //! Runs posterior_analysis();
  void run_posterior_analysis();
sebastien's avatar
sebastien committed
276
277
278
279
  //! Adds an optimization option (string value)
  void optim_options_string(string *name, string *value);
  //! Adds an optimization option (numeric value)
  void optim_options_num(string *name, string *value);
280
  //! Prints varops instructions
281
  void set_varobs();
sebastien's avatar
sebastien committed
282
  void set_trends();
sebastien's avatar
sebastien committed
283
  void set_trend_element(string *arg1, NodeID arg2);
284
  void set_unit_root_vars();
sebastien's avatar
sebastien committed
285
  void optim_weights();
sebastien's avatar
sebastien committed
286
287
  void set_optim_weights(string *name, NodeID value);
  void set_optim_weights(string *name1, string *name2, NodeID value);
288
289
290
291
  void set_osr_params();
  void run_osr();
  void set_olr_inst();
  void run_olr();
sebastien's avatar
sebastien committed
292
  void run_calib_var();
sebastien's avatar
sebastien committed
293
294
295
  void set_calib_var(string *name, string *weight, NodeID expression);
  void set_calib_covar(string *name1, string *name2, string *weight, NodeID expression);
  void set_calib_ac(string *name, string *ar, string *weight, NodeID expression);
sebastien's avatar
sebastien committed
296
  void run_calib(int covar);
297
298
299
  void run_dynasave(string *arg1, string *arg2 = new string);
  void run_dynatype(string *arg1, string *arg2 = new string);
  void add_mc_filename(string *filename, string *prior = new string("1"));
300
  void run_model_comparison();
sebastien's avatar
sebastien committed
301
302
303
304
  //! Begin a planner_objective statement
  void begin_planner_objective();
  //! End a planner objective statement
  void end_planner_objective(NodeID expr);
michel's avatar
michel committed
305
306
  //! ramsey policy statement
  void ramsey_policy();
307
  //! Writes token "arg1=arg2" to model tree
308
309
310
  NodeID add_model_equal(NodeID arg1, NodeID arg2);
  //! Writes token "arg=0" to model tree
  NodeID add_model_equal_with_zero_rhs(NodeID arg);
311
  //! Writes token "arg1+arg2" to model tree
sebastien's avatar
sebastien committed
312
  NodeID add_plus(NodeID arg1, NodeID arg2);
313
  //! Writes token "arg1-arg2" to model tree
sebastien's avatar
sebastien committed
314
  NodeID add_minus(NodeID arg1,  NodeID arg2);
315
  //! Writes token "-arg1" to model tree
sebastien's avatar
sebastien committed
316
  NodeID add_uminus(NodeID arg1);
317
  //! Writes token "arg1*arg2" to model tree
sebastien's avatar
sebastien committed
318
  NodeID add_times(NodeID arg1,  NodeID arg2);
319
  //! Writes token "arg1/arg2" to model tree
sebastien's avatar
sebastien committed
320
  NodeID add_divide(NodeID arg1,  NodeID arg2);
321
  //! Writes token "arg1^arg2" to model tree
sebastien's avatar
sebastien committed
322
  NodeID add_power(NodeID arg1,  NodeID arg2);
323
  //! Writes token "exp(arg1)" to model tree
sebastien's avatar
sebastien committed
324
  NodeID add_exp(NodeID arg1);
325
  //! Writes token "log(arg1)" to model tree
sebastien's avatar
sebastien committed
326
  NodeID add_log(NodeID arg1);
327
  //! Writes token "log10(arg1)" to model tree
sebastien's avatar
sebastien committed
328
  NodeID add_log10(NodeID arg1);
329
  //! Writes token "cos(arg1)" to model tree
sebastien's avatar
sebastien committed
330
  NodeID add_cos(NodeID arg1);
331
  //! Writes token "sin(arg1)" to model tree
sebastien's avatar
sebastien committed
332
  NodeID add_sin(NodeID arg1);
333
  //! Writes token "tan(arg1)" to model tree
sebastien's avatar
sebastien committed
334
  NodeID add_tan(NodeID arg1);
335
  //! Writes token "acos(arg1)" to model tree
sebastien's avatar
sebastien committed
336
  NodeID add_acos(NodeID arg1);
337
  //! Writes token "asin(arg1)" to model tree
sebastien's avatar
sebastien committed
338
  NodeID add_asin(NodeID arg1);
339
  //! Writes token "atan(arg1)" to model tree
sebastien's avatar
sebastien committed
340
  NodeID add_atan(NodeID arg1);
341
  //! Writes token "cosh(arg1)" to model tree
sebastien's avatar
sebastien committed
342
  NodeID add_cosh(NodeID arg1);
343
  //! Writes token "sinh(arg1)" to model tree
sebastien's avatar
sebastien committed
344
  NodeID add_sinh(NodeID arg1);
345
  //! Writes token "tanh(arg1)" to model tree
sebastien's avatar
sebastien committed
346
  NodeID add_tanh(NodeID arg1);
347
  //! Writes token "acosh(arg1)" to model tree
sebastien's avatar
sebastien committed
348
  NodeID add_acosh(NodeID arg1);
349
  //! Writes token "asin(arg1)" to model tree
sebastien's avatar
sebastien committed
350
  NodeID add_asinh(NodeID arg1);
351
  //! Writes token "atanh(arg1)" to model tree
sebastien's avatar
sebastien committed
352
  NodeID add_atanh(NodeID arg1);
353
  //! Writes token "sqrt(arg1)" to model tree
sebastien's avatar
sebastien committed
354
355
356
357
358
  NodeID add_sqrt(NodeID arg1);
  //! Adds an unknwon function argument
  void add_unknown_function_arg(NodeID arg);
  //! Adds an unknown function call node
  NodeID add_unknown_function(string *function_name);
359
360
  //! Adds a native statement
  void add_native(const char *s);
sebastien's avatar
sebastien committed
361
362
  //! Resets data_tree and model_tree pointers to default (i.e. mod_file->expressions_tree)
  void reset_data_tree();
363
364
365
};

#endif // ! PARSING_DRIVER_HH