ParsingDriver.cc 30.8 KB
Newer Older
sebastien's avatar
sebastien committed
1
/*
sebastien's avatar
trunk:    
sebastien committed
2
 * Copyright (C) 2003-2009 Dynare Team
sebastien's avatar
sebastien committed
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>
sebastien's avatar
sebastien committed
21
22
23
#include <fstream>
#include <iostream>

24
#include "ParsingDriver.hh"
sebastien's avatar
sebastien committed
25
#include "Statement.hh"
26

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

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

  return(type != eModFileLocalVariable && type != eUnknownFunction);
36
37
}

38
void
39
40
ParsingDriver::check_symbol_existence(const string &name)
{
sebastien's avatar
sebastien committed
41
  if (!mod_file->symbol_table.exists(name))
42
43
44
    error("Unknown symbol: " + name);
}

sebastien's avatar
sebastien committed
45
46
void
ParsingDriver::set_current_data_tree(DataTree *data_tree_arg)
47
{
sebastien's avatar
sebastien committed
48
49
50
51
52
53
54
55
  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);
56
57
}

58
ModFile *
59
ParsingDriver::parse(istream &in, bool debug)
60
{
sebastien's avatar
sebastien committed
61
62
  mod_file = new ModFile();

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

sebastien's avatar
sebastien committed
65
  reset_data_tree();
66
  estim_params.init(*data_tree);
sebastien's avatar
sebastien committed
67

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

71
  Dynare::parser parser(*this);
72
  parser.set_debug_level(debug);
73
  parser.parse();
sebastien's avatar
sebastien committed
74

75
  delete lexer;
sebastien's avatar
sebastien committed
76

77
  return mod_file;
78
79
80
}

void
81
ParsingDriver::error(const Dynare::parser::location_type &l, const string &m)
82
{
sebastien's avatar
sebastien committed
83
  cerr << "ERROR: " << l << ": " << m << endl;
84
  exit(EXIT_FAILURE);
85
86
87
}

void
88
ParsingDriver::error(const string &m)
89
{
sebastien's avatar
sebastien committed
90
  error(location, m);
91
92
}

93
void
sebastien's avatar
sebastien committed
94
95
ParsingDriver::warning(const string &m)
{
sebastien's avatar
sebastien committed
96
  cerr << "WARNING: " << location << ": " << m << endl;
sebastien's avatar
sebastien committed
97
98
99
}

void
100
ParsingDriver::declare_symbol(string *name, SymbolType type, string *tex_name)
101
{
sebastien's avatar
sebastien committed
102
103
104
105
106
107
108
109
110
111
112
113
  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!");
    }

114
115
  delete name;
  delete tex_name;
116
117
}

sebastien's avatar
sebastien committed
118
119
120
121
122
123
void
ParsingDriver::declare_endogenous(string *name, string *tex_name)
{
  declare_symbol(name, eEndogenous, tex_name);
}

124
125
void
ParsingDriver::declare_exogenous(string *name, string *tex_name)
126
{
sebastien's avatar
sebastien committed
127
  declare_symbol(name, eExogenous, tex_name);
128
129
}

130
131
void
ParsingDriver::declare_exogenous_det(string *name, string *tex_name)
132
{
sebastien's avatar
sebastien committed
133
  declare_symbol(name, eExogenousDet, tex_name);
134
135
}

136
137
void
ParsingDriver::declare_parameter(string *name, string *tex_name)
138
{
sebastien's avatar
sebastien committed
139
  declare_symbol(name, eParameter, tex_name);
140
141
}

142
NodeID
sebastien's avatar
sebastien committed
143
ParsingDriver::add_constant(string *constant)
144
{
sebastien's avatar
sebastien committed
145
  NodeID id = data_tree->AddNumConstant(*constant);
146
  delete constant;
sebastien's avatar
sebastien committed
147
  return id;
148
149
}

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
NodeID
ParsingDriver::add_model_variable(string *name)
164
{
sebastien's avatar
sebastien committed
165
  return add_model_variable(name, new string("0"));
166
167
}

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

sebastien's avatar
sebastien committed
175
176
177
  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
sebastien's avatar
sebastien committed
188

sebastien's avatar
sebastien committed
189
  NodeID id = model_tree->AddVariable(*name, lag);
sebastien's avatar
sebastien committed
190

191
192
193
  delete name;
  delete olag;
  return id;
194
195
}

sebastien's avatar
sebastien committed
196
NodeID
197
ParsingDriver::add_expression_variable(string *name)
198
{
sebastien's avatar
sebastien committed
199
  // If symbol doesn't exist, then declare it as a mod file local variable
sebastien's avatar
sebastien committed
200
201
  if (!mod_file->symbol_table.exists(*name))
    mod_file->symbol_table.addSymbol(*name, eModFileLocalVariable);
sebastien's avatar
sebastien committed
202

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

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

209
  delete name;
sebastien's avatar
sebastien committed
210
  return id;
211
212
}

213
void
sebastien's avatar
sebastien committed
214
ParsingDriver::periods(string *periods)
215
{
sebastien's avatar
sebastien committed
216
217
218
  int periods_val = atoi(periods->c_str());
  mod_file->addStatement(new PeriodsStatement(periods_val));
  delete periods;
219
220
}

sebastien's avatar
sebastien committed
221
222
223
void
ParsingDriver::cutoff(string *cutoff)
{
224
  double cutoff_val = atof(cutoff->c_str());
sebastien's avatar
sebastien committed
225
226
227
228
  mod_file->addStatement(new CutoffStatement(cutoff_val));
  delete cutoff;
}

229
230
231
232
233
234
235
236
void
ParsingDriver::markowitz(string *markowitz)
{
  double markowitz_val = atof(markowitz->c_str());
  mod_file->addStatement(new MarkowitzStatement(markowitz_val));
  delete markowitz;
}

237
void
sebastien's avatar
sebastien committed
238
ParsingDriver::dsample(string *arg1)
239
{
sebastien's avatar
sebastien committed
240
241
242
  int arg1_val = atoi(arg1->c_str());
  mod_file->addStatement(new DsampleStatement(arg1_val));
  delete arg1;
243
244
}

245
void
sebastien's avatar
sebastien committed
246
ParsingDriver::dsample(string *arg1, string *arg2)
247
{
sebastien's avatar
sebastien committed
248
249
250
251
252
253
254
  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

sebastien's avatar
sebastien committed
256
void
sebastien's avatar
sebastien committed
257
ParsingDriver::init_param(string *name, NodeID rhs)
sebastien's avatar
sebastien committed
258
259
{
  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)
sebastien's avatar
sebastien committed
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));
sebastien's avatar
sebastien committed
265

266
  delete name;
267
268
}

269
void
sebastien's avatar
sebastien committed
270
ParsingDriver::init_val(string *name, NodeID rhs)
271
{
sebastien's avatar
sebastien committed
272
  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);
sebastien's avatar
sebastien committed
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));

sebastien's avatar
sebastien committed
283
  delete name;
284
285
}

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

293
void
sebastien's avatar
sebastien committed
294
ParsingDriver::hist_val(string *name, string *lag, NodeID rhs)
295
{
sebastien's avatar
sebastien committed
296
  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

sebastien's avatar
sebastien committed
300
301
302
303
  if (type != eEndogenous
      && type != eExogenous
      && type != eExogenousDet)
    error("hist_val: " + *name + " should be an endogenous or exogenous variable");
304

sebastien's avatar
sebastien committed
305
  int ilag = atoi(lag->c_str());
sebastien's avatar
sebastien committed
306
  pair<int, int> key(symb_id, ilag);
307

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

sebastien's avatar
sebastien committed
311
  hist_values[key] = rhs;
312

sebastien's avatar
sebastien committed
313
314
  delete name;
  delete lag;
sebastien's avatar
sebastien committed
315
316
}

317
318
319
320
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
void
sebastien's avatar
sebastien committed
343
ParsingDriver::use_dll()
344
{
sebastien's avatar
sebastien committed
345
  model_tree->mode = eDLLMode;
346
347
}

sebastien's avatar
sebastien committed
348
349
350
void
ParsingDriver::sparse_dll()
{
sebastien's avatar
sebastien committed
351
  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);*/
