考虑以下 BNF 语法
考虑以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要更进一步 -
是一个非终结符,您应该将其缩减为
。此外,您应该从
开始(然后将其简化为
),而不是从
开始> 直接。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.