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.Spiritのconfix_pパーサーについて少し調べた

Posted at

Boost.Spiritにはきめの細かいparserがいろいろある。。

confix_pというパーサーがある。


 open >> (expr - close) >> close

    16          definition( const RegExpLiteral& ) {                                                                                                                                         
    17              using phoenix::arg1;                                                                                                                                                     
    18              top  = expr;                                                                                                                                                             
    19              expr = confix_p( '/', *anychar_p, '/' );                                                                                                                                 
    20          }                    

confix_pは3つの引数を取る。


    19              expr = confix_p( '/', *anychar_p, '/' );    

↑は、"/" があり、任意の文字の連続があり、"/"があるという意味。
コードを見てみる。

 1#include <iostream>                                                                                                                                                                    
 2#include <string>                                                                                                                                                                      
 3#include <boost/spirit.hpp>                                                                                                                                                            
 4using namespace std;                                                                                                                                                                   
 5using namespace boost::spirit;                                                                                                                                                         
 6                                                                                                                                                                                       
 7struct RegExpLiteral : grammar<RegExpLiteral>                                                                                                                                          
 8{                                                                                                                                                                                      
 9    struct Bracket : closure<Bracket,char> { member1 br; };                                                                                                                            
10                                                                                                                                                                                       
11    template<typename ScannerT>                                                                                                                                                        
12      struct definition                                                                                                                                                                
13      {                                                                                                                                                                                
14          rule<ScannerT> top;                                                                                                                                                          
15          rule<ScannerT,Bracket::context_t> expr;                                                                                                                                      
16          definition( const RegExpLiteral& ) {                                                                                                                                         
17              using phoenix::arg1;                                                                                                                                                     
18              top  = expr;                                                                                                                                                             
19              expr = confix_p( '/', *anychar_p, '/' );                                                                                                                                 
20          }                                                                                                                                                                            
21          const rule<ScannerT>& start() const  { return top; }                                                                                                                         
22      };                                                                                                                                                                               
23};                                                                                                                                                                                     
24                                                                                                                                                                                       
25int main()                                                                                                                                                                             
26{                                                                                                                                                                                      
27    RegExpLiteral parser;                                                                                                                                                              
28                                                                                                                                                                                       
29    string line;                                                                                                                                                                       
30    while( cout<<"# ", getline(cin, line) )                                                                                                                                            
31    {                                                                                                                                                                                  
32        parse_info<string::const_iterator> info =                                                                                                                                      
33            parse( line.begin(), line.end(), parser );                                                                                                                                 
34        cout << (info.full ? "parsed." : "failed.") << endl;                                                                                                                           
35    }                                                                                                                                                                                  
36    return 0;                                                                                                                                                                          
37}                                     

実行してみる。。。


# ./a.out 
# /hoge
failed.
# /hoge/
parsed.
# /http
failed.
# /http/
parsed.
# htt
failed.
# /http//
failed.
# /http:/
parsed.
# /http://
failed.

(`ー´)b

Confix Parsers recognize a sequence out of three independent elements: an opening, an expression and a closing.

Confixパーサーは、3つの要素からなる:opening, express, closing.
C言語のコメント認識にも使われる。


    rule<> c_comment_rule
        =   confix_p("/*", *anychar_p, "*/")
        ;
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?