1.对于典型的if-else二义性文法,存在移进-接受(移进-归约)冲突,在我的LALR分析器中采用了和YACC相同的方法,首先处理移进操作.这样就可消除if-else的二义性.
if-else文法:
S->iEStS
S->iES
S->s
E->e
说明:
i代表if
s代表stmt
e代表exp
t代表else
对于以上文法生成的LALR(1)分析表为:

对于if exp stmt语句分析结果为:

对于if exp stmt else stmt语句分析结果为:

对于if exp if exp stmt语句分析结果为:

对于if exp if exp stmt else stmt语句分析结果为:

2.众所周知LALR文法同样存在归约-归约冲突,在我的LALR分析器中使用先书写的产生式进行归约.
对于给定的存在归约-归约冲突的文法:
S->T
S->i
T->i
生成LALR(1)分析表为:

分析输入串i的过程为:

以上就是我的LALR(1)分析器处理冲突的方法了.
下面逐步对这个分析器进行扩充,以实现一个能自由输入文法的分析器.
posted on 2010-07-22 15:04
lwch 阅读(2035)
评论(0) 编辑 收藏 引用 所属分类:
NScript