我有一个定义以下规则的语法:
constantValue = qi::token(ID_FLOAT) | qi::token(ID_INTEGER);
postfixExpression = primaryExpression |
(postfixExpression >> qi::token(ID_OPENBRACKET) >> qi::token(ID_INTEGER) >> qi::token(ID_CLOSEBRACKET)) |
(postfixExpression >> qi::token(ID_DOT) >> qi::token(ID_IDENTIFIER));
primaryExpression = qi::token(ID_IDENTIFIER) |
constantValue |
(qi::token(ID_OPENPAREN) >> primaryExpression >> qi::token(ID_CLOSEPAREN));
ges = postfixExpression >> qi::eoi;
我希望它匹配以下字符串:
测试[1] testident.ident
且不应匹配
测试[1.2] testident.5
但它无法匹配前两个字符串。
词法分析器构造函数如下:
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)
("\\(", ID_OPENPAREN)
("\\)", ID_CLOSEPAREN)
("\\[", ID_OPENBRACKET)
("\\]", ID_CLOSEBRACKET)
("\\.", ID_DOT)
(punctuator, ID_PUNCTUATOR)
;
this->self("WS") = white_space;
}
为什么我没有得到上述字符串的匹配?
谢谢 托拜厄斯
答案 0 :(得分:2)
我找到了原因 - 我不得不重新说出这条规则:
postfixExpression = primaryExpression >> *((qi::token(ID_OPENBRACKET) >> qi::token(ID_INTEGER) >> qi::token(ID_CLOSEBRACKET)) | (qi::token(ID_DOT) >> qi::token(ID_IDENTIFIER)));
我不知道为什么有必要,但现在它似乎有效。