1、给出一个非终结符A,那么集合Follow(A)则是由终结符组成,此外可能还含有#(#是题目约定的字符串结束符)。集合Follow(A)的定义如下:1. 若A是开始符号,则#在Follow(A)中。2. 若存在产生式BA,则First()- 在Follow(A)中。3. 若存在产生式BA,且在First()中,则Follow(A)包括Follow(B)。3、Select集定义 对于产生式A。集合select(A)定义如下:1. 若不能推出,则select(A) = first()。2. 若能推出,则select(A)= first() follow(A)。4、含左递归文法一个文法G,若存在P经过
2、一次或多次推导得到Pa(即能推导出以P开头的式子), 则称G是左递归的。 左递归分为直接左递归和间接左递归。直接左递归经过一次推导就可以看出文法存在左递归,如PPab。间接左递归侧需多次推导才可以看出文法存在左递归,如文法:SQcc,QRbb,RSaa有S =Qc =Rbc =Sabc四、实验思路 本次实验采用python完成。 1、求非终结符是否能导出空 a. 第一轮扫描。当前的产生式还没被删除,非终结符lp可以导出空,将以该非终结符为左部的产生式标记为要删除的。产生式右部分解,若该产生式右部包含终结符,删除该产生式因为由它不会导出空。判断没有被删除的产生式中是否还有以该非终结符为左部的产生
3、式。 b. 第二轮扫描。逐一扫描每一条产生右部的每一个符号,循化直至每个非终结符的状态都确定下来。 2、求First集算法 存储每一个非终结符对应的First集,扫描每一条产生式,记录每一轮扫描是每个非终结符First集是否增大过。全部初始化为没有增大的状态,对于课本的五种类型依次求解,每次将结果加入对应的集合中,若一次扫描First集没有增大,则说明循环结束。 3、求Follow集算法 存储每一个非终结符对应的Follow集,将#加入文法的开始符号的Follow集合中,记录每一轮扫描是每个非终结符Follow集合是否增大过,全部初始化为没有增大的状态,扫描每一条产生式的右部,扫描到非终结符,
4、判断在该非终结符之后的子串能否推导空,若该符号串可以推导出空,还要将Follow(lp)加入到里面。4、求Select集算法 初始化每条产生式对应的Select集合为空,若产生式右部不能推导出空,则将右部的First集加入Select集,如果可以推出空,则需要同时将左部的Follow集合右部的First集去掉空的部分加入Select集。五、实验小结 通过本次实验,知道了如何判断一个文法是不是LL(1)文法,同时对于First、Follow以及Select集的求解原理变得更加熟悉,并且知道了如何用计算机语言求解First,Follow以及Select集。不足之处是,没有完成判断文法是否为左递归文
5、法以及左递归文法的转换部分。六、附件1、源代码class Gw: def _init_(self): with open(Gw.txt) as f: content = f.readlines() content = line.strip() for line in content self.Vn = content0.split( ) self.Vt = content1.split( self.start = content2 self.produce = self.left = self.right = for i in range(3,len(content): self.produc
6、e.append(contenti) self.left.append(contenti.split(-)0) self.right.append(contenti.split()1) def showGw(self): print(非终结符:,self.Vn)终 结 符:,self.Vt)开始符号:,self.start)产生式如下: for l,r in zip(self.left,self.right): print(l+r) def canEmpty(self): self.isEmpty = dict() for i in range(len(self.Vn): self.isEmp
7、tyself.Vni = -1 print(self.isEmpty) temp = self.produce: deleteIndex= pointer = 0 while pointerlen(temp): if pointer not in deleteIndex: lp = temppointer.split()0 rp = temppointer.split()1 if rp=! self.isEmptylp = 1 for i in range(len(temp): if tempi.split()0=lp and i not in deleteIndex: deleteIndex
8、.append(i) l = list(rp) isContainVt = i in self.Vt for i in l if True in isContainVt: deleteIndex.append(pointer) for k in range(len(temp): if k not in deleteIndex: if tempk.split()0=lp: break else: self.isEmptylp = 0 pointer = pointer+1 while -1 in self.isEmpty.values(): if i not in deleteIndex: lp
9、 = tempi.split( rp = tempi.split( rlsit = list(rp) for j in range(len(rlsit): if self.isEmptyrlsitj=1: if j=len(rlsit)-1: self.isEmptylp=1 elif self.isEmptyrlsitj=0: continue def show(self):非终结符能否推导出空的信息: for v in self.Vn: if self.isEmptyv=1: yon = 是否%s:%s%(v,yon) def getFirst(self): self.First = di
10、ct() for i in self.Vn: self.Firsti = list() isChange = dict() while True: for k in self.Vn: isChangek = 0 for i in range(len(self.produce): lp = self.producei.split( rp = self.producei.split( rlist = list(rp) if rlist0= or rlist0 in self.Vt: if rlist0 not in self.Firstlp: self.Firstlp.append(rlist0)
11、 isChangelp=1 for j in rlist: if j in self.Vn: if self.isEmptyj=1: oldsize = len(self.Firstlp) templist = self.Firstj: if in templist: templist.remove( for x in templist: if x not in self.Firstlp: self.Firstlp.append(x) if rp.endswith(j) and not in self.Firstlp: self.Firstlp.append( newsize = len(se
12、lf.Firstlp) if oldsize!=newsize: if j not in self.Firstlp: if 1 not in isChange.values():First集合不在增大!First集合有增大! pass def showFirst(self):First集合信息: print(v,self.Firstv) def canCauseEmpty(self,plist): first = list() if len(plist)=0: first.append( for i in plist: if i in self.Vn: if self.isEmptyi=1:
13、t = self.Firsti: in t: t.remove( for k in t: if k not in first: first.append(k).join(plist).endswith(i) and not in first: for k in self.Firsti: if i not in first: first.append(i) return first def getFollow(self): self.Follow = dict() self.Followi = list() self.Followself.start.append( for j in range
14、(len(rlist): if rlistj in self.Vn: reslist = self.canCauseEmpty(rlistj+1:) in reslist: oldsize = len(self.Followrlistj) for y in self.Followlp: if y not in self.Followrlistj: self.Followrlistj.append(y) newsize = len(self.Followrlistj) isChangerlistj = 1 for x in reslist: if x!= and x not in self.Fo
15、llowrlistj: self.Followrlistj.append(x) def showFollow(self):Follow集合信息: for key in self.Vn: print(key,self.Followkey) def getSelect(self): self.Select = dict() for i in self.produce: self.Selecti = list() for v in self.Followlp: if v not in self.Selectself.producei: self.Selectself.producei.append(
16、v) elif rlist0 in self.Vt: self.Selectself.producei.append(rlist0) res = self.canCauseEmpty(rlist) not in res: for v in res: if v not in self.Selectself.producei and v! def showSelect(self):Select集合信息: for key in self.produce: print(key,self.Selectkey) def isLLone(self): isright = tset = set() tset.
17、add( tset = tset | set(self.Vt) if k=l: p = l+r tset = tset & set(self.Selectp) if len(tset)=0: isright.append(1) isright.append(0) if 0 in isright:不是LL(1)文法! self.isll1 = False是LL(1)文法! self.isll1 = True print(isright)if _name_=_main_ w = Gw() w.showGw() w.canEmpty() w.show() w.getFirst() w.showFirst() w.getFollow() #res = w.canCauseEmpty(A,D #print(res=,res) w.showFollow() w.getSelect() w.showSelect() w.isLLone()2、运行结果截图
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1