可以使用Boost Spirit来解析字节流数据吗? [英] Can Boost Spirit be used to parse byte stream data?

查看:221
本文介绍了可以使用Boost Spirit来解析字节流数据吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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允许使用 Extended Backus-Naur Form(EBNF) syntax with 模板元程序设计。它非常灵活,并且在可以定制的解析过程的所有阶段中使用抽象类。为了处理二进制数据流,您需要实现自定义扫描器类,因为默认类型是为文本输入量身定制的。您可以在扫描器和解析部分中进一步阅读 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.

我认为,二进制数据流最好用手工滚动序列化代码处理。精神更倾向于形成良好的语法,如标记语言或脚本语言。例如,在EBNF中提供了 Lua的完整语法。因此,使用Spirit构建自定义解析器可能有意义。另一方面,具有同步字节和CRC包围消息的专有串行数据链路将需要更多的工作来定义EBNF,如果

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来解析字节流数据吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