问题描述
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!
推荐答案
Boost Spirit 允许使用 扩展 Backus–Naur 形式 (EBNF) 语法与 模板元编程.它非常灵活,在解析过程的所有阶段都使用抽象类,可以自定义.为了处理二进制数据流,您需要实现自定义扫描器类,因为默认类型是为文本输入量身定制的.您可以在扫描仪和解析部分进一步阅读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.
在我看来,二进制数据流最好使用手动序列化代码来处理.Spirit 更适合结构良好的语法,如标记或脚本语言.例如,EBNF 中提供了 Lua 的完整语法.因此,使用 Spirit 构建自定义解析器可能是有意义的.另一方面,如果 上下文无关语法 甚至为此而存在.
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.
附录
Boost Spirit 的最新版本包括处理二进制数据的功能.
这篇关于Boost Spirit可以用来解析字节流数据吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!