ParsingDriver.cc 30.8 KB
Newer Older
1
/*
sebastien's avatar
trunk:    
sebastien committed
2
 * Copyright (C) 2003-2009 Dynare Team
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 *
 * This file is part of Dynare.
 *
 * Dynare is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Dynare is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
 */

20
#include <cstdlib>
21
22
23
24
25
26
27
#include <fstream>
#include <iostream>

#include "ParsingDriver.hh"
#include "Statement.hh"

bool
28
ParsingDriver::symbol_exists_and_is_not_modfile_local_or_unknown_function(const char *s)
29
30
31
32
{
  if (!mod_file->symbol_table.exists(s))
    return false;

33
34
35
  SymbolType type = mod_file->symbol_table.getType(s);

  return(type != eModFileLocalVariable && type != eUnknownFunction);
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
}

void
ParsingDriver::check_symbol_existence(const string &name)
{
  if (!mod_file->symbol_table.exists(name))
    error("Unknown symbol: " + name);
}

void
ParsingDriver::set_current_data_tree(DataTree *data_tree_arg)
{
  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);
}

ModFile *
59
ParsingDriver::parse(istream &in, bool debug)
60
61
62
{
  mod_file = new ModFile();

sebastien's avatar
sebastien committed
63
  symbol_list.clear();
64
65

  reset_data_tree();
66
  estim_params.init(*data_tree);
67
68

  lexer = new DynareFlex(&in);
69
  lexer->set_debug(debug);
70
71

  Dynare::parser parser(*this);
72
  parser.set_debug_level(debug);
73
74
75
76
77
78
79
80
81
82
83
  parser.parse();

  delete lexer;

  return mod_file;
}

void
ParsingDriver::error(const Dynare::parser::location_type &l, const string &m)
{
  cerr << "ERROR: " << l << ": " << m << endl;
84
  exit(EXIT_FAILURE);
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
}

void
ParsingDriver::error(const string &m)
{
  error(location, m);
}

void
ParsingDriver::warning(const string &m)
{
  cerr << "WARNING: " << location << ": " << m << endl;
}

void
100
ParsingDriver::declare_symbol(string *name, SymbolType type, string *tex_name)
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
{
  try
    {
      mod_file->symbol_table.addSymbol(*name, type, *tex_name);
    }
  catch(SymbolTable::AlreadyDeclaredException &e)
    {
      if (e.same_type)
        warning("Symbol " + *name + " declared twice.");
      else
        error("Symbol " + *name + " declared twice with different types!");
    }

  delete name;
  delete tex_name;
}

void
ParsingDriver::declare_endogenous(string *name, string *tex_name)
{
  declare_symbol(name, eEndogenous, tex_name);
}

void
ParsingDriver::declare_exogenous(string *name, string *tex_name)
{
  declare_symbol(name, eExogenous, tex_name);
}

void
ParsingDriver::declare_exogenous_det(string *name, string *tex_name)
{
  declare_symbol(name, eExogenousDet, tex_name);
}

void
ParsingDriver::declare_parameter(string *name, string *tex_name)
{
  declare_symbol(name, eParameter, tex_name);
}

NodeID
ParsingDriver::add_constant(string *constant)
{
  NodeID id = data_tree->AddNumConstant(*constant);
  delete constant;
  return id;
}

150
151
152
153
154
155
156
157
158
159
160
161
NodeID
ParsingDriver::add_nan_constant()
{
  return data_tree->NaN;
}

NodeID
ParsingDriver::add_inf_constant()
{
  return data_tree->Infinity;
}

162
163
164
NodeID
ParsingDriver::add_model_variable(string *name)
{
sebastien's avatar
sebastien committed
165
  return add_model_variable(name, new string("0"));
166
167
168
169
170
171
}

