allow for vector <int> to be passed by Bison and modified processing of svar statement accordingly.

git-svn-id: https://www.dynare.org/svn/dynare/trunk@3221 ac1d8469-bf42-47a9-8791-bf33cf982152
issue#70
houtanb 2009-12-10 22:49:50 +00:00
parent 1f4d4a2871
commit e1f043485f
6 changed files with 123 additions and 34 deletions

View File

@ -1210,6 +1210,7 @@ void
SvarStatement::writeOutput(ostream &output, const string &basename) const
{
OptionsList::num_options_type::const_iterator it0, it1, it2;
OptionsList::vec_int_options_type::const_iterator itv;
it0 = options_list.num_options.find("ms.chain");
if (it0 != options_list.num_options.end())
@ -1223,11 +1224,17 @@ SvarStatement::writeOutput(ostream &output, const string &basename) const
it0 = options_list.string_options.find("ms.coefficients");
it1 = options_list.string_options.find("ms.variances");
it2 = options_list.string_options.find("ms.constants");
if (it0 != options_list.string_options.end() && it1 == options_list.string_options.end() && it2 == options_list.string_options.end())
if (it0 != options_list.string_options.end() &&
it1 == options_list.string_options.end() &&
it2 == options_list.string_options.end())
output << "." << it0->second;
else if (it0 == options_list.string_options.end() && it1 != options_list.string_options.end() && it2 == options_list.string_options.end())
else if (it0 == options_list.string_options.end() &&
it1 != options_list.string_options.end() &&
it2 == options_list.string_options.end())
output << "." << it1->second;
else if (it0 == options_list.string_options.end() && it1 == options_list.string_options.end() && it2 != options_list.string_options.end())
else if (it0 == options_list.string_options.end() &&
it1 == options_list.string_options.end() &&
it2 != options_list.string_options.end())
output << "." << it2->second;
else
{
@ -1235,12 +1242,27 @@ SvarStatement::writeOutput(ostream &output, const string &basename) const
exit(EXIT_FAILURE);
}
it0 = options_list.num_options.find("ms.equations");
if (it0 != options_list.num_options.end())
if (it0->second.find("[")!=string::npos)
output << ".equations = " << it0->second << "';" << endl;
else
output << ".equations = " << it0->second << ";" << endl;
itv = options_list.vector_int_options.find("ms.equations");
output << ".equations = ";
if (itv != options_list.vector_int_options.end())
{
if (itv->second.size() > 1)
{
output << "[";
for (vector<int>::const_iterator viit=itv->second.begin();
viit!=itv->second.end(); viit++)
output << *viit << ";";
output.seekp((long)output.tellp()-1);
output << "];" << endl;
}
else if (itv->second.size() == 1)
output << itv->second.front() << ";" << endl;
else
{
cerr << "SvarStatement::writeOutput() Should not arrive here (3). Please report this to the Dynare Team." << endl;
exit(EXIT_FAILURE);
}
}
else
output << ".equations = 'ALL';" << endl;
output << "'ALL';" << endl;
}

View File

