Re: Binary File parsing with Flex & Bison???

haberg@math.su.se (Hans Aberg)
14 May 2005 12:18:15 -0400

          From comp.compilers

Related articles
Binary File parsing with Flex & Bison??? usenet.raghu@gmail.com (Raghu Kodali) (2005-05-14)
Re: Binary File parsing with Flex & Bison??? haberg@math.su.se (2005-05-14)
| List of all articles for this month |

From: haberg@math.su.se (Hans Aberg)
Newsgroups: comp.compilers
Date: 14 May 2005 12:18:15 -0400
Organization: Mathematics
References: 05-05-080
Keywords: lex
Posted-Date: 14 May 2005 12:18:15 EDT

"Raghu Kodali" <usenet.raghu@gmail.com> wrote:


> Can I use Flex & Bison to parse a binary file?
>
> I know the BNF of the binary file but I dont know how to do it with
> Flex & Bison.
>
> Can you please let me know if this is possible? if yes, how?


> [Flex is pretty much 8-bit clean, so you can put arbitrary bytes in
> your patterns as \123 or \x42.


Actually, I have started to use Flex to parse UTF-8, by merely feeing
it a UTF-8 .l file, and this seems to work just fine. I use the latest
Flex, available from the CVS at <http://flex.sourceforge.net/>. Older
Flex used to have 7-bit mode, but the later versions seem to have the
8-bit mode as default.


> You might want to check carefully to
> make sure that \000 zero bytes are handled correctly.


There I think Flex is doing the matching correctly. The problem is
that the identifier string is temporarily in the buffer augmented with
a zero byte, and one cannot use C string functions to handle
that. Instead use the yytext and yylen variables, and binary mem...()
functions.


The thing one should watch out with Flex is that long rule matches may
slow it down -- the implemented lexer apparently has some buffer
problems. So in order to avoid that, break down rules into shorter
segments, roughly not exceeding the length of lines in text data.


There is a mailing list for Flex available:
    <http://lists.gnu.org/mailman/listinfo/help-flex>
List-Archive: <http://lists.gnu.org/pipermail/help-flex>


--
    Hans Aberg


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.