NodeID
ParsingDriver::add_model_variable(string *name, string *olag)
{
  check_symbol_existence(*name);
172
  SymbolType type = mod_file->symbol_table.getType(*name);
173
174
175
176
177
  int lag = atoi(olag->c_str());

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

sebastien's avatar
sebastien committed
178
179
180
  if (type == eUnknownFunction)
    error("Symbol " + *name + " is a function name unknown to Dynare. It cannot be used inside model.");

ferhat's avatar
ferhat committed
181
  if (type == eExogenous && lag != 0 && (model_tree->mode != eSparseDLLMode && model_tree->mode != eSparseMode))
sebastien's avatar
sebastien committed
182
183
184
185
186
187
    warning("Exogenous variable " + *name + " has lead/lag " + *olag);

  if (type == eModelLocalVariable && lag != 0)
    error("Model local variable " + *name + " cannot be given a lead or a lag.");

  // It makes sense to allow a lead/lag on parameters: during steady state calibration, endogenous and parameters can be swapped
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202

  NodeID id = model_tree->AddVariable(*name, lag);

  delete name;
  delete olag;
  return id;
}

NodeID
ParsingDriver::add_expression_variable(string *name)
{
  // If symbol doesn't exist, then declare it as a mod file local variable
  if (!mod_file->symbol_table.exists(*name))
    mod_file->symbol_table.addSymbol(*name, eModFileLocalVariable);

203
204
205
206
  // This check must come after the previous one!
  if (mod_file->symbol_table.getType(*name) == eModelLocalVariable)
    error("Variable " + *name + " not allowed outside model declaration. Its scope is only inside model.");

207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
  NodeID id = data_tree->AddVariable(*name);

  delete name;
  return id;
}

void
ParsingDriver::periods(string *periods)
{
  int periods_val = atoi(periods->c_str());
  mod_file->addStatement(new PeriodsStatement(periods_val));
  delete periods;
}

void
ParsingDriver::cutoff(string *cutoff)
{
  double cutoff_val = atof(cutoff->c_str());
  mod_file->addStatement(new CutoffStatement(cutoff_val));
  delete cutoff;
}

void
ParsingDriver::markowitz(string *markowitz)
{
  double markowitz_val = atof(markowitz->c_str());
  mod_file->addStatement(new MarkowitzStatement(markowitz_val));
  delete markowitz;
}

void
ParsingDriver::dsample(string *arg1)
{
  int arg1_val = atoi(arg1->c_str());
  mod_file->addStatement(new DsampleStatement(arg1_val));
  delete arg1;
}

void
ParsingDriver::dsample(string *arg1, string *arg2)
{
  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;
}

255

256
257
258
259
void
ParsingDriver::init_param(string *name, NodeID rhs)
{
  check_symbol_existence(*name);
sebastien's avatar
sebastien committed
260
261
  int symb_id = mod_file->symbol_table.getID(*name);
  if (mod_file->symbol_table.getType(symb_id) != eParameter)
262
263
    error(*name + " is not a parameter");

sebastien's avatar
sebastien committed
264
  mod_file->addStatement(new InitParamStatement(symb_id, rhs, mod_file->symbol_table));
265
266
267
268
269
270
271
272

  delete name;
}

void
ParsingDriver::init_val(string *name, NodeID rhs)
{
  check_symbol_existence(*name);
sebastien's avatar
sebastien committed
273
274
  int symb_id = mod_file->symbol_table.getID(*name);
  SymbolType type = mod_file->symbol_table.getType(symb_id);
275
276
277
278
279

  if (type != eEndogenous
      && type != eExogenous
      && type != eExogenousDet)
    error("initval/endval: " + *name + " should be an endogenous or exogenous variable");
sebastien's avatar
sebastien committed
280
281
282

  init_values.push_back(make_pair(symb_id, rhs));

283
284
285
286
  delete name;
}

void
michel's avatar
michel committed
287
ParsingDriver::initval_file(string *filename)
288
{
sebastien's avatar
sebastien committed
289
  mod_file->addStatement(new InitvalFileStatement(*filename));
290
291
292
293
294
295
296
  delete filename;
}

void
ParsingDriver::hist_val(string *name, string *lag, NodeID rhs)
{
  check_symbol_existence(*name);
sebastien's avatar
sebastien committed
297
298
  int symb_id = mod_file->symbol_table.getID(*name);
  SymbolType type = mod_file->symbol_table.getType(symb_id);
299
300
301
302
303
304
305

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

  int ilag = atoi(lag->c_str());
sebastien's avatar
sebastien committed
306
  pair<int, int> key(symb_id, ilag);
307
308
309
310
311
312
313
314
315
316
317
318
319
320

  if (hist_values.find(key) != hist_values.end())
    error("hist_val: (" + *name + ", " + *lag + ") declared twice");

  hist_values[key] = rhs;

  delete name;
  delete lag;
}

