Parsing Binary Structures; Is there a better way / What is your way?

andrew cooke andrew at acooke.org
Fri Aug 7 07:29:13 EDT 2009


On Aug 5, 10:46 am, "Martin P. Hellwig" <martin.hell... at dcuktec.org>
wrote:
> Hi List,
>
> On several occasions I have needed (and build) a parser that reads a
> binary piece of data with custom structure. For example (bogus one):
>
> BE
> +---------+---------+-------------+-------------+------+--------+
> | Version | Command | Instruction | Data Length | Data | Filler |
> +---------+---------+-------------+-------------+------+--------+
> Version: 6 bits
> Command: 4 bits
> Instruction: 5 bits
> Data Length: 5 bits
> Data: 0-31 bits
> Filler: filling 0 bits to make the packet dividable by 8

hi,

sorry i'm a bit late here, but lepl does exactly this.  also, as you
asked for in another post, the BitString class allows arbitrary
indexing into a sequence of bits.  and because it's part of a
recursive descent parser you can match "anything" (lepl will handle -
although less efficiently - left recursive and ambiguous grammars).

see the example at http://www.acooke.org/lepl/binary.html#matching
which constructs an ethernet frame and then parses it, extracting the
source and destination addresses.

feel free to email me with more questions.

disclaimer: this is quite new and i don't know of anyone that actually
uses it; it is also Python3 only (because it uses bytes()).

andrew





More information about the Python-list mailing list