stuff_vector2.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 identifiers
  11. // This is a variation of stuff_vector.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 <boost/spirit/include/phoenix1_casts.hpp>
  22. #include <iostream>
  23. #include <string>
  24. ///////////////////////////////////////////////////////////////////////////////
  25. using namespace std;
  26. using namespace BOOST_SPIRIT_CLASSIC_NS;
  27. using namespace phoenix;
  28. ///////////////////////////////////////////////////////////////////////////////
  29. //
  30. // Our comma separated list parser
  31. //
  32. ///////////////////////////////////////////////////////////////////////////////
  33. struct push_back_impl
  34. {
  35. template <typename Container, typename Item>
  36. struct result
  37. {
  38. typedef void type;
  39. };
  40. template <typename Container, typename Item>
  41. void operator()(Container& c, Item const& item) const
  42. {
  43. c.push_back(item);
  44. }
  45. };
  46. function<push_back_impl> const push_back = push_back_impl();
  47. bool
  48. parse_identifiers(char const* str, vector<std::string>& v)
  49. {
  50. return parse(str,
  51. // Begin grammar
  52. (
  53. (+alpha_p)
  54. [
  55. push_back(var(v), construct_<std::string>(arg1, arg2))
  56. ]
  57. >>
  58. *(',' >>
  59. (+alpha_p)
  60. [
  61. push_back(var(v), construct_<std::string>(arg1, arg2))
  62. ]
  63. )
  64. )
  65. ,
  66. // End grammar
  67. space_p).full;
  68. }
  69. ////////////////////////////////////////////////////////////////////////////
  70. //
  71. // Main program
  72. //
  73. ////////////////////////////////////////////////////////////////////////////
  74. int
  75. main()
  76. {
  77. cout << "/////////////////////////////////////////////////////////\n\n";
  78. cout << "\t\tA comma separated list parser for Spirit...\n\n";
  79. cout << "/////////////////////////////////////////////////////////\n\n";
  80. cout << "Give me a comma separated list of identifiers.\n";
  81. cout << "An identifier is comprised of one or more alphabetic characters.\n";
  82. cout << "The identifiers will be inserted in a vector of numbers\n";
  83. cout << "Type [q or Q] to quit\n\n";
  84. string str;
  85. while (getline(cin, str))
  86. {
  87. if (str.empty() || str[0] == 'q' || str[0] == 'Q')
  88. break;
  89. vector<std::string> v;
  90. if (parse_identifiers(str.c_str(), v))
  91. {
  92. cout << "-------------------------\n";
  93. cout << "Parsing succeeded\n";
  94. cout << str << " Parses OK: " << endl;
  95. for (vector<std::string>::size_type i = 0; i < v.size(); ++i)
  96. cout << i << ": " << v[i] << endl;
  97. cout << "-------------------------\n";
  98. }
  99. else
  100. {
  101. cout << "-------------------------\n";
  102. cout << "Parsing failed\n";
  103. cout << "-------------------------\n";
  104. }
  105. }
  106. cout << "Bye... :-) \n\n";
  107. return 0;
  108. }