Skip to content

Commit a7200f2

Browse files
committed
update regex and FA code examples
1 parent fcc5b29 commit a7200f2

File tree

5 files changed

+159
-56
lines changed

5 files changed

+159
-56
lines changed

pyformlang/finite_automaton/deterministic_finite_automaton.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ def copy(self) -> "DeterministicFiniteAutomaton":
207207
>>> dfa_copy = dfa.copy()
208208
>>> dfa.is_equivalent_to(dfa_copy)
209209
True
210+
>>> dfa_copy is dfa
211+
False
210212
"""
211213
return self._copy_to(DeterministicFiniteAutomaton())
212214

@@ -283,6 +285,18 @@ def from_epsilon_nfa(cls, enfa: EpsilonNFA) \
283285
Returns
284286
-------
285287
A deterministic automaton equivalent to `enfa`.
288+
289+
Examples
290+
--------
291+
>>> enfa = EpsilonNFA()
292+
>>> enfa.add_transitions([(0, "epsilon", 1), (1, "a", 2), (1, "a", 3)])
293+
>>> enfa.add_start_state(0)
294+
>>> enfa.add_final_state(2)
295+
>>> dfa = DeterministicFiniteAutomaton.from_epsilon_nfa(enfa)
296+
>>> dfa.is_deterministic()
297+
True
298+
>>> dfa.accepts("a")
299+
True
286300
"""
287301
return cls._from_epsilon_nfa_internal(enfa, True)
288302

