Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost::spirit::qi::parse --> No result

Tags:

c++

boost

qi

Consider the following code:

namespace qi  = boost::spirit::qi;

typedef qi::rule<
    std::string::const_iterator
> rule_type;

rule_type value_rule = +qi::char_ - ( '[' | qi::eoi );

std::string input( "Hello World" );
std::string value0, value1;

bool b0 = qi::parse( input.begin( ),
                     input.end( ),
                     value_rule,
                     value0 );

bool b1 = qi::parse( input.begin( ),
                     input.end( ),
                     +qi::char_ - ( '[' | qi::eoi ),
                     value1 );

Result:

b0 = true  
b1 = true  
value0 = ""  
value1 = "Hello World"

I am confused why is the result different. What is the correct definition of the qi::rule type to get the same result ?

like image 278
Maik Avatar asked Dec 02 '25 00:12

Maik


1 Answers

You forgot to make the rule declare its exposed attribute type:

typedef qi::rule<std::string::const_iterator, std::string()> rule_type;

Live On Coliru

#include <boost/spirit/include/qi.hpp>

namespace qi  = boost::spirit::qi;


int main() 
{
    std::string const input( "Hello World" );

    {
        typedef qi::rule<std::string::const_iterator, std::string()> rule_type;
        rule_type value_rule = +qi::char_ - ( '[' | qi::eoi );

        std::string value;
        bool ok = qi::parse( input.begin( ),
                input.end( ),
                value_rule,
                value );

        std::cout << std::boolalpha << ok << "\t" << value << "\n";
    }

    {
        std::string value;
        bool ok = qi::parse( input.begin( ),
                input.end( ),
                +qi::char_ - ( '[' | qi::eoi ),
                value );

        std::cout << std::boolalpha << ok << "\t" << value << "\n";
    }
}

Output

true    Hello World
true    Hello World
like image 88
sehe Avatar answered Dec 03 '25 15:12

sehe