LL(1)文法与左递归

没有超群的记忆力和过目不忘的能力,担心自己忘了,就写下来好啦。


考虑如下文法,

S -> SA | A
A - >a

如何说明该文法是LL(1)的?

我们首先要明确,LL(1)是文法的一种性质,它描述的是这样一种文法,

Predictive parsers, that is, recursive-descent parsers needing no backtracking,
can be constructed for a class of grammars called LL(I) , The first “L” in LL(I)
stands for scanning the input from left to right, the second “L” for producing
a leftmost derivation, and the “I” for using one input symbol of lookahead at
each step to make parsing action decisions.

其次,根据LL(1)文法的充要条件,
这里写图片描述
为了证明给定文法不是LL(1),只需给出三个条件中任意一个的反例。不难找到一个包含以a为开头的字符串的语言,由于SA和A都可以推导出以a为开头的字符串,这与第一个条件相悖,因此此文法不是LL(1)的。

如果我们对原文法进行消除左递归处理,可以得到如下文法,

S -> AS’
S’ -> AS’ | ϵ ϵ
A -> a

对于此文法,可以构造无冲突的预测分析表。所以构造的新文法是LL(1)的。相关解释如下,

For every LL(1) grammar , each parsing-table entry uniquely identifies a
production or signals an error. For some grammars, however, M may have
some entries that are multiply defined. For example , if G is left-recursive or
ambiguous, then M will have at least one multiply defined entry. Although leftrecursion
elimination and left factoring are easy to do, there are some grammars
for which no amount of alteration will produce an LL(1) grammar.

消除左递归的过程其实是对原文法的改变,LL(1)文法的表达能力相对LR(1)文法较弱,也就是说,任何能用LL(1)文法描述的语言都可以被LR(1)文法描述,但反之不一定成立。

查看全文

Since LR lookahead starts from the end of a rule, a LR(1) parser has strictly more information available to it when making a decision than an LL(1) parser. It follows that LR(1) parsers can parse strictly more grammars than LL(1) (modulo LL-only grammar extensions; see below). LR parsers can also handle left recursion, which LL parsers cannot.

LL解析器不能处理左递归,而LR解析器是可以的。


写到最后发现我纠结的问题好弱智orz

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值