C++ boost精神解析标识符

C++ boost精神解析标识符,c++,boost,boost-spirit,boost-spirit-qi,C++,Boost,Boost Spirit,Boost Spirit Qi,我想创建一个解析器来解析以alpha或u开头的标识符,它的主体中可能有alpha、num或u 这就是我到目前为止所做的: #include <boost/config/warning_disable.hpp> #include <boost/spirit/include/qi.hpp> #include <boost/spirit/include/phoenix.hpp> #include <boost/spirit/home/support/itera

我想创建一个解析器来解析以alpha或u开头的标识符,它的主体中可能有alpha、num或u

这就是我到目前为止所做的:

#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;
};
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间boost::spirit;
#包括
////////////////////////////////
//额外设施
结构获取线
{
模板结构结果{typedef size\u t type;};
模板大小运算符()
{
返回获取行(位置);
}
};
结构位置
{
职位(
:行(-1)
{
}
尺寸线;
};
结构标识符:公共位置
{
标识符()
:职位()
,姓名()
{
}
std::字符串名;
};
BOOST\u FUSION\u ADAPT\u结构(标识符,
(std::字符串,名称)
(尺寸,线条)
)
//
////////////////////////////////
模板
结构源_标识符:qi::grammar
{
source\u identifier():source\u identifier::base\u type(开始)
{
使用qi::alpha;
使用qi::alnum;
用气:生的;
使用qi:(u val);
使用气::_1;
名称空间phx=boost::phoenix;
使用phx::at_c;
使用phx::begin;
名称%=(qi::alpha |“124;”)
>>*(qi::alnum |“”);
开始=原始[名称[在c(\u val)=\u 1]]
[
at_c(_val)=获取_行(开始(_1))
]
;
}
boost::phoenix::函数get\u line;
qi::规则开始;
qi::规则名称;
};
为什么这会返回空名称如果标识符中有''.'',它会在没有'.''的情况下工作。

“'.
qi::lit(“'.'
)相同,两者都匹配字符
'.
,但不合成任何属性。您需要的是
qi::char(“''')
。你可以找到更多信息