...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 generators (PrimitiveGenerator
) 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/karma/nonterminal/grammar.hpp> #include <boost/spirit/include/karma_grammar.hpp>
Also, see Include Structure.
Name |
---|
|
template <typename OutputIterator, typename A1, typename A2, typename A3> struct grammar;
Parameter |
Description |
Default |
---|---|---|
|
The underlying output 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 delimiter generator. Specify this if you want the grammar to delimit the generated output. |
|
|
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 OutputIterator> struct my_grammar : grammar<OutputIterator, A1, A2, A3> { my_grammar() : my_grammar::base_type(start, name) { // Rule definitions start = /* ... */; } rule<OutputIterator, 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 generator attribute of the grammar is
RT
, its consumed 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::ascii::space; using boost::spirit::int_; using boost::spirit::karma::grammar; using boost::spirit::karma::rule;
Basic grammar usage:
struct num_list : grammar<output_iterator_type, space_type, std::vector<int>()> { num_list() : base_type(start) { using boost::spirit::int_; num = int_; start = num << *(',' << num); } rule<output_iterator_type, space_type, std::vector<int>()> start; rule<output_iterator_type, space_type, int()> num; };
How to use the example grammar:
num_list nlist; std::vector<int> v; v.push_back(123); v.push_back(456); v.push_back(789); test_generator_attr_delim("123 , 456 , 789", nlist, space, v);