roman.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*=============================================================================
  2. Copyright (c) 2001-2010 Joel de Guzman
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. =============================================================================*/
  6. ///////////////////////////////////////////////////////////////////////////////
  7. //
  8. // A Roman Numerals Parser (demonstrating the symbol table). This is
  9. // discussed in the "Symbols" chapter in the Spirit User's Guide.
  10. //
  11. // [ JDG August 22, 2002 ] spirit1
  12. // [ JDG March 13, 2007 ] spirit2
  13. //
  14. ///////////////////////////////////////////////////////////////////////////////
  15. #include <boost/config/warning_disable.hpp>
  16. #include <boost/spirit/include/qi.hpp>
  17. #include <boost/spirit/include/phoenix_operator.hpp>
  18. #include <iostream>
  19. #include <string>
  20. namespace client
  21. {
  22. namespace qi = boost::spirit::qi;
  23. namespace ascii = boost::spirit::ascii;
  24. ///////////////////////////////////////////////////////////////////////////////
  25. // Parse roman hundreds (100..900) numerals using the symbol table.
  26. // Notice that the data associated with each slot is the parser's attribute
  27. // (which is passed to attached semantic actions).
  28. ///////////////////////////////////////////////////////////////////////////////
  29. //[tutorial_roman_hundreds
  30. struct hundreds_ : qi::symbols<char, unsigned>
  31. {
  32. hundreds_()
  33. {
  34. add
  35. ("C" , 100)
  36. ("CC" , 200)
  37. ("CCC" , 300)
  38. ("CD" , 400)
  39. ("D" , 500)
  40. ("DC" , 600)
  41. ("DCC" , 700)
  42. ("DCCC" , 800)
  43. ("CM" , 900)
  44. ;
  45. }
  46. } hundreds;
  47. //]
  48. ///////////////////////////////////////////////////////////////////////////////
  49. // Parse roman tens (10..90) numerals using the symbol table.
  50. ///////////////////////////////////////////////////////////////////////////////
  51. //[tutorial_roman_tens
  52. struct tens_ : qi::symbols<char, unsigned>
  53. {
  54. tens_()
  55. {
  56. add
  57. ("X" , 10)
  58. ("XX" , 20)
  59. ("XXX" , 30)
  60. ("XL" , 40)
  61. ("L" , 50)
  62. ("LX" , 60)
  63. ("LXX" , 70)
  64. ("LXXX" , 80)
  65. ("XC" , 90)
  66. ;
  67. }
  68. } tens;
  69. //]
  70. ///////////////////////////////////////////////////////////////////////////////
  71. // Parse roman ones (1..9) numerals using the symbol table.
  72. ///////////////////////////////////////////////////////////////////////////////
  73. //[tutorial_roman_ones
  74. struct ones_ : qi::symbols<char, unsigned>
  75. {
  76. ones_()
  77. {
  78. add
  79. ("I" , 1)
  80. ("II" , 2)
  81. ("III" , 3)
  82. ("IV" , 4)
  83. ("V" , 5)
  84. ("VI" , 6)
  85. ("VII" , 7)
  86. ("VIII" , 8)
  87. ("IX" , 9)
  88. ;
  89. }
  90. } ones;
  91. //]
  92. ///////////////////////////////////////////////////////////////////////////////
  93. // roman (numerals) grammar
  94. //
  95. // Note the use of the || operator. The expression
  96. // a || b reads match a or b and in sequence. Try
  97. // defining the roman numerals grammar in YACC or
  98. // PCCTS. Spirit rules! :-)
  99. ///////////////////////////////////////////////////////////////////////////////
  100. //[tutorial_roman_grammar
  101. template <typename Iterator>
  102. struct roman : qi::grammar<Iterator, unsigned()>
  103. {
  104. roman() : roman::base_type(start)
  105. {
  106. using qi::eps;
  107. using qi::lit;
  108. using qi::_val;
  109. using qi::_1;
  110. using ascii::char_;
  111. start = eps [_val = 0] >>
  112. (
  113. +lit('M') [_val += 1000]
  114. || hundreds [_val += _1]
  115. || tens [_val += _1]
  116. || ones [_val += _1]
  117. )
  118. ;
  119. }
  120. qi::rule<Iterator, unsigned()> start;
  121. };
  122. //]
  123. }
  124. ///////////////////////////////////////////////////////////////////////////////
  125. // Main program
  126. ///////////////////////////////////////////////////////////////////////////////
  127. int
  128. main()
  129. {
  130. std::cout << "/////////////////////////////////////////////////////////\n\n";
  131. std::cout << "\t\tRoman Numerals Parser\n\n";
  132. std::cout << "/////////////////////////////////////////////////////////\n\n";
  133. std::cout << "Type a Roman Numeral ...or [q or Q] to quit\n\n";
  134. typedef std::string::const_iterator iterator_type;
  135. typedef client::roman<iterator_type> roman;
  136. roman roman_parser; // Our grammar
  137. std::string str;
  138. unsigned result;
  139. while (std::getline(std::cin, str))
  140. {
  141. if (str.empty() || str[0] == 'q' || str[0] == 'Q')
  142. break;
  143. std::string::const_iterator iter = str.begin();
  144. std::string::const_iterator end = str.end();
  145. //[tutorial_roman_grammar_parse
  146. bool r = parse(iter, end, roman_parser, result);
  147. if (r && iter == end)
  148. {
  149. std::cout << "-------------------------\n";
  150. std::cout << "Parsing succeeded\n";
  151. std::cout << "result = " << result << std::endl;
  152. std::cout << "-------------------------\n";
  153. }
  154. else
  155. {
  156. std::string rest(iter, end);
  157. std::cout << "-------------------------\n";
  158. std::cout << "Parsing failed\n";
  159. std::cout << "stopped at: \": " << rest << "\"\n";
  160. std::cout << "-------------------------\n";
  161. }
  162. //]
  163. }
  164. std::cout << "Bye... :-) \n\n";
  165. return 0;
  166. }