2011-09-26 70 views
3

我使用Boost 1.44,Spirit分析器可以很好地进行数值解析,但对于字符串解析来说真的很棘手。我试图解析一个字符串,使用多个分隔符分割:',',';'要么 ' '。它非常适用于数字,当我这样做(其中VECT =矢量<双重>):Boost Spirit基于语法分割的字符串

qi::parse(first,last,double_ >> *(',' >> double_ | ' ' >> double_ | ';' >> double_), 

VECT,空间);

然而,当我修改用于使用VECT =矢量<字符串>串的语法,

+char_ >> *(',' >> +char_ | ' ' >> +char_ | ';' >> +char_) 

我得到以下错误:

/usr/include/boost/spirit/home/qi/detail/assign_to.hpp:109:13: error: invalid conversion from ‘char’ to ‘const char*’/usr/include/boost/spirit/home/qi/detail/assign_to.hpp:109:13: error: initializing argument 1 of ‘std::basic_string<_CharT, _Traits,_Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]’ 

我缩小的误差是所述第一+ char_在语法语法中被视为一系列字符而不是字符串。有没有办法解决这个问题?

谢谢

+0

另请参阅http://stackoverflow.com/questions/7436481/how-to-make-my-split-work-only-on-one-real-line-and-be-capable-to-skeep-quoted -pa/7462539#7462539 – sehe

回答

7

对于更新版本的Spirit,字符串处理(您这样做的方式)变得更容易。我建议使用Boost V1.47中的Spirit,它重写了属性处理代码。

但即使按照你想要的方式编译,它也不会解析你期望的方式。精神本质上是贪婪的,这意味着+char_将无条件地消耗您的输入内容。这似乎是更好的与这些字符中的恰好一个interpersed设定", ;"为具有不(~

+~char_(", ;") % char_(", ;") 

即一个或多个(+)字符。列表解析器(%)公开vector<A>,其中A是左侧表达式的属性,在上述情况下是vector<string>

+0

+1,之前我还没有注意到〜〜解析器运算符(我仍然使用了'char_ - char_(“,;”)''而不是 – sehe