gpt4 book ai didi

c++ - boost::spirit::qi 和 boost::phoenix::push_back

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

基于 Using boost::spirit::qi and boost::phoenix::push_back 上的 QA ,以下代码运行良好 - 使用 C++14 编译。

#include <string>
#include <vector>
#include <iostream>

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
namespace qi = boost::spirit::qi;

typedef std::vector<unsigned int> uint_vector_t;

std::ostream& operator<<(std::ostream& out, const uint_vector_t &data)
{
for (unsigned int i(0); i < data.size(); i++)
{
out << data[i] << '\n';
}
return out;
}

struct MyStruct
{
uint_vector_t m_aList;
uint_vector_t m_bList;
};


template<typename Iterator, typename Skipper>
struct MyParser : public boost::spirit::qi::grammar<Iterator,
MyStruct(),Skipper>
{
MyParser() :
MyParser::base_type(Parser, "Parser")
{
using boost::spirit::qi::uint_;
using boost::spirit::qi::_val;
using boost::spirit::qi::_1;

using boost::phoenix::at_c;
using boost::phoenix::push_back;
using boost::phoenix::bind;

aParser = "a=" >> uint_;
bParser = "b=" >> uint_;

Parser =
*( aParser [push_back(bind(&MyStruct::m_aList, _val), _1)]
| bParser [push_back(bind(&MyStruct::m_bList, _val), _1)]
);
}
boost::spirit::qi::rule<Iterator, MyStruct(), Skipper> Parser;
boost::spirit::qi::rule<Iterator, unsigned int(), Skipper> aParser, bParser;
};

int main()
{
using boost::spirit::qi::phrase_parse;

std::string input("a=0\nb=7531\na=2\na=3\nb=246\n");
std::string::const_iterator begin = input.begin();
std::string::const_iterator end = input.end();
MyParser<std::string::const_iterator, qi::space_type> parser;

MyStruct result;
bool succes = qi::phrase_parse(begin, end, parser,qi::space,result);
assert(succes);

std::cout << "===A===\n" <<result.m_aList << "===B===\n" << result.m_bList << std::endl;
return 0;
}

结果是:

===A===
0
2
3
===B===
7531
246

在结构中添加另一个 qi::symbols 元素,新添加的元素应该被解析为 FRUIT::APPLE (0),但实际上它是 indeterminate(随机出现)。

#include <string>
#include <vector>
#include <iostream>

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
namespace qi = boost::spirit::qi;

typedef std::vector<unsigned int> uint_vector_t;

std::ostream& operator<<(std::ostream& out, const uint_vector_t &data)
{
for (unsigned int i(0); i < data.size(); i++)
{
out << data[i] << '\n';
}
return out;
}

struct MyStruct
{
enum class FRUIT
{
APPLE,
BANANA,
PEAR,
} fruit;
uint_vector_t m_aList;
uint_vector_t m_bList;
};

BOOST_FUSION_ADAPT_STRUCT
(
MyStruct,
(MyStruct::FRUIT, fruit)
(uint_vector_t, m_aList)
(uint_vector_t, m_bList)
)


template<typename Iterator, typename Skipper>
struct MyParser : public boost::spirit::qi::grammar<Iterator,
MyStruct(),Skipper>
{
MyParser() :
MyParser::base_type(Parser, "Parser")
{
using boost::spirit::qi::uint_;
using boost::spirit::qi::_val;
using boost::spirit::qi::_1;

using boost::phoenix::at_c;
using boost::phoenix::push_back;
using boost::phoenix::bind;

fruiter.add
("apple", MyStruct::FRUIT::APPLE)
("banana", MyStruct::FRUIT::BANANA)
("pear", MyStruct::FRUIT::PEAR)
;

aParser = "a=" >> uint_;
bParser = "b=" >> uint_;

Parser = fruiter >>
*( aParser [push_back(bind(&MyStruct::m_aList, _val), _1)]
| bParser [push_back(bind(&MyStruct::m_bList, _val), _1)]
);
}
boost::spirit::qi::rule<Iterator, MyStruct(), Skipper> Parser;
boost::spirit::qi::rule<Iterator, unsigned int(), Skipper> aParser, bParser;
boost::spirit::qi::symbols<char, MyStruct::FRUIT> fruiter;
};

int main()
{
using boost::spirit::qi::phrase_parse;

std::string input("apple\na=0\nb=7531\na=2\na=3\nb=246\n");
std::string::const_iterator begin = input.begin();
std::string::const_iterator end = input.end();
MyParser<std::string::const_iterator, qi::space_type> parser;

MyStruct result;
bool succes = qi::phrase_parse(begin, end, parser,qi::space,result);
assert(succes);

std::cout << "Fruit: " << int(result.fruit) << "\n===A===\n" <<result.m_aList << "===B===\n" << result.m_bList << std::endl;
return 0;
}

生成的 qi::symbols 元素是随机的而不是 0。示例输出如下所示

Fruit: 29899839
===A===
0
2
3
===B===
7531
246

然而,qi::symbols 元素本身也只能正常工作。

#include <string>
#include <vector>
#include <iostream>

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
namespace qi = boost::spirit::qi;


struct MyStruct
{
enum class FRUIT
{
APPLE,
BANANA,
PEAR,
} fruit;
};

BOOST_FUSION_ADAPT_STRUCT
(
MyStruct,
(MyStruct::FRUIT, fruit)
)