void
ParsingDriver::homotopy_val(string *name, NodeID val1, NodeID val2)
{
  check_symbol_existence(*name);
sebastien's avatar
sebastien committed
321
322
  int symb_id = mod_file->symbol_table.getID(*name);
  SymbolType type = mod_file->symbol_table.getType(symb_id);
323
324
325
326
327
328

  if (type != eParameter
      && type != eExogenous
      && type != eExogenousDet)
    error("homotopy_val: " + *name + " should be a parameter or exogenous variable");

sebastien's avatar
sebastien committed
329
  homotopy_values.push_back(make_pair(symb_id, make_pair(val1, val2)));
330
331
332
333

  delete name;
}

334
335
336
337
338
339
340
341
void
ParsingDriver::forecast()
{
  mod_file->addStatement(new ForecastStatement(symbol_list, options_list));
  symbol_list.clear();
  options_list.clear();
}

342
343
344
345
346
347
348
349
350
351
void
ParsingDriver::use_dll()
{
  model_tree->mode = eDLLMode;
}

void
ParsingDriver::sparse_dll()
{
  model_tree->mode = eSparseDLLMode;
ferhat's avatar
ferhat committed
352
  /*model_tree->block_triangular.init_incidence_matrix(mod_file->symbol_table.endo_nbr);
353
    model_tree->block_triangular.init_incidence_matrix_X(mod_file->symbol_table.exo_nbr);*/
354
355
356
357
358
359
}

void
ParsingDriver::sparse()
{
  model_tree->mode = eSparseMode;
ferhat's avatar
ferhat committed
360
  /*model_tree->block_triangular.init_incidence_matrix(mod_file->symbol_table.endo_nbr);
361
    model_tree->block_triangular.init_incidence_matrix_X(mod_file->symbol_table.exo_nbr);*/
362
363
364
365
366
}

void
ParsingDriver::end_initval()
{
sebastien's avatar
sebastien committed
367
368
  mod_file->addStatement(new InitValStatement(init_values, mod_file->symbol_table));
  init_values.clear();
369
370
371
372
373
}

void
ParsingDriver::end_endval()
{
sebastien's avatar
sebastien committed
374
375
  mod_file->addStatement(new EndValStatement(init_values, mod_file->symbol_table));
  init_values.clear();
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
}

void
ParsingDriver::end_histval()
{
  mod_file->addStatement(new HistValStatement(hist_values, mod_file->symbol_table));
  hist_values.clear();
}

void
ParsingDriver::end_homotopy()
{
  mod_file->addStatement(new HomotopyStatement(homotopy_values, mod_file->symbol_table));
  homotopy_values.clear();
}

void
ParsingDriver::begin_model()
{
sebastien's avatar
sebastien committed
395
  set_current_data_tree(&mod_file->dynamic_model);
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
}

void
ParsingDriver::end_shocks()
{
  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();
}

void
ParsingDriver::end_mshocks()
{
  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();
}

void
ParsingDriver::add_det_shock(string *var)
{
  check_symbol_existence(*var);
426
  SymbolType type = mod_file->symbol_table.getType(*var);
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
  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++)
    {
      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);
    }

  det_shocks[*var] = v;

  det_shocks_periods.clear();
  det_shocks_values.clear();
  delete var;
}

void
ParsingDriver::add_stderr_shock(string *var, NodeID value)
{
  check_symbol_existence(*var);
  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");

  std_shocks[*var] = value;

  delete var;
}

void
ParsingDriver::add_var_shock(string *var, NodeID value)
{
  check_symbol_existence(*var);
  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");

  var_shocks[*var] = value;

  delete var;
}

void
ParsingDriver::add_covar_shock(string *var1, string *var2, NodeID value)
{
  check_symbol_existence(*var1);
  check_symbol_existence(*var2);

  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");

  covar_shocks[key] = value;

  delete var1;
  delete var2;
}

void
ParsingDriver::add_correl_shock(string *var1, string *var2, NodeID value)
{
  check_symbol_existence(*var1);
  check_symbol_existence(*var2);

  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");

  corr_shocks[key] = value;

  delete var1;
  delete var2;
}

