Repository: caleb531/automata

Issue:
problems with GNFA.to_regex
GNFA.to_regex seems to give wrong results sometimes. For example, the following code:
```
from automata.fa.dfa import DFA
from automata.fa.nfa import NFA
from automata.fa.gnfa import GNFA

nfa = NFA.from_regex('a*')
dfa = DFA.from_nfa(nfa)
gnfa = GNFA.from_dfa(dfa)
print(gnfa.to_regex())
```
prints a single-character string `*`, which is not a valid regex. 


Discussion:
coveralls: 
[![Coverage Status](https://coveralls.io/builds/55741278/badge)](https://coveralls.io/builds/55741278)

Coverage: 100.0%. Remained the same when pulling **aea37846d08402971709c8abafce16229c756f0d on eliotwrobson:to_regex_fix** into **5987d5d276cf10662956c2266eb088ffd95b9760 on caleb531:develop**.


Patch:
diff --git a/automata/base/utils.py b/automata/base/utils.py
index 07611a8..98c8706 100644
--- a/automata/base/utils.py
+++ b/automata/base/utils.py
@@ -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:
diff --git a/automata/fa/gnfa.py b/automata/fa/gnfa.py
index b3f3363..e74a027 100644
--- a/automata/fa/gnfa.py
+++ b/automata/fa/gnfa.py
@@ -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})*'
 


Test patch:
diff --git a/tests/test_gnfa.py b/tests/test_gnfa.py
index bdb5ef3..63083f8 100644
--- a/tests/test_gnfa.py
+++ b/tests/test_gnfa.py
@@ -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."""