sebastien's avatar
sebastien committed
354
355
}

ferhat's avatar
ferhat committed
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);*/
ferhat's avatar
ferhat committed
362
363
}

364
365
void
ParsingDriver::end_initval()
366
{
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
void
ParsingDriver::end_endval()
373
{
sebastien's avatar
sebastien committed
374
375
  mod_file->addStatement(new EndValStatement(init_values, mod_file->symbol_table));
  init_values.clear();
376
377
}

378
void
sebastien's avatar
sebastien committed
379
ParsingDriver::end_histval()
380
{
sebastien's avatar
sebastien committed
381
382
  mod_file->addStatement(new HistValStatement(hist_values, mod_file->symbol_table));
  hist_values.clear();
383
384
}

385
386
387
388
389
390
391
void
ParsingDriver::end_homotopy()
{
  mod_file->addStatement(new HomotopyStatement(homotopy_values, mod_file->symbol_table));
  homotopy_values.clear();
}

sebastien's avatar
sebastien committed
392
393
394
void
ParsingDriver::begin_model()
{
sebastien's avatar
sebastien committed
395
  set_current_data_tree(&mod_file->dynamic_model);
sebastien's avatar
sebastien committed
396
397
}

398
void
sebastien's avatar
sebastien committed
399
ParsingDriver::end_shocks()
400
{
sebastien's avatar
sebastien committed
401
402
403
404
405
406
407
  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();
408
409
}

410
void
sebastien's avatar
sebastien committed
411
ParsingDriver::end_mshocks()
412
{
sebastien's avatar
sebastien committed
413
414
415
416
417
418
419
  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();
420
421
}

422
423
void
ParsingDriver::add_det_shock(string *var)
424
{
425
  check_symbol_existence(*var);
426
  SymbolType type = mod_file->symbol_table.getType(*var);
sebastien's avatar
sebastien committed
427
428
429
430
431
432
433
434
435
436
437
438
  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++)
439
    {
sebastien's avatar
sebastien committed
440
441
442
443
444
      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);
445
    }
