ParsingDriver.cc 27.5 KB
Newer Older
1
#include "ParsingDriver.hh"
sebastien's avatar
sebastien committed
2
#include "Statement.hh"
3

4
ParsingDriver::ParsingDriver() : trace_scanning(false), trace_parsing(false)
5
6
7
{
}

8
9
10
11
ParsingDriver::~ParsingDriver()
{
}

12
bool
sebastien's avatar
sebastien committed
13
ParsingDriver::symbol_exists_and_is_not_modfile_local_variable(const char *s)
14
{
sebastien's avatar
sebastien committed
15
16
17
18
  if (!mod_file->symbol_table.Exist(s))
    return false;

  return(mod_file->symbol_table.getType(s) != eModFileLocalVariable);
19
20
}

21
void
22
23
ParsingDriver::check_symbol_existence(const string &name)
{
24
  if (!mod_file->symbol_table.Exist(name))
25
26
27
    error("Unknown symbol: " + name);
}

sebastien's avatar
sebastien committed
28
29
void
ParsingDriver::set_current_data_tree(DataTree *data_tree_arg)
30
{
sebastien's avatar
sebastien committed
31
32
33
34
35
36
37
38
  data_tree = data_tree_arg;
  model_tree = dynamic_cast<ModelTree *>(data_tree_arg);
}

void
ParsingDriver::reset_data_tree()
{
  set_current_data_tree(&mod_file->expressions_tree);
39
40
}

41
ModFile *
42
ParsingDriver::parse(const string &f)
43
{
sebastien's avatar
sebastien committed
44
45
46
47
48
49
  mod_file = new ModFile();

  mod_file->symbol_table.error = error;

  tmp_symbol_table = new TmpSymbolTable(mod_file->symbol_table);

sebastien's avatar
sebastien committed
50
51
  reset_data_tree();

52
53
54
55
56
57
  file = f;
  scan_begin();
  yy::parser parser(*this);
  parser.set_debug_level(trace_parsing);
  parser.parse();
  scan_end();
sebastien's avatar
sebastien committed
58
59
60

  delete tmp_symbol_table;

61
  return mod_file;
62
63
64
}

void
65
ParsingDriver::error(const yy::parser::location_type &l, const string &m)
66
{
67
  cerr << l << ": " << m << endl;
68
69
70
71
  exit(-1);
}

void
72
ParsingDriver::error(const string &m)
73
{
74
75
  extern int yylineno;
  cerr << file << ":" << yylineno << ": " << m << endl;
76
77
78
  exit(-1);
}

79
80
void
ParsingDriver::declare_endogenous(string *name, string *tex_name)
81
{
82
  mod_file->symbol_table.AddSymbolDeclar(*name, eEndogenous, *tex_name);
83
84
  delete name;
  delete tex_name;
85
86
}

87
88
void
ParsingDriver::declare_exogenous(string *name, string *tex_name)
89
{
90
  mod_file->symbol_table.AddSymbolDeclar(*name, eExogenous, *tex_name);
91
92
  delete name;
  delete tex_name;
93
94
}

95
96
void
ParsingDriver::declare_exogenous_det(string *name, string *tex_name)
97
{
98
  mod_file->symbol_table.AddSymbolDeclar(*name, eExogenousDet, *tex_name);
99
100
  delete name;
  delete tex_name;
101
102
}

103
104
void
ParsingDriver::declare_parameter(string *name, string *tex_name)
105
{
106
  mod_file->symbol_table.AddSymbolDeclar(*name, eParameter, *tex_name);
107
108
  delete name;
  delete tex_name;
109
110
}

111
NodeID
sebastien's avatar
sebastien committed
112
ParsingDriver::add_constant(string *constant)
113
{
sebastien's avatar
sebastien committed
114
  NodeID id = data_tree->AddNumConstant(*constant);
115
  delete constant;
sebastien's avatar
sebastien committed
116
  return id;
117
118
}

119
120
NodeID
ParsingDriver::add_model_variable(string *name)
121
{
122
  check_symbol_existence(*name);
sebastien's avatar
sebastien committed
123
  NodeID id = model_tree->AddVariable(*name);
sebastien's avatar
sebastien committed
124
125

  Type type = mod_file->symbol_table.getType(*name);
sebastien's avatar
sebastien committed
126
127
128
129

  if (type == eModFileLocalVariable)
    error("Variable " + *name + " not allowed inside model declaration. Its scope is only outside model.");

sebastien's avatar
sebastien committed
130
  if ((type == eEndogenous) && (model_tree->mode == eSparseDLLMode))
sebastien's avatar
sebastien committed
131
132
    {
      int ID = mod_file->symbol_table.getID(*name);
sebastien's avatar
sebastien committed
133
      model_tree->block_triangular.fill_IM(model_tree->equation_number(), ID, 0);
sebastien's avatar
sebastien committed
134
135
    }

136
137
  delete name;
  return id;
138
139
}

