> boost::spirit::hex % '*' 题: 如何解析有两颗星的-6ren">
gpt4 book ai didi

c++ - Spirit QI解析器结束EOM

转载 作者:行者123 更新时间:2023-12-02 10:32:46 28 4
gpt4 key购买 nike

我的数据定义为:

std::string data("START34*23*43**");

我的语法:
"START" >> boost::spirit::hex % '*'

题:
如何解析有两颗星的邮件结尾?

https://wandbox.org/permlink/oDYjbBDb8fy79zQV

最佳答案

目前尚不清楚您在问什么。假设您只是想“忽略”(或接受)结尾的星号,这是您的罪魁祸首:

if (first != last) // fail if we did not get a full match
return false;

只需删除这些行就可以了:

Live On Coliru (注意已大大简化):
#include <boost/spirit/include/qi.hpp>
#include <iomanip>

namespace qi = boost::spirit::qi;

template <typename Iterator>
bool parse_numbers(Iterator& first, Iterator last, std::vector<unsigned>& v) {
return qi::phrase_parse(first, last, ("START" >> qi::hex % '*'), qi::space, v);
}

int main() {
for (std::string const data : {
"START34*23*43",
"START34 * 23 * 43",
"START34 * 23 * 43 *",
"START34 * 23 * 43**",
"START34 * 23 * 43* *",
})
{
auto f = data.begin(), l = data.end();
std::vector<unsigned> v;

if (parse_numbers(f, l, v)) {
std::cout << std::quoted(data) << " Parses OK: " << std::endl;

for (auto i = 0u; i < v.size(); ++i)
std::cout << i << ": " << v[i] << std::endl;
} else {
std::cout << "Parsing failed\n";
}
if (f != l) {
std::cout << "Remaining unparsed: "
<< std::quoted(std::string(f, l)) << "\n";
}
}
}

版画
"START34*23*43" Parses OK: 
0: 52
1: 35
2: 67
"START34 * 23 * 43" Parses OK:
0: 52
1: 35
2: 67
"START34 * 23 * 43 *" Parses OK:
0: 52
1: 35
2: 67
Remaining unparsed: "*"
"START34 * 23 * 43**" Parses OK:
0: 52
1: 35
2: 67
Remaining unparsed: "**"
"START34 * 23 * 43* *" Parses OK:
0: 52
1: 35
2: 67
Remaining unparsed: "* *"

或者

如果您实际上想忽略相邻的**但仍继续解析,那么有用的更改将是说 -qi::hex % '*'而不是 qi::hex % '*',这只是使 hex成为可选的。

Live On Coliru
#include <boost/spirit/include/qi.hpp>
#include <iomanip>

namespace qi = boost::spirit::qi;

template <typename Iterator>
bool parse_numbers(Iterator& first, Iterator last, std::vector<unsigned>& v) {
return qi::phrase_parse(first, last,
("START" >> -qi::hex % '*'), qi::space, v);
}

int main() {
for (std::string const data : {
"START34**23*43",
"START34 * 23 * 43**",
"START*******",
"START*******1 BOGUS",
})
{
auto f = data.begin(), l = data.end();
std::vector<unsigned> v;

if (parse_numbers(f, l, v)) {
std::cout << std::quoted(data) << " Parses OK: " << std::endl;

for (auto i = 0u; i < v.size(); ++i)
std::cout << i << ": " << v[i] << std::endl;
} else {
std::cout << "Parsing failed\n";
}
if (f != l) {
std::cout << "Remaining unparsed: "
<< std::quoted(std::string(f, l)) << "\n";
}
}
}

版画
"START34**23*43" Parses OK: 
0: 52
1: 35
2: 67
"START34 * 23 * 43**" Parses OK:
0: 52
1: 35
2: 67
"START*******" Parses OK:
"START*******1 BOGUS" Parses OK:
0: 1
Remaining unparsed: "BOGUS"

In this case you might want to re-assert that all input is parsed with >> qi::eoi (which beats checking the iterators manually), see Live On Coliru:

"START34**23*43" OK: 
0: 52
1: 35
2: 67
"START34 * 23 * 43**" OK:
0: 52
1: 35
2: 67
"START*******" OK:
"START*******1 BOGUS" Failed


更新

对于您的 comment中更新的问题:

@sehe So someting like this: coliru.stacked-crooked.com/a/5ecc5462a8dc0081 – user3314011 19 mins ago



您需要否定的前瞻性排除 **:
"START" >> (qi::hex % (qi::lit('*') - "**")) >> "**"

实际上,让我们添加一些期望点( >而不是 >>):
    try {
return qi::parse(first, last, "START" > (qi::hex % (qi::lit('*') - "**")) > "**" > qi::eoi, v);
} catch (qi::expectation_failure<Iterator> const& ef) {
std::ostringstream msg;
msg << "Expected " << ef.what_ << " at " << std::quoted(std::string(ef.first, ef.last), '\'');
throw ParseError(msg.str());
}

现在您也可以看到一些不错的错误消息:

Live On Coliru
#include <boost/spirit/include/qi.hpp>
#include <iomanip>

namespace qi = boost::spirit::qi;

struct ParseError : std::runtime_error {
ParseError(std::string msg) : std::runtime_error(std::move(msg)) {}
};

template <typename Iterator>
bool parse_numbers(Iterator& first, Iterator last, std::vector<unsigned>& v) {
try {
return qi::parse(first, last, "START" > (qi::hex % (qi::lit('*') - "**")) > "**" > qi::eoi, v);
} catch (qi::expectation_failure<Iterator> const& ef) {
std::ostringstream msg;
msg << "Expected " << ef.what_ << " at " << std::quoted(std::string(ef.first, ef.last), '\'');
throw ParseError(msg.str());
}
}

int main() {
for (std::string const data : {
"START34*23*43", // Fail no EOM
"START34 * 23 * 43", // Fail spaces
"START34*23*43*", // Fail no EOM
"START34*23*43**", // OK
"START34*23*43**1", // Fail extra number
})
{
std::cout << std::quoted(data) << " -> ";

auto f = data.begin(), l = data.end();
std::vector<unsigned> v;

try {
if (parse_numbers(f, l, v)) {
std::cout << " OK:";

for (auto i : v)
std::cout << " " << i;
std::cout << "\n";
} else {
std::cout << "Not matched\n";
}
} catch(ParseError const& pe) {
std::cout << "Error: " << pe.what() << "\n";
}
}
}

打印:
"START34*23*43" -> Error: Expected "**" at ''
"START34 * 23 * 43" -> Error: Expected "**" at ' * 23 * 43'
"START34*23*43*" -> Error: Expected "**" at '*'
"START34*23*43**" -> OK: 52 35 67
"START34*23*43**1" -> Error: Expected <eoi> at '1'

关于c++ - Spirit QI解析器结束EOM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61661013/

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