Troskyvs
Troskyvs

Reputation: 8047

Could lex/flex be used to parse binary format source files?

When I learn lex tool, I found it helps to parse source files in text format, like building a new programming languages, etc. I also with to use it to build a tool to analyse some binary input streams, like codec/decoders.

Does lex/flex/yacc/bison support such requirements, do they have special command line options and syntax to enable this?

Thanks!

Upvotes: 2

Views: 783

Answers (1)

rici
rici

Reputation: 241721

Flex (and the other lex implentations I'm familiar with) have no problem with non-ascii characters, including the NUL character. You may have to use the 8bit option, although it is the default unless you request fast state tables.

However, most binary formats use length-prefixed variable length fields, which cannot be expressed in a regular expression. Moreover, it is quite common for fixed-lengtb fields to be context-dependent; you can build a state machine in flex using start conditions, but that's a lot of work and is likely to be a waste of your time and flex's features.

Upvotes: 4

Related Questions