EBNF 中何时使用括号?

发布于 2024-09-25 00:12:41 字数 993 浏览 1 评论 0原文

如果你有这样的语法:

<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 技术交流群。

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

发布评论

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

评论(2

ヤ经典坏疍 2024-10-02 00:12:42

<块引用>

A = B + (C * A)?

前五个步骤,与上面相同,然后...

     => A = B + <term>  
     => A = B + <factor>    
     => A = B + ( <expr>) 
     => A = B + ( <term> )  
     => A = B + ( <term> * <factor> ) 
     => A = B + ( <factor> * <factor> ) 
     => A = B + ( <id> * <id> ) 
     => A = B + (  C   *   A  )

A = B + ( C * A )?

First five steps, same as above, then...

     => A = B + <term>  
     => A = B + <factor>    
     => A = B + ( <expr>) 
     => A = B + ( <term> )  
     => A = B + ( <term> * <factor> ) 
     => A = B + ( <factor> * <factor> ) 
     => A = B + ( <id> * <id> ) 
     => A = B + (  C   *   A  )
人生戏 2024-10-02 00:12:42

( 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 in A = 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>.

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