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
Not-Predicate (!a)
Description

Syntactic predicates assert a certain conditional syntax to be satisfied before evaluating another production. Similar to semantic predicates, eps, syntactic predicates do not consume any input. The not-predicate, !a, is a negative syntactic predicate that returns a zero length match only if its predicate fails to match.

Header
// forwards to <boost/spirit/home/qi/operator/not_predicate.hpp>
#include <boost/spirit/include/qi_not_predicate.hpp>

Also, see Include Structure.

Model of

UnaryParser

Notation

a

A Parser

Expression Semantics

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

Expression

Semantics

!a

If the predicate a matches, fail. Otherwise, return a zero length match.

Attributes

See Compound Attribute Notation.

Expression

Attribute

!a

unused_type

Complexity

The complexity is defined by the complexity of the predicate, a

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::char_;
using boost::spirit::ascii::alpha;
using boost::spirit::qi::lit;
using boost::spirit::qi::symbols;

Here's an alternative to the *(r - x) >> x idiom using the not-predicate instead. This parses a list of characters terminated by a ';':

test_parser("abcdef;", *(!lit(';') >> char_) >> ';');

The following parser ensures that we match distinct keywords (stored in a symbol table). To do this, we make sure that the keyword does not follow an alpha or an underscore:

symbols<char, int> keywords;
keywords = "begin", "end", "for";

// This should fail:
test_parser("beginner", keywords >> !(alpha | '_'));

// This is ok:
test_parser("end ", keywords >> !(alpha | '_'), false);

// This is ok:
test_parser("for()", keywords >> !(alpha | '_'), false);


PrevUpHomeNext