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 semantic actionについて少し調べた(2)

Last updated at Posted at 2020-09-14

Boost Spiritは、parserの動作に細かいチューニングができるようになっているのが特徴である。

 1#include <iostream>
 2#include <string>
 3#include <boost/spirit.hpp>
 4using namespace std;
 5using namespace boost::spirit;
 6
 7void display( int x ) { cout << "you typed: " << x << endl; }
 8
 9struct IntDisplay : grammar<IntDisplay>
10{
11    template<typename ScannerT>
12      struct definition
13      {
14          typedef rule<ScannerT> rule_t;
15          rule_t r;
16          definition( const IntDisplay& )
17          {
18              r = int_p[&display] % ',';
19          }
20          const rule_t& start() const { return r; }
21      };
22};
23
24int main()
25{
26    IntDisplay parser;
27
28    string line;
29    while( cout<<"# ", getline(cin, line) )
30    {
31        parse_info<string::const_iterator> info =
32            parse( line.begin(), line.end(), no_actions_d[parser], space_p );
33        cout << (info.full ? "OK" : "fail") << endl;
34    }
35    return 0;
36}

32行目に、no_actions_dというのがあるが、これをディレクティブと呼ぶ。


    32            parse( line.begin(), line.end(), no_actions_d[parser], space_p );

実行してみる。。。


# g++ boost-semantic-action.cpp -lboost_system
# ./a.out 
# 1,2,3
OK

つまり、18行目とかでセマンティックアクションdisplayを設定してあるが、no_actions_dによってそれを有効にしたり無効にできる。


    18              r = int_p[&display] % ',';
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?