精神规则中的定义函数给出警告

精神规则中的定义函数给出警告

本文介绍了精神规则中的定义函数给出警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

qi :: repeat(1,2)在规则中的功能是给我warrning,我不想忽略该警告,所以我想优化这个代码,例如分隔规则正在执行规则中所执行的相同操作。

>

  std :: ifstream ifs(f: test.txt); 
std :: string line;

//在我的代码的头中
std :: vector< unsigned long long> v_BF_Char;


// qi :: int_parser< uintmax_t,16> hex_int;
static qi :: uint_parser< unsigned long long,16,16,16> hex_int;

while(std :: getline(ifs,line))
{
typedef std :: string :: const_iterator It;
it begin = line.begin(),end = line.end();

//语法规则
qi :: rule< It,unsigned long long()> braced_hex ='&'>> qi :: repeat(1,2)[hex_int]>> '>';


bool ok = qi :: phrase_parse(begin,end,
*('<'>> qi :: repeat(1,2)[hex_int] >>'),qi :: space,v_BF_Char);

并且想做这样的事。

  bool ok = qi :: phrase_parse(begin,end,
braced_hex,qi :: space,v_BF_Char);

test.txt 包含

 < 51dc> < 04001C0180000000000000000EE317BC> 
< 05001C0180000000> < 04001C0180000000000000000EE317BC>
< 51dc> < 30ea30f330ae30c330c8>
< 0000> < fffd>
< 003d> < 00a5>
< 005e> < 007d>
< 005f> < 0303>
< 0060> < 2019> ;.
< 0061> < 005c>
< 0063> < 007c>
< 0064> < 007e>
< 0068> < 2044>
< 0069> < 0192>
< 006a> < 00a7>
< 006b> < 00a4>
< 006c> < 201c>

我的代码只在第二行工作,在向量中提取值, p>

解决方案

修复规则的公开属性:

  qi :: rule< It,std :: vector< unsigned long long>()> braced_hex ='&'>> qi :: repeat(1,2)[hex_int]>> '>'; 

重复



现场演示:



strong>

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

命名空间qi = boost :: spirit :: qi;

int main(){
std :: ifstream ifs(input.txt);

//在我的代码的头中
std :: vector< unsigned long long> v_BF_Char;

// qi :: int_parser< uintmax_t,16> hex_int;
static qi :: uint_parser< uint64_t,16,16,16> hex_int;

std :: string line;
while(std :: getline(ifs,line))
{
typedef std :: string :: const_iterator It;
it begin = line.begin(),end = line.end();

//语法规则
qi :: rule< It,std :: vector< uint64_t>()> braced_hex ='&'>> qi :: repeat(1,2)[hex_int]>> '>';

bool ok = qi :: phrase_parse(begin,end,* braced_hex,qi :: space,v_BF_Char);
assert(ok);
}

std :: cout<< Parsed:<< v_BF_Char.size()<< 64bit elements\\\
;
}

列印:

 已解析:50 64位元素

对于该输入:

 < 1353cd278dd1f003636bc155006ac5ce> < 1e83b053032565f0> < d1e97c841e68153a5d82d57df3074a21> 
< 92adee538fd147a337ebc8a4fc8d0ad3> < 0ed9fb22ab42b3a4> < 756ad64486054d22c62329e8dcaef0c5>
< 16eeaec1108b1159b49c6bf884564519> < b4b87d1fd1aa10af> < 1f710495fd863a1d191355adf1b33d5a>
< 947ac523b4450ec26446840ccde3965b> < faa860f7763b23dc> < 571decbfd0fcfe9a4047f72c101b9d87>
< 50d726028b79b1a531a2c3752a4fdde7> < 644e057721fa7fe1> < 6bf66d2e1ae50351db53eddcee5fae41>
< 6916580258e94f2be66eb71f103d3023> < a427df9bd05edd6d> < d896cfe92e6634867fcab5c6fc2de60b>
< 9e50d5c9cda9e9a2fbf78eeb10f3a6bd> < 9cff72edea319328> < 0aabc7f36fcd058a2dfa7bb94602919a>
< 923832f107c94d4a04b1de96241fda14> < 003c7554390cabaf> < c43d58504fc6659bb226707efc0221b8>
< 1040a8d23eac10e9e4b6abb2efcde1bd> < f38ac3906542529d> < ffbd836c54b0f498d358e4ea50170c94>
< 7c4c6fd86a60cf7b4ac62faa0395c06b> < 61156478683a6b01> < fb92ef7030068f25471e8049fb0f7cd3>




qi::repeat(1,2) funtion in rule is giving me warrning and i dont want to ignore that warning so i want to optimized this code like separating the rule from the parsering method.

qi::phrase_parse is doing the same thing which is in the the rule but i want to sperate the rule and give rule to the pharse_parse funtion.

std::ifstream ifs("f:/test.txt");
 std::string line;

//In header in my code
std::vector<unsigned long long> v_BF_Char;


//qi::int_parser<uintmax_t, 16> hex_int;
static qi::uint_parser<unsigned long long, 16, 16, 16> hex_int;

while (std::getline(ifs, line))
{
    typedef std::string::const_iterator It;
    It begin = line.begin(), end = line.end();

    // rule for grammer
    qi::rule<It, unsigned long long()> braced_hex = '<' >> qi::repeat(1,2)[hex_int] >> '>';


    bool ok = qi::phrase_parse(begin, end,
           *('<' >> qi::repeat(1,2)[ hex_int ] >> '>'),  qi::space, v_BF_Char);

and want to do somthing like this

bool ok = qi::phrase_parse(begin, end,
           braced_hex ,  qi::space, v_BF_Char);

test.txt Contain

<51dc> <04001C0180000000000000000EE317BC>
<05001C0180000000> <04001C0180000000000000000EE317BC>
<51dc> <30ea30f330ae30c330c8>
<0000> <fffd>
<003d> <00a5>
<005d> <00a6>
<005e> <007d>
<005f> <0303>
<0060> <2019>
<0061> <005c>
<0062> <2018>
<0063> <007c>
<0064> <007e>
<0068> <2044>
<0069> <0192>
<006a> <00a7>
<006b> <00a4>
<006c> <201c>

My code is working for only 2nd line and extract values in vector but not other line values.

解决方案

Fix the rule's exposed attribute:

qi::rule<It, std::vector<unsigned long long>()> braced_hex = '<' >> qi::repeat(1,2)[hex_int] >> '>';

Repeat exposes a vector of hex_int's exposed attribute type

Live demo:

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

namespace qi = boost::spirit::qi;

int main() {
    std::ifstream ifs("input.txt");

    //In header in my code
    std::vector<unsigned long long> v_BF_Char;

    //qi::int_parser<uintmax_t, 16> hex_int;
    static qi::uint_parser<uint64_t, 16, 16, 16> hex_int;

    std::string line;
    while (std::getline(ifs, line))
    {
        typedef std::string::const_iterator It;
        It begin = line.begin(), end = line.end();

        // rule for grammer
        qi::rule<It, std::vector<uint64_t>()> braced_hex = '<' >> qi::repeat(1,2)[hex_int] >> '>';

        bool ok = qi::phrase_parse(begin, end, *braced_hex,  qi::space, v_BF_Char);
        assert(ok);
    }

    std::cout << "Parsed: " << v_BF_Char.size() << " 64bit elements\n";
}

Prints:

Parsed: 50 64bit elements

E.g. for this input:

<1353cd278dd1f003636bc155006ac5ce> <1e83b053032565f0> <d1e97c841e68153a5d82d57df3074a21>
<92adee538fd147a337ebc8a4fc8d0ad3> <0ed9fb22ab42b3a4> <756ad64486054d22c62329e8dcaef0c5>
<16eeaec1108b1159b49c6bf884564519> <b4b87d1fd1aa10af> <1f710495fd863a1d191355adf1b33d5a>
<947ac523b4450ec26446840ccde3965b> <faa860f7763b23dc> <571decbfd0fcfe9a4047f72c101b9d87>
<50d726028b79b1a531a2c3752a4fdde7> <644e057721fa7fe1> <6bf66d2e1ae50351db53eddcee5fae41>
<6916580258e94f2be66eb71f103d3023> <a427df9bd05edd6d> <d896cfe92e6634867fcab5c6fc2de60b>
<9e50d5c9cda9e9a2fbf78eeb10f3a6bd> <9cff72edea319328> <0aabc7f36fcd058a2dfa7bb94602919a>
<923832f107c94d4a04b1de96241fda14> <003c7554390cabaf> <c43d58504fc6659bb226707efc0221b8>
<1040a8d23eac10e9e4b6abb2efcde1bd> <f38ac3906542529d> <ffbd836c54b0f498d358e4ea50170c94>
<7c4c6fd86a60cf7b4ac62faa0395c06b> <61156478683a6b01> <fb92ef7030068f25471e8049fb0f7cd3>

这篇关于精神规则中的定义函数给出警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 13:12