Skip to content

Commit a6a34e7

Browse files
committed
correct epsilon checks
1 parent e78a7ab commit a6a34e7

File tree

8 files changed

+16
-13
lines changed

8 files changed

+16
-13
lines changed

pyformlang/cfg/cfg.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -659,8 +659,6 @@ def contains(self, word: Iterable[Hashable]) -> bool:
659659
"""
660660
# Remove epsilons
661661
word = [to_terminal(x) for x in word if x != Epsilon()]
662-
if not word:
663-
return self.generate_epsilon()
664662
cyk_table = CYKTable(self, word)
665663
return cyk_table.generate_word()
666664

pyformlang/finite_automaton/tests/test_epsilon.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ def test_epsilon(self):
1616
symb = Symbol(0)
1717
assert eps0 == eps1
1818
assert eps0 != symb
19+
assert "epsilon" == Epsilon()

pyformlang/objects/cfg_objects/epsilon.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from typing import Any
44

55
from .terminal import Terminal
6+
from ..finite_automaton_objects.epsilon import EPSILON_SYMBOLS
67

78

89
class Epsilon(Terminal):
@@ -12,7 +13,7 @@ def __init__(self) -> None:
1213
super().__init__("epsilon")
1314

1415
def __eq__(self, other: Any) -> bool:
15-
return isinstance(other, Epsilon)
16+
return isinstance(other, Epsilon) or other in EPSILON_SYMBOLS
1617

1718
def __hash__(self) -> int:
1819
return super().__hash__()

pyformlang/objects/cfg_objects/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from .variable import Variable
66
from .terminal import Terminal
7-
from .epsilon import Epsilon
7+
from .epsilon import Epsilon, EPSILON_SYMBOLS
88

99

1010
def to_variable(given: Hashable) -> Variable:
@@ -18,6 +18,6 @@ def to_terminal(given: Hashable) -> Terminal:
1818
""" Transformation into a terminal """
1919
if isinstance(given, Terminal):
2020
return given
21-
if given == "epsilon":
21+
if given in EPSILON_SYMBOLS:
2222
return Epsilon()
2323
return Terminal(given)

pyformlang/objects/finite_automaton_objects/epsilon.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
from .symbol import Symbol
88

9+
EPSILON_SYMBOLS = ["epsilon", "ɛ"]
10+
911

1012
class Epsilon(Symbol):
1113
""" An epsilon transition
@@ -21,7 +23,7 @@ def __init__(self) -> None:
2123
super().__init__("epsilon")
2224

2325
def __eq__(self, other: Any) -> bool:
24-
return isinstance(other, Epsilon)
26+
return isinstance(other, Epsilon) or other in EPSILON_SYMBOLS
2527

2628
def __hash__(self) -> int:
27-
return hash("EPSILON TRANSITION")
29+
return super().__hash__()

pyformlang/objects/finite_automaton_objects/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from .state import State
66
from .symbol import Symbol
7-
from .epsilon import Epsilon
7+
from .epsilon import Epsilon, EPSILON_SYMBOLS
88

99

1010
def to_state(given: Hashable) -> State:
@@ -30,6 +30,6 @@ def to_symbol(given: Hashable) -> Symbol:
3030
"""
3131
if isinstance(given, Symbol):
3232
return given
33-
if given in ("epsilon", "ɛ"):
33+
if given in EPSILON_SYMBOLS:
3434
return Epsilon()
3535
return Symbol(given)

pyformlang/objects/pda_objects/epsilon.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from typing import Any
44

55
from .stack_symbol import StackSymbol
6+
from ..finite_automaton_objects.epsilon import EPSILON_SYMBOLS
67

78

89
class Epsilon(StackSymbol):
@@ -12,7 +13,7 @@ def __init__(self) -> None:
1213
super().__init__("epsilon")
1314

1415
def __eq__(self, other: Any) -> bool:
15-
return isinstance(other, Epsilon)
16+
return isinstance(other, Epsilon) or other in EPSILON_SYMBOLS
1617

1718
def __hash__(self) -> int:
1819
return super().__hash__()

pyformlang/objects/pda_objects/utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from .state import State
66
from .symbol import Symbol
77
from .stack_symbol import StackSymbol
8-
from .epsilon import Epsilon
8+
from .epsilon import Epsilon, EPSILON_SYMBOLS
99

1010

1111
def to_state(given: Hashable) -> State:
@@ -19,7 +19,7 @@ def to_symbol(given: Hashable) -> Symbol:
1919
""" Convert to a symbol """
2020
if isinstance(given, Symbol):
2121
return given
22-
if given == "epsilon":
22+
if given in EPSILON_SYMBOLS:
2323
return Epsilon()
2424
return Symbol(given)
2525

@@ -28,6 +28,6 @@ def to_stack_symbol(given: Hashable) -> StackSymbol:
2828
""" Convert to a stack symbol """
2929
if isinstance(given, StackSymbol):
3030
return given
31-
if given == "epsilon":
31+
if given in EPSILON_SYMBOLS:
3232
return Epsilon()
3333
return StackSymbol(given)

0 commit comments

Comments
 (0)