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

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


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:

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]]:

for i in non_vt:  # 为‘#’建立优先关系
for c in FIRSTVT[i]:
for i in non_vt:
for c in LASTVT[i]:

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')


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

12-01
03-07 1万+
11-07 8651
11-06 1263
05-11 4195
04-18 1508
11-13 2462
05-07 2972
11-23 155
05-26 2681

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