views:

544

answers:

5

Hi all,

I'm writing an experimental language, which is very close to pseudocode, for fun and to learn more about C++. One of the problems is that I need to parse some grammar, and my search to find a good C++ grammar parser has been unsuccessful (I couldn't find anything). What I want to accomplish is this:

set a to 4

And I want the parser to return a map containing something similar to:

command = "set"
var = "a"
value = 4

based on a set of rules I give it.

I ultimately would have to roll my own if I don't find anything useful, but I would like to prevent re-inventing the wheel as much as I can.

So, does anyone here know of a good/sane grammar parser that generates C++ code? It's much better if it's widely used.

Thanks!

+10  A: 

Check if Boost::Spirit can be used. The Spirit framework enables a target grammar to be written exclusively in C++.

aJ
+4  A: 

You can use yacc (with flex as the tokenizer). A quick search yields this page to do C++ usage of yacc, which is implemented in C.

Snazzer
+4  A: 

Lex and Yacc / Flex and Bison

JP Alioto
+4  A: 

You should check out ANTLR. It can generate for a number of languages, including (I believe) C++. [Looking more closely at the documentation, there is a C target; it is not clear that there is a C++ target, but the chances are high that the C target can easily be used with C++.] I'm still old school and tend to use yacc (or bison), but that's because I'm familiar with it rather than because it is still the best (though it is still good).

Jonathan Leffler
+2  A: 

Rolling your own can be as easy as writing the grammar in the first place! It's a great way to learn about parsing, glean a more intimate knowledge of your programming language, and most of all it's fun. The method is called Recursive Descent. It usually comes out much more simple and elegant than what a parser generater, and you'll never have to find a Yacc port/fork again :) For a great tutorial, check out Jack Crenshaw's Let's Build a Compiler

Otherwise, Lex and Yacc are the tradional tools while Boost::Spirit is C++ specific and more modern. I would recommend Boost::Spirit as it will help cement the C++ paradigms in your programming.

Kelden Cowan
Yes a recursive descent can be easy to make by hand, but to make it good is another situation. Its really helpful to use something like ANTLR to make rapid changes, and detect inconsistencies in your grammar, and then transforming it into a hand-crafted parser for speed and good debugging messages.
Unknown
Thanks very much. The book "Let's build a compiler" was immensely helpful and I started reading it. It's quite understandable (so far as I have gotten)! :)Also, I will probably take a look into Boost.Spirit
fengshaun