Ann: Spirit V1.2 C++ Inline Parser Framework

"joel de guzman" <>
20 Oct 2001 22:09:17 -0400

          From comp.compilers

Related articles
Ann: Spirit V1.2 C++ Inline Parser Framework (joel de guzman) (2001-10-20)
| List of all articles for this month |

From: "joel de guzman" <>
Newsgroups: comp.compilers
Date: 20 Oct 2001 22:09:17 -0400
Organization: Compilers Central
Keywords: parse, tools, OOP
Posted-Date: 20 Oct 2001 22:09:17 EDT

    The Spirit Parser Framework
        [ ]

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++. Parser objects are composed through operator
overloading and the result is a backtracking LL(inf) parser that is capable
of parsing rather ambiguous grammars.

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.

A simple EBNF grammar snippet:

        group ::= '(' expr ')'
        expr1 ::= integer | group
        expr2 ::= expr1 (('*' expr1) | ('/' expr1))*
        expr ::= expr2 (('+' expr2) | ('-' expr2))*

is approximated using Spirit's facilities as seen in this code snippet:

        group = '(' >> expr >> ')';
        expr1 = integer | group;
        expr2 = expr1 >> *(('*' >> expr1) | ('/' >> expr1));
        expr = expr2 >> *(('+' >> expr2) | ('-' >> expr2));

Through the magic of expression templates, this is perfectly valid and
executable C++ code. The production rule expr is in fact an object that has a
member function parse that does the work given a source code written in the
grammar that we have just declared.

The latest version of Spirit is v1.2. This version adds a TON of cool new
features such as (8/16/32) bit character sets and symbol tables, an STL-like
iterator interface, more examples (C, Pascal and XML parsers), debugging output,
numeric parsers, run-time parametric parsers and much more.

Version 1.2 of Spirit is 100% ISO/ANSI C++ compliant and has been tested on the
following compilers:

  * Borland C++ V5.5.1
  * GCC 3.0 / 2.95.2
  * Intel Compiler 5.01
  * Comeau 4.2.45.

Spirit is open source using the zlib/libpng license that permits use for any
purpose, including commercial applications. The complete package is available
for free download from

There is a Spirit users mailing list at source forge:

Joel de Guzman

Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.