本文介绍了功能或使用仿函数提振精神解析器库规则保存在向量C值调用++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要分析此行并存储所有的仿函数的十六进制值
< 005F>< 0061> [< 00660066>< 00660069>< 00660066006C>]
这个值txt文件和m通过阅读本线填充线


005F 0061 00660066 00660069 00660066006C
所有的值应该是矢量,但它不工作

精神的规则是分析此行

 规则<>空白= * blank_p;
治<> parse_int =坯和GT;> < >> INT_P [的AddEntry>> >中;
治<> parse_ints = * parse_int;
治<> parse_range = * parse_int>>[>>空白>> * parse_int>> ];INT状态=解析(line.c_str()
*(
     parse_range
 )
 )。充分;

和我的仿函数是这样

 结构的AddEntry
{
    矢量<诠释>清单;
    void运算符()(INT整数)
    {
        list.push_back(整数);
    }
};


解决方案

下面是做这个用精神V2样本

 的#define BOOST_SPIRIT_DEBUG
#包括LT&;升压/精神/有/ qi.hpp>
命名空间补气=的boost ::精神::补气;诠释主(){
    的typedef的std ::字符串::为const_iterator它;
    标准::字符串常量线=下; 005F>&所述; 0061>并[d 00660066>&下; 00660069>&下; 00660066006C]的计算​​值;    它F = line.begin(),L = line.end();    齐:: int_parser< uintmax_t型,16> hex_int;
    齐::规则<它,uintmax_t型()> braced_hex ='<' >> hex_int>> '>';
    BOOST_SPIRIT_DEBUG_NODE(braced_hex);    的std ::矢量<&uintmax_t型GT;清单;
    布尔结果=齐:: phrase_parse(F,L,* braced_hex>>'['>> * braced_hex>>']',补气::空间,清单);    如果(结果){
        性病::法院LT&;< 解析成功:<<则为list.size()&所述;&下; \\ n;        为(自动&安培;五:名单)
            性病::法院LT&;< V族;&下; ;
    }
    其他{
        性病::法院LT&;< 解析失败\\ n;
    }    如果(F!= 1){
        性病::法院LT&;< 剩余未解析:'<<标准::字符串(F,L)LT;< '\\ n;
    }
}

输出:

 解析成功:5
95 97 6684774 6684777 438093348972

调试输出(如果启用):

 < braced_hex>
    <尽量><&005F GT; < 0061> [< 0066< /&尝试GT;
    <成功与GT; < 0061>并[d 00660066> < /成功>
    <&属性GT; [95] LT; /属性>
< / braced_hex>
< braced_hex>
    <尽量>< 0061>并[d 00660066> << /&尝试GT;
    <成功与GT;并[d 00660066> <&006600 LT; /成功>
    <&属性GT; [97]< /属性>
< / braced_hex>
< braced_hex>
    <尽量>< 00660066> < 0066006< /&尝试GT;
    <失败/>
< / braced_hex>
< braced_hex>
    <尽量>< 00660066> < 00660069< /&尝试GT;
    <成功与GT; < 00660069> < 0066006< /成功>
    <&属性GT; [6684774] LT; /属性>
< / braced_hex>
< braced_hex>
    <尽量>< 00660069> < 00660066< /&尝试GT;
    <成功与GT; < 00660066006C>< /成功>
    <&属性GT; [6684777] LT; /属性>
< / braced_hex>
< braced_hex>
    <尽量>< 00660066006C>< /&尝试GT;
    <成功与GT;< /成功>
    <&属性GT; [438093348972] - LT; /属性>
< / braced_hex>
< braced_hex>
    <尽量>< /&尝试GT;
    <失败/>
< / braced_hex>

请注意,我的系统上 INT 不够大,以容纳那些数字,所以解析会失败。为了获得最大范围的我用还会将intmax_t 但也可以使用其他类型,包括任意precision类型:


  • 128位字符串数组使用boost ::精神:: *

另外请注意我preFER不使用语义动作,使用自动属性传播。这真是prevalent在精神V2。另请参见



I want to parse this line and storing all hex values in functor<005F> <0061> [<00660066> <00660069> <00660066006C>]this values in txt file and m reading this fill line by line

like 005F 0061 00660066 00660069 00660066006Call values should be in vector but its not working

the spirit rule is to parse this line is

rule<> blanks = *blank_p;
rule<> parse_int = blanks >> "<" >> int_p [AddEntry] >> ">";
rule<> parse_ints = *parse_int ;
rule<> parse_range = *parse_int >>"[" >> blanks >> *parse_int >> "]";

int status = parse (line.c_str(),
*(
     parse_range 
 )
 ).full;

and my functor is this

struct AddEntry
{
    vector<int> list;   
    void operator()( int integer)
    {
        list.push_back(integer);
    }
};
解决方案

Here's a sample doing this using Spirit V2

#define BOOST_SPIRIT_DEBUG
#include <boost/spirit/include/qi.hpp>
namespace qi  = boost::spirit::qi;

int main() {
    typedef std::string::const_iterator It;
    std::string const line = "<005F> <0061> [<00660066> <00660069> <00660066006C>]";

    It f = line.begin(), l = line.end();

    qi::int_parser<uintmax_t, 16> hex_int;
    qi::rule<It, uintmax_t()> braced_hex = '<' >> hex_int >> '>';
    BOOST_SPIRIT_DEBUG_NODE(braced_hex);

    std::vector<uintmax_t> list;
    bool result = qi::phrase_parse(f, l, *braced_hex >> '[' >> *braced_hex >> ']', qi::space, list);

    if (result) {
        std::cout << "Parse success: " << list.size() << "\n";

        for (auto& v : list)
            std::cout << v << " ";
    }
    else {
        std::cout << "Parse failed\n";
    }

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

Output:

Parse success: 5
95 97 6684774 6684777 438093348972 

Debug output (if enabled):

<braced_hex>
    <try><005F> <0061> [<0066</try>
    <success> <0061> [<00660066> </success>
    <attributes>[95]</attributes>
</braced_hex>
<braced_hex>
    <try><0061> [<00660066> <</try>
    <success> [<00660066> <006600</success>
    <attributes>[97]</attributes>
</braced_hex>
<braced_hex>
    <try>[<00660066> <0066006</try>
    <fail/>
</braced_hex>
<braced_hex>
    <try><00660066> <00660069</try>
    <success> <00660069> <0066006</success>
    <attributes>[6684774]</attributes>
</braced_hex>
<braced_hex>
    <try><00660069> <00660066</try>
    <success> <00660066006C>]</success>
    <attributes>[6684777]</attributes>
</braced_hex>
<braced_hex>
    <try><00660066006C>]</try>
    <success>]</success>
    <attributes>[438093348972]</attributes>
</braced_hex>
<braced_hex>
    <try>]</try>
    <fail/>
</braced_hex>

Note that on my system int wasn't big enough to hold those numbers, so parse would fail. For maximum range I used intmax_t but you can use other types, including arbitrary precision types:

Also note I prefer not to use semantic actions, using automatic attribute propagation. This is really prevalent in Spirit V2. See also

这篇关于功能或使用仿函数提振精神解析器库规则保存在向量C值调用++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 13:11