我正在努力编写一个标识符解析器,它解析一个不是关键字的字母数字字符串。关键字都在一个表中:
struct keywords_t : x3::symbols<x3::unused_type> {
keywords_t() {
add("for", x3::unused)
("in", x3::unused)
("while", x3::unused);
}
} const keywords;
标识符的解析器应该是这样的:
auto const identifier_def =
x3::lexeme[
(x3::alpha | '_') >> *(x3::alnum | '_')
];
现在我尝试将这些组合起来,以便标识符解析器无法解析关键字。我试过这样:
auto const identifier_def =
x3::lexeme[
(x3::alpha | '_') >> *(x3::alnum | '_')
]-keywords;
和这个:
auto const identifier_def =
x3::lexeme[
(x3::alpha | '_') >> *(x3::alnum | '_') - keywords
];
它适用于大多数输入,但是如果字符串以关键字开头,例如int, whilefoo, forbar
解析器,则无法解析此字符串。我怎样才能让这个解析器正确?