-
Notifications
You must be signed in to change notification settings - Fork 0
/
Automaton.py
90 lines (75 loc) · 2.62 KB
/
Automaton.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
78
79
80
81
82
83
84
85
86
87
88
89
class Automaton():
"""This class stores all the information obtained in the json doc"""
statePaht = []
pile = []
transitionPath = []
def __init__(self, rawAutomaton):
#this is a char
self.initialState = rawAutomaton["Initial State"]
#this is a list
self.setOfStates = rawAutomaton["Set Of States"]
#this is a list
self.alphabet = rawAutomaton["Alphabet"]
#this is a complex dictionary
self.transitions = rawAutomaton["Transitions"]
#this is a list
self.setOfFinalStates = rawAutomaton["Set Of Final States"]
#this is a list
self.stringsToEvaluate = rawAutomaton["Strings To Evaluate"]
#this is were the initial symbol has come
self.pile = ["!"]
for string in self.stringsToEvaluate:
print ("Eval string:", string)
eval_string(self,string)
def eval_string(self, stringToEval):
for transition in self.transitions[self.initialState]:
self.pile.insert(0,transition[3])
self.actualState = transition[2]
errorFlag = True
for char in stringToEval:
if eval_char(self,char) == True:
continue
else:
errorFlag = False
break
if self.pile[0] == "!" and errorFlag == True:
print ("---------------- La cadena:", stringToEval, "si pertenece al lenguaje. ----------------")
else:
print ("---------------- La cadena:", stringToEval,"no pertenece al lenguaje. ----------------")
def insert_string(self, toInsert):
for char in toInsert:
self.pile.insert(0,char)
def eval_char(self, char):
canPop = True
while canPop:
canPop = False
for transition in self.transitions[self.actualState]:
if transition[0] == char and transition[0] != "":
if transition[1] == self.pile[0]: # Can consume and can pop the pile
print ("Can pop in trnasition:",transition)
self.pile.remove(transition[1]) #remove pile
if transition[3] != "":
insert_string(self, transition[3])
self.actualState = transition[2]
return True
for transition in self.transitions[self.actualState]:
if len(transition[3]) > 1:
if transition[3][0] == char and self.pile[0] == transition[1]: #can pop the pile and insert the char
print("Maybe this one:",transition)
canPop = True
self.pile.remove(transition[1])
insert_string(self, transition[3])
self.actualState = transition[2]
continue
else:
if transition[3] == char and self.pile[0] == transition[1]: #can pop the pile and insert the char
canPop = True
print("Maybe this one:",transition)
self.pile.remove(transition[1])
if transition[3] != "":
insert_string(self, transition[3])
self.actualState = transition[2]
continue
print("Did we pop?",canPop)
print("Bye")
return False