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

Click here to view the latest version of this page.
PrevUpHomeNext
Kleene Star Generator (*a)
Description

Kleene star generators are used to repeat the execution of an embedded generator zero or more times. Regardless of the success of the embedded generator, the Kleene star generator always succeeds.

Header
// forwards to <boost/spirit/home/karma/operator/kleene.hpp>
#include <boost/spirit/include/karma_kleene.hpp>

Also, see Include Structure.

Model of

UnaryGenerator

Expression Semantics

Semantics of an expression is defined only where it differs from, or is not defined in UnaryGenerator.

Expression

Semantics

*a

The generator a is executed zero or more times depending on the availability of an attribute. The execution of a stops after the attribute values passed to the Kleene star generator are exhausted. The Kleene star always succeeds (unless the underlying output stream reports an error).

[Note] Note

All failing iterations of the embedded generator will consume one element from the supplied attribute.

Attributes

See Compound Attribute Notation.

Expression

Attribute

*a (Kleene star, unary *)

a: A --> *a: vector<A>
a: Unused --> *a: Unused

[Important] Important

The table above uses vector<A> as a placeholder only. The notation of vector<A> stands for any STL container holding elements of type A.

The Kleene star generator will execute its embedded generator once for each element in the provided container attribute as long as the embedded generator succeeds. On each iteration it will pass the next consecutive element from the container attribute to the embedded generator. Therefore the number of iterations will not be larger than the number of elements in the container passed as its attribute. An empty container will make the Kleene star generate no output at all.

It is important to note, that the Kleene star does not perform any buffering of the output generated by its embedded elements. That means that any failing element generator might have already generated some output, which is not rolled back.

[Tip] Tip

The simplest way to force a Kleene star to behave as if it did buffering is to wrap it into a buffering directive (see buffer):

buffer[*a]

which will not generate any output in case of a failing generator *a. The expression:

*(buffer[a])

will not generate any partial output from a generator a if it fails generating in the middle of its output. The overall expression will still generate the output as produced by all successful invocations of the generator a.

Complexity

The overall complexity of the Kleene star generator is defined by the complexity of its embedded generator multiplied by the number of executed iterations. The complexity of the Kleene star itself is O(N), where N is the number of elements in the container passed as its attribute.

Example
[Note] Note

The test harness for the example(s) below is presented in the Basics Examples section.

Some includes:

#include <boost/spirit/include/karma.hpp>
#include <boost/spirit/include/support_utree.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/fusion/include/std_pair.hpp>
#include <iostream>
#include <string>

Some using declarations:

using boost::spirit::karma::double_;
using boost::spirit::karma::space;

Basic usage of a Kleene star generator:

std::vector<double> v;
v.push_back(1.0);
v.push_back(2.0);
v.push_back(3.0);
test_generator_attr_delim("1.0 2.0 3.0 ", *double_, space, v);


PrevUpHomeNext