gpt4 book ai didi

c++ - spirit qi no_case 指令应用于文法

转载 作者:行者123 更新时间:2023-11-30 05:40:57 24 4
gpt4 key购买 nike

我有一个可以正常工作的语法,现在我想用它来解析一个不区分大小写的字符串。

我在 Ubuntu 12.04 Linux 上运行 Boost 1.46。

我尝试了以下代码:

bool parseSuccess = qi::phrase_parse(begin, end,
qi::no_case[grammar], ascii::space, result);

但是 no_case 指令无效。

我是不是做错了什么,或者该指令不能用于非终端?

最佳答案

两件事:

  • no_case[]要求包含以小写形式指定的解析器表达式
  • 它似乎没有被记录在任何地方,但是no_case指令不会Non-Terminal parsers 中传播.

In that sense no_case is similar to e.g. locals<>, or the Skipper in that they are part of the parser context; Only in this case, case-sensitive happens to not be controlled from the rule's template arguments.

我似乎记得 Spirit X3 将在这里获得更通用的设施,可能会消除这些限制。

显示操作限制的示例程序:

靠 Coliru 生存

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

namespace qi = boost::spirit::qi;

template <typename It, typename Skipper>
struct Parser : qi::grammar<It, std::string(), Skipper> {
Parser() : Parser::base_type(start) {
using namespace qi;

a_symbol += "aap", "noot", "mies";
start = raw [ no_case [ a_symbol >> "literal" >> char_("qwerty") >> -subrule_lexeme ] ];
subrule_lexeme = "also works";

BOOST_SPIRIT_DEBUG_NODES((start)(subrule_lexeme))
}
private:
qi::symbols<char, qi::unused_type> a_symbol;
qi::rule<It, std::string(), Skipper> start;
qi::rule<It, std::string()> subrule_lexeme;
};

// test
using It = std::string::const_iterator;

template <typename S = qi::space_type> void test(S const& s = S()) {
Parser<It, S> g;

for (std::string const& input : {
"aap\t literal r",
"aAp\t liTeral R",
// hitting subrule_lexeme:
"aap\t literal r\talso works",
"aAp\t liTeral R\tALSO WoRkS",
})
{
It f = input.begin(), l = input.end();

std::string parsed;
bool ok = qi::phrase_parse(f, l, g, s, parsed);

if (ok)
std::cout << "Parsed successfully: '" << parsed << "'\n";
else
std::cout << "Not matched ('" << input << "')\n";

if (f!=l)
std::cout << " -- remaining unparsed input: '" << std::string(f,l) << "'\n";
}
}

int main()
{
test(qi::space);
}

打印:

Parsed successfully: 'aap    literal r'
Parsed successfully: 'aAp liTeral R'
Parsed successfully: 'aap literal r also works'
Parsed successfully: 'aAp liTeral R '
-- remaining unparsed input: 'ALSO WoRkS'

并带有完整的调试跟踪:

<start>
<try>aap\t literal r</try>
<subrule_lexeme>
<try></try>
<fail/>
</subrule_lexeme>
<success></success>
<attributes>[[a, a, p, , , l, i, t, e, r, a, l, , r]]</attributes>
</start>
Parsed successfully: 'aap literal r'
<start>
<try>aAp\t liTeral R</try>
<subrule_lexeme>
<try></try>
<fail/>
</subrule_lexeme>
<success></success>
<attributes>[[a, A, p, , , l, i, T, e, r, a, l, , R]]</attributes>
</start>
Parsed successfully: 'aAp liTeral R'
<start>
<try>aap\t literal r\talso </try>
<subrule_lexeme>
<try>also works</try>
<success></success>
<attributes>[[a, l, s, o, , w, o, r, k, s]]</attributes>
</subrule_lexeme>
<success></success>
<attributes>[[a, a, p, , , l, i, t, e, r, a, l, , r, , a, l, s, o, , w, o, r, k, s]]</attributes>
</start>
Parsed successfully: 'aap literal r also works'
<start>
<try>aAp\t liTeral R\tALSO </try>
<subrule_lexeme>
<try>ALSO WoRkS</try>
<fail/>
</subrule_lexeme>
<success>ALSO WoRkS</success>
<attributes>[[a, A, p, , , l, i, T, e, r, a, l, , R, ]]</attributes>
</start>
Parsed successfully: 'aAp liTeral R '
-- remaining unparsed input: 'ALSO WoRkS'

关于c++ - spirit qi no_case 指令应用于文法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31312527/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com