基于 Boost Spirit 语法的字符串分割
我使用的是 Boost 1.44,Spirit 解析器对于数字解析效果很好,但对于字符串解析来说确实很棘手。我正在尝试解析要使用多个分隔符分割的字符串: ',' ,';'或者 ' '。当我这样做时,它对数字效果很好(其中 vect = vector
qi::parse(first,last,double_ >> *(',' >> double_ | ' ' >> double_ | ';' >> double_),
vect,space);
但是,当我使用 vect = vector< 修改字符串语法时string >,
+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_ ,该语法被视为一系列字符而不是字符串。有什么办法可以解决这个问题吗?
谢谢
I am using Boost 1.44, The Spirit parser works well for numeric parsing but really is tricky for string parsing. I am trying to parse a string to be split using multiple delimiters: ',' ,';' or ' '. It works well for numbers when I do this (where vect = vector < double >):
qi::parse(first,last,double_ >> *(',' >> double_ | ' ' >> double_ | ';' >> double_),
vect,space);
However when I modify the grammer for strings using vect = vector< string >,
+char_ >> *(',' >> +char_ | ' ' >> +char_ | ';' >> +char_)
I get the following error:
/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>]’
I narrowed down the error to being the first +char_ in the grammar syntax which is being taken as a series of chars rather than a string. Is there any way to fix this issue?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用最新版本的 Spirit,字符串处理(您的处理方式)变得更加容易。我建议使用 Boost V1.47 中的 Spirit,它对属性处理代码进行了重大重写。
但即使它按照您想要的方式编译,它也不会按照您期望的方式解析。 Spirit 本质上是贪婪的,这意味着
+char_
将无条件消耗您输入中剩余的任何内容。最好是在
", ;"
集合中插入一个或多个 (+
) 字符(~
)以外的字符恰好是这些字符之一。列表解析器 (%
) 公开一个vector
,其中A
是左侧表达式的属性,即vector< ;string>
在上面的例子中。String handling (the way you do it) got a lot easier with more recent versions of Spirit. I'd suggest to use Spirit from Boost V1.47, which got a major rewrite of the attribute handling code.
But even if it compiled the way you want, it wouldn't parse the way you expect. Spirit is inherently greedy, that means that
+char_
will consume whatever is left in your input unconditionally. It seems to be better to havei.e. one or more (
+
) characters which are not (~
) in the set", ;"
interpersed with exactly one of those characters. The list parser (%
) exposes avector<A>
, whereA
is the attribute of the left hand expression, avector<string>
in the case above.