Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GNFA regex bug fix #123

Merged
merged 1 commit into from
Jan 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion automata/base/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def refine(self, S):
Not a generator because we need to perform the partition
even if the caller doesn't iterate through the results.
"""
hit = defaultdict(lambda: set())
hit = defaultdict(set)
output = []

for x in S:
Expand Down
2 changes: 1 addition & 1 deletion automata/fa/gnfa.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ def to_regex(self):
if r2 is None:
r2 = ''
elif len(r2) == 1:
r2 = f'{r1}*'
r2 = f'{r2}*'
else:
r2 = f'({r2})*'

Expand Down
32 changes: 22 additions & 10 deletions tests/test_gnfa.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,16 +345,28 @@ def test_to_regex(self):
then generate NFA from regex (already tested method)
and check for equivalence of NFA and previous DFA
"""

nfa = NFA.from_regex('a(aaa*bbcd|abbcd)d*|aa*bb(dcc*|(d|c)b|a?bb(dcc*|(d|c)))ab(c|d)*(ccd)?')
gnfa = GNFA.from_nfa(nfa)
regex = gnfa.to_regex()
nfa = NFA.from_regex(regex)
dfa2 = DFA.from_nfa(nfa)

dfa = DFA.from_nfa(nfa)

self.assertEqual(dfa, dfa2)
regex_strings = [
'a*',
'aa*b|bba*|(cc*)(bb+)',
'a(aaa*bbcd|abbcd)d*|aa*bb(dcc*|(d|c)b|a?bb(dcc*|(d|c)))ab(c|d)*(ccd)?'
]

for regex_str in regex_strings:
nfa_1 = NFA.from_regex(regex_str)
gnfa_1 = GNFA.from_nfa(nfa_1)
regex_1 = gnfa_1.to_regex()
nfa_2 = NFA.from_regex(regex_1)

# Test equality under NFA regex conversion
self.assertEqual(nfa_1, nfa_2)

dfa_1 = DFA.from_nfa(nfa_1)
gnfa_2 = GNFA.from_dfa(dfa_1)
regex_2 = gnfa_2.to_regex()
dfa_2 = DFA.from_nfa(NFA.from_regex(regex_2))

# Test equality through DFA regex conversion
self.assertEqual(dfa_1, dfa_2)

def test_read_input_step_not_implemented(self):
"""Should not implement read_input_stepwise() for GNFA."""
Expand Down