考虑以下 BNF 语法

发布于 2024-10-06 16:47:17 字数 1262 浏览 0 评论 0原文

考虑以下 BNF 语法(其中非终结符括在尖括号中,并且 与任何合法的 Java 变量标识符匹配)。

<exp> ::= <exp> + <term>
      |   <exp> - <term>
      |   <term>
<term> ::= <term> * <factor>
       |   <term> / <factor>
       |   <factor>
<factor> ::= ( <exp> )
         |   <identifier>

为以下表达式生成推导三:

(x - a) * (y + b)

以 exp 开头:

<exp>

将 exp 替换为 term:

<term>

将 term 替换为:

<term> * <factor>

将 term 用因子替换:

<factor> * <factor>

将两个因子替换为 (exp):

( <exp> ) * ( <exp> )

将第一个 exp 替换为 exp - term,将第二个替换为 exp + term

( <exp> - <term> ) * ( <exp> + <term> )

将两个 exp 替换为 term,然后将所有 4 个项替换为因子。

( <factor> - <factor> ) * ( <factor> + <factor> )

用标识符替换所有因素

( <identifier> - <identifier> ) * ( <identifier> + <identifier> )

这是否足够?

Consider the following BNF grammer (where non-terminals are enclosed in angle-brackets and <identifier> matches to any legal Java variable identifier).

<exp> ::= <exp> + <term>
      |   <exp> - <term>
      |   <term>
<term> ::= <term> * <factor>
       |   <term> / <factor>
       |   <factor>
<factor> ::= ( <exp> )
         |   <identifier>

Produce a derivation three for the following expression:

(x - a) * (y + b)

Staring with exp:

<exp>

replace exp with term:

<term>

replace term with:

<term> * <factor>

replace term with factor:

<factor> * <factor>

replace both factors with (exp):

( <exp> ) * ( <exp> )

replace the first exp with exp - term and the second with exp + term

( <exp> - <term> ) * ( <exp> + <term> )

replace both exp's with term, and then replace all 4 terms with factors.

( <factor> - <factor> ) * ( <factor> + <factor> )

replace all factors with identifiers

( <identifier> - <identifier> ) * ( <identifier> + <identifier> )

Does this suffice?

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

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

发布评论

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

评论(1

晌融 2024-10-13 16:47:17

您需要更进一步 - 是一个非终结符,您应该将其缩减为

此外,您应该从 开始(然后将其简化为 ),而不是从 开始> 直接。

You need to go one step further - <factor> is a nonterminal, and you should reduce it down to <identifier>.

Additionally, you should be starting from <expr> (and then reducing it to <term>) rather than starting from <term> directly.

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