Search code examples
c++boost-spirit-qi

check doubled symbols with spirit::qi


I need to check text for doubled symbols. For example "1+1*2" should be ok, but "1**2+3" or "--1+4*3" should not. Consider part of spirit calc example.

     expression =
            term[_val=_1]
            >> *(   ('+' >> term[_val+=_1])
                    |   ('-' >> term[_val-=_1])
                );

      term =
            factor[_val=_1]
            >> *(   ('*' >> factor[_val*=_1])
                |   ('/' >> factor[_val/=_1])
                );

      factor =
            double_[_val=_1]
            |   '(' >> expression[_val=_1] >> ')'
            |   ('-' >> factor[_val=_1])
            |   ('+' >> factor[_val=_1]);

phrase_parse returns true with the expressions like "1+++1" or "1**-1". I tried to use repeat like this:

      term =
            factor[_val=_1]
            >> *(   (repeat(0)[char_('*')] >> factor[_val*=_1])
                |   ('/' >> factor[_val/=_1])
                );

But it doesnt help. What do i miss? Thanks.

EDIT: Found an answer. One should compare string itrators after phrase_parse, but not phrase_parse output.


Solution

  • Found an answer. One should compare string itrators after phrase_parse, but not phrase_parse output.