2013-10-03 48 views
2

我想创建解析器将解析与阿尔法或启动_标识符,它可能有α,NUM或_体内提振精神解析标识符

这是我到目前为止有:

#include <boost/config/warning_disable.hpp> 
#include <boost/spirit/include/qi.hpp> 
#include <boost/spirit/include/phoenix.hpp> 
#include <boost/spirit/home/support/iterators/line_pos_iterator.hpp> 
#include <boost/spirit/repository/include/qi_confix.hpp> 
#include <boost/spirit/include/phoenix_fusion.hpp> 
#include <boost/spirit/include/phoenix_stl.hpp> 

using namespace boost::spirit; 

#include <boost/fusion/include/adapt_struct.hpp> 

//////////////////////////////// 
// extra facilities 
struct get_line_f 
{ 
    template <typename> struct result { typedef size_t type; }; 
    template <typename It> size_t operator()(It const& pos_iter) const 
    { 
     return get_line(pos_iter); 
    } 
}; 

struct Position 
{ 
    Position() 
     : line(-1) 
    { 
    } 

    size_t line; 
}; 

struct Identifier : public Position 
{ 
    Identifier() 
     : Position() 
     , name() 
    { 
    } 

    std::string name; 
}; 

BOOST_FUSION_ADAPT_STRUCT(Identifier, 
          (std::string, name) 
          (size_t,  line) 
         ) 

// 
//////////////////////////////// 

template <typename Iterator> 
struct source_identifier: qi::grammar<Iterator, Identifier(), qi::space_type> 
{ 
    source_identifier() : source_identifier::base_type(start) 
    { 
     using qi::alpha; 
     using qi::alnum; 
     using qi::raw; 
     using qi::_val; 
     using qi::_1; 

     namespace phx = boost::phoenix; 
     using phx::at_c; 
     using phx::begin; 

     name %=  (qi::alpha | "_") 
       >> *(qi::alnum | "_"); 

     start = raw [ name[at_c<0>(_val) = _1] ] 
        [ 
         at_c<1>(_val) = get_line_(begin(_1)) 
        ] 
     ; 
    } 

    boost::phoenix::function<get_line_f> get_line_; 
    qi::rule<Iterator, Identifier(), qi::space_type> start; 
    qi::rule<Iterator, std::string()> name; 
}; 

为什么如果“_”的标识符这将返回一个空的名字,它的工作原理的情况下,没有

回答

5

"_"相同qi::lit("_"),既符合人物'_'但不合成任何属性。你想要的是qi::char_('_')。你可以找到更多的信息here