Stan  2.14.0
probability, sampling & optimization
term_grammar_def.hpp
Go to the documentation of this file.
1 #ifndef STAN_LANG_GRAMMARS_TERM_GRAMMAR_DEF_HPP
2 #define STAN_LANG_GRAMMARS_TERM_GRAMMAR_DEF_HPP
3 
4 #include <stan/lang/ast.hpp>
10 #include <boost/spirit/include/qi.hpp>
11 #include <boost/spirit/include/phoenix_core.hpp>
12 #include <string>
13 #include <sstream>
14 #include <vector>
15 
17  (stan::lang::expression, expr_)
18  (std::vector<std::vector<stan::lang::expression> >,
19  dimss_) )
20 
21 BOOST_FUSION_ADAPT_STRUCT(stan::lang::index_op_sliced,
22  (stan::lang::expression, expr_)
23  (std::vector<stan::lang::idx>, idxs_) )
24 
25 BOOST_FUSION_ADAPT_STRUCT(stan::lang::integrate_ode,
26  (std::string, integration_function_name_)
27  (std::string, system_function_name_)
28  (stan::lang::expression, y0_)
29  (stan::lang::expression, t0_)
30  (stan::lang::expression, ts_)
31  (stan::lang::expression, theta_)
32  (stan::lang::expression, x_)
33  (stan::lang::expression, x_int_) )
34 
35 BOOST_FUSION_ADAPT_STRUCT(stan::lang::integrate_ode_control,
36  (std::string, integration_function_name_)
37  (std::string, system_function_name_)
38  (stan::lang::expression, y0_)
39  (stan::lang::expression, t0_)
40  (stan::lang::expression, ts_)
41  (stan::lang::expression, theta_)
42  (stan::lang::expression, x_)
43  (stan::lang::expression, x_int_)
44  (stan::lang::expression, rel_tol_)
45  (stan::lang::expression, abs_tol_)
46  (stan::lang::expression, max_num_steps_) )
47 
49  (std::string, name_)
50  (std::vector<stan::lang::expression>, args_) )
51 
52 BOOST_FUSION_ADAPT_STRUCT(stan::lang::array_expr,
53  (std::vector<stan::lang::expression>, args_) )
54 
55 BOOST_FUSION_ADAPT_STRUCT(stan::lang::int_literal,
56  (int, val_)
57  (stan::lang::expr_type, type_))
58 
59 BOOST_FUSION_ADAPT_STRUCT(stan::lang::double_literal,
60  (double, val_)
61  (stan::lang::expr_type, type_) )
62 
63 
64 namespace stan {
65 
66  namespace lang {
67 
68  template <typename Iterator>
69  term_grammar<Iterator>::term_grammar(variable_map& var_map,
70  std::stringstream& error_msgs,
71  expression_grammar<Iterator>& eg)
72  : term_grammar::base_type(term_r),
73  var_map_(var_map),
74  error_msgs_(error_msgs),
75  expression_g(eg),
76  indexes_g(var_map, error_msgs, eg) {
77  using boost::spirit::qi::_1;
78  using boost::spirit::qi::_a;
79  using boost::spirit::qi::_b;
80  using boost::spirit::qi::_c;
81  using boost::spirit::qi::char_;
82  using boost::spirit::qi::double_;
83  using boost::spirit::qi::eps;
84  using boost::spirit::qi::int_;
85  using boost::spirit::qi::hold;
86  using boost::spirit::qi::lexeme;
87  using boost::spirit::qi::lit;
88  using boost::spirit::qi::no_skip;
89  using boost::spirit::qi::string;
90  using boost::spirit::qi::_pass;
91  using boost::spirit::qi::_val;
92  using boost::spirit::qi::labels::_r1;
93 
94  term_r.name("expression");
95  term_r
96  = (negated_factor_r(_r1)[assign_lhs_f(_val, _1)]
97  >> *((lit('*') > negated_factor_r(_r1)
98  [multiplication_f(_val, _1,
99  boost::phoenix::ref(error_msgs_))])
100  | (lit('/') > negated_factor_r(_r1)
101  [division_f(_val, _1,
102  boost::phoenix::ref(error_msgs_))])
103  | (lit('%') > negated_factor_r(_r1)
104  [modulus_f(_val, _1, _pass,
105  boost::phoenix::ref(error_msgs_))])
106  | (lit('\\') > negated_factor_r(_r1)
107  [left_division_f(_val, _pass, _1,
108  boost::phoenix::ref(error_msgs_))])
109  | (lit(".*") > negated_factor_r(_r1)
110  [elt_multiplication_f(_val, _1,
111  boost::phoenix::ref(error_msgs_))])
112  | (lit("./") > negated_factor_r(_r1)
113  [elt_division_f(_val, _1,
114  boost::phoenix::ref(error_msgs_))])));
115 
116  negated_factor_r.name("expression");
118  = lit('-') >> negated_factor_r(_r1)
119  [negate_expr_f(_val, _1, _pass,
120  boost::phoenix::ref(error_msgs_))]
121  | lit('!') >> negated_factor_r(_r1)
122  [logical_negate_expr_f(_val, _1,
123  boost::phoenix::ref(error_msgs_))]
124  | lit('+') >> negated_factor_r(_r1)[assign_lhs_f(_val, _1)]
125  | exponentiated_factor_r(_r1)[assign_lhs_f(_val, _1)];
126 
127  exponentiated_factor_r.name("expression");
129  = idx_factor_r(_r1)[assign_lhs_f(_val, _1)]
130  >> -(lit('^')
131  > negated_factor_r(_r1)
132  [exponentiation_f(_val, _1, _r1, _pass,
133  boost::phoenix::ref(error_msgs_))]);
134 
135  idx_factor_r.name("expression");
137  = factor_r(_r1)[assign_lhs_f(_val, _1)]
138  > *( ( (+dims_r(_r1))[assign_lhs_f(_a, _1)]
139  > eps
140  [add_expression_dimss_f(_val, _a, _pass,
141  boost::phoenix::ref(error_msgs_) )] )
142  | (indexes_g(_r1)[assign_lhs_f(_b, _1)]
143  > eps[add_idxs_f(_val, _b, _pass,
144  boost::phoenix::ref(error_msgs_))])
145  | (lit("'")
146  > eps[transpose_f(_val, _pass,
147  boost::phoenix::ref(error_msgs_))]) );
148 
149  integrate_ode_control_r.name("expression");
151  %= ( (string("integrate_ode_rk45") >> no_skip[!char_("a-zA-Z0-9_")])
152  | (string("integrate_ode_bdf") >> no_skip[!char_("a-zA-Z0-9_")]) )
153  >> lit('(') // >> allows backtracking to non-control
154  >> identifier_r // 1) system function name (function only)
155  >> lit(',')
156  >> expression_g(_r1) // 2) y0
157  >> lit(',')
158  >> expression_g(_r1) // 3) t0 (data only)
159  >> lit(',')
160  >> expression_g(_r1) // 4) ts (data only)
161  >> lit(',')
162  >> expression_g(_r1) // 5) theta
163  >> lit(',')
164  >> expression_g(_r1) // 6) x (data only)
165  >> lit(',')
166  >> expression_g(_r1) // 7) x_int (data only)
167  >> lit(',')
168  >> expression_g(_r1) // 8) relative tolerance (data only)
169  >> lit(',')
170  >> expression_g(_r1) // 9) absolute tolerance (data only)
171  >> lit(',')
172  >> expression_g(_r1) // 10) maximum number of steps (data only)
173  > lit(')')
174  [validate_integrate_ode_control_f(_val, boost::phoenix::ref(var_map_),
175  _pass,
176  boost::phoenix::ref(error_msgs_))];
177 
178  integrate_ode_r.name("expression");
180  %= ( (string("integrate_ode_rk45") >> no_skip[!char_("a-zA-Z0-9_")])
181  | (string("integrate_ode_bdf") >> no_skip[!char_("a-zA-Z0-9_")])
182  | (string("integrate_ode") >> no_skip[!char_("a-zA-Z0-9_")])
183  [deprecated_integrate_ode_f(boost::phoenix::ref(error_msgs_))] )
184  > lit('(')
185  > identifier_r // 1) system function name (function only)
186  > lit(',')
187  > expression_g(_r1) // 2) y0
188  > lit(',')
189  > expression_g(_r1) // 3) t0 (data only)
190  > lit(',')
191  > expression_g(_r1) // 4) ts (data only)
192  > lit(',')
193  > expression_g(_r1) // 5) theta
194  > lit(',')
195  > expression_g(_r1) // 6) x (data only)
196  > lit(',')
197  > expression_g(_r1) // 7) x_int (data only)
198  > lit(')')
199  [validate_integrate_ode_f(_val, boost::phoenix::ref(var_map_),
200  _pass, boost::phoenix::ref(error_msgs_))];
201 
202  factor_r.name("expression");
203  factor_r =
204  integrate_ode_control_r(_r1)[assign_lhs_f(_val, _1)]
205  | integrate_ode_r(_r1)[assign_lhs_f(_val, _1)]
206  | (fun_r(_r1)[assign_lhs_f(_b, _1)]
207  > eps[set_fun_type_named_f(_val, _b, _r1, _pass,
208  boost::phoenix::ref(error_msgs_))])
209  | (variable_r[assign_lhs_f(_a, _1)]
210  > eps[set_var_type_f(_a, _val, boost::phoenix::ref(var_map_),
211  boost::phoenix::ref(error_msgs_),
212  _pass)])
213  | int_literal_r[assign_lhs_f(_val, _1)]
214  | double_literal_r[assign_lhs_f(_val, _1)]
215  | (array_expr_r(_r1)[assign_lhs_f(_c, _1)]
216  > eps[set_array_expr_type_f(_val, _c, _r1, _pass,
217  boost::phoenix::ref(var_map_),
218  boost::phoenix::ref(error_msgs_))])
219  | (lit('(')
220  > expression_g(_r1)[assign_lhs_f(_val, _1)]
221  > lit(')'));
222 
223  int_literal_r.name("integer literal");
225  %= int_
226  >> !(lit('.') | lit('e') | lit('E'));
227 
228  double_literal_r.name("real literal");
230  %= double_;
231 
232  fun_r.name("function and argument expressions");
233  fun_r
234  %= (hold[identifier_r[is_prob_fun_f(_1, _pass)]]
235  >> &lit('(')
236  > prob_args_r(_r1))
237  | (identifier_r >> args_r(_r1));
238 
239  identifier_r.name("identifier");
241  %= lexeme[char_("a-zA-Z")
242  >> *char_("a-zA-Z0-9_.")];
243 
244  prob_args_r.name("probability function argument");
246  %= (lit('(') >> lit(')'))
247  | hold[lit('(')
248  >> expression_g(_r1)
249  >> lit(')')]
250  | (lit('(')
251  >> expression_g(_r1)
252  >> (lit(',')
253  [require_vbar_f(_pass, boost::phoenix::ref(error_msgs_))]
254  | (eps > lit('|')))
255  >> (expression_g(_r1) % ',')
256  >> lit(')'));
257 
258  args_r.name("function arguments");
259  args_r
260  %= (lit('(') >> lit(')'))
261  | (lit('(') >> (expression_g(_r1) % ',') >> lit(')'));
262 
263  // mitzi: why no error message if array dim isn't int expr?
264  dim_r.name("array dimension (integer expression)");
265  dim_r
266  %= expression_g(_r1)
267  >> eps[validate_int_expr_silent_f(_val, _pass)];
268 
269  dims_r.name("array dimensions");
270  dims_r
271  %= lit('[')
272  >> (dim_r(_r1)
273  % ',' )
274  >> lit(']');
275 
276  variable_r.name("variable name");
277  variable_r
278  %= identifier_r
279  > !lit('('); // negative lookahead to prevent failure in
280  // fun to try to evaluate as variable [cleaner
281  // error msgs]
282 
283  array_expr_r.name("expression");
285  %= lit('{')
286  >> expression_g(_r1) % ','
287  >> lit('}');
288  }
289  }
290 }
291 #endif
boost::phoenix::function< require_vbar > require_vbar_f
boost::phoenix::function< set_var_type > set_var_type_f
boost::phoenix::function< deprecated_integrate_ode > deprecated_integrate_ode_f
boost::phoenix::function< set_fun_type_named > set_fun_type_named_f
boost::spirit::qi::rule< Iterator, expression(var_origin), whitespace_grammar< Iterator > > term_r
Probability, optimization and sampling library.
boost::spirit::qi::rule< Iterator, fun(var_origin), whitespace_grammar< Iterator > > fun_r
Structure for an indexed expression.
Definition: index_op.hpp:14
boost::spirit::qi::rule< Iterator, int_literal(), whitespace_grammar< Iterator > > int_literal_r
Template specification of functions in std for Stan.
std::stringstream & error_msgs_
boost::spirit::qi::rule< Iterator, integrate_ode_control(var_origin), whitespace_grammar< Iterator > > integrate_ode_control_r
boost::spirit::qi::rule< Iterator, std::vector< expression >var_origin), whitespace_grammar< Iterator > > prob_args_r
boost::phoenix::function< exponentiation_expr > exponentiation_f
boost::spirit::qi::rule< Iterator, std::string(), whitespace_grammar< Iterator > > identifier_r
boost::spirit::qi::rule< Iterator, expression(var_origin), whitespace_grammar< Iterator > > exponentiated_factor_r
boost::phoenix::function< transpose_expr > transpose_f
boost::phoenix::function< multiplication_expr > multiplication_f
boost::spirit::qi::rule< Iterator, expression(var_origin), boost::spirit::qi::locals< std::vector< std::vector< expression > >, std::vector< idx > >, whitespace_grammar< Iterator > > idx_factor_r
BOOST_FUSION_ADAPT_STRUCT(stan::lang::expr_type,(stan::lang::base_expr_type, base_type_)(size_t, num_dims_)) namespace stan
boost::spirit::qi::rule< Iterator, double_literal(), whitespace_grammar< Iterator > > double_literal_r
boost::phoenix::function< add_expression_dimss > add_expression_dimss_f
boost::phoenix::function< set_array_expr_type > set_array_expr_type_f
boost::spirit::qi::rule< Iterator, std::vector< expression >var_origin), whitespace_grammar< Iterator > > dims_r
boost::spirit::qi::rule< Iterator, array_expr(var_origin), whitespace_grammar< Iterator > > array_expr_r
boost::phoenix::function< validate_int_expr_silent > validate_int_expr_silent_f
boost::phoenix::function< is_prob_fun > is_prob_fun_f
boost::spirit::qi::rule< Iterator, std::vector< expression >var_origin), whitespace_grammar< Iterator > > args_r
boost::spirit::qi::rule< Iterator, boost::spirit::qi::locals< variable, fun, array_expr >, expression(var_origin), whitespace_grammar< Iterator > > factor_r
boost::phoenix::function< assign_lhs > assign_lhs_f
boost::phoenix::function< validate_integrate_ode > validate_integrate_ode_f
boost::spirit::qi::rule< Iterator, variable(), whitespace_grammar< Iterator > > variable_r
indexes_grammar< Iterator > indexes_g
boost::spirit::qi::rule< Iterator, expression(var_origin), whitespace_grammar< Iterator > > dim_r
boost::phoenix::function< elt_multiplication_expr > elt_multiplication_f
expression_grammar< Iterator > & expression_g
boost::phoenix::function< logical_negate_expr > logical_negate_expr_f
boost::phoenix::function< add_idxs > add_idxs_f
boost::phoenix::function< division_expr > division_f
boost::phoenix::function< elt_division_expr > elt_division_f
boost::phoenix::function< negate_expr > negate_expr_f
boost::spirit::qi::rule< Iterator, integrate_ode(var_origin), whitespace_grammar< Iterator > > integrate_ode_r
boost::phoenix::function< validate_integrate_ode_control > validate_integrate_ode_control_f
boost::phoenix::function< left_division_expr > left_division_f
boost::phoenix::function< modulus_expr > modulus_f
boost::spirit::qi::rule< Iterator, expression(var_origin), whitespace_grammar< Iterator > > negated_factor_r

     [ Stan Home Page ] © 2011–2016, Stan Development Team.