bind.cpp 3.3 KB

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