After spending some quality time with Flex and Bison, I quickly ran into the limitations of these tools. They are C-era tools, that are designed to generate C language lexers and parsers. While it is certainly true that Flex has a generate a C++ lexer option, I found that it was largely useless since it doesn’t integrate easily with Bison-generated parsers. Also, building meaningful error messages into my generated lexers and parsers was an exercise in frustration.
Some readers of my weblog noticed my frustration, and pointed me to ANTLR.[1] This tool generates C++ and Java lexers and parsers. However, unlike Bison which consumes LALR grammars and generates the corresponding parser, ANTLR consumes LL(k) grammars. If you’re used to writing LALR grammars, writing LL(k) grammars, is, uh, different.
My experience in rewriting my LALR grammars as LL(k) grammars was fairly straightfoward. There are some ANTLR tutorials on the web that greatly help to reduce the amount of pain that you will feel during this process. ANTLR’s documentation is definitely intended to be a reference manual, and not as a learning aid. This is both its strength and its weakness; if you are serious about building parsers using ANTLR, you will find these docs invaluable.
So far so good. I’ve got the core text parsing components of CLAW up and running now using ANTLR. I’m currently converting my console-mode test cases to use CppUnit, and integrating my parsing code into the core codebase of CLAW.
[1] Thanks so much to Dan Dunham for being the first to write in about ANTLR.
1 archived reader comment
**** (2004-12-02)
In your free time, check some helpful info about…