Skip to content

Commit bfb28c2

Browse files
committed
update examples with new implementation
1 parent 711529b commit bfb28c2

16 files changed

+206
-141
lines changed

examples/example1_dfa.py

+13-7
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,22 @@
1111
"""
1212
from fsmdot.dfa import Dfa
1313

14-
Q = ['S1', 'S2']
15-
S = ['0', '1']
16-
T = [
17-
['S2', 'S1'],
18-
['S1', 'S2']
19-
]
14+
Q = {'S1', 'S2'}
15+
S = {'0', '1'}
16+
d = {
17+
'S1': {
18+
'0': 'S2',
19+
'1': 'S1'
20+
},
21+
'S2': {
22+
'0': 'S1',
23+
'1': 'S2'
24+
}
25+
}
2026
q0 = 'S1'
2127
F = {'S1'}
2228

23-
a = Dfa(Q, S, T, q0, F)
29+
a = Dfa(Q, S, d, q0, F)
2430
a.print_table()
2531

2632
print(a.accept('11110'))

examples/example2_dfa.py

+17-8
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,26 @@
1212
"""
1313
from fsmdot.dfa import Dfa
1414

15-
Q = ['S0', 'S1', 'S2']
16-
S = ['0', '1']
17-
T = [
18-
['S0', 'S1'],
19-
['S2', 'S0'],
20-
['S1', 'S2']
21-
]
15+
Q = {'S0', 'S1', 'S2'}
16+
S = {'0', '1'}
17+
d = {
18+
'S0': {
19+
'0': 'S0',
20+
'1': 'S1'
21+
},
22+
'S1': {
23+
'0': 'S2',
24+
'1': 'S0'
25+
},
26+
'S2': {
27+
'0': 'S1',
28+
'1': 'S2'
29+
}
30+
}
2231
q0 = 'S0'
2332
F = {'S0'}
2433

25-
a = Dfa(Q, S, T, q0, F)
34+
a = Dfa(Q, S, d, q0, F)
2635
a.print_table()
2736

2837
print(a.accept('1001'))

examples/example3_nfa.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,18 @@
1111
"""
1212
from fsmdot.nfa import Nfa
1313

14-
Q = ['p', 'q']
15-
S = ['0', '1']
16-
T = [
17-
[{'p'}, {'p', 'q'}],
18-
[{}, {}]
19-
]
14+
Q = {'p', 'q'}
15+
S = {'0', '1'}
16+
d = {
17+
'p': {
18+
'0': {'p'},
19+
'1': {'p', 'q'}
20+
}
21+
}
2022
q0 = 'p'
2123
F = {'q'}
2224

23-
a = Nfa(Q, S, T, q0, F)
25+
a = Nfa(Q, S, d, q0, F)
2426
a.print_table()
2527

2628
print(a.accept('11110'))

examples/example4_nfa.py

+19-9
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,28 @@
99
"""
1010
from fsmdot.nfa import Nfa
1111

12-
Q = [0, 1, 2, 3]
13-
S = ['a', 'b']
14-
T = [
15-
[{0, 1}, {0}],
16-
[{2}, {}],
17-
[{3}, {}],
18-
[{3}, {3}]
19-
]
12+
Q = {0, 1, 2, 3}
13+
S = {'a', 'b'}
14+
d = {
15+
0: {
16+
'a': {0, 1},
17+
'b': {0}
18+
},
19+
1: {
20+
'a': {2}
21+
},
22+
2: {
23+
'a': {3}
24+
},
25+
3: {
26+
'a': {3},
27+
'b': {3}
28+
}
29+
}
2030
q0 = 0
2131
F = {3}
2232

23-
a = Nfa(Q, S, T, q0, F)
33+
a = Nfa(Q, S, d, q0, F)
2434
a.print_table()
2535

2636
print(a.accept('ababaaabaaaaabababa'))

examples/example5_nfa.py

