Can Boost Spirit be used to parse byte stream data

2019-02-03 14:23发布

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!

2条回答
虎瘦雄心在
2楼-- · 2019-02-03 15:07

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

查看更多
Deceive 欺骗
3楼-- · 2019-02-03 15:17

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.

查看更多
登录 后发表回答