Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost Spirit Roman Numeral Parser Example

Trying to learn boost spirit and the example given in the docs have me a little confused.

Referring to this code:

http://www.boost.org/doc/libs/1_46_1/libs/spirit/example/qi/roman.cpp

Particularly this segment of grammar:

        start = eps             [_val = 0] >>
            (
                +lit('M')       [_val += 1000]
                ||  hundreds    [_val += _1]
                ||  tens        [_val += _1]
                ||  ones        [_val += _1]
            )

Could someone explain to me why it is +lit('M') and not *lit('M'). Because after all can't there be zero or more M's versus one or more M's?

like image 571
Integer Avatar asked Nov 04 '22 15:11

Integer


1 Answers

The a || b operator in Spirit means a or b, but b after a, if a occurs. In the meaing of the operator, the case that there is no M is implicit (because the match for M may or may not be present). Also, in the case of *lit('M'), would you say that the first rule is matched if there is NO M? It would be valid anyway, and _val would be incremented by 1000.

like image 163
Diego Sevilla Avatar answered Nov 11 '22 22:11

Diego Sevilla