+24-10
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,33 @@
1313
"""
1414
from fsmdot.nfa import Nfa
1515

16-
Q = ['S0', 'S1', 'S2', 'S3', 'S4']
17-
S = ['0', '1', Nfa.EPSILON]
18-
T = [
19-
[{}, {}, {'S1', 'S3'}],
20-
[{'S2'}, {'S1'}, {}],
21-
[{'S1'}, {'S2'}, {}],
22-
[{'S3'}, {'S4'}, {}],
23-
[{'S4'}, {'S3'}, {}],
24-
]
16+
Q = {'S0', 'S1', 'S2', 'S3', 'S4'}
17+
S = {'0', '1', Nfa.EPSILON}
18+
d = {
19+
'S0': {
20+
Nfa.EPSILON: {'S1', 'S3'}
21+
},
22+
'S1': {
23+
'0': {'S2'},
24+
'1': {'S1'}
25+
},
26+
'S2': {
27+
'0': {'S1'},
28+
'1': {'S2'}
29+
},
30+
'S3': {
31+
'0': {'S3'},
32+
'1': {'S4'}
33+
},
34+
'S4': {
35+
'0': {'S4'},
36+
'1': {'S3'}
37+
}
38+
}
2539
q0 = 'S0'
2640
F = {'S1', 'S3'}
2741

28-
a = Nfa(Q, S, T, q0, F)
42+
a = Nfa(Q, S, d, q0, F)
2943
a.print_table()
3044

3145
for string in ['1001', '10101', '10', '01']:

examples/example6_nfa.py

+19-9
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,28 @@
1414
"""
1515
from fsmdot.nfa import Nfa
1616

17-
Q = [1, 2, 3, 4]
18-
S = [Nfa.EPSILON, '0', '1']
19-
T = [
20-
[{3}, {2}, {}],
21-
[{}, {}, {2, 4}],
22-
[{2}, {4}, {}],
23-
[{}, {3}, {}]
24-
]
17+
Q = {1, 2, 3, 4}
18+
S = {Nfa.EPSILON, '0', '1'}
19+
d = {
20+
1: {
21+
Nfa.EPSILON: {3},
22+
'0': {2}
23+
},
24+
2: {
25+
'1': {2, 4}
26+
},
27+
3: {
28+
Nfa.EPSILON: {2},
29+
'0': {4}
30+
},
31+
4: {
32+
'0': {3}
33+
}
34+
}
2535
q0 = 1
2636
F = {3, 4}
2737

28-
a = Nfa(Q, S, T, q0, F)
38+
a = Nfa(Q, S, d, q0, F)
2939
a.print_table()
3040

3141
G = a.dot_graph()

examples/example7_nfa.py