sebastien's avatar
sebastien committed
446
447
448
449
450

  det_shocks[*var] = v;

  det_shocks_periods.clear();
  det_shocks_values.clear();
451
  delete var;
452
453
}

454
void
sebastien's avatar
sebastien committed
455
ParsingDriver::add_stderr_shock(string *var, NodeID value)
456
{
457
  check_symbol_existence(*var);
sebastien's avatar
sebastien committed
458
459
460
461
  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
462
  std_shocks[*var] = value;
sebastien's avatar
sebastien committed
463

464
  delete var;
465
466
}

467
void
sebastien's avatar
sebastien committed
468
ParsingDriver::add_var_shock(string *var, NodeID value)
469
{
470
  check_symbol_existence(*var);
sebastien's avatar
sebastien committed
471
472
473
474
  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
475
  var_shocks[*var] = value;
sebastien's avatar
sebastien committed
476

477
  delete var;
478
479
}

480
void
sebastien's avatar
sebastien committed
481
ParsingDriver::add_covar_shock(string *var1, string *var2, NodeID value)
482
{
483
484
  check_symbol_existence(*var1);
  check_symbol_existence(*var2);
sebastien's avatar
sebastien committed
485
486
487
488
489
490
491
492
493

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

sebastien's avatar
sebastien committed
495
  covar_shocks[key] = value;
sebastien's avatar
sebastien committed
496

497
498
  delete var1;
  delete var2;
499
500
}

501
void
sebastien's avatar
sebastien committed
502
ParsingDriver::add_correl_shock(string *var1, string *var2, NodeID value)
503
{
504
505
  check_symbol_existence(*var1);
  check_symbol_existence(*var2);
sebastien's avatar
sebastien committed
506
507
508
509
510
511
512
513
514

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

sebastien's avatar
sebastien committed
516
  corr_shocks[key] = value;
sebastien's avatar
sebastien committed
517

518
519
520
521
522
523
524
  delete var1;
  delete var2;
}

