SymbolTable.cc 10.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/*! \file
  \version 1.0
  \date 04/09/2004
  \par This file implements the SymbolTable class methodes.
*/
//------------------------------------------------------------------------------
#include <iostream>
#include <algorithm>
//------------------------------------------------------------------------------
#include "SymbolTable.hh"
#include "Interface.hh"
using namespace std;
//------------------------------------------------------------------------------
// = *(new symbolmap);
map<string, Symbol, less<string> > SymbolTable::symboltable;
16
17
vector< vector<string> > SymbolTable::name_table;
vector< vector<string> > SymbolTable::tex_name_table;
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
ostringstream SymbolTable::output;
void (* SymbolTable::error) (const char* ) = NULL;
//------------------------------------------------------------------------------
SymbolTable::SymbolTable()
{
  name_table.resize(20);
  tex_name_table.resize(20);
}

//------------------------------------------------------------------------------
SymbolTable::~SymbolTable()
{
  // Empty
}

//------------------------------------------------------------------------------
int SymbolTable::AddSymbol(string name,Type type, string tex_name)
{
  symboltable[name].type = type;
  symboltable[name].referenced = eNotReferenced;
  name_table[(int) type].push_back(name);
  tex_name_table[(int) type].push_back(tex_name);

  switch (type)
    {
    case eExogenous:
      symboltable[name].id = ModelParameters::exo_nbr;
      ModelParameters::exo_nbr++;
      return  ModelParameters::exo_nbr-1;
    case eExogenousDet:
      symboltable[name].id = ModelParameters::exo_det_nbr;
      ModelParameters::exo_det_nbr++;
      return  ModelParameters::exo_det_nbr-1;
    case eEndogenous:
      symboltable[name].id = ModelParameters::endo_nbr;
      ModelParameters::endo_nbr++;
      return  ModelParameters::endo_nbr-1;
    case eParameter:
      symboltable[name].id = ModelParameters::parameter_nbr;
      ModelParameters::parameter_nbr++;
      return  ModelParameters::parameter_nbr-1;
    case eRecursiveVariable:
      symboltable[name].id = ModelParameters::recur_nbr;
      ModelParameters::recur_nbr++;
      return  ModelParameters::recur_nbr-1;
    case eLocalParameter:
      symboltable[name].id = ModelParameters::local_parameter_nbr;
      ModelParameters::local_parameter_nbr++;
      return  ModelParameters::local_parameter_nbr-1;
    default:
      // should never happen
      return -1;
    }

}

//------------------------------------------------------------------------------
int SymbolTable::AddSymbolDeclar(string name,Type type, string tex_name)
{
  //Testing if the symbol exist in the map
  if ( !Exist(name) )
    {
      //The symbol dosn't exist, adding it
      return AddSymbol(name,type, tex_name);
    }
  else
    {
      //The symbol exists, testing its type
      if (symboltable[name].type == type)
        {
          cout << "Warning : symbol " << name << " declared more than once.\n";
          return getID(name);
        }
      else
        {
          string msg = "symbol " + name + " declared more than once with different types.";
          (* error) (msg.c_str());
          return -1;
        }
    }

}

//------------------------------------------------------------------------------
void SymbolTable::AddSymbolRange(string name,int nbr,Type type, string tex_name)
{
}

//------------------------------------------------------------------------------
void SymbolTable::AddLag(string name,int lag)
{
  //Testing if the symbol exist in the map
  if ( !Exist(name) )
    {
      return;
    }
  Type type = symboltable[name].type;
  if ((type == eEndogenous) || (type == eExogenous))
    {
      symboltable[name].lags.push_back(lag);
    }
}

//------------------------------------------------------------------------------
void  SymbolTable::ResetType(string name,Type new_type)
{
  symboltable[name].type = new_type;
}

//------------------------------------------------------------------------------
void  SymbolTable::SetReferenced(string name)
{
  symboltable[name].referenced = eReferenced;
}

//------------------------------------------------------------------------------
Reference   SymbolTable::isReferenced(std::string name)
{
  return symboltable[name].referenced;
}

//------------------------------------------------------------------------------
void SymbolTable::clean()
{
  string unused;
  bool   warning = false;
  vector<Type>  types(3);
  vector<int>   nb_type(3);

  types[0] = eEndogenous;
  types[1] = eExogenous;
  types[2] = eExogenousDet;

  nb_type[0] = ModelParameters::endo_nbr;
  nb_type[1] = ModelParameters::exo_nbr;
  nb_type[2] = ModelParameters::exo_det_nbr;

  // Removing unused variables
  for (int t = 0; t < 3; t++)
    {
      // Checking if all variables are used
      for (int s1 = 0; s1 < nb_type[t]; s1++)
        {
          string name = getNameByID(types[t],s1);
          string tex_name = getTexNameByID(types[t],s1);
          if (isReferenced(name) == eNotReferenced)
            {
              symboltable.erase(name);
              vector<string>::iterator it;
              it = find(name_table[types[t]].begin(), name_table[types[t]].end(), name);
              name_table[types[t]].erase(it);
              it = find(tex_name_table[types[t]].begin(), tex_name_table[types[t]].end(), tex_name);
              tex_name_table[types[t]].erase(it);
              nb_type[t]--;
              unused += "fprintf(1,'%-30s";
              switch(types[t])
                {
                case eEndogenous  : unused += "Endogenous variable\\n','";break;
                case eExogenous   : unused += "Exogenous variable\\n','";break;
                case eExogenousDet  : unused += "Exogenous deterministic variable\\n','";break;
                default : ;
                }
              unused += name;
              unused += "');\n";
              warning = true;
              for (int s2 = s1; s2 < nb_type[t]; s2++)
                {
                  name = getNameByID(types[t],s2);
                  // Decrementing symbol table ids in ST
                  symboltable[name].id--;
                }
              s1--;
            }
        }
    }
  ModelParameters::endo_nbr     = nb_type[0];
  ModelParameters::exo_nbr    = nb_type[1];
  ModelParameters::exo_det_nbr  = nb_type[2];
  /*
  // Checking if unused parameters
  for (int s1 = 0; s1 < ModelParameters::parameter_nbr; s1++)
  {
  string name = getNameByID(eParameter,s1);
  if (isReferenced(name) == eNotReferenced)
  {
  unused += "fprintf(1,'%-30sParameter\\n','";
  unused += name;
  unused += "');\n";

  warning = true;
  }
  }
  */
  if (warning)
    {
      output << "fprintf(1,'Warning : symbol(s) :\\n');\n";
      output << unused;
      output << "fprintf(1,'are declared but not used in the model equations. ');\n";
      output << "reply = input('Continue? [y]\\\\n ','s');\n";
      output << "if isempty(reply), reply='y'; end;\n";
      output << "if strcmpi(reply(1),'n'),\n  return;\nend\n";
    }
  //PrintSymbolTable();
}

