1 /*============================================================================= 2 Copyright (c) 2001-2015 Joel de Guzman 3 4 Distributed under the Boost Software License, Version 1.0. (See accompanying 5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 =============================================================================*/ 7 #include <boost/config/warning_disable.hpp> 8 #include <boost/spirit/home/x3.hpp> 9 10 #include <iostream> 11 12 // Presented are various ways to attach semantic actions 13 // * Using plain function pointer 14 // * Using simple function object 15 16 namespace client 17 { 18 namespace x3 = boost::spirit::x3; 19 using x3::_attr; 20 21 struct print_action 22 { 23 template <typename Context> operator ()client::print_action24 void operator()(Context const& ctx) const 25 { 26 std::cout << _attr(ctx) << std::endl; 27 } 28 }; 29 } 30 main()31int main() 32 { 33 using boost::spirit::x3::int_; 34 using boost::spirit::x3::parse; 35 using client::print_action; 36 37 { // example using function object 38 39 char const *first = "{43}", *last = first + std::strlen(first); 40 parse(first, last, '{' >> int_[print_action()] >> '}'); 41 } 42 43 { // example using C++14 lambda 44 45 using boost::spirit::x3::_attr; 46 char const *first = "{44}", *last = first + std::strlen(first); 47 auto f = [](auto& ctx){ std::cout << _attr(ctx) << std::endl; }; 48 parse(first, last, '{' >> int_[f] >> '}'); 49 } 50 51 return 0; 52 } 53