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] % ',';