如何从匹配的字符串中提取无符号值



我需要编写一个词法分析器,能够解析x(t-1)、u(t)、u(t-4)、a0、a1,。。。并且该lexeme的属性应该是"unsigned"(例如,令牌x(t-2)的属性值应该是2)。我可以通过正则表达式定义所有这些标记,但我不知道如何从匹配的字符串中提取属性值。

附言:这个词将用于boost spirit qi语法。

那个么,有人知道我该怎么做吗?

#define BOOST_SPIRIT_USE_PHOENIX_V3
#include <boost/phoenix.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/lex_lexertl.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
...
namespace qi = ::boost::spirit::qi;
namespace mpl = ::boost::mpl;
namespace lex = ::boost::spirit::lex;
...
struct extract_func
{
    template <typename Iterator> struct result
    {
        typedef unsigned type;
    };
    template <typename Iterator> typename result<Iterator>::type operator()(Iterator& begin, Iterator& end) const
    {
        ::std::string n(begin, end);
        ::boost::trim_if(n, !::boost::is_digit());
        return n.empty()
                ? 0U
                : ::boost::lexical_cast<unsigned>(n);
    }
};
const ::boost::phoenix::function<extract_func> EXTRACT;
template <typename L>
struct DynamicExpressionLexer : lex::lexer<L>
{
    lex::token_def<unsigned> OBJECT_USAGE;
    ...
    lex::token_def<lex::omit> WS;
    DynamicExpressionLexer() :
        OBJECT_USAGE("x\ *\(\ *t\ *-\ *[0-9]+\ *\)"),
        ...
        WS("[ \t]+")
    {
        this->self
                = OBJECT_USAGE[lex::_val = EXTRACT(lex::_start, lex::_end)]
                | ...;
        this->self("WS") = WS;
    }
};

最新更新