资源简介
1. 实验内容
1、 让计算机接受一个文法,示例如(仅供参考):
G[S] 为:
S→AB S→bC
A→ε A→b
B→ε B→aD
C→AD C→b
D→aS D→c
2、 编程实现对上述文法是否是LL(1)文法的判断,是则给出肯定回答,否则给出否定回答。
3、判别是否是LL(1)文法
。。。。。。

代码片段和文件信息
‘‘‘
S
S->AB
A->&
B->&
C->AD
D->aS
S->bC
A->b
B->aD
C->b
D->c
#->#
‘‘‘
G = {}
# input
print(“请输入文法的开始符:“)
S = input()
print(“请输入文法.(如:‘S->AB‘).最后文法以‘#->#‘结束.“)
while True:
keyvalue = input().split(“->“)
if key==‘#‘ and value == ‘#‘: break
elif key in G.keys():G[key].append(value)
else: G[key] = [value]
# print(G)
# first
def first(strfirstset=[]):
t = str[0]
if t==‘&‘: firstset.append(‘&‘)
elif t>=‘A‘ and t<=‘Z‘:
for s in G[t]:
tempset = first(s firstset=[])
firstset.extend(tempset)
if ‘&‘ in tempset and len(str)>1:
firstset.extend(first(str[1:]firstset))
else: firstset.append(t)
return set(firstset)
# print first
# print(first(input() firstset=[]))
# follow
def follow(chfollowset=[]father=[]):
father.append(ch)
if ch==S:
followset.append(‘#‘)
# return followset
for key in G:
for s in G[key]:
if ch in s:
# print(ss.index(ch))
i = s.index(ch)
if i == len(s)-1:
# print(father)
if key in father: continue
followset.extend(follow(keyfollowsetfather))
elif i < len(s)-1:
tempset = first(s[i+1:] firstset=[])
#print(tempset)
if ‘&‘ in tempset:
if key in father: continue
# print(‘tempset=‘tempset)
tempset.remove(‘&‘)
followset.extend(tempset)
# print(‘tempset=‘tempset)
followset.extend(follow(keyfollowsetfather))
else:followset.extend(tempset)
return set(followset)
# print follow
# print(follow(input() followset=[]))
# select
selectset={}
def select():
for key in G:
for s in G[key]:
selectset[key+‘->‘+s] = first(s firstset=[])
if ‘&‘ in selectset[key+‘->‘+s]:
selectset[key+‘->‘+s].remove(‘&‘)
selectset[key+‘->‘+s] = selectset[key+‘->‘+s] | set(follow(key followset=[]father=[]))
return selectset
# print selectset
# print(select())
def judeLL1():
flag = True
select()
print(‘文法的select set如下:‘)
for key in selectset:
print(key+‘:‘selectset[key])
print(‘LL(1)判断过程如下(即select集合的求交集过程):‘)
for key1 in selectset:
for key2 in selectset:
if key1==key2: continue
elif key1[0]==key2[0]:
print(selectset[key1]‘U‘selectset[key2]end=‘‘)
tempset = selectset[key1] & selectset[key2]
if tempset:
flag = False
print(‘ =‘tempset)
else:print(‘ = NULL‘)
return flag
# jude LL(1)
#
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 3892 2017-12-21 11:15 SelectSet.py
----------- --------- ---------- ----- ----
3892 1
相关资源
- 编译原理实验工具及参考源码(lex&
- 类pascal语言编译器(编译原理实验)
- 编译原理实验:词法分析,语法分析
- 哈工大威海编译原理实验报告和源代
- 哈工大威海-编译原理实验报告和源码
- 河北工业大学编译原理实验代码及实
- 天津理工大学编译原理实验2
- 编译原理CP lab实验报告.doc
- 西北农林科技大学编译原理实验
- 编译原理实验题——PL/0语言编译器设
- 编译原理实验TINY+编译器
- 华工编译原理实验词法分析+语法分析
- 东南大学编译原理实验 lex&&yacc
- 上海大学 编译原理实验报告
- 编译原理实验指导书 河北工业大学
- 河北工业大学编译原理实验报告及源
- 编译原理实验-flex自动构造词法分析
- 广工编译原理实验、课程设计
- 编译原理实验decaf_PA2
- 北邮编译原理实验二:语法分析程序
- 词法分析程序和语法分析程序 编译原
- 编译原理实验:C编译器源代码
- 编译器词法分析,语法分析
- 编译原理实验报告 表达式中间代码生
- 编译原理实验报告.doc
- 吉林大学2015年编译原理实验源代码
- 编译原理实验LR分析表构造LR分析器
- 编译原理中间代码生成实验报告——
- 编译原理实验报告 熟悉算术表达式的
- 编译原理实验报告分析PL0词法分析程
评论
共有 条评论