【发布时间】:2012-06-19 18:04:31
【问题描述】:
对不起,如果这是一个新手问题,但我需要知道哪个令牌定义产生了某个令牌。当我打印令牌 ID 时,我只得到一个整数。我需要知道哪个正则表达式生成了这个令牌。
编辑:
我是这样定义我的令牌的:
template <typename LexerT>
class Tokens: public lex::lexer<LexerT>
{
public:
Tokens(const std::string& input):
lineNo_(1)
{
using boost::spirit::lex::_start;
using boost::spirit::lex::_end;
using boost::spirit::lex::_pass;
using boost::phoenix::ref;
using boost::phoenix::construct;
// macros
this->self.add_pattern
("EXP", "(e|E)(\\+|-)?\\d+")
("SUFFIX", "[yzafpnumkKMGTPEZY]")
("INTEGER", "-?\\d+")
("FLOAT", "-?(((\\d+)|(\\d*\\.\\d+)|(\\d+\\.\\d*))({EXP}|{SUFFIX})?)")
("SYMBOL", "[a-zA-Z_?@](\\w|\\?|@)*")
("STRING", "\\\"([^\\\"]|\\\\\\\")*\\\"");
// whitespaces and comments
whitespaces_ = "\\s+";
comments_ = "(;[^\\n]*\\n)|(\\/\\*[^*]*\\*+([^/*][^*]*\\*+)*\\/)";
// literals
integer_ = "{INTEGER}";
float_ = "{FLOAT}";
symbol_ = "{SYMBOL}";
string_ = "{STRING}";
// operators
quote_ = "'";
backquote_ = '`';
// ... other tokens
// whitespace and comment rules
this->self += whitespaces_ [ref(lineNo_) += count(_start, _end, '\n'), _pass = lex::pass_flags::pass_ignore];
this->self += comments_ [ref(lineNo_) += count(_start, _end, '\n'), _pass = lex::pass_flags::pass_ignore];
// literal rules
this->self += integer_ | float_ | string_ | symbol_;
// this->self += ... other tokens
}
~Tokens() {}
size_t lineNo() { return lineNo_; }
private:
// ignored tokens
lex::token_def<lex::omit> whitespaces_, comments_;
// literal tokens
lex::token_def<int> integer_;
lex::token_def<std::string> float_, symbol_, string_;
// operator tokens
lex::token_def<> quote_, backquote_;
// ... other token definitions of type lex::token_def<>
// current line number
size_t lineNo_;
};
谢谢, 海瑟姆
【问题讨论】:
-
只需查找声明令牌的枚举。或者启动 GDB,在需要令牌的函数中设置断点,然后通过 GDB 打印令牌;它将打印其正确的枚举名称。
-
嗨 H2CO3,我没有使用枚举来定义我的令牌。我的大部分标记都保存在模板参数默认值上(即 token_def)。我的 Spirit 版本中的默认 Idtype 是 std::size_t。您的意思是在我的令牌定义中更改 Idtype 以使用一些枚举而不是默认值?
-
如果不使用枚举,它们是如何定义的?您应该在问题中显示一些代码。
-
我为令牌定义添加了代码。
-
谢谢。我怀疑
token ID= 模式索引;所以 0 = EXP,2 = 整数等。
标签: c++ boost boost-spirit boost-spirit-lex