From 76d5d441ea28be79197709fedc5f9a14d43ee234 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Wed, 24 Nov 2010 18:26:43 +0100 Subject: [PATCH] Change the syntax for values of deterministic shocks: Arbirtrary expressions after the "values" keywords must now be enclosed within parentheses; consider the following example: periods 1:2; values -1 -2; In the previous syntax, this was interpreted by the preprocessor as a shock of value -1-2 = -3 for periods 1 and 2, which is clearly not the intent of the user; with the new syntax, this will be rejected (too many values compared to the number of ranges). Also note that now commas are no longer required between arbitrary expressions, since the parentheses are sufficient for separating them. --- doc/manual.xml | 11 ++++++++++- preprocessor/DynareBison.yy | 21 +++++++++++++-------- preprocessor/ParsingDriver.cc | 10 ++++++++-- preprocessor/ParsingDriver.hh | 3 ++- 4 files changed, 33 insertions(+), 12 deletions(-) diff --git a/doc/manual.xml b/doc/manual.xml index b046cb99f..227aa57dd 100644 --- a/doc/manual.xml +++ b/doc/manual.xml @@ -1524,7 +1524,7 @@ end; var VARIABLE_NAME; periods INTEGER:INTEGER, INTEGER:INTEGER; - values EXPRESSION , EXPRESSION; + values DOUBLE(EXPRESSION) , DOUBLE(EXPRESSION); @@ -1554,6 +1554,12 @@ end; will have a constant value over the range. + + Note that shock values are not restricted to numerical constants: arbitrary + expressions are also allowed, but you have to enclose them inside + parentheses. + + Example @@ -1567,6 +1573,9 @@ values 0; var v; periods 4:5 6 7:9; values 1 1.1 0.9; +var w; +periods 1 2; +values (1+p) (exp(z)); end; diff --git a/preprocessor/DynareBison.yy b/preprocessor/DynareBison.yy index defb9cc97..bb88b3632 100644 --- a/preprocessor/DynareBison.yy +++ b/preprocessor/DynareBison.yy @@ -740,14 +740,19 @@ period_list : period_list COMMA INT_NUMBER sigma_e : SIGMA_E EQUAL '[' triangular_matrix ']' ';' { driver.do_sigma_e(); }; -value_list - : value_list COMMA expression - {driver.add_value($3);} - | value_list number - {driver.add_value($2);} - | expression - {driver.add_value($1);} - ; +value_list : value_list COMMA '(' expression ')' + { driver.add_value($4); } + | value_list '(' expression ')' + { driver.add_value($3); } + | '(' expression ')' + { driver.add_value($2); } + | value_list COMMA signed_float + { driver.add_value($3); } + | value_list signed_float + { driver.add_value($2); } + | signed_float + { driver.add_value($1); } + ; triangular_matrix : triangular_matrix ';' triangular_row { driver.end_of_row(); } diff --git a/preprocessor/ParsingDriver.cc b/preprocessor/ParsingDriver.cc index a719334af..e3eb96a54 100644 --- a/preprocessor/ParsingDriver.cc +++ b/preprocessor/ParsingDriver.cc @@ -700,9 +700,15 @@ ParsingDriver::add_value(expr_t value) } void -ParsingDriver::add_value(string *p1) +ParsingDriver::add_value(string *v) { - det_shocks_values.push_back(add_constant(p1)); + expr_t id; + if (v->at(0) == '-') + id = data_tree->AddUMinus(data_tree->AddNumConstant(v->substr(1, string::npos))); + else + id = data_tree->AddNumConstant(*v); + delete v; + det_shocks_values.push_back(id); } void diff --git a/preprocessor/ParsingDriver.hh b/preprocessor/ParsingDriver.hh index 32147ee21..a84cf240c 100644 --- a/preprocessor/ParsingDriver.hh +++ b/preprocessor/ParsingDriver.hh @@ -283,7 +283,8 @@ public: //! Adds a deterministic shock value void add_value(expr_t value); //! Adds a deterministic shock value - void add_value(string *p1); + /*! \param v a string containing a (possibly negative) numeric constant */ + void add_value(string *v); //! Writes a Sigma_e block void do_sigma_e(); //! Ends row of Sigma_e block