template<typename Iterator, typename Skipper>
struct MyParser : public boost::spirit::qi::grammar<Iterator,
MyStruct(),Skipper>
{
MyParser() :
MyParser::base_type(Parser, "Parser")
{
using boost::spirit::qi::uint_;
using boost::spirit::qi::_val;
using boost::spirit::qi::_1;

using boost::phoenix::at_c;
using boost::phoenix::push_back;
using boost::phoenix::bind;

fruiter.add
("apple", MyStruct::FRUIT::APPLE)
("banana", MyStruct::FRUIT::BANANA)
("pear", MyStruct::FRUIT::PEAR)
;

Parser = fruiter;
}
boost::spirit::qi::rule<Iterator, MyStruct(), Skipper> Parser;
boost::spirit::qi::symbols<char, MyStruct::FRUIT> fruiter;
};

int main()
{
using boost::spirit::qi::phrase_parse;

std::string input("apple");
std::string::const_iterator begin = input.begin();
std::string::const_iterator end = input.end();
MyParser<std::string::const_iterator, qi::space_type> parser;

MyStruct result;
bool succes = qi::phrase_parse(begin, end, parser,qi::space,result);
assert(succes);

std::cout << "Fruit: " << int(result.fruit) << "\n";
return 0;
}

结果如下:

Fruit: 0

我做错了什么?提前致谢。

最佳答案

语义 Action 禁止属性的自动传播。这显然也是程序的第一个版本不需要对 MyResult 结构进行任何修改的原因。

所以要么

  1. 坚持语义操作¹(放弃 adapt-struct)

    Live On Coliru

        Parser = fruiter[bind(&MyStruct::fruit, _val) = _1] >> 
    *( aParser [push_back(bind(&MyStruct::m_aList, _val), _1)]
    | bParser [push_back(bind(&MyStruct::m_bList, _val), _1)]
    );
  2. 或者使用operator%=重新启用自动属性传播语义。

    // NOTE c++11+ syntax:
    BOOST_FUSION_ADAPT_STRUCT(MyStruct, fruit, m_aList, m_bList)

    Parser %= fruiter >>
    *( aParser [push_back(bind(&MyStruct::m_aList, _val), _1)]
    | bParser [push_back(bind(&MyStruct::m_bList, _val), _1)]
    );

    请注意,如果 fruit 不是第一个改编的序列元素,这很容易崩溃。事实上,只调整预期的元素要干净得多:

    BOOST_FUSION_ADAPT_STRUCT(MyStruct, fruit)

    更清晰地明确哪些属性需要传播:

    Parser %= fruiter >> 
    omit [
    *( aParser [push_back(bind(&MyStruct::m_aList, _val), _1)]
    | bParser [push_back(bind(&MyStruct::m_bList, _val), _1)]
    )
    ];

    Live On Coliru

完整 list

Live On Coliru

#include <iostream>
#include <string>
#include <vector>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/spirit/include/qi.hpp>
namespace qi = boost::spirit::qi;

typedef std::vector<unsigned int> Uints;

namespace std {
std::ostream& operator<<(std::ostream& out, const Uints &data) {
for (auto i : data) out << i << " ";
return out << "\n";
}
}

struct MyStruct {
enum class FRUIT {
APPLE,
BANANA,
PEAR,
} fruit;

friend std::ostream& operator<<(std::ostream& out, FRUIT f) {
switch(f) {
case FRUIT::APPLE: return out << "APPLE";
case FRUIT::BANANA: return out << "BANANA";
case FRUIT::PEAR: return out << "PEAR";
}
return out << "FRUIT[?" << static_cast<int>(f) << "]";
}
Uints m_aList;
Uints m_bList;
};

BOOST_FUSION_ADAPT_STRUCT(MyStruct, fruit)

template <typename Iterator, typename Skipper>
struct MyParser : public qi::grammar<Iterator, MyStruct(), Skipper> {
MyParser() : MyParser::base_type(Parser, "Parser") {
using namespace qi;
using boost::phoenix::push_back;
using boost::phoenix::bind;

fruiter.add("apple", MyStruct::FRUIT::APPLE)("banana", MyStruct::FRUIT::BANANA)("pear", MyStruct::FRUIT::PEAR);

aParser = "a=" >> uint_;
bParser = "b=" >> uint_;

Parser %= fruiter >>
omit [
*( aParser [push_back(bind(&MyStruct::m_aList, _val), _1)]
| bParser [push_back(bind(&MyStruct::m_bList, _val), _1)]
)
];
}
private:
qi::rule<Iterator, MyStruct(), Skipper> Parser;
qi::rule<Iterator, unsigned int(), Skipper> aParser, bParser;
qi::symbols<char, MyStruct::FRUIT> fruiter;
};

int main() {
std::string input("banana\na=0\nb=7531\na=2\na=3\nb=246\n");
using It = std::string::const_iterator;
It begin = input.begin(), end = input.end();
MyParser<It, qi::space_type> parser;

MyStruct result;
bool succes = qi::phrase_parse(begin, end, parser, qi::space, result);

if (succes) {
std::cout
<< "Fruit: " << result.fruit
<< "\n===A===\n" <<result.m_aList << "===B===\n" << result.m_bList << std::endl;
} else {
std::cout << "Parse failed\n";
}
}

打印

Fruit: BANANA
===A===
0 2 3
===B===
7531 246

¹ 重复我的口头禅:Boost Spirit: "Semantic actions are evil"?

关于c++ - boost::spirit::qi 和 boost::phoenix::push_back,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47596403/

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