140
141
NodeID
ParsingDriver::add_model_variable(string *name, string *olag)
142
{
143
  check_symbol_existence(*name);
144
  Type type = mod_file->symbol_table.getType(*name);
145
  int lag = atoi(olag->c_str());
146

sebastien's avatar
sebastien committed
147
148
149
  if (type == eModFileLocalVariable)
    error("Variable " + *name + " not allowed inside model declaration. Its scope is only outside model.");

150
  if ((type == eExogenous) && lag != 0)
151
    {
152
153
154
      cout << "Warning: exogenous variable "
           << *name
           << " has lag " << lag << endl;
155
    }
sebastien's avatar
sebastien committed
156
  NodeID id = model_tree->AddVariable(*name, lag);
sebastien's avatar
sebastien committed
157

sebastien's avatar
sebastien committed
158
159
  if ((type == eEndogenous) && (model_tree->mode == eSparseDLLMode))
    model_tree->block_triangular.fill_IM(model_tree->equation_number(), mod_file->symbol_table.getID(*name), lag);
sebastien's avatar
sebastien committed
160

161
162
163
  delete name;
  delete olag;
  return id;
164
165
}

sebastien's avatar
sebastien committed
166
NodeID
167
ParsingDriver::add_expression_variable(string *name)
168
{
sebastien's avatar
sebastien committed
169
170
171
172
  // 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);

sebastien's avatar
sebastien committed
173
  NodeID id = data_tree->AddVariable(*name);
sebastien's avatar
sebastien committed
174

175
  delete name;
sebastien's avatar
sebastien committed
176
  return id;
177
178
}

179
void
sebastien's avatar
sebastien committed
180
ParsingDriver::periods(string *periods)
181
{
sebastien's avatar
sebastien committed
182
183
184
  int periods_val = atoi(periods->c_str());
  mod_file->addStatement(new PeriodsStatement(periods_val));
  delete periods;
185
186
}

sebastien's avatar
sebastien committed
187
188
189
void
ParsingDriver::cutoff(string *cutoff)
{
190
  double cutoff_val = atof(cutoff->c_str());
sebastien's avatar
sebastien committed
191
192
193
194
  mod_file->addStatement(new CutoffStatement(cutoff_val));
  delete cutoff;
}

195
196
197
198
199
200
201
202
203
void
ParsingDriver::markowitz(string *markowitz)
{
  double markowitz_val = atof(markowitz->c_str());
  mod_file->addStatement(new MarkowitzStatement(markowitz_val));
  delete markowitz;
}


204
void
sebastien's avatar
sebastien committed
205
ParsingDriver::dsample(string *arg1)
206
{
sebastien's avatar
sebastien committed
207
208
209
  int arg1_val = atoi(arg1->c_str());
  mod_file->addStatement(new DsampleStatement(arg1_val));
  delete arg1;
210
211
}

212
void
sebastien's avatar
sebastien committed
213
ParsingDriver::dsample(string *arg1, string *arg2)
214
{
sebastien's avatar
sebastien committed
215
216
217
218
219
220
221
222
  int arg1_val = atoi(arg1->c_str());
  int arg2_val = atoi(arg2->c_str());
  mod_file->addStatement(new DsampleStatement(arg1_val, arg2_val));
  delete arg1;
  delete arg2;
}

void
sebastien's avatar
sebastien committed
223
ParsingDriver::init_param(string *name, NodeID rhs)
sebastien's avatar
sebastien committed
224
225
226
227
228
{
  check_symbol_existence(*name);
  if (mod_file->symbol_table.getType(*name) != eParameter)
    error(*name + " is not a parameter");

sebastien's avatar
sebastien committed
229
  mod_file->addStatement(new InitParamStatement(*name, rhs, mod_file->symbol_table));
sebastien's avatar
sebastien committed
230

sebastien's avatar
sebastien committed
231
232
233
234
235
236
  // Update global eval context
  try
    {
      double val = rhs->eval(mod_file->global_eval_context);
      int symb_id = mod_file->symbol_table.getID(*name);
      mod_file->global_eval_context[make_pair(symb_id, eParameter)] = val;
237
     }
sebastien's avatar
sebastien committed
238
239
240
  catch(ExprNode::EvalException &e)
    {
    }
sebastien's avatar
sebastien committed
241

242
  delete name;
243
244
}

