C++ 如何从匹配的字符串中提取无符号值?
我需要编写词法分析器,能够解析像x(t-1)、u(t)、u(t-4)、a0、a1等标记,。。。这个词素的属性应该是“unsigned”(例如,令牌x(t-2)的属性值应该是2)。我可以通过正则表达式定义所有这些标记,但我不知道如何从匹配的字符串中提取属性值 另外,这个词法将用于语法 那么,有人知道我如何做到这一点吗?C++ 如何从匹配的字符串中提取无符号值?,c++,boost,boost-spirit,C++,Boost,Boost Spirit,我需要编写词法分析器,能够解析像x(t-1)、u(t)、u(t-4)、a0、a1等标记,。。。这个词素的属性应该是“unsigned”(例如,令牌x(t-2)的属性值应该是2)。我可以通过正则表达式定义所有这些标记,但我不知道如何从匹配的字符串中提取属性值 另外,这个词法将用于语法 那么,有人知道我如何做到这一点吗?\35;定义BOOST\u SPIRIT\u USE\u PHOENIX\u V3 #define BOOST_SPIRIT_USE_PHOENIX_V3 #include <
\35;定义BOOST\u SPIRIT\u USE\u PHOENIX\u V3
#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;
}
};
#包括
#包括
#包括
#包括
#包括
...
名称空间qi=::boost::spirit::qi;
名称空间mpl=::boost::mpl;
名称空间lex=::boost::spirit::lex;
...
结构提取函数
{
模板结构结果
{
typedef无符号类型;
};
模板typename结果::类型运算符()(迭代器&开始、迭代器&结束)常量
{
::std::字符串n(开始,结束);
::boost::trim_if(n,!::boost::is_digit());
返回空
?0U
词法转换(n);
}
};
const::boost::phoenix::函数提取;
模板
结构DynamicExpressionExer:lex::lexer
{
lex::token_def OBJECT_用法;
...
lex::token_def WS;
DynamicExpressionExer():
对象的用法(“x\\*\(\\*t\\*-\\*[0-9]+\\\*\\)”,
...
WS(“[\\t]+”)
{
这个->自我
=OBJECT\u用法[lex::\u val=EXTRACT(lex::\u start,lex:\u end)]
| ...;
这->自我(“WS”)=WS;
}
};
是。有人知道怎么做。如果你能稍微含糊一点,我们甚至可以教你怎么做。正如您所知,这不是一个代码编写站点。也许一个新的计划会有所帮助。也是在这里使用莱克斯的一个很好的理由。