gpt4 book ai didi

c++ - 使用 boost::spirit::qi 解析 double 列表

转载 作者:搜寻专家 更新时间:2023-10-31 02:23:09 25 4
gpt4 key购买 nike

我越来越熟悉 boost::spirit 并想像下面这样解析字符串:

double_1 | double_2 | ... | double_n | double_1% | double_2% ... | double_m%

其中 m>=0,n>=0。

例如,下面的所有行都应该解析正常:

91.3 | 44 | 5e-3 | 12% | 11%

91.3 | 44 | 5e-3

12% | 11%

我想使用boost::spirit::qi

所以,我编写了如下两个解析器:

namespace client
{
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;

template <typename Iterator>
bool parse_numbers(Iterator& first, Iterator last, std::vector<double>& v)
{
using qi::double_;
using qi::phrase_parse;
using ascii::space;

bool r = phrase_parse(first, last,

// Begin grammar
(
// double_ % '|'
double_ >> *('|' >> double_ >> '|')
)
,
// End grammar

space, v);
return r;
}

template <typename Iterator>
bool parse_numbersWithPercents(Iterator& first, Iterator last, std::vector<double>& v)
{
using qi::double_;
using qi::phrase_parse;
using ascii::space;

bool r = phrase_parse(first, last,

// Begin grammar
(
(double_ >> '%') % '|'
)
,
// End grammar

space, v);

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

然后,我像下面这样在 main 中调用它们:

int main()
{
std::cout << "Give me a list of numbers in a format double_1 | double_2 | ... | double_n | double_1% | double_2% ... | double_m%\n";
std::cout << "The numbers will be inserted in a vector of numbers\n";
std::cout << "Type [q or Q] to quit\n\n";

std::string str;
while (getline(std::cin, str))
{
if (str.empty() || str[0] == 'q' || str[0] == 'Q')
break;

std::vector<double> v;
std::string::iterator begin = str.begin(), end = str.end();
if (client::parse_numbers(begin, end, v))
{
std::cout << "-------------------------\n";
std::cout << "First Part Parsing succeeded\n";

for (std::vector<double>::size_type i = 0; i < v.size(); ++i)
std::cout << i << ": " << v[i] << std::endl;

std::cout << "\n-------------------------\n";
if(begin != end) {
if('|' == *begin) ++begin;
if(begin != end) {
std::cout << "Parsing second part: " << std::string(begin, end) << std::endl;
std::vector<double> v1;
if (client::parse_numbersWithPercents(begin, end, v1))
{
std::cout << "-------------------------\n";
std::cout << "Second Part Parsing succeeded\n";

for (std::vector<double>::size_type i = 0; i < v1.size(); ++i)
std::cout << i << ": " << v1[i] << std::endl;

std::cout << "\n-------------------------\n";
} else {
std::cout << "-------------------------\n";
std::cout << "Second Part Parsing failed\n";
std::cout << "-------------------------\n";

if(begin != end) {
std::cout << "Remaining part is: " << std::string(begin, end) << std::endl; }
}
}
}
}
else
{
std::cout << "-------------------------\n";
std::cout << "First Part Parsing failed\n";
std::cout << "-------------------------\n";

if(begin != end) {
std::cout << "Remaining part is: " << std::string(begin, end) << std::endl; }
}
}

std::cout << "Bye... :-) \n\n";
return 0;
}

如您所见,此方法不适用于极端情况,例如:

91.3 | 44 | 5e-3

12% | 11%

我很感兴趣是否有另一种方法可以使用 boost 库以更简单的方式执行相同的操作。或者以某种方式更正我的解析器以正确解析上述极端情况。最好将第一部分和第二部分放在单独的容器中。

提前致谢。

最佳答案

哈哈。我的直觉是这应该非常简单。然而,我得出的结论是,这确实有点不平凡。

问题在于将非列表重复分隔符设为可选。我苦思冥想了很久,想出了让它成为可选的最优雅的方式,并想出了这个:

Live On Coliru

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

namespace qi = boost::spirit::qi;

namespace {
using double_vec = std::vector<double>;
using It = std::string::const_iterator;

static const qi::rule<It, double_vec(bool percent), qi::blank_type> doubles_
= (qi::double_ >> (qi::eps(qi::_r1) >> '%' | !qi::lit('%'))) % '|';
}

int main() {
std::string str;
while (std::getline(std::cin, str)) {
It f = str.begin(), l = str.end();

double_vec v, w;

bool ok = qi::phrase_parse(f, l,
(doubles_(false) >> -('|' >> doubles_(true)))
| qi::attr(double_vec{}) >> doubles_(true),
qi::blank, v, w);

if (ok && f == l) {
std::cout << "Parsed " << v.size() << "/" << w.size() << " elements\n";
} else {
std::istringstream iss(str);
if (iss >> str && (str == "q" || str == "Q"))
break;
std::cout << "Invalid input. Remaining '" << std::string(f,l) << "'\n";
}
}
}

在给定测试输入的情况下会产生以下结果:

./test <<INPUT
91.3 | 44 | 5e-3 | 12% | 11%
91.3 | 44 | 5e-3
12% | 11%
q
INPUT
Parsed 3/2 elements
Parsed 3/0 elements
Parsed 0/2 elements

根据你在这里试图/实际/实现的目标,事情可能会更优雅

更新 作为对评论的回应,以下是我通过放宽语法来实际改进的方法。注意我们如何转移忽略'|'致船长:

Live On Coliru

qi::phrase_parse(
f, l, *(qi::double_>>!qi::lit('%')) >> *(qi::double_>>'%'),
qi::blank | '|', v, w);

关于c++ - 使用 boost::spirit::qi 解析 double 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29547144/

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