boiler_plate.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. // Boiler plate [ A template for writing your parser ]
  11. //
  12. // [ JDG 9/17/2002 ]
  13. //
  14. ///////////////////////////////////////////////////////////////////////////////
  15. #include <boost/spirit/include/classic_core.hpp>
  16. #include <iostream>
  17. #include <string>
  18. ///////////////////////////////////////////////////////////////////////////////
  19. using namespace std;
  20. using namespace BOOST_SPIRIT_CLASSIC_NS;
  21. ///////////////////////////////////////////////////////////////////////////////
  22. //
  23. // Semantic actions
  24. //
  25. ///////////////////////////////////////////////////////////////////////////////
  26. struct my_action
  27. {
  28. template <typename IteratorT>
  29. void operator()(IteratorT first, IteratorT last) const
  30. {
  31. string s(first, last);
  32. cout << "\tMy Action got: " << s << endl;
  33. }
  34. };
  35. ///////////////////////////////////////////////////////////////////////////////
  36. //
  37. // My grammar
  38. //
  39. ///////////////////////////////////////////////////////////////////////////////
  40. struct my_grammar : public grammar<my_grammar>
  41. {
  42. template <typename ScannerT>
  43. struct definition
  44. {
  45. definition(my_grammar const& self)
  46. {
  47. my_rule =
  48. *lexeme_d[(+graph_p)[my_action()]]
  49. ;
  50. }
  51. rule<ScannerT> my_rule;
  52. rule<ScannerT> const&
  53. start() const { return my_rule; }
  54. };
  55. };
  56. ///////////////////////////////////////////////////////////////////////////////
  57. //
  58. // Main program
  59. //
  60. ///////////////////////////////////////////////////////////////////////////////
  61. int
  62. main()
  63. {
  64. cout << "/////////////////////////////////////////////////////////\n\n";
  65. cout << "\t\t A boiler-plate parser...\n\n";
  66. cout << "/////////////////////////////////////////////////////////\n\n";
  67. cout << "Type anything or [q or Q] to quit\n\n";
  68. my_grammar g;
  69. string str;
  70. while (getline(cin, str))
  71. {
  72. if (str.empty() || str[0] == 'q' || str[0] == 'Q')
  73. break;
  74. if (parse(str.c_str(), g, space_p).full)
  75. {
  76. cout << "parsing succeeded\n";
  77. }
  78. else
  79. {
  80. cout << "parsing failed\n";
  81. }
  82. }
  83. cout << "Bye... :-) \n\n";
  84. return 0;
  85. }