def testEquivDFA3(self): checkFileName = 'testEquivDFA3' + '.good' outFileName = 'testEquivDFA3' + '.out' with open(outFileName, 'w') as outf: with redirect_stdout(outf): delta1 = { # DFA that accepts strings in {a,b,c}* that has no substring ac (0, 'a') : 1, (0, 'b') : 0, (0, 'c') : 0, (1, 'a') : 1, (1, 'b') : 0, (1, 'c') : 2, (2, 'a') : 2, (2, 'b') : 2, (2, 'c') : 2 } dfa1 = dfa(delta=delta1, start=0, finals={0,1}) print('dfa1 is') print(dfa1) print(40*'-') delta2 = { # another DFA that accepts the same language as dfa1 (1, 'a') : 2, (1, 'b') : 3, (1, 'c') : 1, (2, 'a') : 2, (2, 'b') : 3, (2, 'c') : 5, (3, 'a') : 2, (3, 'b') : 3, (3, 'c') : 4, (4, 'a') : 2, (4, 'b') : 3, (4, 'c') : 4, (5, 'a') : 5, (5, 'b') : 5, (5, 'c') : 5 } dfa2 = dfa(delta=delta2, start=1, finals={1,2,3,4}) print('dfa2 is') print(dfa2) print(40*'-') print(f'That dfa1 is equivalent to dfa2 is {dfa1.equiv(dfa2)}') with open(outFileName) as outf: outStr = outf.read() with open(checkFileName) as f: checkStr = f.read() self.assertEqual(outStr, checkStr)
def testDFAtoRegx4(self): checkFileName = 'testDFAtoRegx4' + '.good' outFileName = 'testDFAtoRegx4' + '.out' with open(outFileName, 'w') as outf: with redirect_stdout(outf): #-----------------------# delta = { # DFA p.100 L&P (1, 'a'): 2, (1, 'b'): 3, (2, 'a'): 4, (2, 'b'): 1, (3, 'a'): 1, (3, 'b'): 4, (4, 'a'): 4, (4, 'b'): 4 } dfa1 = dfa(delta=delta, start=1, finals={1}) print('DFA that accepts (ab U ba)* is') print(40 * '-') print(dfa1) print(40 * '-') r = dfa1.to_regx() print('Corresponding regular expression is') print(r) with open(outFileName) as outf: outStr = outf.read() with open(checkFileName) as f: checkStr = f.read() self.assertEqual(outStr, checkStr)
def testDfaToNfa2(self): checkFileName = 'testDfaToNfa2' + '.good' outFileName = 'testDfaToNfa2' + '.out' with open(outFileName, 'w') as outf: with redirect_stdout(outf): delta = { # a DFA that accepts the empty set } dfa1 = dfa(delta=delta, start='s', finals={'f'}) print('DFA that accepts the empty set is') print(40 * '-') print(dfa1) print(40 * '-') nfa1 = dfa1.to_nfa() print('Equivalent NFA is') print(40 * '-') print(nfa1) print(40 * '-') with open(outFileName) as outf: outStr = outf.read() with open(checkFileName) as f: checkStr = f.read() self.assertEqual(outStr, checkStr)
def testDfaToNfa1(self): checkFileName = 'testDfaToNfa1' + '.good' outFileName = 'testDfaToNfa1' + '.out' with open(outFileName, 'w') as outf: with redirect_stdout(outf): delta = { # a DFA that accepts (01 U 010)* ('s', '0'): '0', ('0', '1'): '01', ('01', '0'): '010', ('010', '0'): '0', ('010', '1'): '01' } dfa1 = dfa(delta=delta, start='s', finals={'s', '01', '010'}) print('DFA that accepts (01 U 010)* is') print(40 * '-') print(dfa1) print(40 * '-') nfa1 = dfa1.to_nfa() print('Equivalent NFA is') print(40 * '-') print(nfa1) print(40 * '-') with open(outFileName) as outf: outStr = outf.read() with open(checkFileName) as f: checkStr = f.read() self.assertEqual(outStr, checkStr)
def testComplementDFA2(self): checkFileName = 'testComplementDFA2' + '.good' outFileName = 'testComplementDFA2' + '.out' with open(outFileName, 'w') as outf: with redirect_stdout(outf): delta = { # a DFA that accepts the empty set } dfa1 = dfa(delta=delta, start='s', finals={'f'}) print('DFA that accepts the empty set is') print(40 * '-') print(dfa1) print(40 * '-') dfa2 = dfa1.complement() print('Complemented DFA is') print(40 * '-') print(dfa2) print(40 * '-') with open(outFileName) as outf: outStr = outf.read() with open(checkFileName) as f: checkStr = f.read() self.assertEqual(outStr, checkStr)
def testDFAtoRegx1(self): checkFileName = 'testDFAtoRegx1' + '.good' outFileName = 'testDFAtoRegx1' + '.out' with open(outFileName, 'w') as outf: with redirect_stdout(outf): delta = { # a DFA that accepts (01 U 010)* ('s', '0'): '0', ('0', '1'): '01', ('01', '0'): '010', ('010', '0'): '0', ('010', '1'): '01' } dfa1 = dfa(delta=delta, start='s', finals={'s', '01', '010'}) print('DFA that accepts (01 U 010)* is') print(40 * '-') print(dfa1) print(40 * '-') r = dfa1.to_regx() print('Corresponding regular expression is') print(r) with open(outFileName) as outf: outStr = outf.read() with open(checkFileName) as f: checkStr = f.read() self.assertEqual(outStr, checkStr)
def testDfaAcpt1(self): checkFileName = 'testDfaAcpt1' + '.good' outFileName = 'testDfaAcpt1' + '.out' with open(outFileName, 'w') as outf: with redirect_stdout(outf): delta = { # a DFA that accepts (01 U 010)* ('s', '0'): '0', ('0', '1'): '01', ('01', '0'): '010', ('010', '0'): '0', ('010', '1'): '01' } dfa1 = dfa(delta=delta, start='s', finals={'s', '01', '010'}) print('DFA that accepts (01 U 010)* is') print(40 * '-') print(dfa1) print(40 * '-') inputlist = [ '0101001', '010010010', '01', '010', '', '1010', '010001', '01101', '010100100101' ] for inpstr in inputlist: if dfa1.accept(inpstr): print(f"'{inpstr}' accepted") else: print(f"'{inpstr}' not accepted") with open(outFileName) as outf: outStr = outf.read() with open(checkFileName) as f: checkStr = f.read() self.assertEqual(outStr, checkStr)
def testDfaAcpt2(self): checkFileName = 'testDfaAcpt2' + '.good' outFileName = 'testDfaAcpt2' + '.out' with open(outFileName, 'w') as outf: with redirect_stdout(outf): delta = { # a DFA that accepts the empty set } dfa1 = dfa(delta=delta, start='s', finals={'f'}) print('DFA that accepts the empty set is') print(40 * '-') print(dfa1) print(40 * '-') inputlist = ['', '0', '00', '000', '0000'] for inpstr in inputlist: if dfa1.accept(inpstr): print(f"'{inpstr}' accepted") else: print(f"'{inpstr}' not accepted") with open(outFileName) as outf: outStr = outf.read() with open(checkFileName) as f: checkStr = f.read() self.assertEqual(outStr, checkStr)
def testEquivDFA5(self): checkFileName = 'testEquivDFA5' + '.good' outFileName = 'testEquivDFA5' + '.out' with open(outFileName, 'w') as outf: with redirect_stdout(outf): delta1 = { # DFA that accepts the empty set (0, 'a') : 1, (0, 'b') : 0, (0, 'c') : 0, (1, 'a') : 1, (1, 'b') : 0, (1, 'c') : 2, (2, 'a') : 2, (2, 'b') : 2, (2, 'c') : 2 } dfa1 = dfa(delta=delta1, start=0, finals=set()) print('dfa1 is') print(dfa1) print(40*'-') delta2 = { # another DFA that accepts the same language as dfa1 (1, 'a') : 2, (1, 'b') : 1, (1, 'c') : 1, (2, 'a') : 2, (2, 'b') : 1, (2, 'c') : 2, (3, 'a') : 2, (3, 'b') : 3, (3, 'c') : 2 } dfa2 = dfa(delta=delta2, start=1, finals={3}) print('dfa2 is') print(dfa2) print(40*'-') print(f'That dfa1 is equivalent to dfa2 is {dfa1.equiv(dfa2)}') with open(outFileName) as outf: outStr = outf.read() with open(checkFileName) as f: checkStr = f.read() self.assertEqual(outStr, checkStr)
def testMinDFA4(self): checkFileName = 'testMinDFA4' + '.good' outFileName = 'testMinDFA4' + '.out' with open(outFileName, 'w') as outf: with redirect_stdout(outf): #-----------------------# delta_n8 = { # DFA Martin, 4th Ed., p.76 (for minimization algo) (0, 'a'): 1, (0, 'b'): 9, (1, 'a'): 8, (1, 'b'): 2, (2, 'a'): 3, (2, 'b'): 2, (3, 'a'): 2, (3, 'b'): 4, (4, 'a'): 5, (4, 'b'): 8, (5, 'a'): 4, (5, 'b'): 5, (6, 'a'): 7, (6, 'b'): 5, (7, 'a'): 6, (7, 'b'): 5, (8, 'a'): 1, (8, 'b'): 3, (9, 'a'): 7, (9, 'b'): 8 } n8 = dfa(delta=delta_n8, start=0, finals={3, 4, 8, 9}) print('\nDFA n8:') print(40 * '-') print(n8) print(40 * '-') print('\nAbout to minimize n8 (method 1: L&P)') min1_n8 = n8.minimized1(verbose=True) print('\nMinimized n8: (method 1: L&P)') print(40 * '-') print(min1_n8) print(40 * '-') print('\nAbout to Minimize n8 (method 2: marking algo)') min2_n8 = n8.minimized2(verbose=True) print('\nMinimized n8: (method 2: marking algo)') print(40 * '-') print(min2_n8) print(40 * '-') #-----------------------# print(f'min1_n8.equiv(min2_n8) is {min1_n8.equiv(min2_n8)}') with open(outFileName) as outf: outStr = outf.read() with open(checkFileName) as f: checkStr = f.read() self.assertEqual(outStr, checkStr)
def testEqualDFA1(self): checkFileName = 'testEqualDFA1' + '.good' outFileName = 'testEqualDFA1' + '.out' with open(outFileName, 'w') as outf: with redirect_stdout(outf): delta1 = { # DFA p.100 L&P with one more final state (1, 'a'): 2, (1, 'b'): 3, (2, 'a'): 4, (2, 'b'): 1, (3, 'a'): 1, (3, 'b'): 4, (4, 'a'): 4, (4, 'b'): 4 } dfa1 = dfa(delta=delta1, start=1, finals={1, 2}) print('dfa1 is') print(dfa1) print(40 * '-') delta2 = { # exactly the same as delta1 (1, 'a'): 2, (1, 'b'): 3, (2, 'a'): 4, (2, 'b'): 1, (3, 'a'): 1, (3, 'b'): 4, (4, 'a'): 4, (4, 'b'): 4 } dfa2 = dfa(delta=delta2, start=1, finals={2, 1}) print('dfa2 is') print(dfa2) print(40 * '-') print(f'dfa1 == dfa2 is {dfa1 == dfa2}') with open(outFileName) as outf: outStr = outf.read() with open(checkFileName) as f: checkStr = f.read() self.assertEqual(outStr, checkStr)
def testEquivDFA2(self): checkFileName = 'testEquivDFA2' + '.good' outFileName = 'testEquivDFA2' + '.out' with open(outFileName, 'w') as outf: with redirect_stdout(outf): delta1 = { # DFA p.100 L&P with one more final state (1, 'a') : 2, (1, 'b') : 3, (2, 'a') : 4, (2, 'b') : 1, (3, 'a') : 1, (3, 'b') : 4, (4, 'a') : 4, (4, 'b') : 4 } dfa1 = dfa(delta=delta1, start=1, finals={1,2}) print('dfa1 is') print(dfa1) print(40*'-') delta2 = { # differs from delta1 only in state names (10, 'a') : 20, (10, 'b') : 30, (20, 'a') : 40, (20, 'b') : 10, (30, 'a') : 10, (30, 'b') : 40, (40, 'a') : 40, (40, 'b') : 40 } dfa2 = dfa(delta=delta2, start=10, finals={10,20}) print('dfa2 is') print(dfa2) print(40*'-') print(f'That dfa1 is equivalent to dfa2 is {dfa1.equiv(dfa2)}') with open(outFileName) as outf: outStr = outf.read() with open(checkFileName) as f: checkStr = f.read() self.assertEqual(outStr, checkStr)
def testRegxEquiv1(self): checkFileName = 'testRegxEquiv1' + '.good' outFileName = 'testRegxEquiv1' + '.out' with open(outFileName, 'w') as outf: with redirect_stdout(outf): r1 = regx('c*(aUbc*)*') print(f'r1 is {r1}') del11 = { (1, 'a'): 2, (1, 'b'): 1, (1, 'c'): 1, (2, 'a'): 2, (2, 'b'): 1, (2, 'c'): 3, (3, 'a'): 3, (3, 'b'): 3, (3, 'c'): 3 } d11 = dfa(delta=del11, start=1, finals={1, 2}) print(f'DFA d11 is') print(40 * '-') print(d11) print(40 * '-') r11 = d11.to_regx() print(f'r11 is {r11}') print(f'r1.equiv(r11) is {r1.equiv(r11)}') print(r1, '==', r11, 'is', r1 == r11) print(40 * '-') r2 = regx('(#Ucc*)(aUb(c*cU#))*') print(f'r2 is {r2}') print(f'r1.equiv(r2) is {r1.equiv(r2)}') print(r1, '==', r2, 'is', r1 == r2) print(f'r11.equiv(r2) is {r11.equiv(r2)}') print(r11, '==', r2, 'is', r11 == r2) print(40 * '-') r3 = regx('(#Ucc*)(aUbc*c)*') print(f'r3 is {r3}') print(f'r1.equiv(r3) is {r1.equiv(r3)}') print(r1, '==', r3, 'is', r1 == r3) print(f'r11.equiv(r3) is {r11.equiv(r3)}') print(r11, '==', r3, 'is', r11 == r3) with open(outFileName) as outf: outStr = outf.read() with open(checkFileName) as f: checkStr = f.read() self.assertEqual(outStr, checkStr)
def testMinDFA3(self): checkFileName = 'testMinDFA3' + '.good' outFileName = 'testMinDFA3' + '.out' with open(outFileName, 'w') as outf: with redirect_stdout(outf): #-----------------------# delta_n7 = { # DFA p.156 HMU, 3rd Ed. (for minimization algo) ('A', 0): 'B', ('A', 1): 'F', ('B', 0): 'G', ('B', 1): 'C', ('C', 0): 'A', ('C', 1): 'C', ('D', 0): 'C', ('D', 1): 'G', ('E', 0): 'H', ('E', 1): 'F', ('F', 0): 'C', ('F', 1): 'G', ('G', 0): 'G', ('G', 1): 'E', ('H', 0): 'G', ('H', 1): 'C' } n7 = dfa(delta=delta_n7, start='A', finals={'C'}) print('\nDFA n7:') print(40 * '-') print(n7) print(40 * '-') print('\nAbout to Minimize n7 (method 1: L&P)') min1_n7 = n7.minimized1() print('\nMinimized n7 is (method 1: L&P)') print(40 * '-') print(min1_n7) print(40 * '-') print('\nAbout to Minimize n7 (method 2: marking algo)') min2_n7 = n7.minimized2() print('\nMinimized n7 is (method 2: marking algo)') print(40 * '-') print(min2_n7) print(40 * '-') with open(outFileName) as outf: outStr = outf.read() with open(checkFileName) as f: checkStr = f.read() self.assertEqual(outStr, checkStr)
def testMinDFA2(self): checkFileName = 'testMinDFA2' + '.good' outFileName = 'testMinDFA2' + '.out' with open(outFileName, 'w') as outf: with redirect_stdout(outf): #-----------------------# delta_n6 = { # DFA p.93 L&P (for eliminating nonreachable states) (1, 'a') : 2, (1, 'b') : 4, (2, 'a') : 5, (2, 'b') : 3, (3, 'a') : 2, (3, 'b') : 6, (4, 'a') : 1, (4, 'b') : 5, (5, 'a') : 5, (5, 'b') : 5, (6, 'a') : 3, (6, 'b') : 5, (7, 'a') : 6, (7, 'b') : 8, (8, 'a') : 7, (8, 'b') : 3, } n6 = dfa(delta=delta_n6, start=1, finals={1, 3, 7}) print('Original DFA is') print(40 * '-') print(n6) print(40 * '-') reachable_n6 = n6.remove_nonreachable() print('reachable DFA is') print(40 * '-') print(reachable_n6) print(40 * '-') minimal_n6 = reachable_n6.minimized1() print() print('Minimized DFA n6 is') print(40 * '-') print(minimal_n6) print(40 * '-') with open(outFileName) as outf: outStr = outf.read() with open(checkFileName) as f: checkStr = f.read() self.assertEqual(outStr, checkStr)
def testRenumberDFA1(self): checkFileName = 'testRenumberDFA1' + '.good' outFileName = 'testRenumberDFA1' + '.out' with open(outFileName, 'w') as outf: with redirect_stdout(outf): delta_n1 = { # DFA p.94 L&P (for minimization algo in L&P) (1, 'a') : 2, (1, 'b') : 4, (2, 'a') : 5, (2, 'b') : 3, (3, 'a') : 2, (3, 'b') : 6, (4, 'a') : 1, (4, 'b') : 5, (5, 'a') : 5, (5, 'b') : 5, (6, 'a') : 3, (6, 'b') : 5 } n1 = dfa(delta=delta_n1, start=1, finals={1, 3}) print('Original DFA is') print(40*'-') print(f'current DFA n1 (starting at {1}):\n{n1}') stnum = 11 m11 = n1.renumbered(startnum=stnum) print(40*'-') print(f'new renumbered DFA m11 (starting at {stnum}):\n{m11}') stnum = 1 m1 = n1.renumbered(startnum=stnum) print(40*'-') print(f'new renumbered DFA m1 (starting at {stnum}):\n{m1}') stnum = 101 m101 = n1.renumbered(startnum=stnum) print(40*'-') print(f'new renumbered DFA m101 (starting at {stnum}):\n{m101}') with open(outFileName) as outf: outStr = outf.read() with open(checkFileName) as f: checkStr = f.read() self.assertEqual(outStr, checkStr)
def testRegxEquiv3(self): checkFileName = 'testRegxEquiv3' + '.good' outFileName = 'testRegxEquiv3' + '.out' with open(outFileName, 'w') as outf: with redirect_stdout(outf): # r1 is the regx of {w in {0,1}*: w ends with 10} r1 = regx('(0U1)*10') r2 = regx('@*(0U1)*10') r3 = regx('@U(0U1)*10') r4 = regx('0*(10*)*10') r5 = regx('1*(01*)*10') print(r1, '==', r2, 'is', r1 == r2) print(r1, '==', r3, 'is', r1 == r3) print(r1, '==', r4, 'is', r1 == r4) print(r1, '==', r5, 'is', r1 == r5) dfa1delta = { # this dfa accepts the same language as r1's (1, '0'): 1, (1, '1'): 2, (2, '0'): 3, (2, '1'): 2, (3, '0'): 1, (3, '1'): 2 } dfa1 = dfa(delta=dfa1delta, start=1, finals={3}) print('dfa1 is') print(dfa1) print(40 * '-') r6 = dfa1.to_regx() print(f'The above dfa accepts the language with regx {r6}') print(40 * '-') print(r1, '==', r6, 'is', r1 == r6) print(40 * '-') r7 = regx('(0U01U011)*') print(r1, '==', r7, 'is', r1 == r7) with open(outFileName) as outf: outStr = outf.read() with open(checkFileName) as f: checkStr = f.read() self.assertEqual(outStr, checkStr)
def testDfaAcpt3(self): checkFileName = 'testDfaAcpt3' + '.good' outFileName = 'testDfaAcpt3' + '.out' with open(outFileName, 'w') as outf: with redirect_stdout(outf): #-----------------------# delta = { # DFA p.94 L&P (for minimization algo) (1, 'a'): 2, (1, 'b'): 4, (2, 'a'): 5, (2, 'b'): 3, (3, 'a'): 2, (3, 'b'): 6, (4, 'a'): 1, (4, 'b'): 5, (5, 'a'): 5, (5, 'b'): 5, (6, 'a'): 3, (6, 'b'): 5 } dfa1 = dfa(delta=delta, start=1, finals={1, 3}) print('DFA that accepts (ab U ba)* is') print(40 * '-') print(dfa1) print(40 * '-') inputlist = [ '', 'a', 'b', 'ab', 'ba', 'abbabaab', 'abaaba', 'bababa', 'babbab' ] for inpstr in inputlist: if dfa1.accept(inpstr): print(f"'{inpstr}' accepted") else: print(f"'{inpstr}' not accepted") with open(outFileName) as outf: outStr = outf.read() with open(checkFileName) as f: checkStr = f.read() self.assertEqual(outStr, checkStr)
def testReachable1(self): checkFileName = 'testReachable1' + '.good' outFileName = 'testReachable1' + '.out' with open(outFileName, 'w') as outf: with redirect_stdout(outf): delta = { # DFA p.93 L&P (for eliminating nonreachable states) (1, 'a'): 2, (1, 'b'): 4, (2, 'a'): 5, (2, 'b'): 3, (3, 'a'): 2, (3, 'b'): 6, (4, 'a'): 1, (4, 'b'): 5, (5, 'a'): 5, (5, 'b'): 5, (6, 'a'): 3, (6, 'b'): 5, (7, 'a'): 6, (7, 'b'): 8, (8, 'a'): 7, (8, 'b'): 3 } dfa1 = dfa(delta=delta, start=1, finals={1, 3, 7}) print('Original DFA is') print(40 * '-') print(dfa1) print(40 * '-') reachable_dfa = dfa1.remove_nonreachable() print('nonreachable-free DFA is') print(40 * '-') print(reachable_dfa) print(40 * '-') with open(outFileName) as outf: outStr = outf.read() with open(checkFileName) as f: checkStr = f.read() self.assertEqual(outStr, checkStr)
def testStanNumDFA1(self): checkFileName = 'testStanNumDFA1' + '.good' outFileName = 'testStanNumDFA1' + '.out' with open(outFileName, 'w') as outf: with redirect_stdout(outf): delta_n1 = { # DFA p.94 L&P (for minimization algo in L&P) ('one', 'a'): 'two', ('one', 'b'): 'four', ('two', 'a'): 'five', ('two', 'b'): 'three', ('three', 'a'): 'two', ('three', 'b'): 'six', ('four', 'a'): 'one', ('four', 'b'): 'five', ('five', 'a'): 'five', ('five', 'b'): 'five', ('six', 'a'): 'three', ('six', 'b'): 'five' } n1 = dfa(delta=delta_n1, start='one', finals={'one', 'three'}) print('Original DFA is') print(40 * '-') print(f'current DFA n1:\n{n1}') m1 = n1.standard_numbered() print(40 * '-') print(f'Standard-numbered DFA m1:\n{m1}') m2 = m1.standard_numbered() print(40 * '-') print(f'Standard-numbered DFA m2:\n{m2}') with open(outFileName) as outf: outStr = outf.read() with open(checkFileName) as f: checkStr = f.read() self.assertEqual(outStr, checkStr)
def testMinDFA1(self): checkFileName = 'testMinDFA1' + '.good' outFileName = 'testMinDFA1' + '.out' with open(outFileName, 'w') as outf: with redirect_stdout(outf): #-----------------------# delta_n5 = { # DFA p.94 L&P (for minimization algo in L&P) (1, 'a'): 2, (1, 'b'): 4, (2, 'a'): 5, (2, 'b'): 3, (3, 'a'): 2, (3, 'b'): 6, (4, 'a'): 1, (4, 'b'): 5, (5, 'a'): 5, (5, 'b'): 5, (6, 'a'): 3, (6, 'b'): 5 } n5 = dfa(delta=delta_n5, start=1, finals={1, 3}) print('Original DFA is') print(40 * '-') print(n5) print(40 * '-') minimal_n5 = n5.minimized1() print('Minimized DFA is') print(40 * '-') print(minimal_n5) print(40 * '-') with open(outFileName) as outf: outStr = outf.read() with open(checkFileName) as f: checkStr = f.read() self.assertEqual(outStr, checkStr)
def testNfaEquivDfa3(self): checkFileName = 'testNfaEquivDfa3' + '.good' outFileName = 'testNfaEquivDfa3' + '.out' with open(outFileName, 'w') as outf: with redirect_stdout(outf): dfa1delta = { # this dfa accepts binary strings that ends with 10 (1, '0'): 1, (1, '1'): 2, (2, '0'): 3, (2, '1'): 2, (3, '0'): 1, (3, '1'): 2 } dfa1 = dfa(delta=dfa1delta, start=1, finals={3}) print('dfa1 is') print(dfa1) print(40 * '-') nfa2delta = { # this nfa accepts the same language as dfa1's (1, '0'): {1}, (1, '1'): {1, 2}, (2, '0'): {3} } nfa2 = nfa(delta=nfa2delta, start=1, finals={3}) print(40 * '-') print('nfa2 is') print(nfa2) print(40 * '-') print(f'nfa2.equiv(dfa1) is {nfa2.equiv(dfa1)}') with open(outFileName) as outf: outStr = outf.read() with open(checkFileName) as f: checkStr = f.read() self.assertEqual(outStr, checkStr)
def testDfaToNfa3(self): checkFileName = 'testDfaToNfa3' + '.good' outFileName = 'testDfaToNfa3' + '.out' with open(outFileName, 'w') as outf: with redirect_stdout(outf): #-----------------------# delta = { # DFA p.94 L&P (for minimization algo) (1, 'a'): 2, (1, 'b'): 4, (2, 'a'): 5, (2, 'b'): 3, (3, 'a'): 2, (3, 'b'): 6, (4, 'a'): 1, (4, 'b'): 5, (5, 'a'): 5, (5, 'b'): 5, (6, 'a'): 3, (6, 'b'): 5 } dfa1 = dfa(delta=delta, start=1, finals={1, 3}) print('DFA that accepts (ab U ba)* is') print(40 * '-') print(dfa1) print(40 * '-') nfa1 = dfa1.to_nfa() print('Equivalent NFA is') print(40 * '-') print(nfa1) print(40 * '-') with open(outFileName) as outf: outStr = outf.read() with open(checkFileName) as f: checkStr = f.read() self.assertEqual(outStr, checkStr)
from reglang.dfa import dfa delta = { # a DFA that accepts (01 U 010)* ('s', '0'): '0', ('0', '1'): '01', ('01', '0'): '010', ('010', '0'): '0', ('010', '1'): '01' } dfa1 = dfa(delta=delta, start='s', finals={'s', '01', '010'}) print('DFA that accepts (01 U 010)* is') print(40 * '-') print(dfa1) print(40 * '-')
from reglang.dfa import dfa #-----------------------# delta_n5 = { # DFA p.94 L&P (for minimization algo in L&P) (1, 'a'): 2, (1, 'b'): 4, (2, 'a'): 5, (2, 'b'): 3, (3, 'a'): 2, (3, 'b'): 6, (4, 'a'): 1, (4, 'b'): 5, (5, 'a'): 5, (5, 'b'): 5, (6, 'a'): 3, (6, 'b'): 5 } n5 = dfa(delta=delta_n5, start=1, finals={1, 3}) print('Original DFA is') print(40 * '-') print(n5) print(40 * '-') minimal_n5 = n5.minimized1() print('Minimized DFA is') print(40 * '-') print(minimal_n5) print(40 * '-')
from reglang.nfa import nfa dfa1delta = { # this dfa accepts (01 U 010)* (0, '0') : 1, (0, '1') : 4, (1, '0') : 4, (1, '1') : 2, (2, '0') : 3, (2, '1') : 4, (3, '0') : 1, (3, '1') : 2, (4, '0') : 4, (4, '1') : 4 } dfa1 = dfa(delta=dfa1delta, start=0, finals={0, 2, 3}) print('dfa1 is') print(dfa1) print(40*'-') nfa2delta = { # this nfa accepts the same language as dfa1's (0, '0') : {1}, (1, '1') : {2}, (2, '0') : {3}, (3, '0') : {1}, (3, '1') : {2} } nfa2 = nfa(delta=nfa2delta, start=0, finals={0, 2, 3}) print(40*'-') print('nfa2 is') print(nfa2)
def testNfaEquivDfa2(self): checkFileName = 'testNfaEquivDfa2' + '.good' outFileName = 'testNfaEquivDfa2' + '.out' with open(outFileName, 'w') as outf: with redirect_stdout(outf): dfa1delta = { # this dfa accepts binary strings that ends with 10 (1, '0'): 1, (1, '1'): 2, (2, '0'): 3, (2, '1'): 2, (3, '0'): 1, (3, '1'): 2 } dfa1 = dfa(delta=dfa1delta, start=1, finals={3}) print('dfa1 is') print(dfa1) print(40 * '-') nfa2delta = { # this nfa accepts (01 U 010)* (0, '0'): {1}, (1, '1'): {2}, (2, '0'): {3}, (3, '0'): {1}, (3, '1'): {2} } nfa2 = nfa(delta=nfa2delta, start=0, finals={0, 2, 3}) print(40 * '-') print('nfa2 is') print(nfa2) print(40 * '-') print(f'nfa2.equiv(dfa1) is {nfa2.equiv(dfa1)}') nfa3delta = { # this nfa accepts (01 U 010)* (0, '0'): {1, 2}, (1, '1'): {0}, (2, '1'): {3}, (3, '0'): {0} } nfa3 = nfa(delta=nfa3delta, start=0, finals={0}) print(40 * '-') print('nfa3 is') print(nfa3) print(40 * '-') print(f'nfa3.equiv(dfa1) is {nfa3.equiv(dfa1)}') nfa4delta = { # this nfa accepts (01 U 010)* (0, '0'): {1}, (1, '1'): {2}, (2, '0'): {0}, (2, ''): {0} } nfa4 = nfa(delta=nfa4delta, start=0, finals={0}) print(40 * '-') print('nfa4 is') print(nfa4) print(40 * '-') print(f'nfa4.equiv(dfa1) is {nfa4.equiv(dfa1)}') with open(outFileName) as outf: outStr = outf.read() with open(checkFileName) as f: checkStr = f.read() self.assertEqual(outStr, checkStr)
from reglang.dfa import dfa from reglang.nfa import nfa dfa1delta = { # this dfa accepts binary strings that ends with 10 (1, '0') : 1, (1, '1') : 2, (2, '0') : 3, (2, '1') : 2, (3, '0') : 1, (3, '1') : 2 } dfa1 = dfa(delta=dfa1delta, start=1, finals={3}) print('dfa1 is') print(dfa1) print(40*'-') nfa2delta = { # this nfa accepts the same language as dfa1's (1, '0') : {1}, (1, '1') : {1, 2}, (2, '0') : {3} } nfa2 = nfa(delta=nfa2delta, start=1, finals={3}) print(40*'-') print('nfa2 is') print(nfa2) print(40*'-') print(f'dfa1.equiv(nfa2) is {dfa1.equiv(nfa2)}')
(3, 'a'): 2, (3, 'b'): 4, (4, 'a'): 5, (4, 'b'): 8, (5, 'a'): 4, (5, 'b'): 5, (6, 'a'): 7, (6, 'b'): 5, (7, 'a'): 6, (7, 'b'): 5, (8, 'a'): 1, (8, 'b'): 3, (9, 'a'): 7, (9, 'b'): 8 } n8 = dfa(delta=delta_n8, start=0, finals={3, 4, 8, 9}) print('\nDFA n8:') print(40 * '-') print(n8) print(40 * '-') print('\nAbout to minimize n8 (method 1: L&P)') min1_n8 = n8.minimized1(verbose=True) print('\nMinimized n8: (method 1: L&P)') print(40 * '-') print(min1_n8) print(40 * '-') print('\nAbout to Minimize n8 (method 2: marking algo)') min2_n8 = n8.minimized2(verbose=True) print('\nMinimized n8: (method 2: marking algo)') print(40 * '-') print(min2_n8)
from reglang.dfa import dfa #-----------------------# delta = { # DFA p.100 L&P (1, 'a'): 2, (1, 'b'): 3, (2, 'a'): 4, (2, 'b'): 1, (3, 'a'): 1, (3, 'b'): 4, (4, 'a'): 4, (4, 'b'): 4 } dfa1 = dfa(delta=delta, start=1, finals={1}) print('DFA that accepts (ab U ba)* is') print(40 * '-') print(dfa1) print(40 * '-') r = dfa1.to_regx() print('Corresponding regular expression is') print(r)