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?
Both
+lit('M')and*lit('M')are correct. But the former is more readable than the latter (semantically), in my opinion, as the former says add1000to_valif there isonematch, and do it repeatedly. On the other hand, the latter is difficult to read, as one could read it as add1000to_valeven for zero-match which is wrong.1000is not added to_valfor zero-times match, yet the parser*lit('M')seems to match for zero-match as well (seems kind of confusing).So
+lit('M')is preferable.Alright. I read your comment.
CCLLIXis not a valid roman number. What do you think its value is?309? If that is so, then what value would be forCCCIX? It’s too 309, and its correct. Yours is wrong. Hence the parser stops when you use*lit('M'). Note also that the parser would also stop even if you use+lit('M')for this wrong input.