• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*=============================================================================
2     Copyright (c) 2002-2003 Joel de Guzman
3     http://spirit.sourceforge.net/
4 
5     Use, modification and distribution is subject to the Boost Software
6     License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7     http://www.boost.org/LICENSE_1_0.txt)
8 =============================================================================*/
9 ///////////////////////////////////////////////////////////////////////////////
10 //
11 //  A parser for summing a list of numbers. Demonstrating phoenix
12 //  This is discussed in the "Phoenix" chapter in the Spirit User's Guide.
13 //
14 //  [ JDG 6/28/2002 ]
15 //
16 ///////////////////////////////////////////////////////////////////////////////
17 #include <boost/spirit/include/classic_core.hpp>
18 #include <boost/spirit/include/phoenix1_primitives.hpp>
19 #include <boost/spirit/include/phoenix1_operators.hpp>
20 #include <iostream>
21 #include <string>
22 
23 ///////////////////////////////////////////////////////////////////////////////
24 using namespace std;
25 using namespace BOOST_SPIRIT_CLASSIC_NS;
26 using namespace phoenix;
27 
28 ///////////////////////////////////////////////////////////////////////////////
29 //
30 //  Our adder
31 //
32 ///////////////////////////////////////////////////////////////////////////////
33 template <typename IteratorT>
adder(IteratorT first,IteratorT last,double & n)34 bool adder(IteratorT first, IteratorT last, double& n)
35 {
36     return parse(first, last,
37 
38         //  Begin grammar
39         (
40             real_p[var(n) = arg1] >> *(',' >> real_p[var(n) += arg1])
41         )
42         ,
43         //  End grammar
44 
45         space_p).full;
46 }
47 
48 ////////////////////////////////////////////////////////////////////////////
49 //
50 //  Main program
51 //
52 ////////////////////////////////////////////////////////////////////////////
53 int
main()54 main()
55 {
56     cout << "/////////////////////////////////////////////////////////\n\n";
57     cout << "\t\tA parser for summing a list of numbers...\n\n";
58     cout << "/////////////////////////////////////////////////////////\n\n";
59 
60     cout << "Give me a comma separated list of numbers.\n";
61     cout << "The numbers are added using Phoenix.\n";
62     cout << "Type [q or Q] to quit\n\n";
63 
64     string str;
65     while (getline(cin, str))
66     {
67         if (str.empty() || str[0] == 'q' || str[0] == 'Q')
68             break;
69 
70         double n;
71         if (adder(str.begin(), str.end(), n))
72         {
73             cout << "-------------------------\n";
74             cout << "Parsing succeeded\n";
75             cout << str << " Parses OK: " << endl;
76 
77             cout << "sum = " << n;
78             cout << "\n-------------------------\n";
79         }
80         else
81         {
82             cout << "-------------------------\n";
83             cout << "Parsing failed\n";
84             cout << "-------------------------\n";
85         }
86     }
87 
88     cout << "Bye... :-) \n\n";
89     return 0;
90 }
91 
92 
93