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

Standard output is empty