Overview

Sweet Parser is a C++ parser library. It requires Microsoft Visual Studio 2008 (MSVC 9.0) and Boost 1.43.

The library generates an LALR(1) parser to parse a language described by an EBNF grammar. The grammar is specified as a C++ string or read from a file and the parser can be generated without the need for a separate generation step. If generation is required there is a stand alone executable that provides Lua scriptable code generation from the parser state machine. Parsers generated this way depend only on the standard libary.

The reduction of productions involving EBNF operators ('*', '+', '?', and parenthesized '|') may not be correct in all cases. The interaction between the EBNF operators and associativity directives ('left', 'right', and 'none') used to resolve conflicts is not well understood and may lead to unreported conflicts. If EBNF operators are not used then these problems are avoided and generated parsers behave just as if they were generated from a BNF grammar.

Features:

Anti-features:

Boost Spirit vs Sweet Parser

Boost Spirit is an awesome library written by Joel de Guzman, Hartmut Kaiser, and others. Boost Spirit uses expression templates to implement an EBNF like language within C++. Documentation is available at http://boost-spirit.com/home/spirit2/libs/spirit/doc/html/index.html.

Because Boost Spirit uses templates compile times and code size can increase quite a bit. Especially with more complicated grammars. However I've not used the latest version and I think it will have improved in these areas.

Boost Spirit is strictly a C++ parser. Sweet Parser is a C++ parser generator that can output code for a parser in any language (provided you're willing to write a little Lua).

Boost Spirit implements a backtracking LL(k) parser while Sweet Parser implements an LALR(1) parser. This affects the way that portions of the grammar are matched and actions are called. Boost Spirit will match some portion of a grammar and call an action for it even if it later backtracks and doesn't ultimately match that portion of the grammar. Sweet Parser uses a different algorithm which means that its actions are only ever called when portions of the grammar ultimately match.

The Sweet Parser library uses Boost Spirit to parse its EBNF grammars. So if you're planning on compiling Sweet Parser yourself or using the C++ library in your own code you're going to depend on Boost as well.

Boost Spirit has been part of the Boost libraries since 2002. It is portable to many compilers and has had extensive use, testing, and review by the Boost community. Sweet Parser has none of these things.

In summary Boost Spirit is a great parser library, especially if you are already using the Boost libraries. If you are finding your compile times growing too long then you might like to try Sweet Parser as an inline C++ replacement or using Sweet Parser to generate a parser offline that you then just use in your code.

LEMON vs Sweet Parser

LEMON is an LALR(1) parser generator for C and C++. It is written by D. Richard Hipp, the author of the SQLite database library. Documentation is available at http://www.hwaci.com/sw/lemon/lemon.html.

Sweet Parser has a built-in lexical analyzer while LEMON does not. Sweet Parser generates its parser from an EBNF grammar while LEMON generates from a BNF grammar. Sweet Parser allows generation of parsers in any programming language by providing Lua bindings to the state machine generated for a parser while LEMON only generates a C/C++ parser. Sweet Parser can be used without a separate generation step while LEMON can not.

LEMON is a simpler and more mature parser generator for C and C++. It doesn't have a built-in lexical analyzer but that is not hard to implement by hand. If you want a native C or C++ parser then you're probably better off using LEMON however if you want to generate a parser in another programming language, would like to play with implementing a parser from already generated parse tables, or prefer a slightly more C++ feel you might like to try Sweet Parser.