Spirit Parser Framework Explained

The Spirit Parser Framework is an object oriented recursive descent parser generator framework implemented using template metaprogramming techniques. Expression templates allow users to approximate the syntax of extended Backus–Naur form (EBNF) completely in C++. Parser objects are composed through operator overloading and the result is a backtracking LL(∞) parser that is capable of parsing rather ambiguous grammars.

Spirit can be used for both lexing and parsing, together or separately.

This framework is part of the Boost libraries.

Operators

Because of limitations of the C++ language, the syntax of Spirit has been designed around the operator precedences of C++, while bearing resemblance to both EBNF and regular expressions.

syntaxexplanation
x >> yMatch x followed by y.
x > yAfter matching x, expect y.
*xMatch x repeated zero or more times. This represents the Kleene star; C++ lacks an unary postfix operator *.
x | yMatch x. If x does not match, try to match y.
+xMatch a series of one or more occurrences of x.
-xMatch x zero or one time.
x & yMatch x and y.
x - yMatch x but not y.
x ^ yMatch x, or y, or both, in any order.
x || yMatch x, or y, or x followed by y.
x [ ''function_expression'' ]Execute the function/functor returned by function_expression, if x matched.
(x)Match x (can be used for priority grouping)
x % yMatch one or more occurrences of x, separated by occurrences of y.
~xMatch anything but x (only with character classes such as ch_p or alnum_p)

Example

This example shows how to use an inline parser expression with a semantic action.

  1. include
  2. include
  3. include
  4. include

int main

External links