Compiler construction Can';I don’我不想为if-else创建明确的语法

Compiler construction Can';I don’我不想为if-else创建明确的语法,compiler-construction,programming-languages,grammar,lex,Compiler Construction,Programming Languages,Grammar,Lex,这是我们教授写的一篇教程,我想不通。我能想出派生词,但我不能仅仅通过分析派生词就想出语法 在本文中,“匹配”指的是什么 你能用简单的话解释一下匹配的if、匹配的stmt、不匹配的if是如何工作的吗 “匹配”表示每个然后都与其他匹配 if语句不必有匹配的else,但如果没有,它就不能在匹配的if语句中,因为这意味着一些else匹配外部then而不是内部then) 语法所做的只是将上述内容形式化 一个类似的问题是为普通算术表达式编写语法,并附加一条可以省去尾随右括号的规则。(例如,你可以写(1+2*

这是我们教授写的一篇教程,我想不通。我能想出派生词,但我不能仅仅通过分析派生词就想出语法

在本文中,“匹配”指的是什么

你能用简单的话解释一下匹配的if、匹配的stmt、不匹配的if是如何工作的吗

“匹配”表示每个
然后
都与
其他
匹配

if
语句不必有匹配的
else
,但如果没有,它就不能在匹配的
if
语句中,因为这意味着一些
else
匹配外部
then
而不是内部
then

语法所做的只是将上述内容形式化

一个类似的问题是为普通算术表达式编写语法,并附加一条可以省去尾随右括号的规则。(例如,你可以写
(1+2*(1+2*(1+2
))这种语言显然是明确无误的,但当你为它编写语法时,你需要处理不匹配的括号,从而处理包含不匹配括号的表达式。这与“匹配”一词的用法相同(解决方案可能有些类似)

The following is an unambiguous grammar for the problem:

stmt → if_stmt | nonif_stmt
if_stmt → matched_if | unmatched_if
matched_if → 'if' logical_expr 'then' matched_stmt 'else' matched_stmt
matched_stmt → mathced_if | nonif_stmt
unmatched_if → 'if' logical_expr 'then' stmt
 | 'if' logical_expr 'then' matched_stmt 'else' unmatched_if
logical_expr → id '==' lit
nonif_stmt → assgn_stmt
assgn_stmt → id '=' expr
expr → expr '+' term | term
term → '(' expr ')' | id
id → 'A' | 'B' | 'C'
lit → '0' | '1' | '2'


Consider the following input:
if A == 0 then
  if B == 1 then
    C = A + B
  else
    B = C

Let us do a leftmost derivation for the input:

stmt
=> if_stmt
=> unmatched_if
=> 'if' logical_expr 'then' stmt
=> 'if' id '==' lit 'then' stmt
=> 'if' 'A' '==' lit 'then' stmt
=> 'if' 'A' '==' '0' 'then' stmt
=> 'if' 'A' '==' '0' 'then' if_stmt
=> 'if' 'A' '==' '0' 'then' matched_if
=> 'if' 'A' '==' '0' 'then' 'if' logical_expr 'then' matched_stmt 'else' matched_stmt
=> 'if' 'A' '==' '0' 'then' 'if' id '==' lit 'then' matched_stmt 'else' matched_stmt
=> 'if' 'A' '==' '0' 'then' 'if' 'B' '==' lit 'then' matched_stmt 'else' matched_stmt
=> 'if' 'A' '==' '0' 'then' 'if' 'B' '==' '1' 'then' matched_stmt 'else' matched_stmt
=> 'if' 'A' '==' '0' 'then' 'if' 'B' '==' '1' 'then' nonif_stmt 'else' matched_stmt
=> 'if' 'A' '==' '0' 'then' 'if' 'B' '==' '1' 'then' assgn_stmt 'else' matched_stmt
=> 'if' 'A' '==' '0' 'then' 'if' 'B' '==' '1' 'then' id '=' expr 'else' matched_stmt
=> 'if' 'A' '==' '0' 'then' 'if' 'B' '==' '1' 'then' 'C' '=' expr '+' term 'else' matched_stmt
=> 'if' 'A' '==' '0' 'then' 'if' 'B' '==' '1' 'then' 'C' '=' term '+' term 'else' matched_stmt
=> 'if' 'A' '==' '0' 'then' 'if' 'B' '==' '1' 'then' 'C' '=' id '+' term 'else' matched_stmt
=> 'if' 'A' '==' '0' 'then' 'if' 'B' '==' '1' 'then' 'C' '=' 'A' + term 'else' matched_stmt
=> 'if' 'A' '==' '0' 'then' 'if' 'B' '==' '1' 'then' 'C' '=' 'A' + 'B' 'else' matched_stmt
=> 'if' 'A' '==' '0' 'then' 'if' 'B' '==' '1' 'then' 'C' '=' 'A' + 'B' 'else' nonif_stmt
=> 'if' 'A' '==' '0' 'then' 'if' 'B' '==' '1' 'then' 'C' '=' 'A' + 'B' 'else' assgn_stmt
=> 'if' 'A' '==' '0' 'then' 'if' 'B' '==' '1' 'then' 'C' '=' 'A' + 'B' 'else' id '=' expr
=> 'if' 'A' '==' '0' 'then' 'if' 'B' '==' '1' 'then' 'C' '=' 'A' + 'B' 'else' 'B' '=' expr
=> 'if' 'A' '==' '0' 'then' 'if' 'B' '==' '1' 'then' 'C' '=' 'A' + 'B' 'else' 'B' '=' term
=> 'if' 'A' '==' '0' 'then' 'if' 'B' '==' '1' 'then' 'C' '=' 'A' + 'B' 'else' 'B' '=' id
=> 'if' 'A' '==' '0' 'then' 'if' 'B' '==' '1' 'then' 'C' '=' 'A' + 'B' 'else' 'B' '=' 'C'