简体   繁体   English

为什么这个 boost::spirit::qi 规则不起作用?

[英]why does this boost::spirit::qi rule not work?

I have a grammar that defines the following rules:我有一个定义以下规则的语法:

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;

and I want it to match the following strings:我希望它匹配以下字符串:

test[1] testident.ident测试[1] testident.ident

and it should not match它不应该匹配

test[1.2] testident.5测试[1.2] testident.5

but it fails to match the first 2 strings.但它无法匹配前 2 个字符串。

The lexer constructor is as follows:词法分析器构造函数如下:

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;
}

Why don't I get a match for the mentioned strings?为什么我没有得到提到的字符串的匹配项?

Thank you Tobias谢谢托比亚斯

I found the reason - I had to re-phrase the rule:我找到了原因——我不得不重新表述规则:

postfixExpression = primaryExpression >> *((qi::token(ID_OPENBRACKET) >> qi::token(ID_INTEGER) >> qi::token(ID_CLOSEBRACKET)) | (qi::token(ID_DOT) >> qi::token(ID_IDENTIFIER)));

I don't know why it's necessary, but now it seems to work.我不知道为什么有必要,但现在它似乎起作用了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM