What advantages are there to using Flex/Bison over just writing a compiler straight in a programming language

algorithmscompiler

I have written an Interpreter / Compiler simply as a programming algorithm using features such as recursive functions and concepts from finite-State machines. To me, these things are ideally suited to building a compiler (Although it does end up getting pretty complex).

Doing a little bit of research on the subject, I came across Flex/Bison. I'm wondering how these tools make it better to develop a compiler. Is is simpler to use that or is the end product better in some way?

Best Answer

Compiler generators (such as Bison) use pretty much the same concepts from finite state machines as you have used in your hand-crafted parser.

The main differences between compiler generators and hand-crafted parsers are

  • Most hand-crafted parsers use a top-down parsing strategy, while compiler generators often create a bottom-up parser. This has a slight impact on the languages that can be supported, although most languages of real interest can be handled by both (possibly after slight tweaks to the grammar rules). This has mostly to do with recursion in the grammar.
  • With a compiler generator, you specify the language's grammar directly, in a form that is usually close to the BNF form that is commonly used for describing language grammars. With a hand-crafted parser, it can sometimes be hard to recognize the language's grammar in the code, which can make it more difficult to diagnose errors resulting from incorrect interpretation of the language rules.
  • With a compiler generator, you may have to learn a new language (the language accepted by the generator). Hand-crafted parsers can be written in most languages.
Related Topic