这个语法有歧义吗
is this grammar ambiguous
我想将此语法转换为无歧义语法:
S -> if E then S
| if E then S else S
| a
E -> b
我找到了一个比我的解决方案更复杂的解决方案,但我不确定我的解决方案是否正确:
S -> if E then T else S
| if E then S
| a
T -> if E then T else T
| a
E -> b
那么我的解决方案是否正确?
我觉得还行。真的和标准方案没有太大区别:
stmt : matched | unmatched
matched : "if" expr "then" matched "else" matched
| other_stmt
unmatched : "if" expr "then" unmatched
| "if" expr "then" matched "else" unmatched
标准解的优点是other_stmt
(你语法中的a
)不重复,语法更容易用其他复合语句扩展。例如,如果我们添加 while
语句:
stmt : matched | unmatched
matched : "if" expr "then" matched "else" matched
| "while" matched
| other_stmt
unmatched : "if" expr "then" unmatched
| "if" expr "then" matched "else" unmatched
| "while" unmatched
我想将此语法转换为无歧义语法:
S -> if E then S
| if E then S else S
| a
E -> b
我找到了一个比我的解决方案更复杂的解决方案,但我不确定我的解决方案是否正确:
S -> if E then T else S
| if E then S
| a
T -> if E then T else T
| a
E -> b
那么我的解决方案是否正确?
我觉得还行。真的和标准方案没有太大区别:
stmt : matched | unmatched
matched : "if" expr "then" matched "else" matched
| other_stmt
unmatched : "if" expr "then" unmatched
| "if" expr "then" matched "else" unmatched
标准解的优点是other_stmt
(你语法中的a
)不重复,语法更容易用其他复合语句扩展。例如,如果我们添加 while
语句:
stmt : matched | unmatched
matched : "if" expr "then" matched "else" matched
| "while" matched
| other_stmt
unmatched : "if" expr "then" unmatched
| "if" expr "then" matched "else" unmatched
| "while" unmatched