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 // This sample demontrates a parser for a comma separated list of numbers
12 // This is the phoenix version of number_list.cpp.
13 // This is discussed in the "Phoenix" chapter in the Spirit User's Guide.
14 //
15 // [ JDG 1/12/2004 ]
16 //
17 ///////////////////////////////////////////////////////////////////////////////
18 #include <boost/spirit/include/classic_core.hpp>
19 #include <boost/spirit/include/classic_operators.hpp>
20 #include <boost/spirit/include/phoenix1_functions.hpp>
21 #include <boost/spirit/include/phoenix1_primitives.hpp>
22 #include <iostream>
23 #include <string>
24
25 ///////////////////////////////////////////////////////////////////////////////
26 using namespace std;
27 using namespace BOOST_SPIRIT_CLASSIC_NS;
28 using namespace phoenix;
29
30 ///////////////////////////////////////////////////////////////////////////////
31 //
32 // Our comma separated list parser
33 //
34 ///////////////////////////////////////////////////////////////////////////////
35 struct push_back_impl
36 {
37 template <typename Container, typename Item>
38 struct result
39 {
40 typedef void type;
41 };
42
43 template <typename Container, typename Item>
operator ()push_back_impl44 void operator()(Container& c, Item const& item) const
45 {
46 c.push_back(item);
47 }
48 };
49
50 function<push_back_impl> const push_back = push_back_impl();
51
52 bool
parse_numbers(char const * str,vector<double> & v)53 parse_numbers(char const* str, vector<double>& v)
54 {
55 return parse(str,
56
57 // Begin grammar
58 (
59 real_p[push_back(var(v), arg1)]
60 >> *(',' >> real_p[push_back(var(v), arg1)])
61 )
62 ,
63 // End grammar
64
65 space_p).full;
66 }
67
68 ////////////////////////////////////////////////////////////////////////////
69 //
70 // Main program
71 //
72 ////////////////////////////////////////////////////////////////////////////
73 int
main()74 main()
75 {
76 cout << "/////////////////////////////////////////////////////////\n\n";
77 cout << "\t\tA comma separated list parser for Spirit...\n\n";
78 cout << "/////////////////////////////////////////////////////////\n\n";
79
80 cout << "Give me a comma separated list of numbers.\n";
81 cout << "The numbers will be inserted in a vector of numbers\n";
82 cout << "Type [q or Q] to quit\n\n";
83
84 string str;
85 while (getline(cin, str))
86 {
87 if (str.empty() || str[0] == 'q' || str[0] == 'Q')
88 break;
89
90 vector<double> v;
91 if (parse_numbers(str.c_str(), v))
92 {
93 cout << "-------------------------\n";
94 cout << "Parsing succeeded\n";
95 cout << str << " Parses OK: " << endl;
96
97 for (vector<double>::size_type i = 0; i < v.size(); ++i)
98 cout << i << ": " << v[i] << endl;
99
100 cout << "-------------------------\n";
101 }
102 else
103 {
104 cout << "-------------------------\n";
105 cout << "Parsing failed\n";
106 cout << "-------------------------\n";
107 }
108 }
109
110 cout << "Bye... :-) \n\n";
111 return 0;
112 }
113
114
115