void
ParsingDriver::add_period(string *p1, string *p2)
{
sebastien's avatar
sebastien committed
525
526
  int p1_val = atoi(p1->c_str());
  int p2_val = atoi(p2->c_str());
sebastien's avatar
sebastien committed
527
  det_shocks_periods.push_back(make_pair(p1_val, p2_val));
528
529
  delete p1;
  delete p2;
530
531
}

532
533
void
ParsingDriver::add_period(string *p1)
534
{
sebastien's avatar
sebastien committed
535
  int p1_val = atoi(p1->c_str());
sebastien's avatar
sebastien committed
536
  det_shocks_periods.push_back(make_pair(p1_val, p1_val));
537
  delete p1;
538
539
}

sebastien's avatar
sebastien committed
540
541
542
543
void
ParsingDriver::add_value(NodeID value)
{
  det_shocks_values.push_back(value);
544
545
}

ferhat's avatar
ferhat committed
546
547
548
549
550
551
void
ParsingDriver::add_value(string *p1)
{
  det_shocks_values.push_back(add_constant(p1));
}

552
553
void
ParsingDriver::do_sigma_e()
554
{
sebastien's avatar
sebastien committed
555
556
557
558
559
560
561
562
563
  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();
564
565
}

566
567
void
ParsingDriver::end_of_row()
568
{
sebastien's avatar
sebastien committed
569
570
  sigmae_matrix.push_back(sigmae_row);
  sigmae_row.clear();
571
572
}

573
void
sebastien's avatar
sebastien committed
574
ParsingDriver::add_to_row_const(string *s)
575
{
sebastien's avatar
sebastien committed
576
  sigmae_row.push_back(add_constant(s));
577
578
579
}

void
sebastien's avatar
sebastien committed
580
ParsingDriver::add_to_row(NodeID v)
581
{
sebastien's avatar
sebastien committed
582
  sigmae_row.push_back(v);
583
584
}

585
586
void
ParsingDriver::steady()
587
{
sebastien's avatar
sebastien committed
588
  mod_file->addStatement(new SteadyStatement(options_list));
sebastien's avatar
sebastien committed
589
  options_list.clear();
590
591
}

592
593
void
ParsingDriver::option_num(const string &name_option, string *opt1, string *opt2)
594
{
sebastien's avatar
sebastien committed
595
596
597
598
  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
599
  options_list.paired_num_options[name_option] = make_pair(*opt1, *opt2);
600
601
  delete opt1;
  delete opt2;
602
603
}

604
605
void
ParsingDriver::option_num(const string &name_option, string *opt)
606
{
607
608
  option_num(name_option, *opt);
  delete opt;
609
610
}

611
612
void
ParsingDriver::option_num(const string &name_option, const string &opt)
613
{
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()))
sebastien's avatar
sebastien committed
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());
sebastien's avatar
sebastien committed
621
  else if (name_option == "cutoff")
sebastien's avatar
sebastien committed
622
    mod_file->dynamic_model.cutoff = atof(opt.c_str());
sebastien's avatar
sebastien committed
623

sebastien's avatar
sebastien committed
624
  options_list.num_options[name_option] = opt;
625
626
}

627
628
void
ParsingDriver::option_str(const string &name_option, string *opt)
629
{
630
631
  option_str(name_option, *opt);
  delete opt;
632
633
}

634
635
void
ParsingDriver::option_str(const string &name_option, const string &opt)
636
{
sebastien's avatar
sebastien committed
637
638
639
640
641
642
643
  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
}

sebastien's avatar
sebastien committed
655
656
657
void
ParsingDriver::linear()
{
sebastien's avatar
sebastien committed
658
  mod_file->linear = true;
659
660
}

661
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
  delete tmp_var;
668
669
}

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

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

684
685
void
ParsingDriver::simul()
686
{
687
  mod_file->addStatement(new SimulStatement(options_list, mod_file->dynamic_model.mode));
sebastien's avatar
sebastien committed
688
  options_list.clear();
689
690
}

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
void
ParsingDriver::check()
700
{
sebastien's avatar
sebastien committed
701
702
  mod_file->addStatement(new CheckStatement(options_list));
  options_list.clear();
703
704
}

