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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With