boost :: spirit :: qi :: parse - >没有结果

时间:2015-07-26 10:08:23

标签: c++ boost qi

请考虑以下代码:

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 );

结果:

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

我很困惑为什么结果不同。为获得相同的结果,qi :: rule类型的正确定义是什么?

1 个答案:

答案 0 :(得分:5)

您忘记让规则声明其公开的属性类型:

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";
    }
}

输出

true    Hello World
true    Hello World