Welcome to Spirit

Spirit is a set of C++ libraries for parsing and output generation implemented as Domain Specific Embedded Languages (DSEL) using Expression templates and Template Meta-Programming. The Spirit libraries enable a target grammar to be written exclusively in C++. Inline grammar specifications can mix freely with other C++ code and, thanks to the generative power of C++ templates, are immediately executable.

Spirit is part of Boost Libraries, a peer-reviewed, open collaborative development effort.

Feb ’13 23

Oh, in case anyone wants to follow the development of X3, here are the Github links:

https://github.com/djowel/spirit_x3
https://github.com/djowel/spirit_x3.git
git@github.com:djowel/spirit_x3.git

The full attribute mechanism works + basic C++lambda support. I’ve ported one example (calc4.cpp) which parses to an AST. The AST remains the same. Only the way the grammars are written had to change.

For a teaser: here’s GCC times:

SpiritX3: TOTAL :   4.27 secs
Spirit2:  TOTAL :  10.00 secs

Even faster at CT than the first Spirit3 attempt. Runtime speed? I expect it to be faster than Spirit-2. The rules have changed (pun intentional). Now, there’s no longer the need for virtual functions and auto is used extensively. I expect code size to be smaller too because the compiler can generate more efficient code.

Here’s the calculator grammar (based on calc3.cpp):

///////////////////////////////////////////////////////////////////////////////
//  The calculator grammar
///////////////////////////////////////////////////////////////////////////////
namespace calculator_grammar
{
    using x3::uint_;
    using x3::char_;

    x3::rule<class expression, ast::program> const expression;
    x3::rule<class term, ast::program> const term;
    x3::rule<class factor, ast::operand> const factor;

    auto const expression_def =
        term
        >> *(   (char_('+') >> term)
            |   (char_('-') >> term)
            )
        ;

    auto const term_def =
        factor
        >> *(   (char_('*') >> factor)
            |   (char_('/') >> factor)
            )
        ;

    auto const factor_def =
            uint_
        |   '(' >> expression >> ')'
        |   (char_('-') >> factor)
        |   (char_('+') >> factor)
        ;

    auto const calculator = x3::grammar(
            expression = expression_def,
            term = term_def,
            factor = factor_def
        );
}

using calculator_grammar::calculator;
Feb ’13 23

My proposal “Inside Spirit X3. Redesigning Boost.Spirit for C++11” has been accepted for presentation at the C++ Now! 2013 in Aspen Colorado. So after a 2 year hiatus, I’m back to Aspen.

Here’s the abstract:

Inside Spirit X3.Redesigning Boost.Spirit for C++11
Joel de Guzman, 2013

The hugely successful BoostCon ’07 ’08, ’09 and ’10 Spirit talks provided walk-through presentations and tutorials on how to use Spirit. This, time, I propose a presentation that will focus on the design and implementation of Spirit. But to add more to the thrill, I will present a major redesign of Spirit from the ground up, taking advantage of the new C++11 features. One important goal of this experimental version of Spirit (named X3) is to bring back the elegant simplicity of “Classic” Spirit, which was somehow lost with the complexity of Spirit-2 primarily due to the lack of important language features that are just starting to appear in C++ compilers. In this 90-minute presentation, I would like to get down and dirty with Modern C++11 code, and along the way, share my experience as well as expose some of C++11’s shortcomings and my wishes for C++1y.

Hope to see you there!

preload preload preload