245
void
sebastien's avatar
sebastien committed
246
ParsingDriver::init_val(string *name, NodeID rhs)
247
{
sebastien's avatar
sebastien committed
248
249
250
251
252
253
254
255
  check_symbol_existence(*name);
  Type type = mod_file->symbol_table.getType(*name);

  if (type != eEndogenous
      && type != eExogenous
      && type != eExogenousDet)
    error("initval/endval: " + *name + " should be an endogenous or exogenous variable");

sebastien's avatar
sebastien committed
256
  init_values.push_back(make_pair(*name, rhs));
sebastien's avatar
sebastien committed
257

sebastien's avatar
sebastien committed
258
259
260
261
262
263
264
265
  // Update global evaluation context
  try
    {
      double val = rhs->eval(mod_file->global_eval_context);
      int symb_id = mod_file->symbol_table.getID(*name);
      mod_file->global_eval_context[make_pair(symb_id, type)] = val;
    }
  catch(ExprNode::EvalException &e)
sebastien's avatar
sebastien committed
266
267
268
    {
    }

sebastien's avatar
sebastien committed
269
  delete name;
270
271
}

sebastien's avatar
sebastien committed
272
273
274
275
276
277
278
279
void
ParsingDriver::init_val_filename(string *filename)
{
  options_list.num_options["INITVAL_FILE"] = 1;
  options_list.string_options["INITVAL_FILENAME"] = *filename;
  delete filename;
}

280
void
sebastien's avatar
sebastien committed
281
ParsingDriver::hist_val(string *name, string *lag, NodeID rhs)
282
{
sebastien's avatar
sebastien committed
283
284
  check_symbol_existence(*name);
  Type type = mod_file->symbol_table.getType(*name);
285

sebastien's avatar
sebastien committed
286
287
288
289
  if (type != eEndogenous
      && type != eExogenous
      && type != eExogenousDet)
    error("hist_val: " + *name + " should be an endogenous or exogenous variable");
290

sebastien's avatar
sebastien committed
291
292
  int ilag = atoi(lag->c_str());
  pair<string, int> key(*name, ilag);
293

sebastien's avatar
sebastien committed
294
295
  if (hist_values.find(key) != hist_values.end())
    error("hist_val: (" + *name + ", " + *lag + ") declared twice");
296

sebastien's avatar
sebastien committed
297
  hist_values[key] = rhs;
298

sebastien's avatar
sebastien committed
299
300
  delete name;
  delete lag;
sebastien's avatar
sebastien committed
301
302
}

303
void
sebastien's avatar
sebastien committed
304
ParsingDriver::use_dll()
305
{
sebastien's avatar
sebastien committed
306
  model_tree->mode = eDLLMode;
307
308
}

sebastien's avatar
sebastien committed
309
310
311
void
ParsingDriver::sparse_dll()
{
sebastien's avatar
sebastien committed
312
313
  model_tree->mode = eSparseDLLMode;
  model_tree->block_triangular.init_incidence_matrix(mod_file->symbol_table.endo_nbr);
sebastien's avatar
sebastien committed
314
315
}

316
317
void
ParsingDriver::end_initval()
318
{
319
  mod_file->addStatement(new InitValStatement(init_values, mod_file->symbol_table));
sebastien's avatar
sebastien committed
320
  init_values.clear();
321
322
}

323
324
void
ParsingDriver::end_endval()
325
{
sebastien's avatar
sebastien committed
326
327
  mod_file->addStatement(new EndValStatement(init_values, mod_file->symbol_table));
  init_values.clear();
328
329
}

330
void
sebastien's avatar
sebastien committed
331
ParsingDriver::end_histval()
332
{
sebastien's avatar
sebastien committed
333
334
  mod_file->addStatement(new HistValStatement(hist_values, mod_file->symbol_table));
  hist_values.clear();
335
336
}

sebastien's avatar
sebastien committed
337
338
339
void
ParsingDriver::begin_model()
{
sebastien's avatar
sebastien committed
340
  set_current_data_tree(&mod_file->model_tree);
sebastien's avatar
sebastien committed
341
342
}

