...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
The grammar encapsulates a set of rules
(as well as primitive parsers (PrimitiveParser
) and sub-grammars).
The grammar is the main mechanism for modularization and composition.
Grammars can be composed to form more complex grammars.
// forwards to <boost/spirit/home/qi/nonterminal/grammar.hpp> #include <boost/spirit/include/qi_grammar.hpp>
Also, see Include Structure.
Name |
---|
|
template <typename Iterator, typename A1, typename A2, typename A3> struct grammar;
Parameter |
Description |
Default |
---|---|---|
|
The underlying iterator type that the rule is expected to work on. |
none |
|
Either |
See table below. |
Here is more information about the template parameters:
Parameter |
Description |
Default |
---|---|---|
|
Specifies the grammar's synthesized (return value) and inherited
attributes (arguments). More on this here: |
|
|
Specifies the grammar's skipper parser. Specify this if you want the grammar to skip white spaces. |
|
|
Specifies the grammar's local variables. See |
|
Notation
g
A grammar
Semantics of an expression is defined only where it differs from, or
is not defined in Nonterminal
.
Expression |
Semantics |
---|---|
template <typename Iterator> struct my_grammar : grammar<Iterator, A1, A2, A3> { my_grammar() : my_grammar::base_type(start, name) { // Rule definitions start = /* ... */; } rule<Iterator, A1, A2, A3> start; // more rule declarations... };
|
Grammar definition. |
Note | |
---|---|
The template parameters of a grammar and its start rule (the rule passed to the grammar's base class constructor) must match, otherwise you will see compilation errors. |
The parser attribute of the grammar is
T
, its synthesized attribute. SeeAttribute
The complexity is defined by the complexity of the its definition.
Note | |
---|---|
The test harness for the example(s) below is presented in the Basics Examples section. |
Some using declarations:
using boost::spirit::ascii::space_type; using boost::spirit::int_; using boost::spirit::qi::grammar; using boost::spirit::qi::rule;
Basic grammar usage:
struct num_list : grammar<char const*, space_type> { num_list() : base_type(start) { using boost::spirit::int_; num = int_; start = num >> *(',' >> num); } rule<char const*, space_type> start, num; };
How to use the example grammar:
num_list nlist; test_phrase_parser("123, 456, 789", nlist);