使用子表达式解析boost :: spirit语法的结果

时间:2013-01-11 16:16:26

标签: c++ boost boost-spirit boost-spirit-qi

我正在努力进入提升精神2,但以下代码无法正常工作:

template<typename Iterator>
struct my_grammar : qi::grammar<Iterator, std::string()>
{
    my_grammar() : my_grammar::base_type(time_literal)
    {
        using ascii::char_;
        using ascii::digit;

        time_literal = digit >> -digit >> char_(':') >> digit >> digit >> -(char_(':') >> digit >> digit);
    }

    qi::rule<Iterator, std::string()> time_literal;
};

void main()
{
    my_grammar<std::string::iterator> g;
    std::string input("01:02:03");
    std::string::iterator begin = input.begin();
    std::string::iterator iter = begin;
    std::string::iterator end = input.end();
    std::string result;
    bool matched = phrase_parse(iter, end, g, ascii::space, result);
    std::cout << (matched ? "matched "+std::string(begin, iter) : "no match") << std::endl;
    if (iter != end)
        std::cout << "remaining: " << std::string(iter, end) << std::endl;
    else
        std::cout << "result: " << result << std::endl;
    std::cout << std::endl;
}

打印:

matched: 01:02:03
result: 01:02:

但我希望看到:

matched: 01:02:03
result: 01:02:03

那么最后两位数字去了哪里?我怎样才能让它们回来?

0 个答案:

没有答案