Compiler construction 把语法弄清楚?

Compiler construction 把语法弄清楚?,compiler-construction,grammar,context-free-grammar,ambiguity,context-free-language,Compiler Construction,Grammar,Context Free Grammar,Ambiguity,Context Free Language,我有以下问题。这种语法模棱两可: stmt->if expr然后stmt stmt'| a stmt'->其他stmt |ε expr->b 我试图修改它,结果是: stmt->如果是expr,则为stmt'| a stmt'->stmt | stmt' stmt'->B其他stmt expr->b 但这并不产生相同的语言 有人能帮我修改这个模棱两可的语法,使它不模棱两可并接受相同的语言吗?使用给定的语法,字符串有两个最左边的派生词如果b,那么如果b,那么a,那么a,如下所示 推导1: if e

我有以下问题。这种语法模棱两可:

stmt->if expr然后stmt stmt'| a

stmt'->其他stmt |ε

expr->b

我试图修改它,结果是:

stmt->如果是expr,则为stmt'| a

stmt'->stmt | stmt'

stmt'->B其他stmt

expr->b

但这并不产生相同的语言


有人能帮我修改这个模棱两可的语法,使它不模棱两可并接受相同的语言吗?

使用给定的语法,字符串有两个最左边的派生词
如果b,那么如果b,那么a,那么a,如下所示

推导1:

if expr then stmt stmt'
if b then stmt stmt'
if b then if expr then stmt stmt' stmt'
if b then if b then stmt stmt' stmt'
if b then if b then a stmt' stmt'
if b then if b then a stmt'
if b then if b then a else stmt
if b then if b then a else a
推导2:

if expr then stmt stmt'
if b then stmt stmt'
if b then if expr then stmt stmt' stmt'
if b then if b then stmt stmt' stmt'
if b then if b then a stmt' stmt'
if b then if b then a else stmt stmt'
if b then if b then a else a stmt'
if b then if b then a else a

解析树在大多数情况下保持不变。但是在导出
如果b那么如果b那么a stmt'stmt'
之后,节点的顺序会发生变化,从而影响树的结构。因此,给定的语法是不明确的。

虽然经过了表决,但OP要求删除语法的不明确性。我甚至很难证明自己的模棱两可。谢谢你的努力。你还没有在第二个语法中定义B。