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++.[1] 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

edit

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.

syntax explanation
x >> y Match x followed by y.
x > y After matching x, expect y.
*x Match x repeated zero or more times. This represents the Kleene star; C++ lacks an unary postfix operator *.
x | y Match x. If x does not match, try to match y.
+x Match a series of one or more occurrences of x.
-x Match x zero or one time.
x & y Match x and y.
x - y Match x but not y.
x ^ y Match x, or y, or both, in any order.
x || y Match 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 % y Match one or more occurrences of x, separated by occurrences of y.
~x Match anything but x (only with character classes such as ch_p or alnum_p)

Example

edit

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

#include <string>
#include <iostream>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
 
int main()
{
  namespace qi = boost::spirit::qi;

  std::string input;
 
  std::cout << "Input a line: \n";
  getline(std::cin, input);
  std::cout << "Got '" << input << "'.\n";
 
  unsigned count = 0;
  /*  
      Next, parse the input (input.c_str()),
      using a parser constructed with the following semantics:
 
      Zero or more occurrences of (
          literal string "cat" (when matched, increment the counter "count")
      or  any character (which will be skipped)
      )

     The parser is constructed by the compiler using operator overloading and
     template matching, so the actual work is done within qi::parse(), and the
     expression starting with * only initializes the rule object that the parse
     function uses.

  */
  auto rule = *(qi::lit("cat") [ ++qi::_val ] | qi::omit[qi::char_]);
  qi::parse(input.begin(), input.end(), rule, count);
  

  // Finally, show results.
  std::cout << "The input contained " << count << " occurrences of 'cat'\n";
}

References

edit
  1. ^ Guzman, Joel de; Kaiser, Hartmut. "Spirit 2.59 - Introduction". Boost.org. Boost. Retrieved 22 March 2025.
edit


📚 Artikel Terkait di Wikipedia

Recursive descent parser

In computer science, a recursive descent parser is a kind of top-down parser built from a set of mutually recursive procedures (or a non-recursive equivalent)

Parsing

LR parser LALR (look-ahead LR) parser Operator-precedence parser Simple LR parser Simple precedence parser Packrat parser: a linear time parsing algorithm

Spirit

The Spirit (statue), a statue of Michael Jordan outside Chicago's United Center Spirit Parser Framework, an object-oriented parser-generator framework Spirit

Extended Backus–Naur form

direct equivalent of EBNF in natural languages Regular expression Spirit Parser Framework Scowen, Roger S. (1993). Extended BNF — A generic base standard

Comparison of parser generators

notes. Retrieved 2026-06-14. "The Lemon Parser Generator". sqlite.org. Retrieved 2023-11-30. "The Lezer Parser System". "Building a ShopifyQL Code Editor"

Expression templates

of vector math, the Spirit parser framework uses expression templates to represent formal grammars and compile these into parsers. Optimizing compiler –

Scrapy

that are given a set of instructions. Following the spirit of other don't repeat yourself frameworks, such as Django, it makes it easier to build and scale

List of unit testing frameworks

This is a list of notable test automation frameworks commonly used for unit testing. Such frameworks are not limited to unit-level testing; they can be