1[/============================================================================== 2 Copyright (C) 2001-2015 Joel de Guzman 3 Copyright (C) 2001-2011 Hartmut Kaiser 4 5 Distributed under the Boost Software License, Version 1.0. (See accompanying 6 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 7===============================================================================/] 8 9[section Number List - stuffing numbers into a std::vector] 10 11This sample demonstrates a parser for a comma separated list of numbers and 12using a semantic action to collect the numbers into a `std::vector`. 13 14 template <typename Iterator> 15 bool parse_numbers(Iterator first, Iterator last, std::vector<double>& v) 16 { 17 using x3::double_; 18 using x3::phrase_parse; 19 using x3::_attr; 20 using ascii::space; 21 22 auto push_back = [&](auto& ctx){ v.push_back(_attr(ctx)); }; 23 24 bool r = phrase_parse(first, last, 25 26 // Begin grammar 27 ( 28 double_[push_back] 29 >> *(',' >> double_[push_back]) 30 ) 31 , 32 // End grammar 33 34 space); 35 36 if (first != last) // fail if we did not get a full match 37 return false; 38 return r; 39 } 40 41The full cpp file for this example can be found here: 42[@../../../example/x3/num_list/num_list2.cpp num_list2.cpp] 43 44This, again, is the same parser as before. This time, instead of summing up the 45numbers, we stuff them in a `std::vector`. 46 47 [&](auto& ctx){ v.push_back(_attr(ctx)); } 48 49[endsect] 50