@@ -299,6 +313,18 @@ def from_nfa(cls, nfa: NondeterministicFiniteAutomaton) \
299313
Returns
300314
-------
301315
A deterministic automaton equivalent to `nfa`.
316+
317+
Examples
318+
--------
319+
>>> nfa = NondeterministicFiniteAutomaton()
320+
>>> nfa.add_transitions([(0, "a", 1), (1, "b", 2), (1, "b", 3)])
321+
>>> nfa.add_start_state(0)
322+
>>> nfa.add_final_state(2)
323+
>>> dfa = DeterministicFiniteAutomaton.from_nfa(nfa)
324+
>>> dfa.is_deterministic()
325+
True
326+
>>> dfa.accepts("ab")
327+
True
302328
"""
303329
return cls._from_epsilon_nfa_internal(nfa, False)
304330

pyformlang/finite_automaton/epsilon_nfa.py

Lines changed: 61 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,12 @@ def accepts(self, word: Iterable[Hashable]) -> bool:
106106
Examples
107107
--------
108108
>>> enfa = EpsilonNFA()
109-
>>> enfa.add_transitions([(0, "abc", 1), (0, "d", 1), \
110-
(0, "epsilon", 2)])
109+
>>> enfa.add_transitions(
110+
[(0, "abc", 1), (0, "d", 1), (0, "epsilon", 2)])
111111
>>> enfa.add_start_state(0)
112112
>>> enfa.add_final_state(1)
113113
>>> enfa.accepts(["abc", "epsilon"])
114114
True
115-
116115
>>> enfa.accepts(["epsilon"])
117116
False
118117
"""
@@ -141,12 +140,12 @@ def eclose_iterable(self, states: Iterable[Hashable]) -> Set[State]:
141140
Examples
142141
--------
143142
>>> enfa = EpsilonNFA()
144-
>>> enfa.add_transitions([(0, "abc", 1), (0, "d", 1), \
145-
(0, "epsilon", 2)])
143+
>>> enfa.add_transitions(
144+
[(0, "abc", 1), (0, "d", 1), (0, "epsilon", 2)])
146145
>>> enfa.add_start_state(0)
147146
>>> enfa.add_final_state(1)
148147
>>> enfa.eclose_iterable([0])
149-
{2}
148+
{0, 2}
150149
"""
151150
states = [to_state(x) for x in states]
152151
res = set()
@@ -169,12 +168,12 @@ def eclose(self, state: Hashable) -> Set[State]:
169168
Examples
170169
--------
171170
>>> enfa = EpsilonNFA()
172-
>>> enfa.add_transitions([(0, "abc", 1), (0, "d", 1), \
173-
(0, "epsilon", 2)])
171+
>>> enfa.add_transitions(
172+
[(0, "abc", 1), (0, "d", 1), (0, "epsilon", 2)])
174173
>>> enfa.add_start_state(0)
175174
>>> enfa.add_final_state(1)
176175
>>> enfa.eclose(0)
177-
{2}
176+
{0, 2}
178177
"""
179178
state = to_state(state)
180179
to_process = [state]
@@ -198,8 +197,7 @@ def is_deterministic(self) -> bool:
198197
Examples
199198
--------
200199
>>> enfa = EpsilonNFA()
201-
>>> enfa.add_transitions([(0, "abc", 1), (0, "d", 1), \
202-
(0, "epsilon", 2)])
200+
>>> enfa.add_transitions([(0, "abc", 1), (0, "epsilon", 2)])
203201
>>> enfa.add_start_state(0)
204202
>>> enfa.add_final_state(1)
205203
>>> enfa.is_deterministic()
@@ -219,13 +217,16 @@ def copy(self) -> "EpsilonNFA":
219217
Examples
220218
--------
221219
>>> enfa = EpsilonNFA()
222-
>>> enfa.add_transitions([(0, "abc", 1), (0, "d", 1), \
223-
(0, "epsilon", 2)])
220+
>>> enfa.add_transitions([(0, "abc", 1), (0, "epsilon", 2)])
224221
>>> enfa.add_start_state(0)
225222
>>> enfa.add_final_state(1)
226223
>>> enfa_copy = enfa.copy()
227-
>>> enfa.is_equivalent_to(enfa_copy)
224+
>>> enfa.states == enfa_copy.states
225+
True
226+
>>> enfa_copy.accepts(["abc"])
228227
True
228+
>>> enfa_copy is enfa
229+
False
229230
"""
230231
return self._copy_to(EpsilonNFA())
231232

@@ -253,8 +254,8 @@ def from_networkx(cls, graph: MultiDiGraph) -> "EpsilonNFA":
253254
Examples
254255
--------
255256
>>> enfa = EpsilonNFA()
256-
>>> enfa.add_transitions([(0, "abc", 1), (0, "d", 1), \
257-
(0, "epsilon", 2)])
257+
>>> enfa.add_transitions(
258+
[(0, "abc", 1), (0, "d", 1), (0, "epsilon", 2)])
258259
>>> enfa.add_start_state(0)
259260
>>> enfa.add_final_state(1)
260261
>>> graph = enfa.to_networkx()
@@ -288,14 +289,13 @@ def get_complement(self) -> "EpsilonNFA":
288289
Examples
289290
--------
290291
>>> enfa = EpsilonNFA()
291-
>>> enfa.add_transitions([(0, "abc", 1), (0, "d", 1), \
292-
(0, "epsilon", 2)])
292+
>>> enfa.add_transitions(
293+
[(0, "abc", 1), (0, "d", 1), (0, "epsilon", 2)])
293294
>>> enfa.add_start_state(0)
294295
>>> enfa.add_final_state(1)
295296
>>> enfa_complement = enfa.get_complement()
296297
>>> enfa_complement.accepts(["epsilon"])
297298
True
298-
299299
>>> enfa_complement.accepts(["abc"])
300300
False
301301
"""
@@ -346,8 +346,8 @@ def get_intersection(self, other: "EpsilonNFA") -> "EpsilonNFA":
346346
Examples
347347
--------
348348
>>> enfa = EpsilonNFA()
349-
>>> enfa.add_transitions([(0, "abc", 1), (0, "d", 1), \
350-
(0, "epsilon", 2)])
349+
>>> enfa.add_transitions(
350+
[(0, "abc", 1), (0, "d", 1), (0, "epsilon", 2)])
351351
>>> enfa.add_start_state(0)
352352
>>> enfa.add_final_state(1)
353353
>>> enfa2 = EpsilonNFA()
@@ -357,7 +357,6 @@ def get_intersection(self, other: "EpsilonNFA") -> "EpsilonNFA":
357357
>>> enfa_inter = enfa.get_intersection(enfa2)
358358
>>> enfa_inter.accepts(["abc"])
359359
False
360-
361360
>>> enfa_inter.accepts(["d"])
362361
True
363362
"""
@@ -414,6 +413,18 @@ def get_union(self, other: "EpsilonNFA") -> "EpsilonNFA":
414413
Returns
415414
-------
416415
The union of the two Epsilon NFAs.
416+
417+
Examples
418+
--------
419+
>>> enfa0 = Regex("ab").to_epsilon_nfa()
420+
>>> enfa1 = Regex("c*").to_epsilon_nfa()
421+
>>> union = enfa0.get_union(enfa1)
422+
>>> union.accepts(["ab"])
423+
True
424+
>>> union.accepts([])
425+
True
426+
>>> union.accepts(["c", "c", "c"])
427+
True
417428
"""
418429
union = EpsilonNFA()
419430
self.__copy_transitions_marked(self, union, 0)
@@ -458,6 +469,18 @@ def concatenate(self, other: "EpsilonNFA") -> "EpsilonNFA":
458469
Returns
459470
-------
460471
The concatenation of the two Epsilon NFAs.
472+
473+
Examples
474+
--------
475+
>>> enfa0 = Regex("a").to_epsilon_nfa()
476+
>>> enfa1 = Regex("b|c").to_epsilon_nfa()
477+
>>> concatenation = enfa0.concatenate(enfa1)
478+
>>> concatenation.accepts(["a", "b"])
479+
True
480+
>>> concatenation.accepts(["a", "c"])
481+
True
482+
>>> concatenation.accepts(["a"])
483+
False
461484
"""
462485
concatenation = EpsilonNFA()
463486
self.__copy_transitions_marked(self, concatenation, 0)
@@ -505,8 +528,8 @@ def get_difference(self, other: "EpsilonNFA") -> "EpsilonNFA":
505528
Examples
506529
--------
507530
>>> enfa = EpsilonNFA()
508-
>>> enfa.add_transitions([(0, "abc", 1), (0, "d", 1), \
509-
(0, "epsilon", 2)])
531+
>>> enfa.add_transitions(
532+
[(0, "abc", 1), (0, "d", 1), (0, "epsilon", 2)])
510533
>>> enfa.add_start_state(0)
511534
>>> enfa.add_final_state(1)
512535
>>> enfa2 = EpsilonNFA()
@@ -516,7 +539,6 @@ def get_difference(self, other: "EpsilonNFA") -> "EpsilonNFA":
516539
>>> enfa_diff = enfa.get_difference(enfa2)
517540
>>> enfa_diff.accepts(["d"])
518541
False
519-
520542
>>> enfa_diff.accepts(["abc"])
521543
True
522544
"""
@@ -587,6 +609,18 @@ def kleene_star(self) -> "EpsilonNFA":
587609
Returns
588610
-------
589611
The kleene closure of current Epsilon NFA.
612+
613+
Examples
614+
--------
615+
>>> enfa = EpsilonNFA()
616+
>>> enfa.add_transition(0, "a", 1)
617+
>>> enfa.add_start_state(0)
618+
>>> enfa.add_final_state(1)
619+
>>> kleene_star = enfa.kleene_star()
620+
>>> kleene_star.accepts([])
621+
True
622+
>>> kleene_star.accepts(["a", "a"])
623+
True
590624
"""
591625
new_start = self.__get_new_state("Start")
592626
kleene_closure = EpsilonNFA(start_states={new_start},
@@ -608,8 +642,8 @@ def is_empty(self) -> bool:
608642
Examples
609643
--------
610644
>>> enfa = EpsilonNFA()
611-
>>> enfa.add_transitions([(0, "abc", 1), (0, "d", 1), \
612-
(0, "epsilon", 2)])
645+
>>> enfa.add_transitions(
646+
[(0, "abc", 1), (0, "d", 1), (0, "epsilon", 2)])
613647
>>> enfa.add_start_state(0)
614648
>>> enfa.add_final_state(1)
615649
>>> enfa.is_empty()

0 commit comments

Comments
 (0)