开发者

Parsing string, with Boost Spirit 2, to fill data in user defined struct

开发者 https://www.devze.com 2022-12-23 22:00 出处:网络
I\'m using Boost.Spirit which was distributed with Boost-1.42.0 with VS2005. My problem is like this.

I'm using Boost.Spirit which was distributed with Boost-1.42.0 with VS2005. My problem is like this.

I've this string which was delimted with commas. The first 3 fields of it are strings and rest are numbers. like this.

String1,String2,String3,12.0,12.1,13.0,13.1,12.4

My rule is like this

qi::rule<string::iterator, qi::skip_type> stringrule = *(char_ - ',')
qi::rule<string::iterator, qi::skip_type> myrule= repeat(3)[*(char_ - ',') >> ','] >> (double_ % ',') ;

I'm trying to store the data in a structure like this.

struct MyStruct
{
   vector<string> stringVector ;
   vector<double> doubleVector ;
} ;

MyStruct var ;

I've wrapped it in BOOST_FUSION_ADAPT_STRUCTURE to use it with spirit.

BOOST_FUSION_ADAPT_STRUCT (MyStruct, (vector<string>, stringVector) (vector<double>, doubleVector))

My parse function parses the line and returns true and after

qi::phrase_parse (iterBegin, iterEnd, myrule, boost::spirit::ascii::space, var) ;

I'm expecting var.stringVector and var.doubleVector are proper开发者_高级运维ly filled. but it is not the case.

What is going wrong ?

The code sample is located here

Thanks in advance, Surya


qi::skip_type is not something you could use a skipper. qi::skip_type is the type of the placeholder qi::skip, which is applicable for the skip[] directive only (to enable skipping inside a lexeme[] or to change skipper in use) and which is not a parser component matching any input on its own. You need to specify your specific skipper type instead (in your case that's boost::spirit::ascii:space_type).

Moreover, in order for your rules to return the parsed attribute, you need to specify the type of the expected attribute while defining your rule. That leaves you with:

qi::rule<string::iterator, std::string(), ascii:space_type> 
    stringrule = *(char_ - ',');
qi::rule<string::iterator, MyStruct(), ascii:space_type> 
    myrule = repeat(3)[*(char_ - ',') >> ','] >> (double_ % ',');

which should do exactly what you expect.

0

精彩评论

暂无评论...
验证码 换一张
取 消