Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for an old version of Boost. Click here to view this page for the latest version.
PrevUpHomeNext
Parser Grammar
Description

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.

Header
// forwards to <boost/spirit/home/qi/nonterminal/grammar.hpp>
#include <boost/spirit/include/qi_grammar.hpp>

Also, see Include Structure.

Namespace

Name

boost::spirit::qi::grammar

Synopsis
template <typename Iterator, typename A1, typename A2, typename A3>
struct grammar;
Template parameters

Parameter

Description

Default

Iterator

The underlying iterator type that the rule is expected to work on.

none

A1, A2, A3

Either Signature, Skipper or Locals in any order. See table below.

See table below.

Here is more information about the template parameters:

Parameter

Description

Default

Signature

Specifies the grammar's synthesized (return value) and inherited attributes (arguments). More on this here: Nonterminal.

unused_type. When Signature defaults to unused_type, the effect is the same as specifying a signature of void() which is also equivalent to unused_type()

Skipper

Specifies the grammar's skipper parser. Specify this if you want the grammar to skip white spaces.

unused_type

Locals

Specifies the grammar's local variables. See Nonterminal.

unused_type

Model of

Nonterminal

Notation

g

A grammar

Expression Semantics

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. name is an optional string that gives the grammar its name, useful for debugging and error handling.

[Note] 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.

Attributes

The parser attribute of the grammar is T, its synthesized attribute. See Attribute

Complexity

The complexity is defined by the complexity of the its definition.

Example
[Note] 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);


PrevUpHomeNext