EBNF 中何时使用括号?
如果你有这样的语法:
<assign> → <id> = <expr>
<id> → A | B | C
<expr> → <expr> + <term>
| <term>
<term> → <term> * <factor>
| <factor>
<factor> → ( <expr> )
| <id>
然后句子 A = B + C * A,你会得到这个最左推导:
<assign> => <id> = <expr>
=> A = <expr>
=> A = <expr> + <term>
=> A = <term> + <term>
=> A = <factor> + <term>
=> A = <id> + <term>
=> A = B + <term>
=> A = B + <term> * <factor>
=> A = B + <factor> * <factor>
=> A = B + <id> * <factor>
=> A = B + C * <factor>
=> A = B + C * <id>
=> A = B + C * A
但是 A = B + ( C * A ) 呢?
If you have grammar like this:
<assign> → <id> = <expr>
<id> → A | B | C
<expr> → <expr> + <term>
| <term>
<term> → <term> * <factor>
| <factor>
<factor> → ( <expr> )
| <id>
And then the sentence A = B + C * A, you get this leftmost derivation:
<assign> => <id> = <expr>
=> A = <expr>
=> A = <expr> + <term>
=> A = <term> + <term>
=> A = <factor> + <term>
=> A = <id> + <term>
=> A = B + <term>
=> A = B + <term> * <factor>
=> A = B + <factor> * <factor>
=> A = B + <id> * <factor>
=> A = B + C * <factor>
=> A = B + C * <id>
=> A = B + C * A
But what about A = B + ( C * A )?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
前五个步骤,与上面相同,然后...
First five steps, same as above, then...
( C * A )
不需要括号,因为 * 具有更高的优先级。您会在A = B * ( C + B)
中看到它的一种情况。您在最后两行中看不到它,因为
将被评估为+
或
。在本例中没有 +,因此它必须是
。( C * A )
doesn't need a paren, because * has greater priority. One case where you'd see it is inA = B * ( C + B )
.You don't see it in the last two lines because
<factor>
is going to eval to be either a<term> + <term>
or an<id>
. In this case there's no + so it has to be an<id>
.