gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 17:08:42 30 4
gpt4 key购买 nike

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

最佳答案

Boost Spirit 允许使用 Extended Backus–Naur Form (EBNF) 定义解析器template meta-programming 的语法.它非常灵活,在可定制的解析过程的所有阶段都使用抽象类。为了处理二进制数据流,您需要实现自定义扫描器类,因为默认类型是为文本输入量身定制的。您可以在 The Scanner and Parsing 中进一步阅读节Spirit User's Guide .

在我看来,二进制数据流最好用手工序列化代码处理。 Spirit 更适合结构良好的语法,如标记或脚本语言。例如,The Complete Syntax of Lua在 EBNF 中提供。因此,使用 Spirit 构建自定义解析器可能是有意义的。另一方面,具有同步字节和 CRC 括号消息的专有串行数据链路将需要更多工作来定义 EBNF,如果 context-free grammar甚至存在。

附录

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

关于c++ - Boost Spirit可以用来解析字节流数据吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/285200/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com