language: C++11 (gcc-4.7.2)
date: 204 days 13 hours ago
link:
visibility: public
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
 
#include <vector>
#include <string>
#include <iomanip>
#include <algorithm>
 
namespace qi    = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
namespace phx   = boost::phoenix;
 
struct my_record {
    std::vector<int>          m_ints;
    std::vector<std::string>  m_strs;
};
 
std::ostream& operator<<( std::ostream& os, const my_record& rec ) {
    for( const auto& x : rec.m_ints )
        os << x << ' ';
    os << '\n';
 
    for( const auto& x : rec.m_strs )
        os << x << ' ';
    return os;
}
 
BOOST_FUSION_ADAPT_STRUCT(
    ::my_record,
    (std::vector<int>,          m_ints)
    (std::vector<std::string>,  m_strs)
)
 
template<typename Iterator>
struct my_grammar
: qi::grammar<Iterator, my_record(), ascii::space_type> {
 
    my_grammar()
    : my_grammar::base_type(start) {
 
        quoted_string %= qi::lexeme['"' >> +(qi::char_ - '"') >> '"'];
 
        start = qi::lit("{")
                >>
                *( "INT:" >> qi::int_
                    [
                        phx::push_back(
                            phx::at_c<0>(
                                qi::_val
                            ),
                            qi::_1
                        )
                    ] % ","
                 | "STR:" >> quoted_string
                    [
                        phx::push_back(
                            phx::bind(
                                &my_record::m_strs,
                                qi::_val
                            ),
                            qi::_1
                        )
                    ] % ","
                 )
                >>
                "}"
                ;
    }
    qi::rule<Iterator, std::string(), ascii::space_type> quoted_string;
    qi::rule<Iterator, my_record(),   ascii::space_type> start;
};
 
int main () {
    std::string        input1 = "{ STR: \"Joe\" INT: 42, 24 STR: \"Smith\" ,\"John\" }";
    auto first=input1.begin(), last=input1.end();
    my_record mr;
    my_grammar<decltype(first)> g;
    bool r=qi::phrase_parse(
        first,
        last,
        g,
        ascii::space,
        mr
    );
    std::cout << std::boolalpha << "Parsed: " << r << '\n' << mr << '\n';
    return r;
}
prog.cpp:1:49: fatal error: boost/fusion/include/adapt_struct.hpp: No such file or directory
compilation terminated.