roman_numerals.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. // A Roman Numerals Parser (demonstrating the symbol table). This is
  11. // discussed in the "Symbols" chapter in the Spirit User's Guide.
  12. //
  13. // [ JDG 8/22/2002 ]
  14. //
  15. ///////////////////////////////////////////////////////////////////////////////
  16. #include <boost/spirit/include/classic_core.hpp>
  17. #include <boost/spirit/include/classic_symbols.hpp>
  18. #include <iostream>
  19. #include <string>
  20. ///////////////////////////////////////////////////////////////////////////////
  21. using namespace std;
  22. using namespace BOOST_SPIRIT_CLASSIC_NS;
  23. ///////////////////////////////////////////////////////////////////////////////
  24. //
  25. // Parse roman hundreds (100..900) numerals using the symbol table.
  26. // Notice that the data associated with each slot is passed
  27. // to attached semantic actions.
  28. //
  29. ///////////////////////////////////////////////////////////////////////////////
  30. struct hundreds : symbols<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_p;
  47. ///////////////////////////////////////////////////////////////////////////////
  48. //
  49. // Parse roman tens (10..90) numerals using the symbol table.
  50. //
  51. ///////////////////////////////////////////////////////////////////////////////
  52. struct tens : symbols<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_p;
  69. ///////////////////////////////////////////////////////////////////////////////
  70. //
  71. // Parse roman ones (1..9) numerals using the symbol table.
  72. //
  73. ///////////////////////////////////////////////////////////////////////////////
  74. struct ones : symbols<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_p;
  91. ///////////////////////////////////////////////////////////////////////////////
  92. //
  93. // Semantic actions
  94. //
  95. ///////////////////////////////////////////////////////////////////////////////
  96. struct add_1000
  97. {
  98. add_1000(unsigned& r_) : r(r_) {}
  99. void operator()(char) const { r += 1000; }
  100. unsigned& r;
  101. };
  102. struct add_roman
  103. {
  104. add_roman(unsigned& r_) : r(r_) {}
  105. void operator()(unsigned n) const { r += n; }
  106. unsigned& r;
  107. };
  108. ///////////////////////////////////////////////////////////////////////////////
  109. //
  110. // roman (numerals) grammar
  111. //
  112. ///////////////////////////////////////////////////////////////////////////////
  113. struct roman : public grammar<roman>
  114. {
  115. template <typename ScannerT>
  116. struct definition
  117. {
  118. definition(roman const& self)
  119. {
  120. first
  121. = +ch_p('M') [add_1000(self.r)]
  122. || hundreds_p [add_roman(self.r)]
  123. || tens_p [add_roman(self.r)]
  124. || ones_p [add_roman(self.r)];
  125. // Note the use of the || operator. The expression
  126. // a || b reads match a or b and in sequence. Try
  127. // defining the roman numerals grammar in YACC or
  128. // PCCTS. Spirit rules! :-)
  129. }
  130. rule<ScannerT> first;
  131. rule<ScannerT> const&
  132. start() const { return first; }
  133. };
  134. roman(unsigned& r_) : r(r_) {}
  135. unsigned& r;
  136. };
  137. ///////////////////////////////////////////////////////////////////////////////
  138. //
  139. // Main driver code
  140. //
  141. ///////////////////////////////////////////////////////////////////////////////
  142. int
  143. main()
  144. {
  145. cout << "/////////////////////////////////////////////////////////\n\n";
  146. cout << "\t\tRoman Numerals Parser\n\n";
  147. cout << "/////////////////////////////////////////////////////////\n\n";
  148. cout << "Type a Roman Numeral ...or [q or Q] to quit\n\n";
  149. // Start grammar definition
  150. string str;
  151. while (getline(cin, str))
  152. {
  153. if (str.empty() || str[0] == 'q' || str[0] == 'Q')
  154. break;
  155. unsigned n = 0;
  156. roman roman_p(n);
  157. if (parse(str.c_str(), roman_p).full)
  158. {
  159. cout << "parsing succeeded\n";
  160. cout << "result = " << n << "\n\n";
  161. }
  162. else
  163. {
  164. cout << "parsing failed\n\n";
  165. }
  166. }
  167. cout << "Bye... :-) \n\n";
  168. return 0;
  169. }