+21-10
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,30 @@
1414
"""
1515
from fsmdot.nfa import Nfa
1616

17-
Q = ['X', '0', '1', '2', '3']
18-
S = ['0', '1']
19-
T = [
20-
[{'X'}, {'X', '0'}],
21-
[{'1'}, {'1'}],
22-
[{'2'}, {'2'}],
23-
[{'3'}, {'3'}],
24-
[{}, {}]
25-
]
17+
Q = {'X', '0', '1', '2', '3'}
18+
S = {'0', '1'}
19+
d = {
20+
'X': {
21+
'0': {'X'},
22+
'1': {'X', '0'}
23+
},
24+
'0': {
25+
'0': {'1'},
26+
'1': {'1'}
27+
},
28+
'1': {
29+
'0': {'2'},
30+
'1': {'2'}
31+
},
32+
'2': {
33+
'0': {'3'},
34+
'1': {'3'}
35+
}
36+
}
2637
q0 = 'X'
2738
F = {'3'}
2839

29-
a = Nfa(Q, S, T, q0, F)
40+
a = Nfa(Q, S, d, q0, F)
3041
a.print_table()
3142

3243
G = a.dot_graph()

examples/graph1_dfa.dot

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
strict digraph DFA {
1+
strict digraph FSM {
22
graph [rankdir=LR];
33
node [shape=circle];
4+
null [shape=point];
45
S1 [shape=doublecircle];
6+
null -> S1;
57
S1 -> S1 [label=1];
68
S1 -> S2 [label=0];
79
S2 -> S1 [label=0];
810
S2 -> S2 [label=1];
9-
null [shape=point];
10-
null -> S1;
1111
}

examples/graph2_dfa.dot

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
strict digraph DFA {
1+
strict digraph FSM {
22
graph [rankdir=LR];
33
node [shape=circle];
4+
null [shape=point];
45
S0 [shape=doublecircle];
6+
null -> S0;
57
S0 -> S0 [label=0];
68
S0 -> S1 [label=1];
9+
S2 -> S2 [label=1];
10+
S2 -> S1 [label=0];
711
S1 -> S0 [label=1];
812
S1 -> S2 [label=0];
9-
S2 -> S1 [label=0];
10-
S2 -> S2 [label=1];
11-
null [shape=point];
12-
null -> S0;
1313
}

examples/graph3_nfa.dot

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
strict digraph NFA {
1+
strict digraph FSM {
22
graph [rankdir=LR];
33
node [shape=circle];
4-
p -> p [label="0, 1"];
5-
q [shape=doublecircle];
6-
p -> q [label=1];
74
null [shape=point];
85
null -> p;
6+
q [shape=doublecircle];
7+
p -> q [label=1];
8+
p -> p [label="0, 1"];
99
}

examples/graph4_nfa.dot

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
strict digraph NFA {
1+
strict digraph FSM {
22
graph [rankdir=LR];
33
node [shape=circle];
4+
null [shape=point];
5+
null -> 0;
46
0 -> 0 [label="a, b"];
57
0 -> 1 [label=a];
68
1 -> 2 [label=a];
79
3 [shape=doublecircle];
810
2 -> 3 [label=a];
911
3 -> 3 [label="a, b"];
10-
null [shape=point];
11-
null -> 0;
1212
}

examples/graph5_nfa.dot

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
strict digraph NFA {
1+
strict digraph FSM {
22
graph [rankdir=LR];
33
node [shape=circle];
4-
S1 [shape=doublecircle];
5-
S0 -> S1 [label=ε];
4+
null [shape=point];
5+
null -> S0;
66
S3 [shape=doublecircle];
7-
S0 -> S3 [label=ε];
8-
S1 -> S1 [label=1];
9-
S1 -> S2 [label=0];
10-
S2 -> S1 [label=0];
11-
S2 -> S2 [label=1];
127
S3 -> S3 [label=0];
138
S3 -> S4 [label=1];
9+
S1 [shape=doublecircle];
10+
S1 -> S1 [label=1];
11+
S1 -> S2 [label=0];
12+
S0 -> S3 [label=ε];
13+
S0 -> S1 [label=ε];
1414
S4 -> S3 [label=1];
1515
S4 -> S4 [label=0];
16-
null [shape=point];
17-
null -> S0;
16+
S2 -> S1 [label=0];
17+
S2 -> S2 [label=1];
1818
}

examples/graph6_dfa.dot

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
strict digraph DFA {
1+
strict digraph FSM {
22
graph [rankdir=LR];
33
node [shape=circle];
4+
null [shape=point];
45
"{1, 2, 3}" [shape=doublecircle];
6+
null -> "{1, 2, 3}";
7+
"{4}" [shape=doublecircle];
8+
"{2, 3}" [shape=doublecircle];
9+
"{4}" -> "{2, 3}" [label=0];
510
"{2, 4}" [shape=doublecircle];
6-
"{1, 2, 3}" -> "{2, 4}" [label="0, 1"];
11+
"{1, 2, 3}" -> "{2, 4}" [label="1, 0"];
712
"{2, 4}" -> "{2, 4}" [label=1];
8-
"{2, 3}" [shape=doublecircle];
913
"{2, 4}" -> "{2, 3}" [label=0];
10-
"{2, 3}" -> "{2, 4}" [label=1];
11-
"{4}" [shape=doublecircle];
1214
"{2, 3}" -> "{4}" [label=0];
13-
"{4}" -> "{2, 3}" [label=0];
14-
null [shape=point];
15-
null -> "{1, 2, 3}";
15+
"{2, 3}" -> "{2, 4}" [label=1];
1616
}

examples/graph6_nfa.dot

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
strict digraph NFA {
1+
strict digraph FSM {
22
graph [rankdir=LR];
33
node [shape=circle];
4+
null [shape=point];
5+
null -> 1;
46
1 -> 2 [label=0];
57
3 [shape=doublecircle];
68
1 -> 3 [label=ε];
@@ -10,6 +12,4 @@ strict digraph NFA {
1012
3 -> 2 [label=ε];
1113
3 -> 4 [label=0];
1214
4 -> 3 [label=0];
13-
null [shape=point];
14-
null -> 1;
1515
}

0 commit comments

Comments
 (0)