混合“替代品”时的 Boost.Spirit 错误与“可选”?

发布于 2024-09-29 14:19:08 字数 1581 浏览 0 评论 0原文

我只使用 Boost.Spirit(来自 Boost 1.44)三天,尝试通过 RFC2822 中的精确语法来解析原始电子邮件。我以为我开始理解它并有所进展,但后来我遇到了一个问题:

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

namespace qi = boost::spirit::qi;
using qi::omit;
using qi::repeat;
using std::cout;
using std::endl;

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

void test(const std::string input, strrule_t rule) {
    std::string target;
    std::string::const_iterator i = input.begin(), ie = input.end();

    if (qi::parse(i, ie, rule, target)) {
        cout << "Success: '" << target << "'" << endl;
    } else {
        cout << "Failed to match." << endl;
    }
}

int main() {
    strrule_t obsolete_year = omit[-qi::char_(" \t")] >> repeat(2)[qi::digit] >>
        omit[-qi::char_(" \t")];
    strrule_t correct_year = repeat(4)[qi::digit];

    test("1776", correct_year | repeat(2)[qi::digit]); // 1: Works, reports 1776.
    test("76",   obsolete_year);                       // 2: Works, reports 76.
    test("76",   obsolete_year | correct_year);        // 3: Works, reports 76.
    test(" 76",  correct_year | obsolete_year);        // 4: Works, reports 76.
    test("76",   correct_year | obsolete_year);        // 5: Fails.
    test("76",   correct_year | repeat(2)[qi::digit]); // 6: Also fails.
}

如果测试 #3 有效,那么为什么测试 #5(两个替代方案相反的完全相同的测试)会失败?

出于同样的原因,如果您原谅这个表达式:如果测试#4有效,并且开头的空格被标记为可选,那么为什么测试#5(具有完全相同的输入的完全相同的测试,保存有输入中没有前导空格)失败?

最后,如果这是 Boost.Spirit 中的一个错误(我怀疑它一定是),我该如何解决它?

I've only been working with Boost.Spirit (from Boost 1.44) for three days, trying to parse raw e-mail messages by way of the exact grammar in RFC2822. I thought I was starting to understand it and get somewhere, but then I ran into a problem:

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

namespace qi = boost::spirit::qi;
using qi::omit;
using qi::repeat;
using std::cout;
using std::endl;

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

void test(const std::string input, strrule_t rule) {
    std::string target;
    std::string::const_iterator i = input.begin(), ie = input.end();

    if (qi::parse(i, ie, rule, target)) {
        cout << "Success: '" << target << "'" << endl;
    } else {
        cout << "Failed to match." << endl;
    }
}

int main() {
    strrule_t obsolete_year = omit[-qi::char_(" \t")] >> repeat(2)[qi::digit] >>
        omit[-qi::char_(" \t")];
    strrule_t correct_year = repeat(4)[qi::digit];

    test("1776", correct_year | repeat(2)[qi::digit]); // 1: Works, reports 1776.
    test("76",   obsolete_year);                       // 2: Works, reports 76.
    test("76",   obsolete_year | correct_year);        // 3: Works, reports 76.
    test(" 76",  correct_year | obsolete_year);        // 4: Works, reports 76.
    test("76",   correct_year | obsolete_year);        // 5: Fails.
    test("76",   correct_year | repeat(2)[qi::digit]); // 6: Also fails.
}

If test #3 works, then why does test #5 -- the exact same test with the two alternatives reversed -- fail?

By the same token, if you'll pardon the expression: if test #4 works, and the space at the beginning is marked as optional, then why does test #5 (the exact same test with the exact same input, save that there's no leading space in the input) fail?

And finally, if this is a bug in Boost.Spirit (as I suspect it must be), how can I work around it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不疑不惑不回忆 2024-10-06 14:19:08

这是因为您遇到了 Spirit 指令 repeat[] 中的错误。感谢您的报告,我在 SVN(修订版 [66167])中修复了这个问题,并将在 Boost V1.45 中提供。同时我想将您的小测试作为回归测试添加到 Spirit 的测试套件中。我希望你不介意我这样做。

That is because you hit a bug in Spirit's directive repeat[]. Thanks for the report, I fixed this problem in SVN (rev. [66167]) and it will be available in Boost V1.45. At the same time I would like to add your small test as a regression test to Spirit's test suite. I hope you don't mind me doing so.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文