number_list.cpp |
/////////////////////////////////////////////////////////////////////////////// // // A parser for a comma separated list of numbers // // [ JDG 5/10/2002 ] // /////////////////////////////////////////////////////////////////////////////// #include <boost/spirit/core.hpp> #include <iostream> #include <vector> /////////////////////////////////////////////////////////////////////////////// using namespace std; using namespace boost::spirit; /////////////////////////////////////////////////////////////////////////////// // // Our comma separated list parser // /////////////////////////////////////////////////////////////////////////////// bool parse_numbers(char const* str, vector<double>& v) { return parse(str, // Begin grammar ( real_p[append(v)] >> *(',' >> real_p[append(v)]) ) , // End grammar space_p).full; } //////////////////////////////////////////////////////////////////////////// // // Main program // //////////////////////////////////////////////////////////////////////////// int main() { cout << "/////////////////////////////////////////////////////////\n\n"; cout << "\t\tA comma separated list parser for Spirit...\n\n"; cout << "/////////////////////////////////////////////////////////\n\n"; cout << "Give me a comma separated list of numbers.\n"; cout << "The numbers will be inserted in a vector of numbers\n"; cout << "Type [q or Q] to quit\n\n"; for (;;) { char str[256]; cin.getline(str, 256); if (str[0] == 'q' || str[0] == 'Q') break; vector<double> v; if (parse_numbers(str, v)) { cout << "-------------------------\n"; cout << "Parsing succeeded\n"; cout << str << " Parses OK: " << endl; for (int i = 0; i < v.size(); ++i) cout << i << ": " << v[i] << endl; cout << "-------------------------\n"; } else { cout << "-------------------------\n"; cout << "Parsing failed\n"; cout << "-------------------------\n"; } } cout << "Bye... :-) \n\n"; return 0; }
Copyright © 1998-2003 Joel de Guzman
Permission to copy, use, modify, sell and distribute this document
is granted provided this copyright notice appears in all copies. This document
is provided "as is" without express or implied warranty, and with
no claim as to its suitability for any purpose.