我正在尝试解析字符流并查找解析的第二个字符以获得第三个字符所需的重复次数。然而,变量 objSize 在重复中没有被正确引用,并且完整解析失败。如果我分配 objSize = 3 我得到完整的解析通行证。这是我正在使用的代码。非常感谢任何帮助。
#define BOOST_SPIRIT_USE_PHOENIX_V3
#include <iostream>
#include <iomanip>
#include <boost/spirit/include/qi.hpp>
#include <boost/phoenix/phoenix.hpp>
#include <boost/spirit/include/qi_char.hpp>
#include <boost/spirit/include/qi_repeat.hpp>
#include <string>
#include <vector>
namespace qi = boost::spirit::qi;
namespace phx = boost::phoenix;
void write_to_con (const std::vector<unsigned char> &v){
for (int i = 0; i <v.size(); ++i)
std::cout << std::setw(2) << std::setfill('0') << std::hex <<int(v[i]) <<" ";
std::cout << std::endl;
}
int getObjSize(unsigned char t)
{
if(t == '\x02')
return 3;
else return 0;
}
int main()
{
std::vector<unsigned char> v = {'\x01','\x02','\x03','\x03','\x03'};
int objSize = 0;
auto it = v.begin();
bool match = boost::spirit::qi::parse(it, v.end(),
//Begin Grammar
(
(qi::char_('\x01'))>>(qi::char_[([&](unsigned char c){phx::ref(objSize) = getObjSize(c);})])>>(qi::repeat(phx::ref(objSize))[qi::char_])
)
//End Grammar
);
if(match){
std::cout << "Parse Success\n";
}
else
std::cout << "Parse Fail\n";
if (it != v.end()){
std::vector<unsigned char> ret(it, v.end());
std::cout << "Full Parse Failed at:";
write_to_con(ret);
}
else
std::cout << "Full Parse Pass\t Size:" <<v.size() << "\n";
return 0;
}