porting_guide_qi.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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_qi_includes
  8. #include <boost/spirit/include/qi.hpp>
  9. #include <boost/spirit/include/phoenix_operator.hpp>
  10. #include <iostream>
  11. #include <string>
  12. #include <algorithm>
  13. //]
  14. //[porting_guide_qi_namespace
  15. using namespace boost::spirit;
  16. //]
  17. //[porting_guide_qi_grammar
  18. template <typename Iterator>
  19. struct roman : qi::grammar<Iterator, unsigned()>
  20. {
  21. roman() : roman::base_type(first)
  22. {
  23. hundreds.add
  24. ("C" , 100)("CC" , 200)("CCC" , 300)("CD" , 400)("D" , 500)
  25. ("DC" , 600)("DCC" , 700)("DCCC" , 800)("CM" , 900) ;
  26. tens.add
  27. ("X" , 10)("XX" , 20)("XXX" , 30)("XL" , 40)("L" , 50)
  28. ("LX" , 60)("LXX" , 70)("LXXX" , 80)("XC" , 90) ;
  29. ones.add
  30. ("I" , 1)("II" , 2)("III" , 3)("IV" , 4)("V" , 5)
  31. ("VI" , 6)("VII" , 7)("VIII" , 8)("IX" , 9) ;
  32. // qi::_val refers to the attribute of the rule on the left hand side
  33. first = eps [qi::_val = 0]
  34. >> ( +lit('M') [qi::_val += 1000]
  35. || hundreds [qi::_val += qi::_1]
  36. || tens [qi::_val += qi::_1]
  37. || ones [qi::_val += qi::_1]
  38. ) ;
  39. }
  40. qi::rule<Iterator, unsigned()> first;
  41. qi::symbols<char, unsigned> hundreds;
  42. qi::symbols<char, unsigned> tens;
  43. qi::symbols<char, unsigned> ones;
  44. };
  45. //]
  46. int main()
  47. {
  48. {
  49. //[porting_guide_qi_parse
  50. std::string input("1,1");
  51. std::string::iterator it = input.begin();
  52. bool result = qi::parse(it, input.end(), qi::int_);
  53. if (result)
  54. std::cout << "successful match!\n";
  55. if (it == input.end())
  56. std::cout << "full match!\n";
  57. else
  58. std::cout << "stopped at: " << std::string(it, input.end()) << "\n";
  59. // seldomly needed: use std::distance to calculate the length of the match
  60. std::cout << "matched length: " << std::distance(input.begin(), it) << "\n";
  61. //]
  62. }
  63. {
  64. //[porting_guide_qi_phrase_parse
  65. std::string input(" 1, 1");
  66. std::string::iterator it = input.begin();
  67. bool result = qi::phrase_parse(it, input.end(), qi::int_, ascii::space);
  68. if (result)
  69. std::cout << "successful match!\n";
  70. if (it == input.end())
  71. std::cout << "full match!\n";
  72. else
  73. std::cout << "stopped at: " << std::string(it, input.end()) << "\n";
  74. // seldomly needed: use std::distance to calculate the length of the match
  75. std::cout << "matched length: " << std::distance(input.begin(), it) << "\n";
  76. //]
  77. }
  78. {
  79. //[porting_guide_qi_use_grammar
  80. std::string input("MMIX"); // MMIX == 2009
  81. std::string::iterator it = input.begin();
  82. unsigned value = 0;
  83. roman<std::string::iterator> r;
  84. if (qi::parse(it, input.end(), r, value))
  85. std::cout << "successfully matched: " << value << "\n";
  86. //]
  87. }
  88. return 0;
  89. }