0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Boost.Qiとlexmeを調べてみた

Last updated at Posted at 2020-08-24

Boost.Qiにlexmeという記法(ディレクティブ)が使える。


qi::lexeme[ascii::digit >> ascii::digit], ascii::space);

ドキュメントを見てみる。

The lexeme directive makes its subject a primitive. In a logical point of view, lexemes (and primitives) are minimal atomic units (e.g. words, numbers, identifiers, etc). These are the things that you'd normally put in the lexer (hinting at the term "lexeme"), but in a lexer-less world, you put these in a lexeme. Seeing its subject as a primitive, the lexeme directive does an initial pre-skip (as all primitives do) and turns off white space skipping.

よくわからない。。

コードを見てみる。

 1#include <boost/spirit/include/qi.hpp>
 2#include <string>
 3#include <iostream>
 4
 5using namespace boost::spirit;
 6
 7int main()
 8{
 9  std::string str;
10  std::getline(std::cin, str);
11  auto it = str.begin();
12  bool match = qi::phrase_parse(it, str.end(),
13    qi::lexeme[ascii::digit >> ascii::digit], ascii::space);
14  std::cout << std::boolalpha << match << '\n';
15  if (it != str.end())
16    std::cout << std::string{it, str.end()} << '\n';
17}

どうやら、lexme[]で囲われたパターン(ascii::digit >> ascii::digit)に関しては、スペースをスキップしないということらしい。

the lexeme directive does an initial pre-skip (as all primitives do) and turns off white space skipping.

実行してみる。


# ./a.out 
111 11
true
1 11

始めの2文字が数字なため成功

# ./a.out 
11 11 11 
true
11 11 

始めの2文字が数字なので成功

# ./a.out 
1 1111 1
false
1 1111 1

2文字目にスペースがある(数字でない)ためfalseになる。

また、lexmeのパターンの前まではスペースをスキップする、ということになる。


# ./a.out 
 11 111
true
111
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?