What's the best way to create a parser in C++ from a file with grammar?
You also might want to have a look at these links:
It depends heavily on the grammar. I tend to like recursive descent parsers, which are normally written by hand (though it's possible to generate one from a description of the grammar).
If you're going to use a parser generator, there are really two good choices: Byacc and Antlr. If you want something that's (reasonably) compatible with yacc, Byacc is (by far) your best choice. If you're starting from the beginning, with neither existing code nor experience that favors using something compatible with yacc, then Antlr is almost certainly your best bet.
Since it's been mentioned, I'll also talk a bit about Bison. I'd avoid Bison like the plague that it is. Brooks's advice to "Plan to throw one away" applies here. Robert Corbett (the author of Byacc) wrote Bison as his first attempt at a parser generator. Unfortunately, he gave it to GNU instead of throwing it away. In a classic case of marketing beating technical excellence, Bison is widely used (and even recommended, by those who don't know better) while Byacc remains relatively obscure.
Edit: I hate to do it, but since it's also been mentioned, I'll also comment on Boost.spirit. While this may be the coolest example of template meta programming around, it has a couple of problems that lead me to recommend against trying to put it to serious use.
Believe me: the fact that you can write something like Spirit at all is right on the border between impressive and amazing -- but I'd still only use it if I was sure the grammar I was dealing with was (and would always remain) quite small and simple.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With