@ -74,6 +74,7 @@ class ParsingDriver;
NodeID node_val;
SymbolType symbol_type_val;
vector<string *> *vector_string_val;
vector<int> *vector_int_val;
};
%{
@ -167,6 +168,7 @@ class ParsingDriver;
%type <string_val> calib_arg2 range number
%type <symbol_type_val> change_type_arg
%type <vector_string_val> change_type_var_list
%type <vector_int_val> vector_int_body vector_int
%%
@ -1871,11 +1873,41 @@ o_number_of_states : NUMBER_OF_STATES EQUAL INT_NUMBER { driver.option_num("ms.n
o_coefficients : COEFFICIENTS { driver.option_str("ms.coefficients","svar_coefficients"); };
o_variances : VARIANCES { driver.option_str("ms.variances","svar_variances"); };
o_constants : CONSTANTS { driver.option_str("ms.constants","svar_constants"); };
o_equations : EQUATIONS EQUAL vec_int
{ driver.option_num("ms.equations",$3); }
o_equations : EQUATIONS EQUAL vector_int
{ driver.option_vec_int("ms.equations",$3); }
| EQUATIONS EQUAL INT_NUMBER
{ driver.option_num("ms.equations",$3); }
{
vector<int> *oneInt = new vector<int>();
oneInt->push_back(atoi((*$3).c_str()));
driver.option_vec_int("ms.equations",oneInt);
delete oneInt;
}
;
vector_int_body : INT_NUMBER
{ $$ = new vector<int>(); $$->push_back(atoi((*$1).c_str())); }
| vector_int_body INT_NUMBER
{ $$ = $1; $1->push_back(atoi((*$2).c_str())); }
| vector_int_body COMMA INT_NUMBER
{ $$ = $1; $1->push_back(atoi((*$3).c_str())); }
;
vector_int : '[' vector_int_body ']'
{ $$ = $2; }
| '[' vector_int_body COMMA ']'
{ $$ = $2; }
| '[' COMMA vector_int_body ']'
{ $$ = $3; }
| '[' COMMA vector_int_body COMMA ']'
{ $$ = $3; }
| '[' INT_NUMBER ':' INT_NUMBER ']'
{
$$ = new vector<int>();
for(int i=atoi((*$2).c_str()); i<=atoi((*$4).c_str()); i++)
$$->push_back(i);
}
;
o_instruments : INSTRUMENTS EQUAL '(' symbol_list ')' {driver.option_symbol_list("instruments"); };
range : symbol ':' symbol

View File

@ -761,6 +761,16 @@ ParsingDriver::option_symbol_list(const string &name_option)
symbol_list.clear();
}
void
ParsingDriver::option_vec_int(const string &name_option, const vector<int> *opt)
{
if (options_list.vector_int_options.find(name_option)
!= options_list.vector_int_options.end())
error("option " + name_option + " declared twice");
options_list.vector_int_options[name_option] = *opt;
}
void
ParsingDriver::linear()
{
@ -1198,6 +1208,7 @@ void
ParsingDriver::svar()
{
OptionsList::num_options_type::const_iterator it0, it1, it2;
OptionsList::vec_int_options_type::const_iterator itv;
it0 = options_list.string_options.find("ms.coefficients");
it1 = options_list.string_options.find("ms.variances");
@ -1221,29 +1232,14 @@ ParsingDriver::svar()
else if (atoi(it0->second.c_str()) <= 0)
error("The value passed to the chain option must be greater than zero.");
it0 = options_list.num_options.find("ms.equations");
if (it0 != options_list.num_options.end())
itv = options_list.vector_int_options.find("ms.equations");
if (itv != options_list.vector_int_options.end())
{
string strNextNumber;
for (string::const_iterator it=it0->second.begin(); it<it0->second.end(); it++)
{
if (*it == '[' ||
*it == ',' ||
*it == ' ' ||
*it == ':' ||
*it == ']')
{
if (!strNextNumber.empty())
if (atoi(strNextNumber.c_str()) <= 0)
error("The value(s) passed to the equation option must be greater than zero.");
strNextNumber.clear();
}
else
strNextNumber += *it;
}
if (itv->second.empty())
error("There was an error in the integers passed to the equation option.");
if (!strNextNumber.empty())
if (atoi(strNextNumber.c_str()) <= 0)
for (vector<int>::const_iterator viit=itv->second.begin(); viit != itv->second.end(); viit++)
if (*viit <= 0)
error("The value(s) passed to the equation option must be greater than zero.");
}

View File

@ -288,6 +288,8 @@ public:
void option_str(const string &name_option, const string &opt);
//! Sets an option to a list of symbols (used in conjunction with add_in_symbol_list())
void option_symbol_list(const string &name_option);
//! Sets an option to a vector of integers
void option_vec_int(const string &name_option, const vector<int> *opt);
//! Indicates that the model is linear
void linear();
//! Adds a variable to temporary symbol list

View File

@ -80,6 +80,23 @@ OptionsList::writeOutput(ostream &output) const
for(symbol_list_options_type::const_iterator it = symbol_list_options.begin();
it != symbol_list_options.end(); it++)
it->second.writeOutput("options_." + it->first, output);
for(vec_int_options_type::const_iterator it = vector_int_options.begin();
it != vector_int_options.end(); it++)
{
output << "options_." << it->first << " = ";
if (it->second.size() > 1)
{
output << "[";
for (vector<int>::const_iterator viit=it->second.begin();
viit!=it->second.end(); viit++)
output << *viit << ";";
output.seekp((long)output.tellp()-1);
output << "];" << endl;
}
else
output << it->second.front() << ";" << endl;
}
}
void
@ -103,6 +120,23 @@ OptionsList::writeOutput(ostream &output, const string &option_group) const
for(symbol_list_options_type::const_iterator it = symbol_list_options.begin();
it != symbol_list_options.end(); it++)
it->second.writeOutput(option_group + "." + it->first, output);
for(vec_int_options_type::const_iterator it = vector_int_options.begin();
it != vector_int_options.end(); it++)
{
output << option_group << "." << it->first << " = ";
if (it->second.size() > 1)
{
output << "[";
for (vector<int>::const_iterator viit=it->second.begin();
viit!=it->second.end(); viit++)
output << *viit << ";";
output.seekp((long)output.tellp()-1);
output << "];" << endl;
}
else
output << it->second.front() << ";" << endl;
}
}
void
@ -112,4 +146,5 @@ OptionsList::clear()
paired_num_options.clear();
string_options.clear();
symbol_list_options.clear();
vector_int_options.clear();
}

View File

@ -94,10 +94,12 @@ public:
typedef map<string, pair<string, string> > paired_num_options_type;
typedef map<string, string> string_options_type;
typedef map<string, SymbolList> symbol_list_options_type;
typedef map<string, vector<int> > vec_int_options_type;
num_options_type num_options;
paired_num_options_type paired_num_options;
string_options_type string_options;
symbol_list_options_type symbol_list_options;
vec_int_options_type vector_int_options;
void writeOutput(ostream &output) const;
void writeOutput(ostream &output, const string &option_group) const;
void clear();