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