二义性文法

stat : 'if' expr 'then' stat
		 | 'if' expr 'then' stat 'else' stat
		 | expr
		 ;

在分析 if a then if b then c else d 会出现歧义

改写后:

stat : matched_stat | open_stat
matched_stat : 'if' expr 'then' matched_stat 'else' matched_stat
						 | expr
						 ;
open_stat : 'if' expr 'then' stat
					| 'if' expr 'then' matched_stat 'else' open_stat
					;

这种改写将每一个 else 和最近的 if 相匹配,但缺点是非常复杂。

Context-Free Grammar

参考 FLA - Formal Languages and Automata