Boost C++ Libraries 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.

Qi Keyword Parser Directive


The kwd[], dkwd[] and ikwd[], idkwd[] provide a powerful and flexible mechanism for parsing keyword based input. It works in conjunction with the / operator to create an effective keyword parsing loop. The keyword parsing loop doesn't require the keywords to appear in a defined order in the input but also provides the possibility to check how many times a keyword appears in the input.

The kwd directive will parse the keywords respecting case sensitivity whereas the ikwd direcive is case insensitive. You can mix the kwd and ikwd directives inside a set of keywords, but be aware that this has a small overhead. It should be preferred not to mix the kwd and ikwd directives.

The dkwd and idkwd provide a mechanism to pase distinct keywords. These directives require that the skipper successeds parsing input right after the keyword part.


is equivalent to:

lit("keyword1") >> skipper+ >> '=' >> int_

All the keyword directives can be mixed inside a keyword list.

The kwd directive is very similar to the repeat directive in that it enables to enforce keyword occurrence constraints but also provides very interesting speed improvement over the pure EBNF syntax or the Nabialek-Trick.

// forwards to <boost/spirit/repository/home/qi/directive/kwd.hpp>
#include <boost/spirit/repository/include/qi_kwd.hpp>




Parse ( "keyword" > subject) zero or more times.


Parse ( "keyword" > subject) exactly n times.

kwd(keyword,min, max)[subject]

Parse ( "keyword" > subject) at least min times and at most max times.

kwd(keyword,min, inf)[subject]

Parse ( "keyword" > subject) at least min or more.

For non case sensitive keywords use the ikwd directive. If distinct keyword parsing is required, use the dkwd and idkwd directive instead.





The parser for the opening (the prefix).


The parser for the input sequence following the keyword part.


Int representing the exact number of times the keyword must be repeated.


Int representing the minimum number of times the keyword must be repeated.


Int representing the maximum number of times the keyword must be repeated.

The keyword as well as the subject parameters can be any valid spirit parser. The parameter n, min and max are integer constants.





a: A --> kwd(k1)[a]: optional<A> or vector<A>
a: Unused --> kwd(k1)[a]: Unused


a: A --> kwd(k1,n)[a]: optional<A> or vector<A>
a: Unused --> kwd(k1,n)[a]: Unused

kwd(k1,min, max)[a]

a: A --> kwd(k1,min, max)[a]: optional<A> or vector<A>
a: Unused --> kwd(k1,min, max)[a]: Unused

kwd(k1,min, inf)[a]

a: A --> kwd(k1,min, inf)[a]: optional<A> or vector<A>
a: Unused --> kwd(k1,min, inf)[a]: Unused


The overall complexity is defined by the complexity of its subject parser. The complexity of the keyword list construct kwd itself is O(N), where N is the number of repetitions executed.

In the case where all the keywords are strings, the complexity of the keyword list itself determined by the complexity of the internal TST contents :

O(log n+k)

Where k is the length of the string to be searched in a TST with n strings.

When the keywords used are complex parsers, then the complexity is the sum of the sub parser complexities.


Please refer to keyword_list.