343
void
sebastien's avatar
sebastien committed
344
ParsingDriver::end_shocks()
345
{
sebastien's avatar
sebastien committed
346
347
348
349
350
351
352
  mod_file->addStatement(new ShocksStatement(det_shocks, var_shocks, std_shocks,
                                             covar_shocks, corr_shocks, mod_file->symbol_table));
  det_shocks.clear();
  var_shocks.clear();
  std_shocks.clear();
  covar_shocks.clear();
  corr_shocks.clear();
353
354
}

355
void
sebastien's avatar
sebastien committed
356
ParsingDriver::end_mshocks()
357
{
sebastien's avatar
sebastien committed
358
359
360
361
362
363
364
  mod_file->addStatement(new MShocksStatement(det_shocks, var_shocks, std_shocks,
                                              covar_shocks, corr_shocks, mod_file->symbol_table));
  det_shocks.clear();
  var_shocks.clear();
  std_shocks.clear();
  covar_shocks.clear();
  corr_shocks.clear();
365
366
}

367
368
void
ParsingDriver::add_det_shock(string *var)
369
{
370
  check_symbol_existence(*var);
sebastien's avatar
sebastien committed
371
372
373
374
375
376
377
378
379
380
381
382
383
  Type type = mod_file->symbol_table.getType(*var);
  if (type != eExogenous && type != eExogenousDet)
    error("shocks: shocks can only be applied to exogenous variables");

  if (det_shocks.find(*var) != det_shocks.end())
    error("shocks: variable " + *var + " declared twice");

  if (det_shocks_periods.size() != det_shocks_values.size())
    error("shocks: variable " + *var + ": number of periods is different from number of shock values");

  vector<ShocksStatement::DetShockElement> v;

  for(unsigned int i = 0; i < det_shocks_periods.size(); i++)
384
    {
sebastien's avatar
sebastien committed
385
386
387
388
389
      ShocksStatement::DetShockElement dse;
      dse.period1 = det_shocks_periods[i].first;
      dse.period2 = det_shocks_periods[i].second;
      dse.value = det_shocks_values[i];
      v.push_back(dse);
390
    }
sebastien's avatar
sebastien committed
391
392
393
394
395

  det_shocks[*var] = v;

  det_shocks_periods.clear();
  det_shocks_values.clear();
396
  delete var;
397
398
}

399
void
sebastien's avatar
sebastien committed
400
ParsingDriver::add_stderr_shock(string *var, NodeID value)
401
{
402
  check_symbol_existence(*var);
sebastien's avatar
sebastien committed
403
404
405
406
  if (var_shocks.find(*var) != var_shocks.end()
      || std_shocks.find(*var) != std_shocks.end())
    error("shocks: variance or stderr of shock on " + *var + " declared twice");

sebastien's avatar
sebastien committed
407
  std_shocks[*var] = value;
sebastien's avatar
sebastien committed
408

409
  delete var;
410
411
}

412
void
sebastien's avatar
sebastien committed
413
ParsingDriver::add_var_shock(string *var, NodeID value)
414
{
415
  check_symbol_existence(*var);
sebastien's avatar
sebastien committed
416
417
418
419
  if (var_shocks.find(*var) != var_shocks.end()
      || std_shocks.find(*var) != std_shocks.end())
    error("shocks: variance or stderr of shock on " + *var + " declared twice");

sebastien's avatar
sebastien committed
420
  var_shocks[*var] = value;
sebastien's avatar
sebastien committed
421

422
  delete var;
423
424
}

425
void
sebastien's avatar
sebastien committed
426
ParsingDriver::add_covar_shock(string *var1, string *var2, NodeID value)
427
{
428
429
  check_symbol_existence(*var1);
  check_symbol_existence(*var2);
sebastien's avatar
sebastien committed
430
431
432
433
434
435
436
437
438

  pair<string, string> key(*var1, *var2), key_inv(*var2, *var1);

  if (covar_shocks.find(key) != covar_shocks.end()
      || covar_shocks.find(key_inv) != covar_shocks.end()
      || corr_shocks.find(key) != corr_shocks.end()
      || corr_shocks.find(key_inv) != corr_shocks.end())
    error("shocks: covariance or correlation shock on variable pair (" + *var1 + ", "
          + *var2 + ") declared twice");
439

sebastien's avatar
sebastien committed
440
  covar_shocks[key] = value;
sebastien's avatar
sebastien committed
441

442
443
  delete var1;
  delete var2;
444
445
}

