stuff_vector.cpp 3.4 KB

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