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 // Demonstrates use of boost::bind and spirit
12 // This is discussed in the "Functional" chapter in the Spirit User's Guide.
13 //
14 // [ JDG 9/29/2002 ]
15 //
16 ///////////////////////////////////////////////////////////////////////////////
17 #include <boost/spirit/include/classic_core.hpp>
18 #include <boost/bind/bind.hpp>
19 #include <iostream>
20 #include <vector>
21 #include <string>
22
23 ///////////////////////////////////////////////////////////////////////////////
24 using namespace std;
25 using namespace BOOST_SPIRIT_CLASSIC_NS;
26 using namespace boost;
27
28 ///////////////////////////////////////////////////////////////////////////////
29 //
30 // Our comma separated list parser
31 //
32 ///////////////////////////////////////////////////////////////////////////////
33 class list_parser
34 {
35 public:
36
37 typedef list_parser self_t;
38
39 bool
parse(char const * str)40 parse(char const* str)
41 {
42 using namespace boost::placeholders;
43 return BOOST_SPIRIT_CLASSIC_NS::parse(str,
44
45 // Begin grammar
46 (
47 real_p
48 [
49 bind(&self_t::add, this, _1)
50 ]
51
52 >> *( ','
53 >> real_p
54 [
55 bind(&self_t::add, this, _1)
56 ]
57 )
58 )
59 ,
60 // End grammar
61
62 space_p).full;
63 }
64
65 void
add(double n)66 add(double n)
67 {
68 v.push_back(n);
69 }
70
71 void
print() const72 print() const
73 {
74 for (vector<double>::size_type i = 0; i < v.size(); ++i)
75 cout << i << ": " << v[i] << endl;
76 }
77
78 vector<double> v;
79 };
80
81 ////////////////////////////////////////////////////////////////////////////
82 //
83 // Main program
84 //
85 ////////////////////////////////////////////////////////////////////////////
86 int
main()87 main()
88 {
89 cout << "/////////////////////////////////////////////////////////\n\n";
90 cout << "\tA comma separated list parser for Spirit...\n";
91 cout << "\tDemonstrates use of boost::bind and spirit\n";
92 cout << "/////////////////////////////////////////////////////////\n\n";
93
94 cout << "Give me a comma separated list of numbers.\n";
95 cout << "The numbers will be inserted in a vector of numbers\n";
96 cout << "Type [q or Q] to quit\n\n";
97
98 string str;
99 while (getline(cin, str))
100 {
101 if (str.empty() || str[0] == 'q' || str[0] == 'Q')
102 break;
103
104 list_parser lp;
105 if (lp.parse(str.c_str()))
106 {
107 cout << "-------------------------\n";
108 cout << "Parsing succeeded\n";
109 cout << str << " Parses OK: " << endl;
110
111 lp.print();
112
113 cout << "-------------------------\n";
114 }
115 else
116 {
117 cout << "-------------------------\n";
118 cout << "Parsing failed\n";
119 cout << "-------------------------\n";
120 }
121 }
122
123 cout << "Bye... :-) \n\n";
124 return 0;
125 }
126
127
128