首页 > 解决方案 > 有没有办法编写程序可以接受任何文本文件而不管布局结构的代码?

问题描述

我想知道是否有一种方法可以编写代码,它接受任何输出所有状态、状态数、字母大小、字母符号、所涉及的转换、开始状态、最终开始状态的数量和最终无论布局/过程是否以不同方式完成

This is my current code:
#User enters text file names
user_input = input("Enter First DFA Text file: ")
user_input2 = input("Enter Second DFA Text file: ")

#Opens the inputted text files
dfaFile = open(user_input, "r")
DFA1 = dfaFile.read().splitlines()

dfaFile2 = open(user_input2, "r")
DFA2 = dfaFile2.read().splitlines()

Lines = dfaFile.readlines()
Lines2 = dfaFile2.read().splitlines()

#Construction Method #1: Getting the states for DFA "N"
DFA_N_States = set()
for r in DFA1[1].split(): 
    for s in DFA2[1].split(): 
        DFA_N_States.add(r + s) 
print(DFA_N_States)

#Construction Method #2: Getting the transitions between each state in DFA "N"
t1 = {
(DFA1[1].split()[0], DFA1[3].split()[0]): DFA1[4].split()[0], 
(DFA1[1].split()[0], DFA1[3].split()[1]): DFA1[4].split()[1],
(DFA1[1].split()[1], DFA1[3].split()[0]): DFA1[4].split()[1],
(DFA1[1].split()[1], DFA1[3].split()[1]): DFA1[4].split()[0],
}

t2 = {
(DFA2[1].split()[1], DFA2[3].split()[0]): DFA2[4].split()[0],
(DFA2[1].split()[1], DFA2[3].split()[1]): DFA2[4].split()[0],
(DFA2[1].split()[1], DFA2[3].split()[1]): DFA2[4].split()[0],
(DFA2[1].split()[2], DFA2[3].split()[0]): DFA2[4].split()[1],
(DFA2[1].split()[2], DFA2[3].split()[1]): DFA2[4].split()[0],
}

t3 = dict()

for r in DFA1[1].split()[0], DFA1[1].split()[1]:

    for s in DFA2[1].split()[1], DFA2[1].split()[2]:

        for c in [DFA1[3].split()[0], DFA1[3].split()[1], DFA2[3].split()[0], DFA2[3].split()[1]]:

            t3[(r + s, c)] = t1[(r, c)] + t2[(s, c)]
            #
print(t3)

#Construction Method #3: Getting the Start State of DFA "N"
DFAN_StartState = set()
for DFA1_StartState in DFA1[6].split():
    for DFA2_StartState in DFA2[7].split():
        DFAN_StartState.add(DFA1_StartState + DFA2_StartState)
print(DFAN_StartState)

这个实现的代码接受这两个文本文件结构。

DFA1 文本文件:

2 #Number of DFA states - line index[0]
A B #DFA States - line index[1], A[split index 0], B[split index 1]
2 #Alphabet size - line index[2]
a b #alphabet symbols - line index [3] , symbol "a"[split index 0], symbol "b"[split index 1]
B A #Transition - line index [4], B[split index 0] & A[split index 1]
A B #Transition - line index [5], A[split index 0] & B[split index 2] 
A #DFA Start State - line index [6]
1 #Number of Final DFA States - line index [7]
A #DFA Final State - line index [8]

DFA2 文本文件:

3 #Number of DFA states - line index[0]
x y z #DFA states - line index[1] x[split index 0], y[split index 1], z[split index 2]
2 #Alphabet size - line index [3]
a b #alphabet symbols - line index [4] symbol "a" [split line index 0]. b[split line index 1]
z y # Transition - line index [5], z[split index 0] & y[split index 1]
z z # Transition - line index [6], z[split index 0] & z[split index 1]
y z # Transition - line index [7], y[split index 0] & z[split index 1]
x # DFA Start State - line index [8]
2 # Number of DFA Final States - line index [9]
x y # DFA Final States - line index [10]

这两个文本文件的输出是:

{'Ax', 'By', 'Az', 'Bx', 'Bz', 'Ay'}
{('Ay', 'a'): 'Bz', ('Ay', 'b'): 'Az', ('Az', 'a'): 'By', ('Az', 'b'): 'Az', ('By', 'a'): 'Az', ('By', 'b'): 'Bz', ('Bz', 'a'): 'Ay', ('Bz', 'b'): 'Bz'}
{'Ax'}

我想知道的是如何编写一个可以接受任何文本文件的程序,并执行与这两个文本文件相同的事情,尽管结构不同。例如,我有这两个要配对的文本文件:

DFA 示例文本文件 1:

6 - #Number of states, line index [0]
1 2 3 4 5 6 - #States, line index [1], 1 [split index 0], 2[split index 1], 3[split index 2], 4[split index 3], 5[split index 4], 6[split index 5]
2 – Alphabet, Line index [2] 
a b #alphabet symbols - line index [3] symbol "a" [split line index 0]. b[split line index 1]
3 2 – # Transition - line index [4], 3[split index 0] & 2[split index 1]
4 2 – # Transition - line index [5], 4[split index 0] & 2[split index 1]
5 4 – # Transition - line index [6], 5[split index 0] & 4[split index 1]
5 4 – # Transition - line index [7], 5[split index 0] & 4[split index 1]
6 6 – # Transition - line index [8], 6[split index 0] & 6[split index 1]
6 6 – # Transition - line index [9], 5[split index 0] & 4[split index 1]
1 – Start State, Line index [10]
1 – Number of Final states, Line index [11]
5 – Final state, Line index[12]

DFA 示例文本文件 2:

4 – #Number of states, line index [0]
1 2 3 4 – - #States, line index [1], 1 [split index 0], 2[split index 1], 3[split index 2], 4[split index 3],
2 – Alphabet, Line index [2] 
a b #alphabet symbols - line index [3] symbol "a" [split line index 0]. b[split line index 1]
2 1 – # Transition - line index [4], 2[split index 0] & 1[split index 1]
3 2 – # Transition - line index [5], 3[split index 0] & 2[split index 1]
4 4 –  # Transition - line index [6], 4[split index 0] & 4[split index 1]
4 4 – # Transition - line index [7], 4[split index 0] & 4[split index 1]
1 – Start State, Line index [8]
1 – Number of final states, Index [9]
3 – Final State [Index 10]

有没有一种可能的方法来编写我的代码,它需要任何两个文本文件,并且仍然像我的 DFA1 和 DFA2 文本文件一样成功输出,尽管结构不同?想法/建议将不胜感激。

标签: pythontext-filesdfa

解决方案


推荐阅读