void
ParsingDriver::add_period(string *p1, string *p2)
{
  int p1_val = atoi(p1->c_str());
  int p2_val = atoi(p2->c_str());
  det_shocks_periods.push_back(make_pair(p1_val, p2_val));
  delete p1;
  delete p2;
}

void
ParsingDriver::add_period(string *p1)
{
  int p1_val = atoi(p1->c_str());
  det_shocks_periods.push_back(make_pair(p1_val, p1_val));
  delete p1;
}

void
ParsingDriver::add_value(NodeID value)
{
  det_shocks_values.push_back(value);
}

void
ParsingDriver::add_value(string *p1)
{
  det_shocks_values.push_back(add_constant(p1));
}

void
ParsingDriver::do_sigma_e()
{
  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();
}

void
ParsingDriver::end_of_row()
{
  sigmae_matrix.push_back(sigmae_row);
  sigmae_row.clear();
}

void
ParsingDriver::add_to_row_const(string *s)
{
  sigmae_row.push_back(add_constant(s));
}

void
ParsingDriver::add_to_row(NodeID v)
{
  sigmae_row.push_back(v);
}

void
ParsingDriver::steady()
{
sebastien's avatar
sebastien committed
588
  mod_file->addStatement(new SteadyStatement(options_list));
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
  options_list.clear();
}

void
ParsingDriver::option_num(const string &name_option, string *opt1, string *opt2)
{
  if (options_list.paired_num_options.find(name_option)
      != options_list.paired_num_options.end())
    error("option " + name_option + " declared twice");

  options_list.paired_num_options[name_option] = make_pair(*opt1, *opt2);
  delete opt1;
  delete opt2;
}

void
ParsingDriver::option_num(const string &name_option, string *opt)
{
  option_num(name_option, *opt);
  delete opt;
}

void
ParsingDriver::option_num(const string &name_option, const string &opt)
{
614
615
616
  // Since "periods" option automatically sets "simul" option, we don't want to fail if user explicitly sets both "simul" and "periods"
  if (name_option != "simul"
      && (options_list.num_options.find(name_option) != options_list.num_options.end()))
617
618
    error("option " + name_option + " declared twice");

sebastien's avatar
sebastien committed
619
620
  if ((name_option == "periods") && (mod_file->dynamic_model.mode == eSparseDLLMode || mod_file->dynamic_model.mode == eSparseMode))
    mod_file->dynamic_model.block_triangular.periods = atoi(opt.c_str());
621
  else if (name_option == "cutoff")
sebastien's avatar
sebastien committed
622
    mod_file->dynamic_model.cutoff = atof(opt.c_str());
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643

  options_list.num_options[name_option] = opt;
}

void
ParsingDriver::option_str(const string &name_option, string *opt)
{
  option_str(name_option, *opt);
  delete opt;
}

void
ParsingDriver::option_str(const string &name_option, const string &opt)
{
  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;
}

644
void
sebastien's avatar
sebastien committed
645
ParsingDriver::option_symbol_list(const string &name_option)
646
{
sebastien's avatar
sebastien committed
647
648
  if (options_list.symbol_list_options.find(name_option)
      != options_list.symbol_list_options.end())
649
650
    error("option " + name_option + " declared twice");

sebastien's avatar
sebastien committed
651
652
  options_list.symbol_list_options[name_option] = symbol_list;
  symbol_list.clear();
653
654
}

655
656
657
658
659
660
661
void
ParsingDriver::linear()
{
  mod_file->linear = true;
}

void
sebastien's avatar
sebastien committed
662
ParsingDriver::add_in_symbol_list(string *tmp_var)
663
{
664
665
  if (*tmp_var != ":")
    check_symbol_existence(*tmp_var);
sebastien's avatar
sebastien committed
666
  symbol_list.addSymbol(*tmp_var);
667
668
669
670
671
  delete tmp_var;
}

void ParsingDriver::rplot()
{
sebastien's avatar
sebastien committed
672
  mod_file->addStatement(new RplotStatement(symbol_list, options_list));
673
  options_list.clear();
sebastien's avatar
sebastien committed
674
  symbol_list.clear();
675
676
677
678
}

