def testNFAtoRegx1(self): checkFileName = 'testNFAtoRegx1' + '.good' outFileName = 'testNFAtoRegx1' + '.out' with open(outFileName, 'w') as outf: with redirect_stdout(outf): delta9 = { # NFA L&P p.81 (to convert to regexp) (1, 'a'): {1}, (1, 'b'): {3}, (2, 'a'): {2}, (2, 'b'): {1}, (3, 'a'): {3}, (3, 'b'): {2} } m9 = nfa(delta=delta9, start=1, finals={3}) print() print('NFA m9 is as follows:') print(m9) print() n9 = m9.snfa() print('simplified NFA for m9 is as follows:') print(n9) print() r9recursive = m9.to_regx2() print('[recursive algo]\nregexp is', r9recursive) r9iterative = m9.to_regx() print('\n[iterative algo]\nregexp is', r9iterative) with open(outFileName) as outf: outStr = outf.read() with open(checkFileName) as f: checkStr = f.read() self.assertEqual(outStr, checkStr)
def testNfaDef2(self): checkFileName = 'testNfaDef2' + '.good' outFileName = 'testNfaDef2' + '.out' with open(outFileName, 'w') as outf: with redirect_stdout(outf): delta04 = { # an NFA p75 ex.2.2.9(a) L&P: language a*b(aUb)* (0, 'a'): {0}, (0, 'b'): {0, 2}, (0, ''): {1}, (1, 'b'): {2, 4}, (2, 'a'): {3}, (3, ''): {4}, (4, 'a'): {3} } m04 = nfa(delta=delta04, start=0, finals={3, 4}) dash40 = 40 * '-' print('NFA is as follows:') print(dash40) print(m04) print(dash40) with open(outFileName) as outf: outStr = outf.read() with open(checkFileName) as f: checkStr = f.read() self.assertEqual(outStr, checkStr)
def testEClosure1(self): checkFileName = 'testEClosure1' + '.good' outFileName = 'testEClosure1' + '.out' with open(outFileName, 'w') as outf: with redirect_stdout(outf): delta02 = { # NFA fig 2.9 p.70 L&P (0, 'b'): {2}, (0, ''): {1}, (1, 'a'): {0, 4}, (1, ''): {2, 3}, (2, 'b'): {4}, (3, 'a'): {4}, (4, ''): {3} } m02 = nfa(delta=delta02, start=0, finals={4}) dash40 = 40 * '-' print('NFA is as follows:') print(dash40) print(m02) print(dash40) for q in m02.states: closure = m02._ep_closure(q) print(f'E-closure of state {q} = {closure}') print(dash40) with open(outFileName) as outf: outStr = outf.read() with open(checkFileName) as f: checkStr = f.read() self.assertEqual(outStr, checkStr)
def testNFAtoRegx3(self): checkFileName = 'testNFAtoRegx3' + '.good' outFileName = 'testNFAtoRegx3' + '.out' with open(outFileName, 'w') as outf: with redirect_stdout(outf): delta11 = { # NFA L&P exercise 2.3.7d (to convert to regexp) (1, 'a'): {2}, (1, 'b'): {4}, (2, 'b'): {3, 4}, (3, 'a'): {3}, (3, 'b'): {3}, (4, 'a'): {2, 4} } m11 = nfa(delta=delta11, start=1, finals={3}) print() print('NFA m11 is as follows:') print(m11) print() n11 = m11.snfa() print('simplified NFA for m11 is as follows:') print(n11) print() r11recursive = m11.to_regx2() print('[recursive algo]\nregexp is', r11recursive) r11iterative = m11.to_regx() print('\n[iterative algo]\nregexp is', r11iterative) with open(outFileName) as outf: outStr = outf.read() with open(checkFileName) as f: checkStr = f.read() self.assertEqual(outStr, checkStr)
def testMinDFA5(self): checkFileName = 'testMinDFA5' + '.good' outFileName = 'testMinDFA5' + '.out' with open(outFileName, 'w') as outf: with redirect_stdout(outf): delta = { # another NFA from lecture note about thm2.2.1 (0, 'a'): {1}, (0, 'b'): {0}, (0, ''): {2}, (1, 'b'): {3}, (1, ''): {0, 4}, (2, 'a'): {1}, (2, 'b'): {2, 5}, (2, ''): {4}, (3, ''): {4}, (4, 'a'): {4}, (4, ''): {6}, (5, 'b'): {5}, (5, ''): {6}, (6, 'a'): {6} } m = nfa(delta=delta, start=0, finals={4}) # accepts all binary strings dash40 = 40 * '-' print('NFA is as follows:') print(dash40) print(m) print(dash40) n = m.to_dfa().standard_numbered() print('Equivalent DFA is as follows:') print(dash40) print(n) print(dash40) nmin1 = n.minimized1(verbose=True) print(nmin1) print(dash40) nmin2 = n.minimized2(verbose=True) print(nmin2) print(dash40) print(f'nmin1.equiv(nmin2) is {nmin1.equiv(nmin2)}') with open(outFileName) as outf: outStr = outf.read() with open(checkFileName) as f: checkStr = f.read() self.assertEqual(outStr, checkStr)
def testNfaAcpt3(self): checkFileName = 'testNfaAcpt3' + '.good' outFileName = 'testNfaAcpt3' + '.out' with open(outFileName, 'w') as outf: with redirect_stdout(outf): delta_m4 = { # NFA ex.2.2.6 (a): L = (ab U aab U aba)* (0, 'a'): {1}, (1, 'a'): {2}, (1, 'b'): {0, 3}, (2, 'b'): {0}, (3, 'a'): {0} } m4 = nfa(delta=delta_m4, start=0, finals={0}) print() print('NFA m4 is as follows:') print(m4) print() tlist = [ '', 'a', 'b', 'ab', 'aab', 'aba', 'bab', 'abab', 'abaa', 'abaaababab', 'abaaabb' ] for inpstr in tlist: print('[accept version 1]') print( f'NFA m4 {"accepts" if m4._accept0(inpstr) else "does not accept"} "{inpstr}"' ) print('[accept version 2]') print( f'NFA m4 {"accepts" if m4.accept(inpstr) else "does not accept"} "{inpstr}"' ) print() with open(outFileName) as outf: outStr = outf.read() with open(checkFileName) as f: checkStr = f.read() self.assertEqual(outStr, checkStr)
def testNfaAcpt2(self): checkFileName = 'testNfaAcpt2' + '.good' outFileName = 'testNfaAcpt2' + '.out' with open(outFileName, 'w') as outf: with redirect_stdout(outf): delta_m3 = { # NFA fig 2.9 p.70 L&P, language: (aa* U aa*b U b)(ep U (aUb)a*) (0, 'b'): {2}, (0, ''): {1}, (1, 'a'): {0, 4}, (1, ''): {2, 3}, (2, 'b'): {4}, (3, 'a'): {4}, (4, ''): {3} } m3 = nfa(delta=delta_m3, start=0, finals={4}) print() print('NFA m3 is as follows:') print(m3) print() for inpstr in [ '', 'a', 'b', 'aab', 'aaba', 'aabba', 'aabbb', 'aabbabab' ]: print('[accept version 1]') print( f'NFA m3 {"accepts" if m3._accept0(inpstr) else "does not accept"} "{inpstr}"' ) print('[accept version 2]') print( f'NFA m3 {"accepts" if m3.accept(inpstr) else "does not accept"} "{inpstr}"' ) print() 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 testNfaAcpt1(self): checkFileName = 'testNfaAcpt1' + '.good' outFileName = 'testNfaAcpt1' + '.out' with open(outFileName, 'w') as outf: with redirect_stdout(outf): delta_m2 = { # an NFA p75 ex.2.2.9(a) L&P: language a*b(aUb)* (0, 'a'): {0}, (0, 'b'): {0, 2}, (0, ''): {1}, (1, 'b'): {2, 4}, (2, 'a'): {3}, (3, ''): {4}, (4, 'a'): {3} } m2 = nfa(delta=delta_m2, start=0, finals={3, 4}) print() print('NFA m2:') print(m2) for inpstr in [ 'b', 'bb', 'bba', 'ab', 'aba', 'aababaabb', 'a', 'aa', '' ]: print('[accept version 1]') print( f'NFA m2 {"accepts" if m2._accept0(inpstr) else "does not accept"} "{inpstr}"' ) print('[accept version 2]') print( f'NFA m2 {"accepts" if m2.accept(inpstr) else "does not accept"} "{inpstr}"' ) print() with open(outFileName) as outf: outStr = outf.read() with open(checkFileName) as f: checkStr = f.read() self.assertEqual(outStr, checkStr)
def testRenumberNFA1(self): checkFileName = 'testRenumberNFA1' + '.good' outFileName = 'testRenumberNFA1' + '.out' with open(outFileName, 'w') as outf: with redirect_stdout(outf): delta12 = { # NFA example in the sheet (to be converted to regexp) (1, 'a') : {1, 2}, (1, 'b') : {2}, (2, 'a') : {5}, (3, 'b') : {4}, (4, 'a') : {1}, (4, 'b') : {3}, (4, '') : {2}, (5, 'b') : {4} } m12 = nfa(delta=delta12, start=1, finals={4, 5}) print() print(f'current NFA m12 (starting at {1}):\n{m12}') stnum = 11 n12 = m12.renumbered(startnum=stnum) print(f'new renumbered NFA n12 (starting at {stnum}):\n{n12}') stnum = 1 nn12 = n12.renumbered(startnum=stnum) print(f'new renumbered NFA nn12 (starting at {stnum}):\n{nn12}') stnum = 101 nnn12 = m12.renumbered(startnum=stnum) print(f'new renumbered NFA nnn12 (starting at {stnum}):\n{nnn12}') with open(outFileName) as outf: outStr = outf.read() with open(checkFileName) as f: checkStr = f.read() self.assertEqual(outStr, checkStr)
def testNFAtoRegx5(self): checkFileName = 'testNFAtoRegx5' + '.good' outFileName = 'testNFAtoRegx5' + '.out' with open(outFileName, 'w') as outf: with redirect_stdout(outf): delta = { # NFA example in Kleene's Thm-2 slides (to convert to regexp) (1, 'a'): {2}, (1, 'b'): {1, 3}, (2, 'a'): {3}, (3, 'a'): {3}, (3, 'b'): {3}, (3, ''): {4}, (4, 'a'): {2}, (4, 'b'): {2, 4} } m = nfa(delta=delta, start=1, finals={2, 4}) print() print('NFA m is as follows:') print(m) print() n = m.snfa() print('simplified NFA for m is as follows:') print(n) print() r_recursive = m.to_regx2() print('[recursive algo]\nregexp is', r_recursive) r_iterative = m.to_regx() print('\n[iterative algo]\nregexp is', r_iterative) with open(outFileName) as outf: outStr = outf.read() with open(checkFileName) as f: checkStr = f.read() self.assertEqual(outStr, checkStr)
def testNFAtoRegx4(self): checkFileName = 'testNFAtoRegx4' + '.good' outFileName = 'testNFAtoRegx4' + '.out' with open(outFileName, 'w') as outf: with redirect_stdout(outf): delta12 = { # NFA example in the sheet (to convert to regexp) (1, 'a'): {1, 2}, (1, 'b'): {2}, (2, 'a'): {5}, (3, 'b'): {4}, (4, 'a'): {1}, (4, 'b'): {3}, (4, ''): {2}, (5, 'b'): {4} } m12 = nfa(delta=delta12, start=1, finals={4, 5}) print() print('NFA m12 is as follows:') print(m12) print() n12 = m12.snfa() print('simplified NFA for m12 is as follows:') print(n12) print() r12recursive = m12.to_regx2() print('[recursive algo]\nregexp is', r12recursive) r12iterative = m12.to_regx() print('\n[iterative algo]\nregexp is', r12iterative) with open(outFileName) as outf: outStr = outf.read() with open(checkFileName) as f: checkStr = f.read() self.assertEqual(outStr, checkStr)
def testEClosure2(self): checkFileName = 'testEClosure2' + '.good' outFileName = 'testEClosure2' + '.out' with open(outFileName, 'w') as outf: with redirect_stdout(outf): delta1 = { # an NFA (0, 'a'):{1}, (0, '') :{2}, (1, 'b'):{3}, (1, '') :{0, 4}, (2, 'a'):{1, 5}, (2, 'b'):{2}, (2, '') :{4}, (3, '') :{4}, (4, '') :{6}, (5, 'a'):{5}, (5, '') :{6}, } m1 = nfa(delta=delta1, start=0, finals={6}) dash40 = 40 * '-' print('NFA is as follows:') print(dash40) print(m1) print(dash40) for q in m1.states: closure = m1._ep_closure(q) print(f'E-closure of state {q} = {closure}') print(dash40) with open(outFileName) as outf: outStr = outf.read() with open(checkFileName) as f: checkStr = f.read() self.assertEqual(outStr, checkStr)
from reglang.nfa import nfa delta02 = { # NFA fig 2.9 p.70 L&P (0, 'b'):{2}, (0, '') :{1}, (1, 'a'):{0, 4}, (1, '') :{2, 3}, (2, 'b'):{4}, (3, 'a'):{4}, (4, '') :{3} } m02 = nfa(delta=delta02, start=0, finals={4}) dash40 = 40*'-' print('NFA is as follows:') print(dash40) print(m02) print(dash40) n02 = m02.to_dfa() print('Equivalent DFA is as follows:') print(dash40) print(n02) print(dash40)
from reglang.nfa import nfa nfa1delta = { # this nfa is essentially a dfa that 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} } nfa1 = nfa(delta=nfa1delta, start=0, finals={0, 2, 3}) print('nfa1 is') print(nfa1) print(40 * '-') nfa2delta = { # this nfa accepts the same language as nfa1'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)
from reglang.nfa import nfa delta12 = { # NFA example in the sheet (to be converted to regexp) (1, 'a'): {1, 2}, (1, 'b'): {2}, (2, 'a'): {5}, (3, 'b'): {4}, (4, 'a'): {1}, (4, 'b'): {3}, (4, ''): {2}, (5, 'b'): {4} } m12 = nfa(delta=delta12, start=1, finals={4, 5}) print() print(f'current NFA m12 (starting at {1}):\n{m12}') stnum = 11 n12 = m12.renumbered(startnum=stnum) print(f'new renumbered NFA n12 (starting at {stnum}):\n{n12}') stnum = 1 nn12 = n12.renumbered(startnum=stnum) print(f'new renumbered NFA nn12 (starting at {stnum}):\n{nn12}') stnum = 101 nnn12 = m12.renumbered(startnum=stnum) print(f'new renumbered NFA nnn12 (starting at {stnum}):\n{nnn12}')
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.nfa import nfa delta_m2 = { # an NFA p75 ex.2.2.9(a) L&P: language a*b(aUb)* (0, 'a'): {0}, (0, 'b'): {0, 2}, (0, ''): {1}, (1, 'b'): {2, 4}, (2, 'a'): {3}, (3, ''): {4}, (4, 'a'): {3} } m2 = nfa(delta=delta_m2, start=0, finals={3, 4}) print() print('NFA m2:') print(m2) for inpstr in ['b', 'bb', 'bba', 'ab', 'aba', 'aababaabb', 'a', 'aa', '']: print('[accept version 1]') print( f'NFA m2 {"accepts" if m2._accept0(inpstr) else "does not accept"} "{inpstr}"' ) print('[accept version 2]') print( f'NFA m2 {"accepts" if m2.accept(inpstr) else "does not accept"} "{inpstr}"' ) print()
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)}')
delta = { # another NFA from lecture note about thm2.2.1 (0, 'a'):{1}, (0, 'b'):{0}, (0, '' ):{2}, (1, 'b'):{3}, (1, '' ):{0, 4}, (2, 'a'):{1}, (2, 'b'):{2, 5}, (2, '' ):{4}, (3, '' ):{4}, (4, 'a'):{4}, (4, '' ):{6}, (5, 'b'):{5}, (5, '' ):{6}, (6, 'a'):{6} } m = nfa(delta=delta, start=0, finals={3}) dash40 = 40*'-' print('NFA is as follows:') print(dash40) print(m) print(dash40) n = m.to_dfa() print('Equivalent DFA is as follows:') print(dash40) print(n) print(dash40)
from reglang.nfa import nfa delta05 = { # another NFA for ex.2.2.6 (a): L = (ab U aab U aba)* (0, 'a'): {1, 2, 4}, (1, 'b'): {0}, (2, 'a'): {3}, (3, 'b'): {0}, (4, 'b'): {5}, (5, 'a'): {0} } m05 = nfa(delta=delta05, start=0, finals={0}) dash40 = 40 * '-' print('NFA is as follows:') print(dash40) print(m05) print(dash40) n05 = m05.to_dfa() print('Equivalent DFA is as follows:') print(dash40) print(n05) print(dash40)
(0, 'b'): {0}, (0, ''): {2}, (1, 'b'): {3}, (1, ''): {0, 4}, (2, 'a'): {1}, (2, 'b'): {2, 5}, (2, ''): {4}, (3, ''): {4}, (4, 'a'): {4}, (4, ''): {6}, (5, 'b'): {5}, (5, ''): {6}, (6, 'a'): {6} } m = nfa(delta=delta, start=0, finals={4}) # accepts all binary strings dash40 = 40 * '-' print('NFA is as follows:') print(dash40) print(m) print(dash40) n = m.to_dfa().standard_numbered() print('Equivalent DFA is as follows:') print(dash40) print(n) print(dash40) nmin1 = n.minimized1(verbose=True) print(nmin1) print(dash40)
from reglang.nfa import nfa delta9 = { # NFA L&P p.81 (to convert to regexp) (1, 'a'): {1}, (1, 'b'): {3}, (2, 'a'): {2}, (2, 'b'): {1}, (3, 'a'): {3}, (3, 'b'): {2} } m9 = nfa(delta=delta9, start=1, finals={3}) print() print('NFA m9 is as follows:') print(m9) print() n9 = m9.snfa() print('simplified NFA for m9 is as follows:') print(n9) print() r9recursive = m9.to_regx2() print('[recursive algo]\nregexp is', r9recursive) r9iterative = m9.to_regx() print('\n[iterative algo]\nregexp is', r9iterative)
from reglang.nfa import nfa nfa1delta = { # this nfa is essentially a dfa that accepts the complement of (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} } nfa1 = nfa(delta=nfa1delta, start=0, finals={1, 4}) print('nfa1 is') print(nfa1) print(40 * '-') nfa2delta = { # this nfa accepts a different language from nfa1's (0, '0'): {1}, (1, '1'): {2}, (2, '0'): {3}, (3, '0'): {1}, (3, '1'): {2} } nfa2 = nfa(delta=nfa2delta, start=0, finals={2, 3}) print(40 * '-') print('nfa2 is') print(nfa2)
def testDfaEquivNfa1(self): checkFileName = 'testDfaEquivNfa1' + '.good' outFileName = 'testDfaEquivNfa1' + '.out' with open(outFileName, 'w') as outf: with redirect_stdout(outf): 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) print(40 * '-') print(f'dfa1.equiv(nfa2) is {dfa1.equiv(nfa2)}') nfa3delta = { # this nfa accepts the same language as dfa1's (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'dfa1.equiv(nfa3) is {dfa1.equiv(nfa3)}') nfa4delta = { # this nfa accepts the same language as dfa1's (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'dfa1.equiv(nfa4) is {dfa1.equiv(nfa4)}') with open(outFileName) as outf: outStr = outf.read() with open(checkFileName) as f: checkStr = f.read() self.assertEqual(outStr, checkStr)
def testEquivNfa2(self): checkFileName = 'testEquivNfa2' + '.good' outFileName = 'testEquivNfa2' + '.out' with open(outFileName, 'w') as outf: with redirect_stdout(outf): nfa1delta = { # this nfa is essentially a dfa that accepts the complement of (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} } nfa1 = nfa(delta=nfa1delta, start=0, finals={1, 4}) print('nfa1 is') print(nfa1) print(40*'-') nfa2delta = { # this nfa accepts a different language from nfa1's (0, '0') : {1}, (1, '1') : {2}, (2, '0') : {3}, (3, '0') : {1}, (3, '1') : {2} } nfa2 = nfa(delta=nfa2delta, start=0, finals={2,3}) print(40*'-') print('nfa2 is') print(nfa2) print(40*'-') print(f'nfa1.equiv(nfa2) is {nfa1.equiv(nfa2)}') print(f'nfa2.equiv(nfa1) is {nfa2.equiv(nfa1)}') nfa3delta = { # this nfa accepts a different language from nfa1's (0, '0') : {1, 2}, (1, '1') : {0}, (2, '1') : {3}, (3, '0') : {0} } nfa3 = nfa(delta=nfa3delta, start=0, finals={1,3}) print(40*'-') print('nfa3 is') print(nfa3) print(40*'-') print(f'nfa1.equiv(nfa3) is {nfa1.equiv(nfa3)}') print(f'nfa2.equiv(nfa3) is {nfa2.equiv(nfa3)}') nfa4delta = { # this nfa accepts a different language from nfa1's (0, '0') : {1}, (1, '1') : {2}, (2, '0') : {0}, (2, '') : {0} } nfa4 = nfa(delta=nfa4delta, start=0, finals={0,1,2}) print(40*'-') print('nfa4 is') print(nfa4) print(40*'-') print(f'nfa1.equiv(nfa4) is {nfa1.equiv(nfa4)}') print(f'nfa2.equiv(nfa4) is {nfa2.equiv(nfa4)}') print(f'nfa3.equiv(nfa4) is {nfa3.equiv(nfa4)}') with open(outFileName) as outf: outStr = outf.read() with open(checkFileName) as f: checkStr = f.read() self.assertEqual(outStr, checkStr)
from reglang.nfa import nfa delta04 = { # an NFA p75 ex.2.2.9(a) L&P: language a*b(aUb)* (0, 'a'):{0}, (0, 'b'):{0, 2}, (0, '') :{1}, (1, 'b'):{2, 4}, (2, 'a'):{3}, (3, '') :{4}, (4, 'a'):{3} } m04 = nfa(delta=delta04, start=0, finals={3, 4}) dash40 = 40*'-' print('NFA is as follows:') print(dash40) print(m04) print(dash40)
from reglang.nfa import nfa delta_m4 = { # NFA ex.2.2.6 (a): L = (ab U aab U aba)* (0, 'a'):{1}, (1, 'a'):{2}, (1, 'b'):{0, 3}, (2, 'b'):{0}, (3, 'a'):{0} } m4 = nfa(delta=delta_m4, start=0, finals={0}) print() print('NFA m4 is as follows:') print(m4) print() tlist = ['', 'a', 'b', 'ab', 'aab', 'aba', 'bab', 'abab','abaa', 'abaaababab', 'abaaabb'] for inpstr in tlist: print('[accept version 1]') print(f'NFA m4 {"accepts" if m4._accept0(inpstr) else "does not accept"} "{inpstr}"') print('[accept version 2]') print(f'NFA m4 {"accepts" if m4.accept(inpstr) else "does not accept"} "{inpstr}"') print()
from reglang.nfa import nfa delta_m3 = { # NFA fig 2.9 p.70 L&P, language: (aa* U aa*b U b)(ep U (aUb)a*) (0, 'b'): {2}, (0, ''): {1}, (1, 'a'): {0, 4}, (1, ''): {2, 3}, (2, 'b'): {4}, (3, 'a'): {4}, (4, ''): {3} } m3 = nfa(delta=delta_m3, start=0, finals={4}) print() print('NFA m3 is as follows:') print(m3) print() for inpstr in ['', 'a', 'b', 'aab', 'aaba', 'aabba', 'aabbb', 'aabbabab']: print('[accept version 1]') print( f'NFA m3 {"accepts" if m3._accept0(inpstr) else "does not accept"} "{inpstr}"' ) print('[accept version 2]') print( f'NFA m3 {"accepts" if m3.accept(inpstr) else "does not accept"} "{inpstr}"' ) print()
from reglang.nfa import nfa delta1 = { # an NFA (0, 'a'):{1}, (0, '') :{2}, (1, 'b'):{3}, (1, '') :{0, 4}, (2, 'a'):{1, 5}, (2, 'b'):{2}, (2, '') :{4}, (3, '') :{4}, (4, '') :{6}, (5, 'a'):{5}, (5, '') :{6}, } m1 = nfa(delta=delta1, start=0, finals={6}) dash40 = 40 * '-' print('NFA is as follows:') print(dash40) print(m1) print(dash40) for q in m1.states: closure = m1._ep_closure(q) print(f'E-closure of state {q} = {closure}') print(dash40)