number_list.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*=============================================================================
  2. Copyright (c) 2002-2003 Joel de Guzman
  3. http://spirit.sourceforge.net/
  4. Use, modification and distribution is subject to the Boost Software
  5. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. http://www.boost.org/LICENSE_1_0.txt)
  7. =============================================================================*/
  8. ///////////////////////////////////////////////////////////////////////////////
  9. //
  10. // This sample demontrates a parser for a comma separated list of numbers
  11. // This is discussed in the "Quick Start" chapter in the Spirit User's Guide.
  12. //
  13. // [ JDG 5/10/2002 ]
  14. //
  15. ///////////////////////////////////////////////////////////////////////////////
  16. #include <boost/spirit/include/classic_core.hpp>
  17. #include <boost/spirit/include/classic_push_back_actor.hpp>
  18. #include <iostream>
  19. #include <vector>
  20. #include <string>
  21. ///////////////////////////////////////////////////////////////////////////////
  22. using namespace std;
  23. using namespace BOOST_SPIRIT_CLASSIC_NS;
  24. ///////////////////////////////////////////////////////////////////////////////
  25. //
  26. // Our comma separated list parser
  27. //
  28. ///////////////////////////////////////////////////////////////////////////////
  29. bool
  30. parse_numbers(char const* str, vector<double>& v)
  31. {
  32. return parse(str,
  33. // Begin grammar
  34. (
  35. real_p[push_back_a(v)] >> *(',' >> real_p[push_back_a(v)])
  36. )
  37. ,
  38. // End grammar
  39. space_p).full;
  40. }
  41. ////////////////////////////////////////////////////////////////////////////
  42. //
  43. // Main program
  44. //
  45. ////////////////////////////////////////////////////////////////////////////
  46. int
  47. main()
  48. {
  49. cout << "/////////////////////////////////////////////////////////\n\n";
  50. cout << "\t\tA comma separated list parser for Spirit...\n\n";
  51. cout << "/////////////////////////////////////////////////////////\n\n";
  52. cout << "Give me a comma separated list of numbers.\n";
  53. cout << "The numbers will be inserted in a vector of numbers\n";
  54. cout << "Type [q or Q] to quit\n\n";
  55. string str;
  56. while (getline(cin, str))
  57. {
  58. if (str.empty() || str[0] == 'q' || str[0] == 'Q')
  59. break;
  60. vector<double> v;
  61. if (parse_numbers(str.c_str(), v))
  62. {
  63. cout << "-------------------------\n";
  64. cout << "Parsing succeeded\n";
  65. cout << str << " Parses OK: " << endl;
  66. for (vector<double>::size_type i = 0; i < v.size(); ++i)
  67. cout << i << ": " << v[i] << endl;
  68. cout << "-------------------------\n";
  69. }
  70. else
  71. {
  72. cout << "-------------------------\n";
  73. cout << "Parsing failed\n";
  74. cout << "-------------------------\n";
  75. }
  76. }
  77. cout << "Bye... :-) \n\n";
  78. return 0;
  79. }