[英]How extract unsigned value from matched string?
I need to write lexical analyzer with ability to parse tokens like x(t-1), u(t), u(t-4), a0, a1,... and attributes of this lexemes should be "unsigned" (as example attribute value for token x(t-2) should be 2). 我需要编写具有语法分析功能的词法分析器,例如x(t-1),u(t),u(t-4),a0,a1 ...,并且此词素的属性应为“无符号”(如令牌x(t-2)的示例属性值应为2)。 I can define all this tokens via regular expressions, but i don't know how i can extract attribute value from matched string.
我可以通过正则表达式定义所有这些标记,但是我不知道如何从匹配的字符串中提取属性值。
PS This lexer will be used in boost spirit qi grammar. PS此词法分析器将用于增强气韵语法。
So, does anybody know way how i can do this? 那么,有人知道我该怎么做吗?
#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;
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.