<< Scala: Still Uncomfortable After Five Years | Home | Friday Scala Quiz: DSL This! >>

A Little C++ To Boost Your Spirit

C++: The Ultimate DSL

Take a look at the following snippet of valid C++ code ...

group = '(' >> expr >> ')';
fact = integer | group;
term = fact >> *( ('*' >> fact) | ('/' >> fact) );
expr = term >> *( ('+' >> term) | ('-' >> term) );

... and tell me C++ is not the perfect language for DSLs! ;=)

Seriously, I copied down the above from Adam Mitz's OCI internal C++ lunch talk about Boost.Spirit yesterday.

Boost.Spirit documentation: Spirit is an object-oriented recursive-descent parser generator framework implemented using template meta-programming techniques. Expression templates allow us to approximate the syntax of Extended Backus-Normal Form (EBNF) completely in C++.

The Spirit framework enables a target grammar to be written exclusively in C++. Inline EBNF grammar specifications can mix freely with other C++ code and, thanks to the generative power of C++ templates, are immediately executable. In retrospect, conventional compiler-compilers or parser-generators have to perform an additional translation step from the source EBNF code to C or C++ code.

As parser generators go, Boost.Spirit is pretty powerful stuff. As an example, Adam redid Mark Volkmann's Math example from the ANTLR 3 talk, ASTs and all.

Geeky as Boost.Spirit may seem, can think of one occasion in past projects where we could have used it instead of coding a recursive descent parser by hand.

Tags :


Re: A Little C++ To Boost Your Spirit

C++ is not the perfect language for creating DSLs. C++ might be the best choice when you need to write a device driver or have to run on an obscure embedded OS and memory is at a premium, etc. For everything else, the high syntax complexity and human cost of code maintenance make just about any other language look more appealing to people and companies that realize development and maintenance costs are important for years to come.

Re: A Little C++ To Boost Your Spirit

@Eric, I just want to make sure the smiley showed up properly on your browser.

What I intended to say is that if you are stuck with C++, you can create clever DSLs more easily than some other languages, such as Java.

Re: A Little C++ To Boost Your Spirit

Eric said "C++ is not the perfect language for creating DSLs." Sure it's not _the perfect_ language for DSLs, but it's at least capable of hosting useful DSLs.

Of course I couldn't agree less with the rest of Eric's comment. C++ is my language of choice over Java. When someone asks me (pays me) to code in Java for compatibility with an existing system, or because that's what people know, then fine, I'll live with it. But when I have a choice, it's going to be C++ (or maybe Perl/Python if scripting is an option). Luckily for me, C++ also pays the bills. As demonstrated here, the designer of C++ had the foresight to trust the programmer to make good choices, and not to confine the programmer into one particular model. I don't think Spirit is "clever", on the contrary, it's a straightforward way of representing the problem domain in the program.

Re: A Little C++ To Boost Your Spirit

Java feels like coding with big fuzzy mittens on, and your mother coming every five minutes to make sure you're not getting into trouble. Anybody who thinks that average people can learn to "program" in java is correct. But since the average person will not understand the problems solved by most computer programs, there is little benifit to that. Lots more silly little video games, I guess.

Re: A Little C++ To Boost Your Spirit

So are there any good C++ frameworks for writing web applications? ;)

Re: A Little C++ To Boost Your Spirit

The obvious google search reveals:
  • Wt at http://www.webtoolkit.eu/wt
  • ATL Server (for Windows platforms)
  • A discussion of this issue at http://www.jroller.com/craiger/entry/where_are_all_the_c
Take a look at that last link, the blog author posted a conversation he had with Bjarne Stroustrup.

Since I don't write web apps for work, I don't have experience with any of these. Any web app work I've done on my own time has been in Perl (a long time ago) or in Python (more recently).

Re: A Little C++ To Boost Your Spirit

boost::spirit is a wonderful library. It's fast, easy to read, easy to understand and easy to maintain code that uses it.

Add a comment Send a TrackBack