void ParsingDriver::stoch_simul()
{
679
  mod_file->addStatement(new StochSimulStatement(symbol_list, options_list, mod_file->dynamic_model.mode));
sebastien's avatar
sebastien committed
680
  symbol_list.clear();
681
682
683
684
685
686
  options_list.clear();
}

void
ParsingDriver::simul()
{
687
  mod_file->addStatement(new SimulStatement(options_list, mod_file->dynamic_model.mode));
688
689
690
  options_list.clear();
}

691
692
693
void
ParsingDriver::model_info()
{
ferhat's avatar
ferhat committed
694
  mod_file->addStatement(new ModelInfoStatement(options_list));
695
696
697
  options_list.clear();
}

698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
void
ParsingDriver::check()
{
  mod_file->addStatement(new CheckStatement(options_list));
  options_list.clear();
}

void
ParsingDriver::add_estimated_params_element()
{
  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);
713
  estim_params.init(*data_tree);
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
}

void
ParsingDriver::estimated_params()
{
  mod_file->addStatement(new EstimatedParamsStatement(estim_params_list, mod_file->symbol_table));
  estim_params_list.clear();
}

void
ParsingDriver::estimated_params_init()
{
  mod_file->addStatement(new EstimatedParamsInitStatement(estim_params_list, mod_file->symbol_table));
  estim_params_list.clear();
}

void
ParsingDriver::estimated_params_bounds()
{
  mod_file->addStatement(new EstimatedParamsBoundsStatement(estim_params_list, mod_file->symbol_table));
  estim_params_list.clear();
}

void
ParsingDriver::set_unit_root_vars()
{
sebastien's avatar
sebastien committed
740
741
  mod_file->addStatement(new UnitRootVarsStatement(symbol_list));
  symbol_list.clear();
742
743
744
745
746
}

void
ParsingDriver::run_estimation()
{
sebastien's avatar
sebastien committed
747
748
  mod_file->addStatement(new EstimationStatement(symbol_list, options_list));
  symbol_list.clear();
749
750
751
752
753
754
  options_list.clear();
}

void
ParsingDriver::run_prior_analysis()
{
sebastien's avatar
sebastien committed
755
756
  mod_file->addStatement(new PriorAnalysisStatement(symbol_list, options_list));
  symbol_list.clear();
757
758
759
760
761
762
  options_list.clear();
}

void
ParsingDriver::run_posterior_analysis()
{
sebastien's avatar
sebastien committed
763
764
  mod_file->addStatement(new PosteriorAnalysisStatement(symbol_list, options_list));
  symbol_list.clear();
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
  options_list.clear();
}

void
ParsingDriver::dynare_sensitivity()
{
  mod_file->addStatement(new DynareSensitivityStatement(options_list));
  options_list.clear();
}

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;
}

void
ParsingDriver::optim_options_num(string *name, string *value)
{
  optim_options_helper(*name);
  options_list.string_options["optim_opt"] += *value;
  delete name;
  delete value;
}

void
ParsingDriver::set_varobs()
{
sebastien's avatar
sebastien committed
806
807
  mod_file->addStatement(new VarobsStatement(symbol_list));
  symbol_list.clear();
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
}

void
ParsingDriver::set_trends()
{
  mod_file->addStatement(new ObservationTrendsStatement(trend_elements, mod_file->symbol_table));
  trend_elements.clear();
}

void
ParsingDriver::set_trend_element(string *arg1, NodeID arg2)
{
  check_symbol_existence(*arg1);
  if (trend_elements.find(*arg1) != trend_elements.end())
    error("observation_trends: " + *arg1 + " declared twice");
  trend_elements[*arg1] = arg2;
  delete arg1;
}

void
ParsingDriver::set_optim_weights(string *name, NodeID value)
{
  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");
  var_weights[*name] = value;
  delete name;
}

void
ParsingDriver::set_optim_weights(string *name1, string *name2, NodeID value)
{
  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");

  covar_weights[covar_key] = value;
  delete name1;
  delete name2;
}

void
ParsingDriver::optim_weights()
{
  mod_file->addStatement(new OptimWeightsStatement(var_weights, covar_weights, mod_file->symbol_table));
  var_weights.clear();
  covar_weights.clear();
}

void
ParsingDriver::set_osr_params()
{
sebastien's avatar
sebastien committed
872
873
  mod_file->addStatement(new OsrParamsStatement(symbol_list));
  symbol_list.clear();
874
875
876
877
878
}

