Question

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

and it should not match

test[1.2] testident.5

but it fails to match the first 2 strings.

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top