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

时间:2013-02-06 15:45:03

标签: c++ boost boost-spirit

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

P.S。这个词法分析器将用于提升精神qi语法。

那么,是否有人知道如何做到这一点?

1 个答案:

答案 0 :(得分:1)

#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;
    }
};