编译原理 NFA -> DFA 确定化 + 最小化

测试NFA状态转换图:

当时测试的时候把把一些数据写死了,写在了一个data.txt文件里了,文件的内容是这样的;

0 1 e
0 7 e
1 2 e
1 4 e
2 3 a
4 5 b
3 6 e
5 6 e
6 7 e
7 8 a
8 9 b
9 10 b
6 1 e

源码:

from tabulate import tabulate

def e_closure(idx,c):
    closure = set()
    q = [idx]
    closure.add(idx)
    while len(q):
        t = q[0]
        for i in node_list[t]:
            if i[1] == c:
                closure.add(i[0])
                q.append(i[0])
        q.pop(0)
    return closure


def n_closure(idx,c):
    closure = set()
    for i in node_list[idx]:
        if i[1] == c:
            closure.add(i[0])
    return closure

# state_num = eval(input('状态数量:'))
# char_num = input('字母表中字母:')
# e_num = eval(input('定义式数量:'))
# st_state = eval(input('开始状态:'))
# ed_state = eval(input('结束状态:'))
state_num = 11
char_num = 'ab'
e_num = 13
st_state = 0
ed_state = 10

existed = []
node_list = [[] for i in range(state_num)]
d_queue =[]

for line in open('data.txt','r'):
    xyw = line.strip('\n')
    pro = xyw.split(' ')
    x = eval(pro[0])
    y = eval(pro[1])
    w = (pro[2])
    node_list[x].append((y,w))
if not st_state:
    for i in range(st_state):
        node_list.pop(0)
for i in  range(len(node_list)):
    print(i,':',node_list[i])

queue = []  # 状态I列表
check_queue = []
t = e_closure(st_state,'e')
queue.append(t)
check_queue.append(t)
while(len(queue)):
    tt = queue[0]
    tmp = []
    tmp.append(tt)
    for c in char_num:
        I = set()
        for i in tt:
            I = I.union(n_closure(i,c))
        for i in I:
            I = I.union(e_closure(i,'e'))
        if I not in check_queue:
            queue.append(I)
            check_queue.append(I)
        tmp.append(I)
    d_queue.append(tmp.copy())
    queue.pop(0)

print('子集法状态表:','\n',tabulate(d_queue,headers=['I','Ia','Ib'],tablefmt='plain'),end = '\n')

equal_list =[]
hash_map = dict()
cnt = 0
for i in d_queue:
    for j in i:
        j = str(j)
        if  j not in hash_map.keys():
            hash_map[j] = cnt
            cnt += 1
for i in d_queue:
    process = []
    for j in i:
        j = str(j)
        process.append(chr(65 + hash_map[j]))
    equal_list.append(process.copy())

print('确定化状态表:','\n',tabulate(equal_list,headers=['I','Ia','Ib'],tablefmt='plain',numalign='right'))

n_state = []
t_state = []

for i in range(len(d_queue)):
    if ed_state in d_queue[i][0]:
        t_state.append(i)
    else:
        n_state.append(i)
print('\n','第一次分组:')
print('不含终态的子集:',n_state)
print('含终态的子集:',t_state,'\n')

vis = [0 for i in range(1000)]
a = [0 for i in range(1000)]
ans = [[] for i in range(1000)]
nums = []
ok = [0 for i in range(1000)]

for i in n_state:
    vis[i] = 1
for i in t_state:
    vis[i] = 2

for i in range(len(equal_list)):
    n = 0
    for j in range(len(equal_list)):
        x = vis[j] * pow(10,len(equal_list[j]) - 1)
        for zz in range(1,len(equal_list[j])):
            aa = ord(equal_list[j][zz]) - ord('A')
            x += vis[aa] * pow(10,len(equal_list[j]) - zz - 1)
        if x == a[j]:
            n += 1
        a[j] = x
        ans[x].append(j)
        if ok[x] == 0:
            nums.append(x)
            ok[x] = 1
    ok = [0 for i in range(1000)]
    if n== state_num:
        break
    lenn = 1
    for j in range(len(nums)):
        for k in range(len(ans[nums[j]])):
            vis[ans[nums[j]][k]] = lenn
        lenn+=1
    ans = [[] for i in range(1000)]
    nums = []

res = []

for i in range(len(equal_list)):
    qwq = []
    qwq.append(chr(65 + i))
    qwq.append(vis[i])
    res.append(qwq.copy())

print(tabulate(res,headers=['状态','集合'],tablefmt='plain',numalign='left'))

给老师看可以加分!!!!

发布了341 篇原创文章 · 获赞 32 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_41514525/article/details/103134483