705
void
sebastien's avatar
sebastien committed
706
ParsingDriver::add_estimated_params_element()
707
{
sebastien's avatar
sebastien committed
708
709
710
711
712
  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
void
sebastien's avatar
sebastien committed
717
ParsingDriver::estimated_params()
718
{
sebastien's avatar
sebastien committed
719
720
  mod_file->addStatement(new EstimatedParamsStatement(estim_params_list, mod_file->symbol_table));
  estim_params_list.clear();
721
722
}

723
void
sebastien's avatar
sebastien committed
724
ParsingDriver::estimated_params_init()
725
{
sebastien's avatar
sebastien committed
726
727
  mod_file->addStatement(new EstimatedParamsInitStatement(estim_params_list, mod_file->symbol_table));
  estim_params_list.clear();
728
729
}

730
void
sebastien's avatar
sebastien committed
731
ParsingDriver::estimated_params_bounds()
732
{
sebastien's avatar
sebastien committed
733
734
  mod_file->addStatement(new EstimatedParamsBoundsStatement(estim_params_list, mod_file->symbol_table));
  estim_params_list.clear();
735
736
}

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

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

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

void
ParsingDriver::run_posterior_analysis()
{
sebastien's avatar
sebastien committed
763
764
  mod_file->addStatement(new PosteriorAnalysisStatement(symbol_list, options_list));
  symbol_list.clear();
sebastien's avatar
sebastien committed
765
766
767
  options_list.clear();
}

768
769
770
771
772
773
774
void
ParsingDriver::dynare_sensitivity()
{
  mod_file->addStatement(new DynareSensitivityStatement(options_list));
  options_list.clear();
}

sebastien's avatar
sebastien committed
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
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
792
793
}

794
void
sebastien's avatar
sebastien committed
795
ParsingDriver::optim_options_num(string *name, string *value)
796
{
sebastien's avatar
sebastien committed
797
798
799
800
  optim_options_helper(*name);
  options_list.string_options["optim_opt"] += *value;
  delete name;
  delete value;
801
802
}

803
804
void
ParsingDriver::set_varobs()
805
{
sebastien's avatar
sebastien committed
806
807
  mod_file->addStatement(new VarobsStatement(symbol_list));
  symbol_list.clear();
808
809
}

810
void
sebastien's avatar
sebastien committed
811
ParsingDriver::set_trends()
812
{
sebastien's avatar
sebastien committed
813
814
  mod_file->addStatement(new ObservationTrendsStatement(trend_elements, mod_file->symbol_table));
  trend_elements.clear();
815
816
}

817
void
sebastien's avatar
sebastien committed
818
ParsingDriver::set_trend_element(string *arg1, NodeID arg2)
819
{
sebastien's avatar
sebastien committed
820
821
822
  check_symbol_existence(*arg1);
  if (trend_elements.find(*arg1) != trend_elements.end())
    error("observation_trends: " + *arg1 + " declared twice");
sebastien's avatar
sebastien committed
823
  trend_elements[*arg1] = arg2;
824
  delete arg1;
825
826
}

827
void
sebastien's avatar
sebastien committed
828
ParsingDriver::set_optim_weights(string *name, NodeID value)
829
{
sebastien's avatar
sebastien committed
830
831
832
833
834
  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
835
  var_weights[*name] = value;
sebastien's avatar
sebastien committed
836
  delete name;
837
838
}

839
void
sebastien's avatar
sebastien committed
840
ParsingDriver::set_optim_weights(string *name1, string *name2, NodeID value)
841
{
sebastien's avatar
sebastien committed
842
843
844
845
846
847
848
849
850
851
852
853
854
855
  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
856
  covar_weights[covar_key] = value;
sebastien's avatar
sebastien committed
857
858
  delete name1;
  delete name2;
859
860
}

861
void
sebastien's avatar
sebastien committed
862
ParsingDriver::optim_weights()
863
{
sebastien's avatar
sebastien committed
864