为什么这个提升::精神::qi规则与输入不匹配
Why does this boost::spirit::qi rule not match the input?
我试图继续处理我之前的例子并扩展规则。我的问题是,使用 ID_IDENTIFIER 的规则不起作用 - 尽管我知道词法分析器正在工作(使用单元测试)。
下面是示例:
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/lex_lexertl.hpp>
namespace qi = boost::spirit::qi;
namespace lex = boost::spirit::lex;
enum LexerIDs { ID_IDENTIFIER, ID_WHITESPACE, ID_INTEGER, ID_FLOAT, ID_PUNCTUATOR };
template <typename Lexer>
struct custom_lexer : lex::lexer<Lexer>
{
custom_lexer()
: identifier ("[a-zA-Z_][a-zA-Z0-9_]*")
, white_space ("[ \t\n]+")
, integer_value ("[1-9][0-9]*")
, hex_value ("0[xX][0-9a-fA-F]+")
, float_value ("[0-9]*\.[0-9]+([eE][+-]?[0-9]+)?")
, float_value2 ("[0-9]+\.([eE][+-]?[0-9]+)?")
, punctuator ("\[|\]|\(|\)|\.|&>|\*\*|\*|\+|-|~|!|\/|%|<<|>>|<|>|<=|>=|==|!=|\^|&|\||\^\^|&&|\|\||\?|:|,")// [ ] ( ) . &> ** * + - ~ ! / % << >> < > <= >= == != ^ & | ^^ && || ? : ,
{
using boost::spirit::lex::_start;
using boost::spirit::lex::_end;
this->self.add
(identifier , ID_IDENTIFIER)
/*(white_space , ID_WHITESPACE)*/
(integer_value, ID_INTEGER)
(hex_value , ID_INTEGER)
(float_value , ID_FLOAT)
(float_value2 , ID_FLOAT)
(punctuator , ID_PUNCTUATOR);
this->self("WS") = white_space;
}
lex::token_def<std::string> identifier;
lex::token_def<lex::omit> white_space;
lex::token_def<int> integer_value;
lex::token_def<int> hex_value;
lex::token_def<double> float_value;
lex::token_def<double> float_value2;
lex::token_def<> punctuator;
};
template< typename Iterator, typename Skipper>
struct custom_grammar : qi::grammar<Iterator, Skipper>
{
template< typename TokenDef >
custom_grammar(const TokenDef& tok) : custom_grammar::base_type(ges)
{
ges = qi::token(ID_IDENTIFIER);
BOOST_SPIRIT_DEBUG_NODE(ges);
}
qi::rule<Iterator, Skipper > ges;
};
int main(int argc, _TCHAR* argv[])
{
std::string test("testidentifier");
typedef char const* Iterator;
typedef lex::lexertl::token<Iterator, lex::omit, boost::mpl::true_> token_type;
typedef lex::lexertl::lexer<token_type> lexer_type;
typedef qi::in_state_skipper<custom_lexer<lexer_type>::lexer_def> skipper_type;
typedef custom_lexer<lexer_type>::iterator_type iterator_type;
custom_lexer<lexer_type> my_lexer;
custom_grammar<iterator_type, skipper_type> my_grammar(my_lexer);
Iterator first = test.c_str();
Iterator last = &first[test.size()];
bool r = lex::tokenize_and_phrase_parse(first,last,my_lexer,my_grammar,qi::in_state( "WS" )[ my_lexer.self ]);
std::cout << std::boolalpha << r << "n";
std::cout << "Remaining unparsed: '" << std::string(first,last) << "'n";
return 0;
}
类似的规则与"1234"匹配ID_INTEGER
问题在于您在枚举中定义令牌 ID 的方式。如果这样做,ID_IDENTIFIER
得到的值为 0,这不是有效的 lex 令牌 ID。 幸运的是,Spirit.Lex 定义了一个值boost::spirit::lex::min_token_id
,您可以使用该值来确保获得有效的令牌。使用它,您的枚举将是:
enum LexerIDs { ID_IDENTIFIER=boost::spirit::lex::min_token_id+1, ID_WHITESPACE, ID_INTEGER, ID_FLOAT, ID_PUNCTUATOR };
相关文章:
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 2D数组来自文本输入,中间有空格
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 检查输入是否不是整数或数字
- 正在尝试了解输入验证循环
- 读取文件并输入到矢量中
- C++如何通过用户输入删除列表元素
- 用c++从输入文件中读取另一行
- 读取文件的最后一行并输入到链接列表时出错
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何使用用户输入在C++中正确填充2D数组
- 此代码是否违反一个定义规则
- C++MySQL C api用户输入行
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- 用户定义函数中的指针和输入
- 如何在C++中检查2D数组中负值的输入验证
- 生成文件不对文件使用隐式规则
- 如何只允许用户输入正整数
- Bison/flex 在识别规则后等待输入
- 为什么这个提升::精神::qi规则与输入不匹配