446
void
sebastien's avatar
sebastien committed
447
ParsingDriver::add_correl_shock(string *var1, string *var2, NodeID value)
448
{
449
450
  check_symbol_existence(*var1);
  check_symbol_existence(*var2);
sebastien's avatar
sebastien committed
451
452
453
454
455
456
457
458
459

  pair<string, string> key(*var1, *var2), key_inv(*var2, *var1);

  if (covar_shocks.find(key) != covar_shocks.end()
      || covar_shocks.find(key_inv) != covar_shocks.end()
      || corr_shocks.find(key) != corr_shocks.end()
      || corr_shocks.find(key_inv) != corr_shocks.end())
    error("shocks: covariance or correlation shock on variable pair (" + *var1 + ", "
          + *var2 + ") declared twice");
460

sebastien's avatar
sebastien committed
461
  corr_shocks[key] = value;
sebastien's avatar
sebastien committed
462

463
464
465
466
467
468
469
  delete var1;
  delete var2;
}

void
ParsingDriver::add_period(string *p1, string *p2)
{
sebastien's avatar
sebastien committed
470
471
  int p1_val = atoi(p1->c_str());
  int p2_val = atoi(p2->c_str());
sebastien's avatar
sebastien committed
472
  det_shocks_periods.push_back(make_pair(p1_val, p2_val));
473
474
  delete p1;
  delete p2;
475
476
}

477
478
void
ParsingDriver::add_period(string *p1)
479
{
sebastien's avatar
sebastien committed
480
  int p1_val = atoi(p1->c_str());
sebastien's avatar
sebastien committed
481
  det_shocks_periods.push_back(make_pair(p1_val, p1_val));
482
  delete p1;
483
484
}

sebastien's avatar
sebastien committed
485
486
487
488
void
ParsingDriver::add_value(NodeID value)
{
  det_shocks_values.push_back(value);
489
490
}

491
492
void
ParsingDriver::do_sigma_e()
493
{
sebastien's avatar
sebastien committed
494
495
496
497
498
499
500
501
502
  try
    {
      mod_file->addStatement(new SigmaeStatement(sigmae_matrix));
    }
  catch(SigmaeStatement::MatrixFormException &e)
    {
      error("Sigma_e: matrix is neither upper triangular nor lower triangular");
    }
  sigmae_matrix.clear();
503
504
}

505
506
void
ParsingDriver::end_of_row()
507
{
sebastien's avatar
sebastien committed
508
509
  sigmae_matrix.push_back(sigmae_row);
  sigmae_row.clear();
510
511
}

512
void
sebastien's avatar
sebastien committed
513
ParsingDriver::add_to_row_const(string *s)
514
{
sebastien's avatar
sebastien committed
515
  sigmae_row.push_back(add_constant(s));
516
517
518
}

void
sebastien's avatar
sebastien committed
519
ParsingDriver::add_to_row(NodeID v)
520
{
sebastien's avatar
sebastien committed
521
  sigmae_row.push_back(v);
522
523
}

524
525
void
ParsingDriver::steady()
526
{
sebastien's avatar
sebastien committed
527
528
  mod_file->addStatement(new SteadyStatement(options_list));
  options_list.clear();
529
530
}

531
532
void
ParsingDriver::option_num(const string &name_option, string *opt1, string *opt2)
533
{
sebastien's avatar
sebastien committed
534
535
536
537
  if (options_list.paired_num_options.find(name_option)
      != options_list.paired_num_options.end())
    error("option " + name_option + " declared twice");

sebastien's avatar
sebastien committed
538
  options_list.paired_num_options[name_option] = make_pair(*opt1, *opt2);
539
540
  delete opt1;
  delete opt2;
541
542
}

543
544
void
ParsingDriver::option_num(const string &name_option, string *opt)
545
{
546
547
  option_num(name_option, *opt);
  delete opt;
548
549
}

550
551
void
ParsingDriver::option_num(const string &name_option, const string &opt)
552
{
sebastien's avatar
sebastien committed
553
554
555
556
  if (options_list.num_options.find(name_option)
      != options_list.num_options.end())
    error("option " + name_option + " declared twice");

sebastien's avatar
sebastien committed
557
  if ((name_option == "periods") && (mod_file->model_tree.mode == eSparseDLLMode))
sebastien's avatar
sebastien committed
558
559
    mod_file->model_tree.block_triangular.periods = atoi(opt.c_str());
  else if (name_option == "cutoff")
sebastien's avatar
sebastien committed
560
    mod_file->model_tree.cutoff = atof(opt.c_str());
sebastien's avatar
sebastien committed
561

sebastien's avatar
sebastien committed
562
  options_list.num_options[name_option] = opt;
563
564
}

