编译原理词法分析器,ll1,lr0,python实现代码
合集下载
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
character =”"
return
# MAIN FUNCTION
defmain():
globaltoken
globalcharacter
globallocation
s = getchar()
getbe()
if'a’〈= s <='z’or’A’〈= s〈='Z':
whileletter()ordigit():
# IS IDENTIFIER
defreserve():
iftokeninKEYWORD_LIST:
returnCATEGORY_DICT[token]
else:
return0
#RETRACT
defretract():
globallocation
globalcharacter
# location = location—1
if__name__ ==’__main__’:
character =””
token =””
all_string = read_file(”program。txt",”r”)
location =0
whilelocation +1<len(ຫໍສະໝຸດ Baidull_string):
main()
print('KEYWORDTABLE:’,KEYWORDTABLE)
temp_str =""
try:
file =open(path, method)
forlineinfile:
line = line.replace('\n’,””)
temp_str += line
temp_str =str(temp_str)
exceptIOErrorase:
print(e)
exit()
Follow[Y[i]]|= First[Y[j]]—set(Epsilon)#步骤2 FIRST(β)/~加入到FOLLOW(B)中.
Change =True
ifEpsilonnot inFirst[Y[j]]:
Flag =False
break
ifFlag:
if notFollow[X]〈= Follow[Y[i]]:#步骤3β—〉~,把FOLLOW(A)加到FOLLOW(B)中
location = location +1
character = all_string[location]
returncharacter
#LINK TOKEN
defconcatenation():
globaltoken
globalcharacter
token = token + character
三、
1.词法分析器
program.txt中存放要分析的文法:
E—>TR
R-〉+TR|—TR|~
T->FG
G-〉*FG|/FG|~
F—〉(E)|i
代码:
KEYWORD_LIST =['while’,’if',’else’,’switch',’case']
SEPARATOR_LIST =[’;’,’:’,',’,'(',’)’,'[’,']',’{’,’}’]
forAinNonTermSet:
Follow[A]=set()
Follow[StartSym].add(EndSym)#将结束符号加入Follow[开始符号]中
Change =True
whileChange:#当Follow集没有更新算法结束
Change =False
forXinNonTermSet:
2.LL(1)分析器
program.txt中存放要分析的文法:
E—>TR
R->+TR|-TR|~
T—>FG
G—〉*FG|/FG|~
F—〉(E)|i
输入串:
i+i*i
代码:
NonTermSet =set()#非终结符集合
TermSet =set()#终结符集合
First ={}# First集
Follow ={}# Follow集
计算机科学与通信工程学院
编译原理实验报告
题目:1.词法分析器
2。LL(1)分析器
3.LR(0)分析器
班级:
姓名:
学号:
指导老师:
2017年月
一、
1.词法分析器
分析一段程序代码,将代码中的单词符号分解出来,并对其进行检查,输出token表和error表
2.LL(1)文法分析器
分析给定文法.求出文法的FIRST集,FOLLOW集,并构建分析表,对给定输入串进行分析。
print('TOKENTABLE:', TOKENTABLE)
print('CONSTANTTABLE:’,CONSTANTTABLE)
print('OPERATORTABLE:', OPERATORTABLE)
print(’SEPARATORTABLE:’,SEPARATORTABLE)
运行结果:
else:
KEYWORDTABLE.append(token)
print("这是保留字:",CATEGORY_DICT[token])
elif'0'<= s〈=’9':
whiledigit():
concatenation()
location = location +1
character = all_string[location]
OPERATOR_LIST1 = [’+',’—',’*’]
OPERATOR_LIST2 =['<=',’〈’,’==',’=',’〉’,’〉=']
CATEGORY_DICT = {
# KEYWORD
"while":{”while":”"},
”if”:{"if”:"”},
"else”: {"else":"”},
globalNonTermSet,TermSet,First, Follow, FirstA
forXinNonTermSet:
First[X] =set()#初始化非终结符First集为空
forXinTermSet:
First[X] =set(X)#初始化终结符First集为自己
Change =True
#IS NUMBER
defdigit():
if'0’〈= character〈='9':
return True
return False
#IS ALPHABET
defletter():
if’A’〈= character <=’Z'or’a'〈= character〈=’z':
return True
return False
print("这是单操作符:",CATEGORY_DICT[s])
elifsinOPERATOR_LIST2:
location = location +1
character = all_string[location]
ifcharacter ==’=':
OPERATORTABLE。append(s + character)
GramaDict = {}#处理过的产生式
Code = []#读入的产生式
AnalysisList ={}#分析表
StartSym =””#开始符号
EndSym ='#’#结束符号为“#“
Epsilon ="~”#由于没有epsilon符号用“~”代替
#构造First集
defgetFirst():
print(”这是双操作符:”, CATEGORY_DICT[s + character])
else:
retract()
location = location +1
OPERATORTABLE.append(s)
print(”这是单操作符:”, CATEGORY_DICT[s])
elifsinSEPARATOR_LIST:
Continue =False
k +=1
ifContinue:# X->~或者Y1到Yk均有~产生式
First[X] |=set(Epsilon)
#FirstA[Y] |= set(Epsilon)
#构造Follow集
defgetFollow():
globalNonTermSet,TermSet,First,Follow, StartSym
forYinGramaDict[X]:
foriinrange(len(Y)):
ifY[i]inTermSet:
continue
Flag =True
forjinrange(i +1,len(Y)):# continue
if notFirst[Y[j]] -set(Epsilon)<= Follow[Y[i]]:
finally:
file.close()
returntemp_str.strip()+" "
# GETBE
defgetbe():
globaltoken
getchar()
token =”"
return
#GETCHAR
defgetchar():
globalcharacter
globallocation
whileall_string[location] ==””:
location = location +1
SEPARATORTABLE.append(s)
print(”这是分隔符:”,CATEGORY_DICT[s])
else:
location +=1
UNDEFINEDTABLE。append(s)
print("error:undefined identity :'", s,"’")
3.LR(0)文法分析器
分析给定文法。用Ꜫ_CLOSURE方法构造文法的LR(0)项目集规范族,根据状态转换函数GO构造出文法的DFA,并转换为分析表,对给定输入串进行分析.
二、
1.学会词法分析器的实现思路。
2.学会求解FIRST集,FOLLOW集,构造LL(1)分析表。
3.学会Ꜫ_CLOSURE方法,状态转换函数GO,构造LR(0)分析表。
whileChange:#当First集没有更新则算法结束
Change =False
forXinNonTermSet:
forYinGramaDict[X]:
k =0
Continue =True
whileContinueandk〈len(Y):
if notFirst[Y[k]]-set(Epsilon)<= First[X]:#没有一样的就添加,并且改变标志
"〉": {”relop”:”GT"},
”==”:{"relop":”EQ"},
"=”:{"=":””},
#SEPARATOR
";":{”;”:"”},
”:":{”:":""},
",":{",”:”"},
”(":{”(”:”"},
”)”: {")":""},
"[":{"]":””},
”]”:{”]”:”"},
"{”:{"{”:""},
”}”:{"}":"”},
}
CONSTANTTABLE = []
TOKENTABLE = []
OPERATORTABLE = []
KEYWORDTABLE =[]
SEPARATORTABLE =[]
UNDEFINEDTABLE =[]
#READ FILE
defread_file(path,method):
”switch”: {"switch”:””},
"case”:{"case":”"},
# OPERATOR
”+":{”+”:"”},
"-”:{”-":""},
”*":{"*”:”"},
”<=": {”relop”:”LE”},
”<":{"relop”:"LT”},
”〉=”:{"relop”:"GE"},
Follow[Y[i]] |= Follow[X]
Change =True
#构造分析表
defgetAnalysisList():
fornonXinNonTermSet:
ifEpsilonnot inFirst[Y[k]]andY[k]inNonTermSetandk〉0:# Y1到Yi候选式都有~存在
Continue =False
else:
First[X] |= First[Y[k]] -set(Epsilon)
Change =True
ifEpsilonnot inFirst[Y[k]]:
retract()
CONSTANTTABLE.append(token)
print("这是常数:{’", token,”’:’",CONSTANTTABLE.index(token),”’}")
elifsinOPERATOR_LIST1:
location = location +1
OPERATORTABLE.append(s)
concatenation()
location = location +1
character = all_string[location]
retract()
c = reserve()
ifc ==0:
TOKENTABLE。append(token)
print(”这是标识符:{'”,token,"’:'",TOKENTABLE。index(token),”'}")
return
# MAIN FUNCTION
defmain():
globaltoken
globalcharacter
globallocation
s = getchar()
getbe()
if'a’〈= s <='z’or’A’〈= s〈='Z':
whileletter()ordigit():
# IS IDENTIFIER
defreserve():
iftokeninKEYWORD_LIST:
returnCATEGORY_DICT[token]
else:
return0
#RETRACT
defretract():
globallocation
globalcharacter
# location = location—1
if__name__ ==’__main__’:
character =””
token =””
all_string = read_file(”program。txt",”r”)
location =0
whilelocation +1<len(ຫໍສະໝຸດ Baidull_string):
main()
print('KEYWORDTABLE:’,KEYWORDTABLE)
temp_str =""
try:
file =open(path, method)
forlineinfile:
line = line.replace('\n’,””)
temp_str += line
temp_str =str(temp_str)
exceptIOErrorase:
print(e)
exit()
Follow[Y[i]]|= First[Y[j]]—set(Epsilon)#步骤2 FIRST(β)/~加入到FOLLOW(B)中.
Change =True
ifEpsilonnot inFirst[Y[j]]:
Flag =False
break
ifFlag:
if notFollow[X]〈= Follow[Y[i]]:#步骤3β—〉~,把FOLLOW(A)加到FOLLOW(B)中
location = location +1
character = all_string[location]
returncharacter
#LINK TOKEN
defconcatenation():
globaltoken
globalcharacter
token = token + character
三、
1.词法分析器
program.txt中存放要分析的文法:
E—>TR
R-〉+TR|—TR|~
T->FG
G-〉*FG|/FG|~
F—〉(E)|i
代码:
KEYWORD_LIST =['while’,’if',’else’,’switch',’case']
SEPARATOR_LIST =[’;’,’:’,',’,'(',’)’,'[’,']',’{’,’}’]
forAinNonTermSet:
Follow[A]=set()
Follow[StartSym].add(EndSym)#将结束符号加入Follow[开始符号]中
Change =True
whileChange:#当Follow集没有更新算法结束
Change =False
forXinNonTermSet:
2.LL(1)分析器
program.txt中存放要分析的文法:
E—>TR
R->+TR|-TR|~
T—>FG
G—〉*FG|/FG|~
F—〉(E)|i
输入串:
i+i*i
代码:
NonTermSet =set()#非终结符集合
TermSet =set()#终结符集合
First ={}# First集
Follow ={}# Follow集
计算机科学与通信工程学院
编译原理实验报告
题目:1.词法分析器
2。LL(1)分析器
3.LR(0)分析器
班级:
姓名:
学号:
指导老师:
2017年月
一、
1.词法分析器
分析一段程序代码,将代码中的单词符号分解出来,并对其进行检查,输出token表和error表
2.LL(1)文法分析器
分析给定文法.求出文法的FIRST集,FOLLOW集,并构建分析表,对给定输入串进行分析。
print('TOKENTABLE:', TOKENTABLE)
print('CONSTANTTABLE:’,CONSTANTTABLE)
print('OPERATORTABLE:', OPERATORTABLE)
print(’SEPARATORTABLE:’,SEPARATORTABLE)
运行结果:
else:
KEYWORDTABLE.append(token)
print("这是保留字:",CATEGORY_DICT[token])
elif'0'<= s〈=’9':
whiledigit():
concatenation()
location = location +1
character = all_string[location]
OPERATOR_LIST1 = [’+',’—',’*’]
OPERATOR_LIST2 =['<=',’〈’,’==',’=',’〉’,’〉=']
CATEGORY_DICT = {
# KEYWORD
"while":{”while":”"},
”if”:{"if”:"”},
"else”: {"else":"”},
globalNonTermSet,TermSet,First, Follow, FirstA
forXinNonTermSet:
First[X] =set()#初始化非终结符First集为空
forXinTermSet:
First[X] =set(X)#初始化终结符First集为自己
Change =True
#IS NUMBER
defdigit():
if'0’〈= character〈='9':
return True
return False
#IS ALPHABET
defletter():
if’A’〈= character <=’Z'or’a'〈= character〈=’z':
return True
return False
print("这是单操作符:",CATEGORY_DICT[s])
elifsinOPERATOR_LIST2:
location = location +1
character = all_string[location]
ifcharacter ==’=':
OPERATORTABLE。append(s + character)
GramaDict = {}#处理过的产生式
Code = []#读入的产生式
AnalysisList ={}#分析表
StartSym =””#开始符号
EndSym ='#’#结束符号为“#“
Epsilon ="~”#由于没有epsilon符号用“~”代替
#构造First集
defgetFirst():
print(”这是双操作符:”, CATEGORY_DICT[s + character])
else:
retract()
location = location +1
OPERATORTABLE.append(s)
print(”这是单操作符:”, CATEGORY_DICT[s])
elifsinSEPARATOR_LIST:
Continue =False
k +=1
ifContinue:# X->~或者Y1到Yk均有~产生式
First[X] |=set(Epsilon)
#FirstA[Y] |= set(Epsilon)
#构造Follow集
defgetFollow():
globalNonTermSet,TermSet,First,Follow, StartSym
forYinGramaDict[X]:
foriinrange(len(Y)):
ifY[i]inTermSet:
continue
Flag =True
forjinrange(i +1,len(Y)):# continue
if notFirst[Y[j]] -set(Epsilon)<= Follow[Y[i]]:
finally:
file.close()
returntemp_str.strip()+" "
# GETBE
defgetbe():
globaltoken
getchar()
token =”"
return
#GETCHAR
defgetchar():
globalcharacter
globallocation
whileall_string[location] ==””:
location = location +1
SEPARATORTABLE.append(s)
print(”这是分隔符:”,CATEGORY_DICT[s])
else:
location +=1
UNDEFINEDTABLE。append(s)
print("error:undefined identity :'", s,"’")
3.LR(0)文法分析器
分析给定文法。用Ꜫ_CLOSURE方法构造文法的LR(0)项目集规范族,根据状态转换函数GO构造出文法的DFA,并转换为分析表,对给定输入串进行分析.
二、
1.学会词法分析器的实现思路。
2.学会求解FIRST集,FOLLOW集,构造LL(1)分析表。
3.学会Ꜫ_CLOSURE方法,状态转换函数GO,构造LR(0)分析表。
whileChange:#当First集没有更新则算法结束
Change =False
forXinNonTermSet:
forYinGramaDict[X]:
k =0
Continue =True
whileContinueandk〈len(Y):
if notFirst[Y[k]]-set(Epsilon)<= First[X]:#没有一样的就添加,并且改变标志
"〉": {”relop”:”GT"},
”==”:{"relop":”EQ"},
"=”:{"=":””},
#SEPARATOR
";":{”;”:"”},
”:":{”:":""},
",":{",”:”"},
”(":{”(”:”"},
”)”: {")":""},
"[":{"]":””},
”]”:{”]”:”"},
"{”:{"{”:""},
”}”:{"}":"”},
}
CONSTANTTABLE = []
TOKENTABLE = []
OPERATORTABLE = []
KEYWORDTABLE =[]
SEPARATORTABLE =[]
UNDEFINEDTABLE =[]
#READ FILE
defread_file(path,method):
”switch”: {"switch”:””},
"case”:{"case":”"},
# OPERATOR
”+":{”+”:"”},
"-”:{”-":""},
”*":{"*”:”"},
”<=": {”relop”:”LE”},
”<":{"relop”:"LT”},
”〉=”:{"relop”:"GE"},
Follow[Y[i]] |= Follow[X]
Change =True
#构造分析表
defgetAnalysisList():
fornonXinNonTermSet:
ifEpsilonnot inFirst[Y[k]]andY[k]inNonTermSetandk〉0:# Y1到Yi候选式都有~存在
Continue =False
else:
First[X] |= First[Y[k]] -set(Epsilon)
Change =True
ifEpsilonnot inFirst[Y[k]]:
retract()
CONSTANTTABLE.append(token)
print("这是常数:{’", token,”’:’",CONSTANTTABLE.index(token),”’}")
elifsinOPERATOR_LIST1:
location = location +1
OPERATORTABLE.append(s)
concatenation()
location = location +1
character = all_string[location]
retract()
c = reserve()
ifc ==0:
TOKENTABLE。append(token)
print(”这是标识符:{'”,token,"’:'",TOKENTABLE。index(token),”'}")