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