565
566
void
ParsingDriver::option_str(const string &name_option, string *opt)
567
{
568
569
  option_str(name_option, *opt);
  delete opt;
570
571
}

572
573
void
ParsingDriver::option_str(const string &name_option, const string &opt)
574
{
sebastien's avatar
sebastien committed
575
576
577
578
579
580
581
582
583
584
  if (options_list.string_options.find(name_option)
      != options_list.string_options.end())
    error("option " + name_option + " declared twice");

  options_list.string_options[name_option] = opt;
}

void
ParsingDriver::linear()
{
sebastien's avatar
sebastien committed
585
  mod_file->linear = true;
586
587
}

588
589
void
ParsingDriver::add_tmp_var(string *tmp_var1, string *tmp_var2)
590
{
sebastien's avatar
sebastien committed
591
592
593
  check_symbol_existence(*tmp_var1);
  check_symbol_existence(*tmp_var2);
  tmp_symbol_table->AddTempSymbol(*tmp_var1, *tmp_var2);
594
595
  delete tmp_var1;
  delete tmp_var2;
596
597
}

598
599
void
ParsingDriver::add_tmp_var(string *tmp_var)
600
{
sebastien's avatar
sebastien committed
601
602
  check_symbol_existence(*tmp_var);
  tmp_symbol_table->AddTempSymbol(*tmp_var);
603
  delete tmp_var;
604
605
}

606
void ParsingDriver::rplot()
607
{
sebastien's avatar
sebastien committed
608
609
610
  mod_file->addStatement(new RplotStatement(*tmp_symbol_table, options_list));
  options_list.clear();
  tmp_symbol_table->clear();
611
612
}

613
void ParsingDriver::stoch_simul()
614
{
sebastien's avatar
sebastien committed
615
616
617
  mod_file->addStatement(new StochSimulStatement(*tmp_symbol_table, options_list));
  tmp_symbol_table->clear();
  options_list.clear();
618
619
}

sebastien's avatar
sebastien committed
620
621
void ParsingDriver::simulate()
{
sebastien's avatar
sebastien committed
622
  if (mod_file->model_tree.mode == eSparseDLLMode)
sebastien's avatar
sebastien committed
623
624
625
626
627
628
629
630
    simul_sparse();
  else
    simul();
}

void
ParsingDriver::simul_sparse()
{
sebastien's avatar
sebastien committed
631
  mod_file->addStatement(new SimulSparseStatement(options_list, mod_file->model_tree.compiler));
sebastien's avatar
sebastien committed
632
633
634
635
636
637
  options_list.clear();
}

void
ParsingDriver::init_compiler(int compiler_type)
{
sebastien's avatar
sebastien committed
638
  mod_file->model_tree.compiler = compiler_type;
sebastien's avatar
sebastien committed
639
640
}

641
642
void
ParsingDriver::simul()
643
{
sebastien's avatar
sebastien committed
644
645
  mod_file->addStatement(new SimulStatement(options_list));
  options_list.clear();
646
647
}

648
649
void
ParsingDriver::check()
650
{
sebastien's avatar
sebastien committed
651
652
  mod_file->addStatement(new CheckStatement(options_list));
  options_list.clear();
653
654
}

655
void
sebastien's avatar
sebastien committed
656
ParsingDriver::add_estimated_params_element()
657
{
sebastien's avatar
sebastien committed
658
659
660
661
662
663
  check_symbol_existence(estim_params.name);
  if (estim_params.name2.size() > 0)
    check_symbol_existence(estim_params.name2);

  estim_params_list.push_back(estim_params);
  estim_params.clear();
664
665
}

666
void
sebastien's avatar
sebastien committed
667
ParsingDriver::estimated_params()
668
{
sebastien's avatar
sebastien committed
669
670
  mod_file->addStatement(new EstimatedParamsStatement(estim_params_list, mod_file->symbol_table));
  estim_params_list.clear();
671
672
}

673
void
sebastien's avatar
sebastien committed
674
ParsingDriver::estimated_params_init()
675
{
sebastien's avatar
sebastien committed
676
677
  mod_file->addStatement(new EstimatedParamsInitStatement(estim_params_list, mod_file->symbol_table));
  estim_params_list.clear();
678
679
}

680
void
sebastien's avatar
sebastien committed
681
ParsingDriver::estimated_params_bounds()
682
{
sebastien's avatar
sebastien committed
683
684
  mod_file->addStatement(new EstimatedParamsBoundsStatement(estim_params_list, mod_file->symbol_table));
  estim_params_list.clear();
685
686
}

