Boost Spirit可以用来解析字节流数据吗?

发布于 2024-07-08 15:10:30 字数 88 浏览 6 评论 0 原文

Spirit(Boost C++ 库的一部分)可以用来解析来自流的二进制数据吗? 例如,它可以用来将来自套接字的数据解析为结构、字节和单独的位标志吗? 谢谢!

Can Spirit (part of Boost C++ library) be used to parse out binary data coming from a stream? For example, can it be used to parse data coming from a socket into structures, bytes, and individual bit flags? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

你げ笑在眉眼 2024-07-15 15:10:30

Boost Spirit 允许使用 扩展巴科斯-诺尔范式 (EBNF) 定义解析器 语法与模板元编程。 它非常灵活,并且在解析过程的所有阶段都使用可以定制的抽象类。 为了处理二进制数据流,您需要实现自定义扫描仪类,因为默认类型是针对文本输入定制的。 您可以在扫描器和解析部分进一步阅读Spirit 用户指南

以我的拙见,二进制数据流最好使用手工序列化代码来处理。 Spirit 更适合格式良好的语法,例如标记或脚本语言。 例如,EBNF中提供了Lua的完整语法。 因此,使用 Spirit 构建自定义解析器可能是有意义的。 另一方面,如果 上下文无关语法甚至存在。

附录

最新版本的 Boost Spirit 包含处理二进制数据的功能。

Boost Spirit allows for a parser to be defined using Extended Backus–Naur Form (EBNF) syntax with template meta-programming. It is very flexible and uses abstract classes in all phases of the parsing process that can be customized. In order to process a binary data stream, you would need to implement custom scanner classes since the default types are tailored for text input. You can read further in The Scanner and Parsing section of the Spirit User's Guide.

In my humble opinion, binary data streams are best handled with hand rolled serialization code. Spirit is geared more towards well formed grammars like mark-up or scripting languages. For example, The Complete Syntax of Lua is provided in EBNF. So, it might make sense to use Spirit to build a custom parser. On the other hand, a proprietary serial data link with synchronization bytes and CRC bracketing messages would require a lot more work just to define the EBNF if a context-free grammar even exists for it.

Addendum

The latest version of Boost Spirit includes functionality for dealing with binary data.

┾廆蒐ゝ 2024-07-15 15:10:30

刚刚发布的 Spirit2 具有解析二进制的功能。 看看这个< /a>.

Spirit2, just released, has facilities for parsing binary. Check out this.

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