Menu
Home Explore People Places Arts History Plants & Animals Science Life & Culture Technology
On this page
Spirit Parser Framework

The Spirit Parser Framework is an object oriented recursive descent parser generator implemented using template metaprogramming in C++. It leverages expression templates to approximate the syntax of extended Backus–Naur form (EBNF), enabling users to compose parser objects via operator overloading. The resulting backtracking LL(∞) parser can handle ambiguous grammars, and Spirit supports both lexing and parsing. This framework is included in the Boost libraries, making it a powerful tool for modern C++ parsing needs.

We don't have any images related to Spirit Parser Framework yet.
We don't have any YouTube videos related to Spirit Parser Framework yet.
We don't have any PDF documents related to Spirit Parser Framework yet.
We don't have any Books related to Spirit Parser Framework yet.
We don't have any archived web articles related to Spirit Parser Framework yet.

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.

#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

  1. Guzman, Joel de; Kaiser, Hartmut. "Spirit 2.59 - Introduction". Boost.org. Boost. Retrieved 22 March 2025. https://www.boost.org/doc/libs/1_87_0/libs/spirit/doc/html/spirit/introduction.html