687
688
void
ParsingDriver::set_unit_root_vars()
689
{
sebastien's avatar
sebastien committed
690
691
  mod_file->addStatement(new UnitRootVarsStatement(*tmp_symbol_table));
  tmp_symbol_table->clear();
692
693
}

694
695
void
ParsingDriver::run_estimation()
696
{
sebastien's avatar
sebastien committed
697
698
699
  mod_file->addStatement(new EstimationStatement(*tmp_symbol_table, options_list));
  tmp_symbol_table->clear();
  options_list.clear();
700
701
}

michel's avatar
michel committed
702
703
704
void
ParsingDriver::run_prior_analysis()
{
sebastien's avatar
sebastien committed
705
706
707
  mod_file->addStatement(new PriorAnalysisStatement(*tmp_symbol_table, options_list));
  tmp_symbol_table->clear();
  options_list.clear();
michel's avatar
michel committed
708
709
710
711
712
}

void
ParsingDriver::run_posterior_analysis()
{
sebastien's avatar
sebastien committed
713
714
715
716
717
  mod_file->addStatement(new PosteriorAnalysisStatement(*tmp_symbol_table, options_list));
  tmp_symbol_table->clear();
  options_list.clear();
}

718
719
720
721
722
723
724
void
ParsingDriver::dynare_sensitivity()
{
  mod_file->addStatement(new DynareSensitivityStatement(options_list));
  options_list.clear();
}

sebastien's avatar
sebastien committed
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
void
ParsingDriver::optim_options_helper(const string &name)
{
  if (options_list.string_options.find("optim_opt") == options_list.string_options.end())
    options_list.string_options["optim_opt"] = "";
  else
    options_list.string_options["optim_opt"] += ",";
  options_list.string_options["optim_opt"] += "''" + name + "'',";
}

void
ParsingDriver::optim_options_string(string *name, string *value)
{
  optim_options_helper(*name);
  options_list.string_options["optim_opt"] += "''" + *value + "''";
  delete name;
  delete value;
michel's avatar
michel committed
742
743
}

744
void
sebastien's avatar
sebastien committed
745
ParsingDriver::optim_options_num(string *name, string *value)
746
{
sebastien's avatar
sebastien committed
747
748
749
750
  optim_options_helper(*name);
  options_list.string_options["optim_opt"] += *value;
  delete name;
  delete value;
751
752
}

753
754
void
ParsingDriver::set_varobs()
755
{
sebastien's avatar
sebastien committed
756
757
  mod_file->addStatement(new VarobsStatement(*tmp_symbol_table));
  tmp_symbol_table->clear();
758
759
}

760
void
sebastien's avatar
sebastien committed
761
ParsingDriver::set_trends()
762
{
sebastien's avatar
sebastien committed
763
764
  mod_file->addStatement(new ObservationTrendsStatement(trend_elements, mod_file->symbol_table));
  trend_elements.clear();
765
766
}

767
void
sebastien's avatar
sebastien committed
768
ParsingDriver::set_trend_element(string *arg1, NodeID arg2)
769
{
sebastien's avatar
sebastien committed
770
771
772
  check_symbol_existence(*arg1);
  if (trend_elements.find(*arg1) != trend_elements.end())
    error("observation_trends: " + *arg1 + " declared twice");
sebastien's avatar
sebastien committed
773
  trend_elements[*arg1] = arg2;
774
  delete arg1;
775
776
}

777
void
sebastien's avatar
sebastien committed
778
ParsingDriver::set_optim_weights(string *name, NodeID value)
779
{
sebastien's avatar
sebastien committed
780
781
782
783
784
  check_symbol_existence(*name);
  if (mod_file->symbol_table.getType(*name) != eEndogenous)
    error("optim_weights: " + *name + " isn't an endogenous variable");
  if (var_weights.find(*name) != var_weights.end())
    error("optim_weights: " + *name + " declared twice");
sebastien's avatar
sebastien committed
785
  var_weights[*name] = value;
sebastien's avatar
sebastien committed
786
  delete name;
787
788
}