void
ParsingDriver::run_osr()
{
sebastien's avatar
sebastien committed
879
880
  mod_file->addStatement(new OsrStatement(symbol_list, options_list));
  symbol_list.clear();
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
  options_list.clear();
}

void
ParsingDriver::set_calib_var(string *name, string *weight, NodeID expression)
{
  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");

  calib_var[*name] = make_pair(*weight, expression);

  delete name;
  delete weight;
}

void
ParsingDriver::set_calib_covar(string *name1, string *name2,
                               string *weight, NodeID expression)
{
  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");

  calib_covar[covar_key] = make_pair(*weight, expression);

  delete name1;
  delete name2;
  delete weight;
}

void
ParsingDriver::set_calib_ac(string *name, string *ar,
                            string *weight, NodeID expression)
{
  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");

  calib_ac[ac_key] = make_pair(*weight, expression);

  delete name;
  delete ar;
  delete weight;
}

void
ParsingDriver::run_calib_var()
{
  mod_file->addStatement(new CalibVarStatement(calib_var, calib_covar, calib_ac,
                                               mod_file->symbol_table));
  calib_var.clear();
  calib_covar.clear();
  calib_ac.clear();
}

void
ParsingDriver::run_calib(int covar)
{
  mod_file->addStatement(new CalibStatement(covar));
}

void
sebastien's avatar
trunk:    
sebastien committed
964
ParsingDriver::run_dynatype(string *filename)
965
{
sebastien's avatar
trunk:    
sebastien committed
966
  mod_file->addStatement(new DynaTypeStatement(symbol_list, *filename));
sebastien's avatar
sebastien committed
967
  symbol_list.clear();
968
969
970
971
  delete filename;
}

void
sebastien's avatar
trunk:    
sebastien committed
972
ParsingDriver::run_dynasave(string *filename)
973
{
sebastien's avatar
trunk:    
sebastien committed
974
  mod_file->addStatement(new DynaSaveStatement(symbol_list, *filename));
sebastien's avatar
sebastien committed
975
  symbol_list.clear();
976
977
978
  delete filename;
}

979
980
981
void
ParsingDriver::run_load_params_and_steady_state(string *filename)
{
sebastien's avatar
sebastien committed
982
  mod_file->addStatement(new LoadParamsAndSteadyStateStatement(*filename, mod_file->symbol_table));
983
984
985
986
987
988
989
990
991
992
  delete filename;
}

void
ParsingDriver::run_save_params_and_steady_state(string *filename)
{
  mod_file->addStatement(new SaveParamsAndSteadyStateStatement(*filename));
  delete filename;
}

993
994
995
void
ParsingDriver::add_mc_filename(string *filename, string *prior)
{
sebastien's avatar
trunk:    
sebastien committed
996
997
998
999
1000
  for(ModelComparisonStatement::filename_list_type::iterator it = filename_list.begin();
      it != filename_list.end(); it++)
    if ((*it).first == *filename)
      error("model_comparison: filename " + *filename + " declared twice");
  filename_list.push_back(make_pair(*filename, *prior));
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
  delete filename;
  delete prior;
}

void
ParsingDriver::run_model_comparison()
{
  mod_file->addStatement(new ModelComparisonStatement(filename_list, options_list));
  filename_list.clear();
  options_list.clear();
}

void
ParsingDriver::begin_planner_objective()
{
sebastien's avatar
sebastien committed
1016
  set_current_data_tree(new StaticModel(mod_file->symbol_table, mod_file->num_constants));
1017
1018
1019
1020
1021
1022
1023
1024
1025
}

void
ParsingDriver::end_planner_objective(NodeID expr)
{
  // Add equation corresponding to expression
  NodeID eq = model_tree->AddEqual(expr, model_tree->Zero);
  model_tree->addEquation(eq);

sebastien's avatar
sebastien committed
1026
  mod_file->addStatement(new PlannerObjectiveStatement(dynamic_cast<StaticModel *>(model_tree)));
1027
1028
1029
1030
1031
1032
1033

  reset_data_tree();
}

void
ParsingDriver::ramsey_policy()
{
sebastien's avatar
sebastien committed
1034
1035
  mod_file->addStatement(new RamseyPolicyStatement(symbol_list, options_list));
  symbol_list.clear();
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
  options_list.clear();
}

