Stan  2.10.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::int_literal,
53  (int, val_)
54  (stan::lang::expr_type, type_))
55 
56 BOOST_FUSION_ADAPT_STRUCT(stan::lang::double_literal,
57  (double, val_)
58  (stan::lang::expr_type, type_) )
59 
60 
61 namespace stan {
62 
63  namespace lang {
64 
65  template <typename Iterator>
66  term_grammar<Iterator>::term_grammar(variable_map& var_map,
67  std::stringstream& error_msgs,
68  expression_grammar<Iterator>& eg)
69  : term_grammar::base_type(term_r),
70  var_map_(var_map),
71  error_msgs_(error_msgs),
72  expression_g(eg),
73  indexes_g(var_map, error_msgs, eg) {
74  using boost::spirit::qi::_1;
75  using boost::spirit::qi::_a;
76  using boost::spirit::qi::_b;
77  using boost::spirit::qi::char_;
78  using boost::spirit::qi::double_;
79  using boost::spirit::qi::eps;
80  using boost::spirit::qi::int_;
81  using boost::spirit::qi::hold;
82  using boost::spirit::qi::lexeme;
83  using boost::spirit::qi::lit;
84  using boost::spirit::qi::no_skip;
85  using boost::spirit::qi::string;
86  using boost::spirit::qi::_pass;
87  using boost::spirit::qi::_val;
88  using boost::spirit::qi::labels::_r1;
89 
90  term_r.name("expression");
91  term_r
92  = (negated_factor_r(_r1)[assign_lhs_f(_val, _1)]
93  >> *((lit('*') > negated_factor_r(_r1)
94  [multiplication_f(_val, _1,
95  boost::phoenix::ref(error_msgs_))])
96  | (lit('/') > negated_factor_r(_r1)
97  [division_f(_val, _1,
98  boost::phoenix::ref(error_msgs_))])
99  | (lit('%') > negated_factor_r(_r1)
100  [modulus_f(_val, _1, _pass,
101  boost::phoenix::ref(error_msgs_))])
102  | (lit('\\') > negated_factor_r(_r1)
103  [left_division_f(_val, _pass, _1,
104  boost::phoenix::ref(error_msgs_))])
105  | (lit(".*") > negated_factor_r(_r1)
106  [elt_multiplication_f(_val, _1,
107  boost::phoenix::ref(error_msgs_))])
108  | (lit("./") > negated_factor_r(_r1)
109  [elt_division_f(_val, _1,
110  boost::phoenix::ref(error_msgs_))])));
111 
112  negated_factor_r.name("expression");
114  = lit('-') >> negated_factor_r(_r1)
115  [negate_expr_f(_val, _1, _pass,
116  boost::phoenix::ref(error_msgs_))]
117  | lit('!') >> negated_factor_r(_r1)
118  [logical_negate_expr_f(_val, _1,
119  boost::phoenix::ref(error_msgs_))]
120  | lit('+') >> negated_factor_r(_r1)[assign_lhs_f(_val, _1)]
121  | exponentiated_factor_r(_r1)[assign_lhs_f(_val, _1)];
122 
123  exponentiated_factor_r.name("expression");
125  = idx_factor_r(_r1)[assign_lhs_f(_val, _1)]
126  >> -(lit('^')
127  > negated_factor_r(_r1)
128  [exponentiation_f(_val, _1, _r1, _pass,
129  boost::phoenix::ref(error_msgs_))]);
130 
131  idx_factor_r.name("expression");
133  = factor_r(_r1)[assign_lhs_f(_val, _1)]
134  > *( ( (+dims_r(_r1))[assign_lhs_f(_a, _1)]
135  > eps
136  [add_expression_dimss_f(_val, _a, _pass,
137  boost::phoenix::ref(error_msgs_) )] )
138  | (indexes_g(_r1)[assign_lhs_f(_b, _1)]
139  > eps[add_idxs_f(_val, _b, _pass,
140  boost::phoenix::ref(error_msgs_))])
141  | (lit("'")
142  > eps[transpose_f(_val, _pass,
143  boost::phoenix::ref(error_msgs_))]) );
144 
145  integrate_ode_control_r.name("expression");
147  %= ( (string("integrate_ode_rk45") >> no_skip[!char_("a-zA-Z0-9_")])
148  | (string("integrate_ode_bdf") >> no_skip[!char_("a-zA-Z0-9_")]) )
149  >> lit('(') // >> allows backtracking to non-control
150  >> identifier_r // system function name (function only)
151  >> lit(',')
152  >> expression_g(_r1) // y0
153  >> lit(',')
154  >> expression_g(_r1) // t0 (data only)
155  >> lit(',')
156  >> expression_g(_r1) // ts (data only)
157  >> lit(',')
158  >> expression_g(_r1) // theta
159  >> lit(',')
160  >> expression_g(_r1) // x (data only)
161  >> lit(',')
162  >> expression_g(_r1) // x_int (data only)
163  >> lit(',')
164  >> expression_g(_r1) // relative tolerance (data only)
165  >> lit(',')
166  >> expression_g(_r1) // absolute tolerance (data only)
167  >> lit(',')
168  >> expression_g(_r1) // maximum number of steps (data only)
169  > lit(')')
170  [validate_integrate_ode_control_f(_val, boost::phoenix::ref(var_map_),
171  _pass,
172  boost::phoenix::ref(error_msgs_))];
173 
174  integrate_ode_r.name("expression");
176  %= ( (string("integrate_ode_rk45") >> no_skip[!char_("a-zA-Z0-9_")])
177  | (string("integrate_ode_bdf") >> no_skip[!char_("a-zA-Z0-9_")])
178  | (string("integrate_ode") >> no_skip[!char_("a-zA-Z0-9_")])
179  [deprecated_integrate_ode_f(boost::phoenix::ref(error_msgs_))] )
180  > lit('(')
181  > identifier_r // system function name (function only)
182  > lit(',')
183  > expression_g(_r1) // y0
184  > lit(',')
185  > expression_g(_r1) // t0 (data only)
186  > lit(',')
187  > expression_g(_r1) // ts (data only)
188  > lit(',')
189  > expression_g(_r1) // theta
190  > lit(',')
191  > expression_g(_r1) // x (data only)
192  > lit(',')
193  > expression_g(_r1) // x_int (data only)
194  > lit(')')
195  [validate_integrate_ode_f(_val, boost::phoenix::ref(var_map_),
196  _pass, boost::phoenix::ref(error_msgs_))];
197 
198  factor_r.name("expression");
199  factor_r =
200  integrate_ode_control_r(_r1)[assign_lhs_f(_val, _1)]
201  | integrate_ode_r(_r1)[assign_lhs_f(_val, _1)]
202  | (fun_r(_r1)[assign_lhs_f(_b, _1)]
203  > eps[set_fun_type_named_f(_val, _b, _r1, _pass,
204  boost::phoenix::ref(error_msgs_))])
205  | (variable_r[assign_lhs_f(_a, _1)]
206  > eps[set_var_type_f(_a, _val, boost::phoenix::ref(var_map_),
207  boost::phoenix::ref(error_msgs_),
208  _pass)])
209  | int_literal_r[assign_lhs_f(_val, _1)]
210  | double_literal_r[assign_lhs_f(_val, _1)]
211  | (lit('(')
212  > expression_g(_r1)[assign_lhs_f(_val, _1)]
213  > lit(')'));
214 
215  int_literal_r.name("integer literal");
217  %= int_
218  >> !(lit('.') | lit('e') | lit('E'));
219 
220  double_literal_r.name("real literal");
222  %= double_;
223 
224  fun_r.name("function and argument expressions");
225  fun_r
226  %= (hold[identifier_r[is_prob_fun_f(_1, _pass)]] > prob_args_r(_r1))
227  | (identifier_r >> args_r(_r1));
228 
229  identifier_r.name("identifier");
231  %= lexeme[char_("a-zA-Z")
232  >> *char_("a-zA-Z0-9_.")];
233 
234  prob_args_r.name("probability function arguments");
236  %= (lit('(') >> lit(')'))
237  | hold[lit('(')
238  >> expression_g(_r1)
239  >> lit(')')]
240  | (lit('(')
241  >> expression_g(_r1)
242  >> lit('|')
243  >> (expression_g(_r1) % ',')
244  >> lit(')'));
245 
246  args_r.name("function arguments");
247  args_r
248  %= (lit('(') >> lit(')'))
249  | (lit('(') >> (expression_g(_r1) % ',') >> lit(')'));
250 
251  dim_r.name("array dimension (integer expression)");
252  dim_r
253  %= expression_g(_r1)
254  >> eps[validate_int_expression_f(_val, _pass)];
255 
256  dims_r.name("array dimensions");
257  dims_r
258  %= lit('[')
259  >> (dim_r(_r1)
260  % ',' )
261  >> lit(']');
262 
263  variable_r.name("variable name");
264  variable_r
265  %= identifier_r
266  > !lit('('); // negative lookahead to prevent failure in
267  // fun to try to evaluate as variable [cleaner
268  // error msgs]
269  }
270 
271  }
272 }
273 #endif
boost::spirit::qi::rule< Iterator, boost::spirit::qi::locals< variable, fun >, expression(var_origin), whitespace_grammar< Iterator > > factor_r
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
boost::spirit::qi::rule< Iterator, int_literal(), whitespace_grammar< Iterator > > int_literal_r
Template specification of functions in std for Stan.
boost::phoenix::function< validate_int_expression > validate_int_expression_f
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::spirit::qi::rule< Iterator, std::vector< expression >var_origin), whitespace_grammar< Iterator > > dims_r
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::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.