789
void
sebastien's avatar
sebastien committed
790
ParsingDriver::set_optim_weights(string *name1, string *name2, NodeID value)
791
{
sebastien's avatar
sebastien committed
792
793
794
795
796
797
798
799
800
801
802
803
804
805
  check_symbol_existence(*name1);
  if (mod_file->symbol_table.getType(*name1) != eEndogenous)
    error("optim_weights: " + *name1 + " isn't an endogenous variable");

  check_symbol_existence(*name2);
  if (mod_file->symbol_table.getType(*name2) != eEndogenous)
    error("optim_weights: " + *name2 + " isn't an endogenous variable");

  pair<string, string> covar_key(*name1, *name2);

  if (covar_weights.find(covar_key) != covar_weights.end())
    error("optim_weights: pair of variables (" + *name1 + ", " + *name2
          + ") declared twice");

sebastien's avatar
sebastien committed
806
  covar_weights[covar_key] = value;
sebastien's avatar
sebastien committed
807
808
  delete name1;
  delete name2;
809
810
}

811
void
sebastien's avatar
sebastien committed
812
ParsingDriver::optim_weights()
813
{
sebastien's avatar
sebastien committed
814
815
816
  mod_file->addStatement(new OptimWeightsStatement(var_weights, covar_weights, mod_file->symbol_table));
  var_weights.clear();
  covar_weights.clear();
817
818
}

819
820
void
ParsingDriver::set_osr_params()
821
{
sebastien's avatar
sebastien committed
822
823
  mod_file->addStatement(new OsrParamsStatement(*tmp_symbol_table));
  tmp_symbol_table->clear();
824
825
}

826
827
void
ParsingDriver::run_osr()
828
{
sebastien's avatar
sebastien committed
829
830
831
  mod_file->addStatement(new OsrStatement(*tmp_symbol_table, options_list));
  tmp_symbol_table->clear();
  options_list.clear();
832
833
}

834
void
sebastien's avatar
sebastien committed
835
ParsingDriver::set_calib_var(string *name, string *weight, NodeID expression)
836
{
sebastien's avatar
sebastien committed
837
838
839
840
841
842
843
844
  check_symbol_existence(*name);
  if (mod_file->symbol_table.getType(*name) != eEndogenous
      && mod_file->symbol_table.getType(*name) != eExogenous)
    error("calib_var: " + *name + " isn't an endogenous or exogenous variable");

  if (calib_var.find(*name) != calib_var.end())
    error("calib_var: " + *name + " declared twice");

sebastien's avatar
sebastien committed
845
  calib_var[*name] = make_pair(*weight, expression);
sebastien's avatar
sebastien committed
846

847
848
  delete name;
  delete weight;
849
850
}

851
void
sebastien's avatar
sebastien committed
852
ParsingDriver::set_calib_covar(string *name1, string *name2,
sebastien's avatar
sebastien committed
853
                               string *weight, NodeID expression)
854
{
sebastien's avatar
sebastien committed
855
856
857
858
859
860
861
862
863
864
865
866
867
868
  check_symbol_existence(*name1);
  check_symbol_existence(*name2);
  if (mod_file->symbol_table.getType(*name1) != mod_file->symbol_table.getType(*name2))
    error("calib_var: " + *name1 + " and " + *name2 + "dont't have the same type");
  if (mod_file->symbol_table.getType(*name1) != eEndogenous
      && mod_file->symbol_table.getType(*name1) != eExogenous)
    error("calib_var: " + *name1 + " and " + *name2 + "aren't endogenous or exogenous variables");

  pair<string, string> covar_key(*name1, *name2);

  if (calib_covar.find(covar_key) != calib_covar.end())
    error("calib_var: pair of variables (" + *name1 + ", " + *name2
          + ") declared twice");

sebastien's avatar
sebastien committed
869
  calib_covar[covar_key] = make_pair(*weight, expression);
sebastien's avatar
sebastien committed
870

871
872
873
  delete name1;
  delete name2;
  delete weight;
874
875
}

876
877
void
ParsingDriver::set_calib_ac(string *name, string *ar,
sebastien's avatar
sebastien committed
878
                            string *weight, NodeID expression)
879
{
sebastien's avatar
sebastien committed
880
881
882
883
884
885
886
887
888
889
  check_symbol_existence(*name);
  if (mod_file->symbol_table.getType(*name) != eEndogenous)
    error("calib_var: " + *name + "isn't an endogenous variable");

  int iar = atoi(ar->c_str());
  pair<string, int> ac_key(*name, iar);

  if (calib_ac.find(ac_key) != calib_ac.end())
    error("calib_var: autocorr " + *name + "(" + *ar + ") declared twice");

sebastien's avatar
sebastien committed
890
  calib_ac[ac_key] = make_pair(*weight, expression);
sebastien's avatar
sebastien committed
891