void
ParsingDriver::bvar_density(string *maxnlags)
{
  mod_file->addStatement(new BVARDensityStatement(atoi(maxnlags->c_str()), options_list));
  options_list.clear();
  delete maxnlags;
}

void
ParsingDriver::bvar_forecast(string *nlags)
{
  mod_file->addStatement(new BVARForecastStatement(atoi(nlags->c_str()), options_list));
  options_list.clear();
  delete nlags;
}

NodeID
ParsingDriver::add_model_equal(NodeID arg1, NodeID arg2)
{
  NodeID id = model_tree->AddEqual(arg1, arg2);
  model_tree->addEquation(id);
  return id;
}

NodeID
ParsingDriver::add_model_equal_with_zero_rhs(NodeID arg)
{
  return add_model_equal(arg, model_tree->Zero);
}

void
ParsingDriver::declare_and_init_model_local_variable(string *name, NodeID rhs)
{
  try
    {
      mod_file->symbol_table.addSymbol(*name, eModelLocalVariable);
    }
  catch(SymbolTable::AlreadyDeclaredException &e)
    {
      error("Local model variable " + *name + " declared twice.");
    }

sebastien's avatar
sebastien committed
1081
  model_tree->AddLocalVariable(*name, rhs);
1082
1083
1084
  delete name;
}

1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
void
ParsingDriver::change_type(SymbolType new_type, vector<string *> *var_list)
{
  for(vector<string *>::iterator it = var_list->begin();
      it != var_list->end(); it++)
    {
      int id;
      try
        {
          id = mod_file->symbol_table.getID(**it);
        }
      catch(SymbolTable::UnknownSymbolNameException &e)
        {
          error("Unknown variable " + **it);
        }

      // Check if symbol already used in a VariableNode
      if (mod_file->expressions_tree.isSymbolUsed(id)
sebastien's avatar
sebastien committed
1103
          || mod_file->dynamic_model.isSymbolUsed(id))
1104
1105
1106
1107
1108
1109
1110
1111
1112
        error("You cannot modify the type of symbol " + **it + " after having used it in an expression");

      mod_file->symbol_table.changeType(id, new_type);

      delete *it;
    }
  delete var_list;
}

1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
NodeID
ParsingDriver::add_plus(NodeID arg1, NodeID arg2)
{
  return data_tree->AddPlus(arg1, arg2);
}

NodeID
ParsingDriver::add_minus(NodeID arg1, NodeID arg2)
{
  return data_tree->AddMinus(arg1, arg2);
}

NodeID
ParsingDriver::add_uminus(NodeID arg1)
{
  return data_tree->AddUMinus(arg1);
}

NodeID
ParsingDriver::add_times(NodeID arg1, NodeID arg2)
{
  return data_tree->AddTimes(arg1, arg2);
}

NodeID
ParsingDriver::add_divide(NodeID arg1, NodeID arg2)
{
  return data_tree->AddDivide(arg1, arg2);
}

NodeID
ParsingDriver::add_less(NodeID arg1, NodeID arg2)
{
  return data_tree->AddLess(arg1, arg2);
}

NodeID
ParsingDriver::add_greater(NodeID arg1, NodeID arg2)
{
  return data_tree->AddGreater(arg1, arg2);
}

NodeID
ParsingDriver::add_less_equal(NodeID arg1, NodeID arg2)
{
  return data_tree->AddLessEqual(arg1, arg2);
}

NodeID
ParsingDriver::add_greater_equal(NodeID arg1, NodeID arg2)
{
  return data_tree->AddGreaterEqual(arg1, arg2);
}

NodeID
ParsingDriver::add_equal_equal(NodeID arg1, NodeID arg2)
{
  return data_tree->AddEqualEqual(arg1, arg2);
}

NodeID
ParsingDriver::add_different(NodeID arg1, NodeID arg2)
{
  return data_tree->AddDifferent(arg1, arg2);
}

NodeID
ParsingDriver::add_power(NodeID arg1, NodeID arg2)
{
  return data_tree->AddPower(arg1, arg2);
}

NodeID
ParsingDriver::add_exp(NodeID arg1)
{
  return data_tree->AddExp(arg1);
}

