Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Boost Spirit be used to parse byte stream data?

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!

like image 437
Brian Avatar asked Nov 12 '08 20:11

Brian


2 Answers

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.

like image 105
Judge Maygarden Avatar answered Sep 19 '22 12:09

Judge Maygarden


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

like image 27
Joel de Guzman Avatar answered Sep 19 '22 12:09

Joel de Guzman