porting_guide_classic.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Hartmut Kaiser
  3. http://spirit.sourceforge.net/
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. =============================================================================*/
  7. //[porting_guide_classic_includes
  8. #include <boost/spirit/include/classic.hpp>
  9. #include <boost/spirit/include/phoenix1.hpp>
  10. #include <iostream>
  11. #include <string>
  12. //]
  13. //[porting_guide_classic_namespace
  14. using namespace boost::spirit::classic;
  15. //]
  16. //[porting_guide_classic_grammar
  17. struct roman : public grammar<roman>
  18. {
  19. template <typename ScannerT>
  20. struct definition
  21. {
  22. definition(roman const& self)
  23. {
  24. hundreds.add
  25. ("C" , 100)("CC" , 200)("CCC" , 300)("CD" , 400)("D" , 500)
  26. ("DC" , 600)("DCC" , 700)("DCCC" , 800)("CM" , 900) ;
  27. tens.add
  28. ("X" , 10)("XX" , 20)("XXX" , 30)("XL" , 40)("L" , 50)
  29. ("LX" , 60)("LXX" , 70)("LXXX" , 80)("XC" , 90) ;
  30. ones.add
  31. ("I" , 1)("II" , 2)("III" , 3)("IV" , 4)("V" , 5)
  32. ("VI" , 6)("VII" , 7)("VIII" , 8)("IX" , 9) ;
  33. first = eps_p [phoenix::var(self.r) = phoenix::val(0)]
  34. >> ( +ch_p('M') [phoenix::var(self.r) += phoenix::val(1000)]
  35. || hundreds [phoenix::var(self.r) += phoenix::_1]
  36. || tens [phoenix::var(self.r) += phoenix::_1]
  37. || ones [phoenix::var(self.r) += phoenix::_1]
  38. ) ;
  39. }
  40. rule<ScannerT> first;
  41. symbols<unsigned> hundreds;
  42. symbols<unsigned> tens;
  43. symbols<unsigned> ones;
  44. rule<ScannerT> const& start() const { return first; }
  45. };
  46. roman(unsigned& r_) : r(r_) {}
  47. unsigned& r;
  48. };
  49. //]
  50. int main()
  51. {
  52. {
  53. //[porting_guide_classic_parse
  54. std::string input("1,1");
  55. parse_info<std::string::iterator> pi = parse(input.begin(), input.end(), int_p);
  56. if (pi.hit)
  57. std::cout << "successful match!\n";
  58. if (pi.full)
  59. std::cout << "full match!\n";
  60. else
  61. std::cout << "stopped at: " << std::string(pi.stop, input.end()) << "\n";
  62. std::cout << "matched length: " << pi.length << "\n";
  63. //]
  64. }
  65. {
  66. //[porting_guide_classic_phrase_parse
  67. std::string input(" 1, 1");
  68. parse_info<std::string::iterator> pi = parse(input.begin(), input.end(), int_p, space_p);
  69. if (pi.hit)
  70. std::cout << "successful match!\n";
  71. if (pi.full)
  72. std::cout << "full match!\n";
  73. else
  74. std::cout << "stopped at: " << std::string(pi.stop, input.end()) << "\n";
  75. std::cout << "matched length: " << pi.length << "\n";
  76. //]
  77. }
  78. {
  79. //[porting_guide_classic_use_grammar
  80. std::string input("MMIX"); // MMIX == 2009
  81. unsigned value = 0;
  82. roman r(value);
  83. parse_info<std::string::iterator> pi = parse(input.begin(), input.end(), r);
  84. if (pi.hit)
  85. std::cout << "successfully matched: " << value << "\n";
  86. //]
  87. }
  88. return 0;
  89. }