NodeID
ParsingDriver::add_log(NodeID arg1)
{
  return data_tree->AddLog(arg1);
}

NodeID
ParsingDriver::add_log10(NodeID arg1)
{
  return data_tree->AddLog10(arg1);
}

NodeID
ParsingDriver::add_cos(NodeID arg1)
{
  return data_tree->AddCos(arg1);
}

NodeID
ParsingDriver::add_sin(NodeID arg1)
{
  return data_tree->AddSin(arg1);
}

NodeID
ParsingDriver::add_tan(NodeID arg1)
{
  return data_tree->AddTan(arg1);
}

NodeID
ParsingDriver::add_acos(NodeID arg1)
{
sebastien's avatar
sebastien committed
1224
  return data_tree->AddAcos(arg1);
1225
1226
1227
1228
1229
}

NodeID
ParsingDriver::add_asin(NodeID arg1)
{
sebastien's avatar
sebastien committed
1230
  return data_tree->AddAsin(arg1);
1231
1232
1233
1234
1235
}

NodeID
ParsingDriver::add_atan(NodeID arg1)
{
sebastien's avatar
sebastien committed
1236
  return data_tree->AddAtan(arg1);
1237
1238
1239
1240
1241
}

NodeID
ParsingDriver::add_cosh(NodeID arg1)
{
sebastien's avatar
sebastien committed
1242
  return data_tree->AddCosh(arg1);
1243
1244
1245
1246
1247
}

NodeID
ParsingDriver::add_sinh(NodeID arg1)
{
sebastien's avatar
sebastien committed
1248
  return data_tree->AddSinh(arg1);
1249
1250
1251
1252
1253
}

NodeID
ParsingDriver::add_tanh(NodeID arg1)
{
sebastien's avatar
sebastien committed
1254
  return data_tree->AddTanh(arg1);
1255
1256
1257
1258
1259
}

NodeID
ParsingDriver::add_acosh(NodeID arg1)
{
sebastien's avatar
sebastien committed
1260
  return data_tree->AddAcosh(arg1);
1261
1262
1263
1264
1265
}

NodeID
ParsingDriver::add_asinh(NodeID arg1)
{
sebastien's avatar
sebastien committed
1266
  return data_tree->AddAsinh(arg1);
1267
1268
1269
1270
1271
}

NodeID
ParsingDriver::add_atanh(NodeID arg1)
{
sebastien's avatar
sebastien committed
1272
  return data_tree->AddAtanh(arg1);
1273
1274
1275
1276
1277
}

NodeID
ParsingDriver::add_sqrt(NodeID arg1)
{
sebastien's avatar
sebastien committed
1278
  return data_tree->AddSqrt(arg1);
1279
1280
1281
1282
1283
}

NodeID
ParsingDriver::add_max(NodeID arg1, NodeID arg2)
{
sebastien's avatar
sebastien committed
1284
  return data_tree->AddMax(arg1,arg2);
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
}

NodeID
ParsingDriver::add_min(NodeID arg1, NodeID arg2)
{
  return data_tree->AddMin(arg1,arg2);
}

NodeID
ParsingDriver::add_normcdf(NodeID arg1, NodeID arg2, NodeID arg3)
{
  return data_tree->AddNormcdf(arg1,arg2,arg3);
}

sebastien's avatar
sebastien committed
1299
1300
1301
1302
1303
1304
NodeID
ParsingDriver::add_normcdf(NodeID arg)
{
  return add_normcdf(arg, data_tree->Zero, data_tree->One);
}

1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
void
ParsingDriver::add_unknown_function_arg(NodeID arg)
{
  unknown_function_args.push_back(arg);
}

NodeID
ParsingDriver::add_unknown_function(string *function_name)
{
  if (mod_file->symbol_table.exists(*function_name))
    {
      if (mod_file->symbol_table.getType(*function_name) != eUnknownFunction)
        error("Symbol " + *function_name + " is not a function name.");
    }
  else
    mod_file->symbol_table.addSymbol(*function_name, eUnknownFunction);

  NodeID id = data_tree->AddUnknownFunction(*function_name, unknown_function_args);
  unknown_function_args.clear();
  return id;
}

void
ParsingDriver::add_native(const char *s)
{
  mod_file->addStatement(new NativeStatement(s));
}