Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ernesto Terra dos Santos - 202110469 #8

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 55 additions & 37 deletions src/automata.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,68 @@
"""Implementação de autômatos finitos."""
from typing import List, Dict, Tuple


def load_automata(filename):
"""
Lê os dados de um autômato finito a partir de um arquivo.
def load_automata(filename: str) -> Tuple:
try:
with open(filename, "r") as file:
lines = file.readlines()

A estsrutura do arquivo deve ser:
Sigma = lines[0].strip().split()
Q = lines[1].strip().split()
F = lines[2].strip().split()
q0 = lines[3].strip()
delta = {}

<lista de símbolos do alfabeto, separados por espaço (' ')>
<lista de nomes de estados>
<lista de nomes de estados finais>
<nome do estado inicial>
<lista de regras de transição, com "origem símbolo destino">
# Check if final states are in the set of states
if not set(F).issubset(Q):
raise Exception("Final states are not present in the set of states")

Um exemplo de arquivo válido é:
# Check if initial state is in the set of states
if q0 not in Q:
raise Exception("Initial state is not present in the set of states")

```
a b
q0 q1 q2 q3
q0 q3
q0
q0 a q1
q0 b q2
q1 a q0
q1 b q3
q2 a q3
q2 b q0
q3 a q1
q3 b q2
```
for line in lines[4:]:
src, symbol, dest = line.strip().split()

Caso o arquivo seja inválido uma exceção Exception é gerada.
# Check if transition leads to a state that is not in the set of states
if dest not in Q:
raise Exception(
"Transition leads to a state that is not in the set of states"
)

"""
# Check if transition starts from a state that is not in the set of states
if src not in Q:
raise Exception(
"Transition starts from a state that is not in the set of states"
)

with open(filename, "rt") as arquivo:
# processa arquivo...
pass
# Check if transition uses an invalid symbol
if symbol not in Sigma:
raise Exception("Transition uses an invalid symbol")

if src not in delta:
delta[src] = {}
delta[src][symbol] = dest

def process(automata, words):
"""
Processa a lista de palavras e retora o resultado.

Os resultados válidos são ACEITA, REJEITA, INVALIDA.
"""
return Q, Sigma, delta, q0, F
except Exception as e:
raise Exception("Invalid automaton file format") from e


def process(automata: Tuple, words: List[str]) -> Dict[str, str]:
Q, Sigma, delta, q0, F = automata
results = {}

for word in words:
# tenta reconhecer `word`
state = q0
for symbol in word:
if symbol not in Sigma:
results[word] = "INVALIDA"
break
if symbol not in delta[state]:
results[word] = "REJEITA"
break
state = delta[state][symbol]
else:
results[word] = "ACEITA" if state in F else "REJEITA"

return results
Loading