Skip to content

Commit 0ff60b6

Browse files
committed
Automata v.1.2.0
NFA module incorporated; examples of DFA and NFA setted on different files and with differents automatons :3
1 parent 8e65c7e commit 0ff60b6

File tree

7 files changed

+366
-72
lines changed

7 files changed

+366
-72
lines changed

dfa-example-1.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from dfa import DFA
2+
3+
if __name__ == "__main__":
4+
5+
# First example of DFA Automata instance:
6+
# Language of the Automata:
7+
# L(Automata) = {(b^n)a(w): n>=0, w in {a, b}*};
8+
# Any string with at least one "a";
9+
Automata = DFA()
10+
Automata.setStates({"q0", "q1"})
11+
Automata.setAlphabet({"a", "b"})
12+
Automata.setInitial("q0")
13+
Automata.setFinals({"q1"})
14+
Automata.addTransition(("q0", "a", "q1"))
15+
Automata.addTransition(("q0", "b", "q0"))
16+
Automata.addTransition(("q1", "a", "q1"))
17+
Automata.addTransition(("q1", "b", "q1"))
18+
19+
#/ Executes the Automata:
20+
while True:
21+
print()
22+
word = input("Cadena: ")
23+
if Automata.accepts(word, stepByStep=True):
24+
print(f"La cadena \"{word}\" SÍ es aceptada!")
25+
else:
26+
print(f"La cadena \"{word}\" NO es aceptada!")

dfa-example-2.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
from dfa import DFA
2+
3+
if __name__ == "__main__":
4+
5+
# Second example of DFA Automata instance:
6+
# Language of the Automata:
7+
# L(Automata) = All strings with at least one "a",
8+
# and exactly two b's:
9+
#* States:
10+
Q = {"q0", "qa", "q1", "qb", "q2", "qf", "qx"}
11+
12+
#* Alphabet:
13+
A = {"a", "b"}
14+
15+
#* Transitions:
16+
T = []
17+
18+
#* Starting state:
19+
S = "q0"
20+
21+
#* Finals states:
22+
F = {"qf"}
23+
24+
#* Transitions definition:
25+
T.append( ("q0", "a", "qa") )
26+
T.append( ("q0", "b", "q1") )
27+
28+
T.append( ("qa", "a", "qa") )
29+
T.append( ("qa", "b", "qb") )
30+
31+
T.append( ("q1", "a", "qb") )
32+
T.append( ("q1", "b", "q2") )
33+
34+
T.append( ("qb", "a", "qb") )
35+
T.append( ("qb", "b", "qf") )
36+
37+
T.append( ("q2", "a", "qf") )
38+
T.append( ("q2", "b", "qx") )
39+
40+
T.append( ("qf", "a", "qf") )
41+
T.append( ("qf", "b", "qx") )
42+
43+
T.append( ("qx", "a", "qx") )
44+
T.append( ("qx", "b", "qx") )
45+
46+
#? Automata:
47+
Automata = DFA(Q, A, T, S, F)
48+
49+
#/ Executes the Automata:
50+
while True:
51+
print()
52+
word = input("Cadena: ")
53+
if Automata.accepts(word, stepByStep=True):
54+
print(f"La cadena \"{word}\" SÍ es aceptada!")
55+
else:
56+
print(f"La cadena \"{word}\" NO es aceptada!")

dfa.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def __init__(self, states: set={}, alphabet: set={}, transitions: list=[], initi
4848
* "a" is the symbol that will read on the actual state;
4949
* "q1" is the next state after the symbol reading;
5050
Example of transitions set:
51-
{} ("q0", "a", "q1"), ("q0", "b", "q1"), ("q1", "a", "q1")" };
51+
{ ("q0", "a", "q1"), ("q0", "b", "q1"), ("q1", "a", "q1")" };
5252
5353
4. "initial" (string): Represents your initial state.
5454
If it is not included in "states", it will add on it;
@@ -62,16 +62,14 @@ def __init__(self, states: set={}, alphabet: set={}, transitions: list=[], initi
6262

6363
# The values of the automata #
6464
self.States = states
65-
self.Alphabet = states
65+
self.Alphabet = alphabet
6666
self.Transitions = transitions
6767
self.Initial = initial
6868
self.Finals = finals
6969
self.actual = initial
70-
self.transitionsCount = 0
7170

7271
#* Getter:
7372
def __getattribute__(self, __name: str):
74-
# print('__getattribute__ ', __name)
7573
return super(DFA, self).__getattribute__(__name)
7674

7775
#* Setters:

examples-dfa.py

Lines changed: 0 additions & 68 deletions
This file was deleted.

nfa-example-1.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from nfa import NFA
2+
3+
if __name__ == "__main__":
4+
5+
# First example of NFA Automata instance:
6+
# Language of the Automata:
7+
# L(Automata) = {aba(b^n): n>0} U
8+
# {ab(a^n): n>=0};
9+
#* States:
10+
Q = {"q0", "qa", "qab", "qf", "qabf"}
11+
12+
#* Alphabet:
13+
A = {"a", "b"}
14+
15+
#* Transitions:
16+
T = []
17+
18+
#* Starting state:
19+
S = "q0"
20+
21+
#* Finals states:
22+
F = {"qf", "qabf"}
23+
24+
#* Transitions definition:
25+
T.append( ("q0", "a", "qa") )
26+
T.append( ("qa", "b", "qab") )
27+
T.append( ("qab", "a", "qf") )
28+
T.append( ("qf", "b", "qf") )
29+
T.append( ("qab", "", "qabf") )
30+
T.append( ("qabf", "a", "qabf") )
31+
32+
#? Automata:
33+
Automata = NFA(Q, A, T, S, F)
34+
35+
#/ Executes the Automata:
36+
while True:
37+
print()
38+
word = input("Cadena: ")
39+
if Automata.accepts(word, stepByStep=True):
40+
print(f"La cadena \"{word}\" SÍ es aceptada!")
41+
else:
42+
print(f"La cadena \"{word}\" NO es aceptada!")

nfa-example-2.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from nfa import NFA
2+
3+
if __name__ == "__main__":
4+
5+
# Second example of NFA Automata instance:
6+
# Language of the Automata:
7+
# L(Automata) = {(a^n): n>=0} U {(b^n)a: n>=1};
8+
Automata = NFA()
9+
Automata.setStates({"q0", "qb", "qa", "qba"})
10+
Automata.setAlphabet({"a", "b"})
11+
Automata.setInitial("q0")
12+
Automata.setFinals({"qa", "qba"})
13+
Automata.addTransition(("q0", "", "qa"))
14+
Automata.addTransition(("q0", "b", "qb"))
15+
Automata.addTransition(("qa", "a", "qa"))
16+
Automata.addTransition(("qb", "a", "qba"))
17+
Automata.addTransition(("qb", "b", "qb"))
18+
19+
#/ Executes the Automata:
20+
while True:
21+
print()
22+
word = input("Cadena: ")
23+
if Automata.accepts(word, stepByStep=True):
24+
print(f"La cadena \"{word}\" SÍ es aceptada!")
25+
else:
26+
print(f"La cadena \"{word}\" NO es aceptada!")

0 commit comments

Comments
 (0)