首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何从匹配的字符串中提取无符号值?

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

Stack Overflow用户
提问于 2013-02-06 23:45:03
回答 1查看 117关注 0票数 0

我需要写词法分析器有能力解析像x(t-1),u(t),u(t-4),a0,a1,...并且这些词位的属性应该是“无符号的”(例如,标记x(t-2)的属性值应该是2)。我可以通过正则表达式定义所有这些标记,但我不知道如何从匹配的字符串中提取属性值。

附注:此词法分析器将用于提气语法。

那么,有谁知道我怎么做的吗?

EN

回答 1

Stack Overflow用户

发布于 2013-02-22 18:30:26

代码语言:javascript
运行
复制
#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;
    }
};
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14732982

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档