基于 Boost Spirit 语法的字符串分割

发布于 2024-12-06 14:08:07 字数 1009 浏览 3 评论 0原文

我使用的是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

滥情稳全场 2024-12-13 14:08:07

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

但即使它按照您想要的方式编译,它也不会按照您期望的方式解析。 Spirit 本质上是贪婪的,这意味着 +char_ 将无条件消耗您输入中剩余的任何内容。最好是

+~char_(", ;") % 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 have

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

i.e. one or more (+) characters which are not (~) in the set ", ;" interpersed with exactly one of those characters. The list parser (%) exposes a vector<A>, where A is the attribute of the left hand expression, a vector<string> in the case above.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文