gpt4 book ai didi

c++ - 在使用 boost spirit 解析时更改属性值

转载 作者:行者123 更新时间:2023-11-28 04:23:50 24 4
gpt4 key购买 nike

给定一个字符串 "1, 2, 3, 10, 15",预期的输出应该是 01、02、03、10、15——也就是说,如果它的大小不是 2,则将“0”附加到已解析的属性。我很容易用 lambda 作为语义 Action 来做到这一点,但是如何在没有使用它们?我怀疑这里应该玩一些棘手的 qi::_val 和 qi::_1 游戏。我真正想要的是

s = qi::repeat(1,2)[qi::digit] 
[(
[](auto& parsed_number)
{
return parsed_number.size()==2 ?
parsed_number : std::string("0") + parsed_number;
}
]);

但是这样不行

#include <boost/spirit/include/qi.hpp>
#include <vector>
#include <string>
#include <iostream>

namespace qi = boost::spirit::qi;

using V = std::vector<std::string>;

template<typename It, typename Skipper>
struct G: qi::grammar<It, V(), Skipper>
{
G(): G::base_type(v)
{
v = s % ',';
s = qi::repeat(1,2)[qi::digit];
}
private:
qi::rule<It, V(), Skipper> v;
qi::rule<It, std::string(), Skipper> s;
};


int main()
{
const std::string s = "1, 2, 3, 10, 15";
std::string::const_iterator beg(s.begin()), e;
G<decltype(beg), qi::space_type> g;
V R;
bool b = qi::phrase_parse(beg, e, g, qi::space, R);
if(!b){
std::cerr << "parsing failed\n";
return -1;
}

for(const auto& r: R) std::cout << r << '\n';
}

最佳答案

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/fusion/include/at.hpp>
#include <vector>
#include <string>
#include <iostream>

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

using V = std::vector<std::string>;

template<typename It, typename Skipper>
struct G: qi::grammar<It, V(), Skipper>
{
G(): G::base_type(v)
{
v = s % ',';
//s = qi::hold[qi::repeat(2)[qi::digit]]|(qi::attr('0') >> qi::digit);
//s = (&qi::repeat(2)[qi::digit] | qi::attr('0')) >> qi::repeat(1,2)[qi::digit];
//s = qi::as_string[qi::repeat(1,2)[qi::digit]] [qi::_val = phx::if_else(phx::size(qi::_1)==2,qi::_1,phx::val('0')+qi::_1)];
s = qi::as_string[qi::repeat(1,2)[qi::digit]]
[(
[](auto& parsed_number, auto& ctx)
{
fusion::at_c<0>(ctx.attributes) = parsed_number.size()==2 ?
parsed_number : '0' + parsed_number;
}
)];
}
private:
qi::rule<It, V(), Skipper> v;
qi::rule<It, std::string(), Skipper> s;
};


int main()
{
const std::string s = "1, 2, 3, 10, 15";
std::string::const_iterator beg(s.begin()), e;
G<decltype(beg), qi::space_type> g;
V R;
bool b = qi::phrase_parse(beg, e, g, qi::space, R);
if(!b){
std::cerr << "parsing failed\n";
return -1;
}

for(const auto& r: R) std::cout << r << '\n';
}

关于c++ - 在使用 boost spirit 解析时更改属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54887955/

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