-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_parser.py
77 lines (59 loc) · 2.2 KB
/
file_parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from typing import IO, List, Optional, TypeVar
from afd import Afd
from afd_types import State, Symbol, Transition, Transitions
T = TypeVar("T")
class FileParser:
def __init__(self, file: Optional[IO]) -> None:
self.file: Optional[IO] = file.read()
self.lines = str(self.file).split("\n")
def process_words(self):
return self.lines
def process_afd(self) -> Afd:
return Afd(
self.__get_name(),
self.__get_states(),
self.__get_alphabet(),
self.__get_initial_state(),
self.__get_final_states(),
self.__get_transitions()
)
def __get_name(self) -> str:
return self.lines[0]
def __get_sublist(self, index) -> str:
return self.lines[index].split(" ")[1]
def __get_states(self) -> List[State]:
state_substr: State = self.__get_sublist(1)
return FileParser.parse_to_list(state_substr)
def __get_alphabet(self) -> List[Symbol]:
symbol_substr: State = self.__get_sublist(2)
return FileParser.parse_to_list(symbol_substr)
def __get_initial_state(self) -> State:
symbol_substr: State = self.__get_sublist(3)
return symbol_substr
def __get_final_states(self) -> List[State]:
state_substr: State = self.__get_sublist(4)
return FileParser.parse_to_list(state_substr)
def __get_transitions(self) -> Transitions:
break_point = self.lines.index("")
transitions_in_str = self.lines[break_point + 1:]
return list(
filter(None, map(
lambda s: FileParser.parse_transition(s),
transitions_in_str
)
)
)
@staticmethod
def parse_to_list(str_list: T) -> List[T]:
return str_list.split(",")
@staticmethod
def parse_transition(transition: str) -> Transition:
if transition == '':
return
else:
transition = transition.replace("(", "").replace(")", "")
return tuple(transition.split(","))
if __name__ == "__main__":
arquivo = open("automato.txt", mode="r")
meu_processador = FileParser(arquivo)
afd_gerado = meu_processador.process_afd()