gpt4 book ai didi

c++ - 使用 boost :qi 解析两个字符串 vector

转载 作者:太空狗 更新时间:2023-10-29 21:13:41 25 4
gpt4 key购买 nike

我刚开始使用气,遇到了困难。我希望解析如下输入:

X + Y + Z , A + B

分为两个字符串 vector 。

我有代码可以做到这一点,但前提是语法解析单个字符。理想情况下,以下行应该是可读的:

习 + 夜 + 邹 , 敖 + 毕

使用简单的替换,例如 elem = +(char_ - '+') % '+' 无法解析,因为它会消耗第一个 elem 上的 ',',但我'我还没有找到解决这个问题的简单方法。

这是我的单字符代码,供引用:

#include <bits/stdc++.h>

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

namespace qi = boost::spirit::qi;
namespace phx = boost::phoenix;

typedef std::vector<std::string> element_array;

struct reaction_t
{
element_array reactants;
element_array products;
};

BOOST_FUSION_ADAPT_STRUCT(reaction_t, (element_array, reactants)(element_array, products))

template<typename Iterator>
struct reaction_parser : qi::grammar<Iterator,reaction_t(),qi::blank_type>
{
reaction_parser() : reaction_parser::base_type(reaction)
{
using namespace qi;

elem = char_ % '+';
reaction = elem >> ',' >> elem;

BOOST_SPIRIT_DEBUG_NODES((reaction)(elem));
}
qi::rule<Iterator, reaction_t(), qi::blank_type> reaction;
qi::rule<Iterator, element_array(), qi::blank_type> elem;
};
int main()
{

const std::string input = "X + Y + Z, A + B";
auto f = begin(input), l = end(input);

reaction_parser<std::string::const_iterator> p;
reaction_t data;

bool ok = qi::phrase_parse(f, l, p, qi::blank, data);

if (ok) std::cout << "success\n";
else std::cout << "failed\n";

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

最佳答案

Using a simple replacement such as elem = +(char_ - '+') % '+' fails to parse, because it will consume the ',' on the first elem, but I've not discovered a simple way around this.

好吧,完整(脑残)简单的解决方案是使用 +(char_ - '+' - ',')+~char_("+,").

不过,实际上,我会为 element 制定更具体的规则,例如:

    elem     = qi::lexeme [ +alpha ] % '+';

See Boost spirit skipper issues about lexeme and skippers

Live On Coliru

#include <boost/fusion/adapted.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>

namespace qi = boost::spirit::qi;
namespace phx = boost::phoenix;

typedef std::vector<std::string> element_array;

struct reaction_t
{
element_array reactants;
element_array products;
};

BOOST_FUSION_ADAPT_STRUCT(reaction_t, (element_array, reactants)(element_array, products))

template<typename Iterator>
struct reaction_parser : qi::grammar<Iterator,reaction_t(),qi::blank_type>
{
reaction_parser() : reaction_parser::base_type(reaction) {
using namespace qi;

elem = qi::lexeme [ +alpha ] % '+';
reaction = elem >> ',' >> elem;

BOOST_SPIRIT_DEBUG_NODES((reaction)(elem));
}
qi::rule<Iterator, reaction_t(), qi::blank_type> reaction;
qi::rule<Iterator, element_array(), qi::blank_type> elem;
};

int main()
{
reaction_parser<std::string::const_iterator> p;

for (std::string const input : {
"X + Y + Z, A + B",
"Xi + Ye + Zou , Ao + Bi",
})
{
std::cout << "----- " << input << "\n";
auto f = begin(input), l = end(input);

reaction_t data;

bool ok = qi::phrase_parse(f, l, p, qi::blank, data);

if (ok) {
std::cout << "success\n";
for (auto r : data.reactants) { std::cout << "reactant: " << r << "\n"; }
for (auto p : data.products) { std::cout << "product: " << p << "\n"; }
}
else
std::cout << "failed\n";

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

打印:

----- X + Y + Z, A + B
success
reactant: X
reactant: Y
reactant: Z
product: A
product: B
----- Xi + Ye + Zou , Ao + Bi
success
reactant: Xi
reactant: Ye
reactant: Zou
product: Ao
product: Bi

关于c++ - 使用 boost :qi 解析两个字符串 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43336716/

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