| 1 | /*============================================================================= |
| 2 | Copyright (c) 2002-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 | /////////////////////////////////////////////////////////////////////////////// |
| 8 | // |
| 9 | // A parser for summing a comma-separated list of numbers using phoenix. |
| 10 | // |
| 11 | // [ JDG June 28, 2002 ] spirit1 |
| 12 | // [ JDG March 24, 2007 ] spirit2 |
| 13 | // [ JDG May 12, 2015 ] spirit X3 |
| 14 | // |
| 15 | /////////////////////////////////////////////////////////////////////////////// |
| 16 | |
| 17 | #include <boost/spirit/home/x3.hpp> |
| 18 | #include <iostream> |
| 19 | #include <string> |
| 20 | |
| 21 | namespace client |
| 22 | { |
| 23 | namespace x3 = boost::spirit::x3; |
| 24 | namespace ascii = boost::spirit::x3::ascii; |
| 25 | |
| 26 | using x3::double_; |
| 27 | using ascii::space; |
| 28 | using x3::_attr; |
| 29 | |
| 30 | /////////////////////////////////////////////////////////////////////////// |
| 31 | // Our adder |
| 32 | /////////////////////////////////////////////////////////////////////////// |
| 33 | |
| 34 | template <typename Iterator> |
| 35 | bool adder(Iterator first, Iterator last, double& n) |
| 36 | { |
| 37 | auto assign = [&](auto& ctx){ n = _attr(ctx); }; |
| 38 | auto add = [&](auto& ctx){ n += _attr(ctx); }; |
| 39 | |
| 40 | bool r = x3::phrase_parse(first, last, |
| 41 | |
| 42 | // Begin grammar |
| 43 | ( |
| 44 | double_[assign] >> *(',' >> double_[add]) |
| 45 | ) |
| 46 | , |
| 47 | // End grammar |
| 48 | |
| 49 | space); |
| 50 | |
| 51 | if (first != last) // fail if we did not get a full match |
| 52 | return false; |
| 53 | return r; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | //////////////////////////////////////////////////////////////////////////// |
| 58 | // Main program |
| 59 | //////////////////////////////////////////////////////////////////////////// |
| 60 | int |
| 61 | main() |
| 62 | { |
| 63 | std::cout << "/////////////////////////////////////////////////////////\n\n" ; |
| 64 | std::cout << "\t\tA parser for summing a list of numbers...\n\n" ; |
| 65 | std::cout << "/////////////////////////////////////////////////////////\n\n" ; |
| 66 | |
| 67 | std::cout << "Give me a comma separated list of numbers.\n" ; |
| 68 | std::cout << "The numbers are added using Phoenix.\n" ; |
| 69 | std::cout << "Type [q or Q] to quit\n\n" ; |
| 70 | |
| 71 | std::string str; |
| 72 | while (getline(is&: std::cin, str&: str)) |
| 73 | { |
| 74 | if (str.empty() || str[0] == 'q' || str[0] == 'Q') |
| 75 | break; |
| 76 | |
| 77 | double n; |
| 78 | if (client::adder(first: str.begin(), last: str.end(), n)) |
| 79 | { |
| 80 | std::cout << "-------------------------\n" ; |
| 81 | std::cout << "Parsing succeeded\n" ; |
| 82 | std::cout << str << " Parses OK: " << std::endl; |
| 83 | |
| 84 | std::cout << "sum = " << n; |
| 85 | std::cout << "\n-------------------------\n" ; |
| 86 | } |
| 87 | else |
| 88 | { |
| 89 | std::cout << "-------------------------\n" ; |
| 90 | std::cout << "Parsing failed\n" ; |
| 91 | std::cout << "-------------------------\n" ; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | std::cout << "Bye... :-) \n\n" ; |
| 96 | return 0; |
| 97 | } |
| 98 | |