-
Notifications
You must be signed in to change notification settings - Fork 0
/
FSA.py
46 lines (34 loc) · 982 Bytes
/
FSA.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
import sys
from DFA import DFA
from GUI import Window
automaton = (sys.argv)[1]
stringFile = (sys.argv)[2]
def loadFileContent(fname):
with open(fname) as file:
content = file.read()
content = content.replace("\n", "")
return content
def tokenizeContent(content):
content = content.split(';')
for element in content:
element = element.strip()
return content
def printMessage(isAccepted):
if(isAccepted):
print(f"String '{string}' is accepted")
else:
print(f"String '{string}' is rejected")
tokens = loadFileContent(automaton)
fsaArray = tokenizeContent(tokens)
string = loadFileContent(stringFile)
fsa = DFA(fsaArray)
if(fsa.errorsExist()):
fsa.printErrors()
else:
if(fsa.validateString(string)):
isAccepted = fsa.isStringAccepted(string)
printMessage(isAccepted)
else:
print("Illegal string: character(s) not in alphabet")
window = Window(fsa)
window.showFSA()