Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ create a parser [closed]

What's the best way to create a parser in C++ from a file with grammar?

like image 652
Szymon Lipiński Avatar asked Dec 03 '09 22:12

Szymon Lipiński


2 Answers

  • I'd suggest the use of boost.spirit

You also might want to have a look at these links:

  • Ragel State Machine Compiler
  • ANTLR parser generator
  • Metacompilers tutorial
like image 191
KeatsPeeks Avatar answered Oct 16 '22 01:10

KeatsPeeks


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.

  1. Compile times with it can get excruciating -- 10 minutes is common, and a larger/more complex grammar can take even longer (assuming it doesn't crash the compiler).
  2. If you make any mistake at all, it can and frequently will produce insanely long error messages that are virtually impossible to decipher. Error messages from template-heavy code are notoriously bad anyway, and Spirit stresses the system more than almost anything else.

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.

like image 25
Jerry Coffin Avatar answered Oct 16 '22 02:10

Jerry Coffin