boost spirit to parse a structure

I would like to use boost::spirit to parse a structure from a std::string. In my example the string looks like what will be read from a file

Do you know why it is not parsed at all? I copied the classic employee example but the string is not parsed.


std::string temp_1 = "MyStruct { \n" " a ""A"", \n" " b ""B"", \n" " c ""C"", \n" " d ""D"" \n" " } \n" ; namespace client{ namespace qi = boost::spirit::qi; namespace ascii = boost::spirit::ascii; struct MyStruct{ std::string a; std::string b; std::string c; std::string d; }; }; BOOST_FUSION_ADAPT_STRUCT( client::MyStruct, (std::string, a) (std::string, b) (std::string, c) (std::string, d) ) namespace client { template <typename Iterator> struct MyStruct_parser : qi::grammar<Iterator, MyStruct(), ascii::space_type> { MyStruct_parser() : MyStruct_parser::base_type(start) { using qi::lexeme; using ascii::char_; using qi::lit; quoted_string %= lexeme['"' >> +(char_ - '"') >> '"']; start %= lit("MyStruct") >> '{' >> "a" >> quoted_string >> ',' >> "b" >> quoted_string >> ',' >> "c" >> quoted_string >> ',' >> "d" >> quoted_string >> '}' ; } qi::rule<Iterator, std::string(), ascii::space_type> quoted_string; qi::rule<Iterator, MyStruct(), ascii::space_type> start; }; } int main( int argc, char** argv ){ using boost::spirit::ascii::space; typedef std::string::const_iterator iterator_type; typedef client::MyStruct_parser<iterator_type> MyStruct_parser; std::stringstream inp(temp_1); std::string line; while( std::getline( line, inp ) ){ std::string::const_iterator a = line.begin(); std::string::const_iterator b = line.end(); MyStruct_parser g; // Our grammar client::MyStruct obj; bool r = phrase_parse( a, b, g, space, obj); if( r ){ std::cout << "parsed" << std::endl; }else{ std::cout << "not parsed" << std::endl; } } return 1; }

-------------Problems Reply------------

It seems your strings in temp_1 arn't quoted, but your grammar expects quoted strings. To add a quote to string literal, use escape \"

If you write your input data as:

std::string temp_1 =
"MyStruct {\n"
" a \"A\",\n"
" b \"B\",\n"
" c \"C\",\n"
" d \"D\"\n"
" }\n"

(i.e. add proper quoting) it will work.

Category:c# Views:0 Time:2011-07-06

Related post

  • How do you use a variable stored in a boost spirit closure as input to a boost spirit loop parser? 2009-09-25

    I would like to use a parsed value as the input to a loop parser. The grammar defines a header that specifies the (variable) size of the following string. For example, say the following string is the input to some parser. 12\r\nTest Payload The parse

  • Boost Spirit Auto Parser fails for a tuple of doubles 2012-02-25

    At the following code I am trying to use Boost Spirit Auto Parser for a sequence or two doubles, but it doesn't compile. What am I doing wrong here? // file main.cpp #include <boost/tuple/tuple.hpp> #include <boost/spirit/include/qi.hpp>

  • What is the problem with this simple boost::spirit::qi parser? 2010-06-30

    I've got this simple parser intended to parse VB style double quoted strings. Thus, the parser should turn something like "This is a quoted string containing quotes ("" "")" into an output of This is a quoted string containing quotes (" ") Here is th

  • Why does boost::spirit::qi::parse() not set this boost::variant's value? 2011-07-04

    When trying to parse text into a boost::variant, the variant's value does not get changed. The parsers by themselves appear to work fine, so my assumption is that I'm doing something wrong with the variant code. I'm using boost 1.46.1 and the followi

  • Boost::Spirit Expression Parser 2011-12-11

    I have another problem with my boost::spirit parser. template<typename Iterator> struct expression: qi::grammar<Iterator, ast::expression(), ascii::space_type> { expression() : expression::base_type(expr) { number %= lexeme[double_]; varn

  • How to use Boost Spirit to parse Chinese(unicode utf-16)? 2012-03-24

    My program does not recognize Chinese. How to use spirit to recognize Chinese? I use wstring and has convert it to utf-16. Here is my header file: #pragma once #define BOOST_SPIRIT_UNICODE #include <boost/spirit/include/qi.hpp> #include <str

  • boost:spirit::qi parser using multiple grammars and phoenix::construct 2012-04-08

    I'm having trouble writing a Qi grammar which utilizes another Qi grammar. A similar question was asked here, but I'm also trying to use phoenix::construct and having compilation difficulties. Here's a simplified version of what I'm trying to do. I r

  • How to use boost::spirit to parse a sequence of words into a vector? 2012-05-02

    I'm trying to learn boost::spirit. As an example, I'm trying to parse a sequence of words into a vector<string>. I tried this: #include <boost/spirit/include/qi.hpp> #include <boost/foreach.hpp> namespace qi = boost::spirit::qi; int

  • Boost Spirit char parser 2012-02-25

    Here is a code sample: // file main.cpp #include <iostream> #include <boost/tuple/tuple.hpp> #include <boost/tuple/tuple_io.hpp> #include <boost/spirit/include/qi.hpp> int main() { std::string s( "1 A" ); boost::tuple<doubl

  • boost spirit qi numeric parsing of integer and floating points 2010-06-26

    i am trying to make sense of the following result. The test case code is #include <boost/config/warning_disable.hpp> #include <boost/spirit/include/qi.hpp> #include <boost/spirit/include/phoenix_core.hpp> #include <boost/spirit/i

  • How to parse escaped string using `c_escape_ch_p` from boost::spirit? 2011-12-08

    I'm trying to use c_escape_ch_p (see here) from boost::spirit to parse an escaped C++ string. But I'm getting a compiler error. Here is my code: #include <boost/config/warning_disable.hpp> #include <boost/spirit/include/qi.hpp> #include

  • Template compilation errors with Boost::Spirit::Qi for simple grammar 2015-03-24

    I am trying to parse the grammar: @ ( ) using Boost::Spirit::Qi. I got the parsing working with phrase_parse and now I'm creating a grammar class (one step at a time). Here are my failure messages from Visual Studio 2010 (on Windows 7): 1>------ B

  • Boost Binary Endian parser not working? 2010-05-19

    I am studying how to use boost spirit Qi binary endian parser. I write a small test parser program according to here and basics examples, but it doesn't work proper. It gave me the msg:"Error:no match". Here is my code. #include "boost/spirit/include

  • Is it possible to create custom Parsers in Boost.Spirit? 2010-08-17

    I was trying to create a custom Parser class in Boost.Spirit (2.3), but it didn't work out. The code is: template <class Iter> class crule : public boost::spirit::qi::parser<crule<Iter> > { rule<Iter> r_; public: crule(const r

  • String to int using boost spirit 2010-10-15

    I hear that spirit is really fast at converting string to int. I am however unable to create a simple function that can do so. Something like int string_to_int(string& s) { /*?????*/ } Can anybody use boost spirit to fill in this function. By the

  • Is there any way to compose separately compiled boost::spirit::qi grammars? 2011-06-26

    Boost Spirit Qi parser grammars are wonderful, and I use them for small things all the time. However, there are times when I would like to be able to compose separate grammars. This is easy to do all-in-one in a single compile by #including the appro

  • boost::spirit char/string mixing 2011-08-16

    I'm trying to get a handle on using boost::spirit to parse character tokens, and am having great difficulty. Here is the sample code I'm working on: #include <boost/spirit/include/qi.hpp> #include <iostream> #include <string> using

  • Compiling simple boost spirit grammar 2012-02-23

    I'm having trouble getting a small boost::spirit grammar to compile. It's a small part of a larger grammar that I'm having trouble with and I'm trying to test smaller parts to find my problem. Basicly what this grammar should do is parse a double val

  • Why doesn't boost::spirit::qi semantic action work with two arguments when I use boost::bind? 2012-03-18

    I've tried to use boost semantic actions. In my case boost::bind was the easiest solution. The first example is working well; here I'm using only one arguments in semantic action. #include <boost/config/warning_disable.hpp> #include <boost/s

Copyright (C), All Rights Reserved.

processed in 0.083 (s). 11 q(s)