• 大小: 89KB
    文件类型: .rar
    金币: 2
    下载: 1 次
    发布日期: 2021-07-17
  • 语言: Java
  • 标签: python  

资源简介

词法分析器,鉴于网上基本上是java或者c++写的LR_0分析器,我将自己写的python版发布,供大家参考学习。里面也有我的实验包告,帮助快速熟悉程序功能

资源截图

代码片段和文件信息

from tkinter import*
from re import *
import copy
from tkinter.scrolledtext import ScrolledText

root = Tk()

root.title(‘LR(0)分析程序‘)
frame_entry = frame(root)
label = Label(frame_entrytext=“输入:“)
label.grid(row=0column=0padx=10)
entry = Entry(frame_entrywidth=40)
entry.grid(row=0column=1)
textvar = StringVar()
frame_entry.grid(row=0column=0padx=10pady=5sticky=W)


V_N = list()
V_T = list()
p_temp = list()
p = list()

# 保存状态集
res=[]
# 扩展产生式
pz=[]
point=‘.‘


# action表goto 表
action_tb = []
goto_tb = []
# print(V_TV_N)
def init_p():
    num = 1
    for s in globals()[‘p_temp‘]:
        temp = []
        temp[0:]=[nums]
        num+=1
        p.append(temp)


# 0为接受项目,1为归约项目,2为移进项目,3为待约项目
def init_Z():
    num = 1
    for i in p:
        s=i[1]
        length = len(s)
        for j in range(3length+1):
            tem = list(s)
            if j < length and s[j] in V_N:flag=3
            elif j < length and s[j] in V_T:flag=2
            elif j == length and s[j-1]==V_N[1]:flag=0
            else:flag = 1
            if flag!=1:tem[j:j] = globals()[‘point‘]
            else:tem[j:]=globals()[‘point‘]
            pz.append([num‘‘.join(tem)flag])
            num+=1

# s是扩广文法的产生式
# 如果现在新建的状态是不完整的,怎么去判断是否会被重复
def coluser(num s):
    state = list()
    dic = dict()
    state.append(num)
    temp = list()
    temp.append(s)
    while True:
        count = len(temp)
        # print(‘temp‘temp)
        for tem_pz in temp:
            ind = tem_pz[1].index(‘.‘)
            if tem_pz[2] == 3:
                for j in pz:
                    if j[1][0] == tem_pz[1][ind + 1] and j not in temp and j[1].index(‘.‘) == 3:
                        temp.append(j)
        if count == len(temp):break
    state.append(temp)
    state.append(dic)
    if test_coluser(state) == 1:
        res.append(state)
        return state[0]
    else:
        # 返回完全包含的状态编号
        return test_coluser(state)[0]


# 任何一个不与其余的相等
def test_coluser(s):
    s_len = len(s[1])
    for state in res:
        sta_len = len(state[1])
        # flag == 0 表示相等
        flag = 0
        # 计算相等的数目
        num = 0
        for i in range(0s_len):
            for j in range(0sta_len):
                # print(‘s‘s[1][i]state[1][j])
                if s[1][i] == state[1][j]:
                    num += 1
                    break
            #     flag == 1表示不一样
        # 注意for语句块,break是跳出哪个语句块
        if num != s_len:
            flag = 1
        #         return -1 存在
        if flag == 0 and s_len == sta_len:return state
    return 1
# 状态,字符,产生式
def add_coluser(stacharpro):
    # sta是找到的要转换过去的状态,要去补充它
    state = copy.deepcopy(sta)
    temp = list()
    if pro not in sta[1]:
        temp.append(pro)
    # 数量不增多就是已经加完了
        while True:
            count = len(temp)
            for tem_pz in temp:
                ind = tem_pz[1].index(‘.‘)
                if tem_pz

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件      12453  2019-04-29 22:25  语法分析器的设计\Gui_Lr.py

     文件      95986  2019-05-03 08:30  语法分析器的设计\实验报告(教程).docx

     目录          0  2019-05-03 08:30  语法分析器的设计

----------- ---------  ---------- -----  ----

               108439                    3


评论

共有 条评论