python带界面任意文法求FIRSTVT、LASTVT集和算符优先分析表

由于做课设的时候发现网上的代码都或多或少没有按照书上给出的求解思路导致我答辩差点团灭,所以写了这份代码给有需要的人。 求FIRSTVT和LASTVT集以及算符优先分析表的代码是完全按照陈火旺编译原理课本上给出的形式代码得来,有需要的小伙伴可以参考!

求FIRSTVT集算法:

def getfirst(vt, non_vt):
    for a in non_vt:
        for b in vt:
            addtwodimdict(mapdict, a, b, False)  # 遍历,为每个布尔数组赋值为False

    for a in GRAMMER:
        infer = GRAMMER[a]  # 得到推导式
        for b in infer:
            if b[0] in vt:
                insert(a, b[0])  # P->a...形式
            else:
                if len(b) > 1:
                    if b[1] in vt:
                        insert(a, b[1])  # P->Qa...形式

    while len(STACK) != 0:
        (Q, a) = STACK[-1]
        STACK.pop()
        for line in GRAMMER:
            infer = GRAMMER[line]
            for b in infer:
                if b[0] == Q:  # P->Q...形式
                    insert(line, a)

    for a in non_vt:
        FIRSTVT[a] = list()
        for b in vt:
            if mapdict[a][b]:
                FIRSTVT[a].append(b)
        print('FIRSTVT('+a +')' + ':', FIRSTVT[a])

    return FIRSTVT

求LASTVT集算法:

def getlast(vt, non_vt):
    for a in non_vt:
        for b in vt:
            addtwodimdict(mapdict, a, b, False)  # 遍历,为每个布尔数组赋值为False

    for a in GRAMMER:
        infer = GRAMMER[a]  # 得到推导式
        for b in infer:
            if b[-1] in vt:
                insert(a, b[-1])  # P->...a形式
            else:
                if len(b) > 1:
                    if b[-2] in vt:
                        insert(a, b[-2])  # P->...aQ形式

    while len(STACK) != 0:
        (Q, a) = STACK[-1]
        STACK.pop()
        for line in GRAMMER:
            infer = GRAMMER[line]
            for b in infer:
                if b[-1] == Q:  # P->...Q形式
                    insert(line, a)

    for a in non_vt:
        LASTVT[a] = list()
        for b in vt:
            if mapdict[a][b]:
                LASTVT[a].append(b)
        print('LASTVT('+a +')'+ ':', LASTVT[a])

    return LASTVT

求算符优先分析表算法:

def gettable(vt, non_vt):
    for a in vt:
        for b in vt:
            addtwodimdict(relation, a, b, 'err')

    for a in GRAMMER:
        infer = GRAMMER[a]
        for b in infer:
            n = len(b)
            if n == 1:
                pass
            else:
                for i in range(1, n):
                    if b[i - 1] and b[i] in vt:  # Xi和Xi-1均为终结符
                        addtwodimdict(relation, b[i - 1], b[i], '=·')
                    if i <= n - 2 and (b[i - 1] and b[i + 1] in vt) and (
                            b[i] in non_vt):  # i<=n-2且Xi和Xi+2都为终结符,但Xi+1为非终结符
                        addtwodimdict(relation, b[i - 1], b[i + 1], '=·')
                    if b[i - 1] in vt and b[i] in non_vt:  # Xi和Xi-1均为非终结符
                        for c in FIRSTVT[b[i]]:
                            addtwodimdict(relation, b[i - 1], c, '<·')
                    if b[i - 1] in non_vt and b[i] in vt:  # Xi为非终结符Xi+1为终结符
                        for c in LASTVT[b[i - 1]]:
                            addtwodimdict(relation, c, b[i], '·>')

                for i in non_vt:  # 为‘#’建立优先关系
                    for c in FIRSTVT[i]:
                        addtwodimdict(relation, '#', c, '<·')
                for i in non_vt:
                    for c in LASTVT[i]:
                        addtwodimdict(relation, c, '#', '·>')
                addtwodimdict(relation, '#', '#', '=·')

    n = len(vt) + 1
    gra = [[' ' for col in range(n)] for row in range(n)]
    i, j = 1, 1
    for a in vt:
        j = 1
        for b in vt:
            gra[i][j] = relation[a][b]
            j += 1
        i += 1
    i, j = 0, 1
    for a in vt:
        gra[i][j] = a
        j += 1
    i, j = 1, 0
    for a in vt:
        gra[i][j] = a
        i += 1

    for i in range(n):
        for j in range(n):
            print(gra[i][j], end='\t')
            s = gra[i][j] + '\t'
            with open("abs.txt", "a", encoding="utf-8") as f:
                f.write(s)
        print()
        with open("abs.txt", "a", encoding="utf-8") as f:
            f.write('\n')
    print()
    with open("abs.txt", "a", encoding="utf-8") as f:
        f.write('\n')

完整程序请移步:下载链接
在这里插入图片描述

由于我电脑分辨率有点高,所以下载后运行的界面可能出现移位,自己调整一下就可以使用了,运行前记得下载wxpython库

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值