string SymbolTable::get()
{
  if (ModelParameters::exo_nbr > 0)
    {
      output << "M_.exo_names = '" << getNameByID(eExogenous, 0) << "';\n";
      output << "M_.exo_names_tex = '" << getTexNameByID(eExogenous, 0) << "';\n";
      for (int id = 1; id < ModelParameters::exo_nbr; id++)
        {
          output << "M_.exo_names = " + interfaces::strvcat("M_.exo_names","'"+getNameByID(eExogenous, id)+"'") + ";\n";
          output << "M_.exo_names_tex = " + interfaces::strvcat("M_.exo_names_tex","'"+getTexNameByID(eExogenous, id)+"'") + ";\n";
        }
    }
  if (ModelParameters::exo_det_nbr > 0)
    {
      output << "lgxdet_ = '" << getNameByID(eExogenousDet, 0) << "';\n";
      output << "lgxdet_tex_ = '" << getTexNameByID(eExogenousDet, 0) << "';\n";
      for (int id = 1; id < ModelParameters::exo_det_nbr; id++)
        {
          output << "lgxdet_ = " + interfaces::strvcat("lgxdet_","'"+getNameByID(eExogenousDet, id)+"'") + ";\n";
          output << "lgxdet_tex_ = " + interfaces::strvcat("lgxdet_tex_","'"+getTexNameByID(eExogenousDet, id)+"'") + ";\n";
        }
    }
  if (ModelParameters::endo_nbr > 0)
    {
      output << "M_.endo_names = '" << getNameByID(eEndogenous, 0) << "';\n";
      output << "M_.endo_names_tex = '" << getTexNameByID(eEndogenous, 0) << "';\n";
      for (int id = 1; id < ModelParameters::endo_nbr; id++)
        {
          output << "M_.endo_names = " + interfaces::strvcat("M_.endo_names","'"+getNameByID(eEndogenous, id)+"'") + ";\n";
          output << "M_.endo_names_tex = " + interfaces::strvcat("M_.endo_names_tex","'"+getTexNameByID(eEndogenous, id)+"'") + ";\n";
        }
    }
  if (ModelParameters::recur_nbr > 0)
    {
      output << "M_.recur_names = '" << getNameByID(eRecursiveVariable, 0) << "';\n";
      output << "M_.recur_names_tex = '" << getTexNameByID(eRecursiveVariable, 0) << "';\n";
      for (int id = 1; id < ModelParameters::recur_nbr; id++)
        {
          output << "M_.recur_names = " + interfaces::strvcat("M_.recur_names","'"+getNameByID(eRecursiveVariable, id)+"'") + ";\n";
          output << "M_.recur_names_tex = " + interfaces::strvcat("M_.recur_names_tex","'"+getTexNameByID(eRecursiveVariable, id)+"'") + ";\n";
        }
    }
  if (ModelParameters::parameter_nbr > 0)
    {
      output << "M_.param_names = '" << getNameByID(eParameter, 0) << "';\n";
      output << "M_.param_names_tex = '" << getTexNameByID(eParameter, 0) << "';\n";
      for (int id = 1; id < ModelParameters::parameter_nbr; id++)
        {
          output << "M_.param_names = " + interfaces::strvcat("M_.param_names","'"+getNameByID(eParameter, id)+"'") + ";\n";
          output << "M_.param_names_tex = " + interfaces::strvcat("M_.param_names_tex","'"+getTexNameByID(eParameter, id)+"'") + ";\n";
        }
    }

  output << "M_.exo_det_nbr = " << ModelParameters::exo_det_nbr << ";\n";
  output << "M_.exo_nbr = " << ModelParameters::exo_nbr << ";\n";
  output << "M_.Sigma_e = zeros(" << ModelParameters::exo_nbr
         << ", " << ModelParameters::exo_nbr << ");\n";
  output << "M_.endo_nbr = " << ModelParameters::endo_nbr << ";\n";
  output << "M_.recur_nbr = " << ModelParameters::recur_nbr << ";\n";
  output << "M_.param_nbr = " << ModelParameters::parameter_nbr << ";\n";
  return output.str();
}

//------------------------------------------------------------------------------
void SymbolTable::erase_local_parameters(void)
{
  std::map<std::string, Symbol, std::less<std::string> >::iterator i = symboltable.begin();
  for(; i != symboltable.end(); ++i)
    {
      if ((i->second).type == eLocalParameter)
        {
          symboltable.erase(i);
        }
    }
}

//------------------------------------------------------------------------------