boost spirit qi解析器发布失败并通过调试 [英] boost spirit qi parser failed in release and pass in debug

查看:50
本文介绍了boost spirit qi解析器发布失败并通过调试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 #include <boost/spirit/include/qi.hpp>
 #include <string>
 #include <vector>
 #include <iterator>
 #include <algorithm>
 #include <iostream>
using namespace boost::spirit;
int main()
{
std::string s;
std::getline(std::cin, s);
auto specialtxt = *(qi::char_('-', '.', '_'));
auto txt = no_skip[*(qi::char_("a-zA-Z0-9_.\\:$\'-"))];
auto anytxt = *(qi::char_("a-zA-Z0-9_.\\:${}[]+/()-"));
qi::rule <std::string::iterator, void(),ascii::space_type> rule2 = txt     ('=') >> ('[') >> (']');
auto begin = s.begin();
auto end = s.end();
if (qi::phrase_parse(begin, end, rule2, ascii::space))
{
    std::cout << "MATCH" << std::endl;
}
else
{
    std::cout << "NO MATCH" << std::endl;
}

}

此代码在调试模式下可以正常工作解析器在发布模式下失败规则是只解析text = [];除此以外的任何其他操作都将失败,它在调试模式下可以正常工作,但在发布模式下则不能,它显示的结果与任何字符串都不匹配.

this code works fine in debug mode parser fails in release mode rule is to just parse text=[]; any thing else than this should fail it works fine in debug mode but not in release mode it shows result no match for any string.

如果我输入

abc=[];

这将按预期方式通过调试,但在发行版中失败

this passes in debug as expected but fails in release

推荐答案

您不能将自动与Spirit v2一起使用:

You can't use auto with Spirit v2:

您有未定义的行为

我试图使(更多)其余的代码有意义.有各种各样的实例永远都行不通:

I tried to make (more) sense of the rest of the code. There were various instances that would never work:

  1. txt('=')是无效的Qi表达式.我以为你想要 txt>>('=')代替

  1. txt('=') is an invalid Qi expression. I assumed you wanted txt >> ('=') instead

qi :: char _("a-zA-Z0-9 _.\\:$ \\-{} [] +/()")不起作用考虑一下,因为 $-{实际上是字符范围" \ x24- \ x7b ...转义-(或将其放在就像在其他char_调用中一样,集的结尾/开始.

qi::char_("a-zA-Z0-9_.\\:$\\-{}[]+/()") doesn't do what you think because $-{ is actually the character "range" \x24-\x7b... Escape the - (or put it at the very end/start of the set like in the other char_ call).

qi :: char _('-','.','_')不起作用.您是说 qi :: char _("-._")吗?

qi::char_('-','.','_') can't work. Did you mean qi::char_("-._")?

specialtxt anytxt 未使用...

更喜欢 const_iterator

上方使用名称空间优先使用名称空间别名,以防止难以检测的错误

prefer namespace aliases above using namespace to prevent hard-to-detect errors

在Coliru上直播

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

namespace qi = boost::spirit::qi;

int main() {
    std::string const s = "abc=[];";

    auto specialtxt = qi::copy(*(qi::char_("-._")));
    auto anytxt     = qi::copy(*(qi::char_("a-zA-Z0-9_.\\:$\\-{}[]+/()")));
    (void) specialtxt;
    (void) anytxt;

    auto txt        = qi::copy(qi::no_skip[*(qi::char_("a-zA-Z0-9_.\\:$\'-"))]);

    qi::rule<std::string::const_iterator, qi::space_type> rule2 = txt >> '=' >> '[' >> ']';

    auto begin = s.begin();
    auto end   = s.end();

    if (qi::phrase_parse(begin, end, rule2, qi::space)) {
        std::cout << "MATCH" << std::endl;
    } else {
        std::cout << "NO MATCH" << std::endl;
    }

    if (begin != end) {
        std::cout << "Trailing unparsed: '" << std::string(begin, end) << "'\n";
    }

}

打印

MATCH
Trailing unparsed: ';'

这篇关于boost spirit qi解析器发布失败并通过调试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