Boost Spirit 罗马数字解析器示例

发布于 2024-12-01 03:18:29 字数 616 浏览 3 评论 0原文

尝试学习 boost Spirit 和文档中给出的示例让我有点困惑。

参考此代码:

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

特别是这一段语法:

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

有人可以向我解释一下为什么它是 +lit('M') 而不是*点燃('M')。因为毕竟不能有零个或多个 M 与一个或多个 M 相对吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

三生路 2024-12-08 03:18:29

a || Spirit 中的 b 运算符表示 ab,但 ba 之后,如果 出现。在运算符的含义中,不存在 M 的情况是隐式的(因为 M 的匹配可能存在也可能不存在)。另外,在*lit('M')的情况下,如果NOM,你会说第一条规则匹配吗?无论如何它都是有效的,并且 _val 将增加 1000。

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.

追星践月 2024-12-08 03:18:29

+lit('M')*lit('M') 都是正确的。但在我看来,前者比后者更具可读性(语义上),因为前者表示 add 1000_val 如果有 < code>one 匹配,并重复执行。另一方面,后者很难阅读,因为即使对于零匹配,也可以将其读作 add 1000_val错误的。 1000 未添加到 _val 中进行零次匹配,但解析器 *lit('M') 似乎匹配零次匹配以及(似乎有点令人困惑)。

所以 +lit('M') 是更好的选择。


好吧。我读了你的评论。 CCLLIX 不是有效的罗马数字。你认为它的价值是什么? 309?如果是这样,那么 CCCIX 的价值是多少?太309了,而且是正确的。你的是错的。因此,当您使用 *lit('M') 时,解析器会停止。另请注意,即使您使用 +lit('M') 来处理此错误输入,解析器也会停止。

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 add 1000 to _val if there is one match, and do it repeatedly. On the other hand, the latter is difficult to read, as one could read it as add 1000 to _val even for zero-match which is wrong. 1000 is not added to _val for 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. CCLLIX is not a valid roman number. What do you think its value is? 309? If that is so, then what value would be for CCCIX? 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.

心舞飞扬 2024-12-08 03:18:29

它是(一个或多个女士)或数百或数十或几个。 (零个或多个 Ms)OR 数百个或数十个 OR 个将匹配没有 Ms 又名空字符串并毫无意义地添加 1000。

It's (One or more Ms) OR hundreds OR tens OR ones. (Zero or more Ms) OR hundreds OR tens OR ones would match no Ms aka the empty string and meaninglessly add 1000.

或十年 2024-12-08 03:18:29

匹配表达式 A || Qi 中的 B 表示仅匹配 A,或者仅匹配 BA 后跟 B。因此,在您的情况下 +lit('M') ||数百表示+lit('M'),或数百+lit('M')后跟数百。因此,语法允许匹配任何不以 M 开头的罗马数字。

Matching the expression A || B in Qi means either matching just A, or just B or A followed by B. Therefore, in your case +lit('M') || hundreds means +lit('M'), or hundreds or +lit('M')followed by hundreds. For this reason the grammar allows to match any roman numbers not even starting with an M.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文