[ganesh@line-ganesh concepts]$ which dot which dot /usr/local/bin/dot [ganesh@line-ganesh l2]$ python3 python3 Python 3.2.1 (v3.2.1:ac1f7e5c0510, Jul 9 2011, 01:03:53) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> from math import * def nthnumeric(N): """Assume that Sigma is {a,b}. Produce the Nth string in numeric order, where N >= 0. Idea : Given N, get b = floor(log_2(N+1)) - need that many places; what to fill in the places is the binary code for N - (2^b - 1) with 0 as a and 1 as b. """ if(N==0): return '' else: width = floor(log(N+1, 2)) tofill = int(N - pow(2, width) + 1) relevant_binstr = bin(tofill)[2::] # strip the 0b leading string len_to_makeup = width - len(relevant_binstr) return "a"*len_to_makeup + homos(relevant_binstr, lambda x: 'b' if x=='1' else 'a') from math import * >>> >>> def nthnumeric(N): ... """Assume that Sigma is {a,b}. Produce the Nth string in numeric order, where N >= 0. ... Idea : Given N, get b = floor(log_2(N+1)) - need that many places; what to ... fill in the places is the binary code for N - (2^b - 1) with 0 as a and 1 as b. ... """ ... if(N==0): ... return '' ... else: ... width = floor(log(N+1, 2)) ... tofill = int(N - pow(2, width) + 1) ... relevant_binstr = bin(tofill)[2::] # strip the 0b leading string ... len_to_makeup = width - len(relevant_binstr) ... return "a"*len_to_makeup + homos(relevant_binstr, lambda x: 'b' if x=='1' else 'a') ... >>> nthnumeric(0) nthnumeric(0) '' >>> nthnumeric(1) nthnumeric(1) Traceback (most recent call last): File "", line 1, in File "", line 13, in nthnumeric NameError: global name 'homos' is not defined >>> def homos(S,f): """String homomorphism wrt lambda f homos("abcd",hm) --> 'bcde' where hm = lambda x: chr( (ord(x)+1) % 256 ) """ return "".join(map(f,S)) def homos(S,f): ... """String homomorphism wrt lambda f ... homos("abcd",hm) --> 'bcde' where hm = lambda x: chr( (ord(x)+1) % 256 ) ... """ ... return "".join(map(f,S)) ... >>> nthnumeric(1) nthnumeric(1) 'a' >>> nthnumeric(2) nthnumeric(2) 'b' >>> nthnumeric(3) nthnumeric(3) 'aa' >>> nthnumeric(4) nthnumeric(4) 'ab' >>> nthnumeric(23123123213223) nthnumeric(23123123213223) 'ababaaaaabbbbbaaababbaaababbbaabaabbbababaaa' >>> nthnumeric(231231232132233434343) nthnumeric(231231232132233434343) 'baabaaabaaabbbbbabaababbbaabaaaaabaaabbbaabbabababaaaaaaaaaaaaaaaaa' >>> nthnumeric(63) nthnumeric(63) 'aaaaaa' >>> nthnumeric(64) nthnumeric(64) 'aaaaab' >>> nthnumeric(62) nthnumeric(62) 'bbbbb' >>> nthnumeric(65) nthnumeric(65) 'aaaaba' >>> nthnumeric(254) nthnumeric(254) 'bbbbbbb' >>> nthnumeric(253) nthnumeric(253) 'bbbbbba' >>> nthnumeric(255) nthnumeric(255) 'aaaaaaaa' >>> nthnumeric(256) nthnumeric(256) 'aaaaaaab' >>> ^D [ganesh@line-ganesh concepts]$ python3 python3 Python 3.2.1 (v3.2.1:ac1f7e5c0510, Jul 9 2011, 01:03:53) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> from dfa import * from dfa import * Q: {'Real_I', 'Real_F', 'S3', 'S2', 'S1', 'S0', 'BH'} Sigma: {'a', '', 'c', 'b', 'e', 'd', 'g', 'f'} q0: Real_I F: {'Real_F'} Delta: Real_I Real_F S3 S2 S1 S0 BH ---------------------------------------------------------------------------------------- BH BH BH BH BH S1 BH a S0 BH Real_F Real_F BH BH BH BH BH BH BH S3 BH BH c BH BH BH BH S1 BH BH b BH BH BH BH S2 BH BH e BH BH S1 BH BH BH BH d BH BH BH BH BH S2 BH g BH BH BH S3 BH BH BH f **** Eliminating state S3 **** {'Q': {'S2', 'S1', 'S0', 'BH', 'Real_I', 'Real_F'}, 'q0': 'Real_I', 'F': {'Real_F'}, 'Sigma': {'', 'a', 'b', 'e', 'g', ' (c) ', 'EPS + (f) ', ' (f) (d) ', 'b + (c) (d) '}, 'Delta': {('S2', 'a'): 'BH', ('Real_F', 'g'): 'BH', ('BH', 'g'): 'BH', ('S1', 'a'): 'BH', ('BH', 'b'): 'BH', ('Real_I', 'b'): 'BH', ('S1', 'b + (c) (d) '): 'S1', ('S0', 'e'): 'BH', ('S1', 'e'): 'S2', ('S2', 'b'): 'BH', ('S0', 'a'): 'S1', ('Real_I', 'e'): 'BH', ('Real_F', ''): 'BH', ('S1', ''): 'BH', ('S2', 'g'): 'BH', ('Real_I', 'a'): 'BH', ('Real_F', 'e'): 'BH', ('BH', 'a'): 'BH', ('S2', 'EPS + (f) '): 'Real_F', ('S0', 'b'): 'BH', ('S0', ''): 'BH', ('Real_F', 'a'): 'BH', ('BH', 'e'): 'BH', ('Real_I', ''): 'S0', ('S0', 'g'): 'S2', ('BH', ''): 'BH', ('S1', ' (c) '): 'Real_F', ('S1', 'g'): 'BH', ('S2', ' (f) (d) '): 'S1', ('S2', 'e'): 'BH', ('Real_I', 'g'): 'BH', ('Real_F', 'b'): 'BH'}} Q: {'Real_I', 'Real_F', 'S2', 'S1', 'S0', 'BH'} Sigma: {'', 'a', 'b', 'e', 'g', ' (c) ', 'EPS + (f) ', ' (f) (d) ', 'b + (c) (d) '} q0: Real_I F: {'Real_F'} Delta: Real_I Real_F S2 S1 S0 BH ---------------------------------------------------------------------------------------- S0 BH BH BH BH BH BH BH BH BH S1 BH a BH BH BH BH BH BH b BH BH BH S2 BH BH e BH BH BH BH S2 BH g BH BH BH Real_F BH BH (c) BH BH Real_F BH BH BH EPS + (f) BH BH S1 BH BH BH (f) (d) BH BH BH S1 BH BH b + (c) (d) **** Eliminating state S1 **** {'Q': {'Real_F', 'S0', 'BH', 'Real_I', 'S2'}, 'q0': 'Real_I', 'F': {'Real_F'}, 'Sigma': {'', ' (a) (b + (c) (d) )* ( (c) ) ', 'EPS + (f) ', 'g + (a) (b + (c) (d) )* (e) ', 'g', ' ( (f) (d) ) (b + (c) (d) )* (e) ', 'EPS + (f) + ( (f) (d) ) (b + (c) (d) )* ( (c) ) '}, 'Delta': {('Real_I', 'EPS + (f) '): 'BH', ('S0', ' (a) (b + (c) (d) )* ( (c) ) '): 'Real_F', ('S2', ''): 'BH', ('S2', 'EPS + (f) + ( (f) (d) ) (b + (c) (d) )* ( (c) ) '): 'Real_F', ('BH', ''): 'BH', ('S0', 'g + (a) (b + (c) (d) )* (e) '): 'S2', ('Real_F', 'g'): 'BH', ('BH', 'g'): 'BH', ('S0', 'EPS + (f) '): 'BH', ('S0', ''): 'BH', ('Real_F', ''): 'BH', ('Real_I', 'g'): 'BH', ('Real_F', 'EPS + (f) '): 'BH', ('S2', 'g'): 'BH', ('Real_I', ''): 'S0', ('BH', 'EPS + (f) '): 'BH', ('S2', ' ( (f) (d) ) (b + (c) (d) )* (e) '): 'S2'}} Q: {'Real_I', 'S2', 'Real_F', 'S0', 'BH'} Sigma: {'', ' (a) (b + (c) (d) )* ( (c) ) ', 'EPS + (f) ', 'g + (a) (b + (c) (d) )* (e) ', 'g', ' ( (f) (d) ) (b + (c) (d) )* (e) ', 'EPS + (f) + ( (f) (d) ) (b + (c) (d) )* ( (c) ) '} q0: Real_I F: {'Real_F'} Delta: Real_I S2 Real_F S0 BH ---------------------------------------------------------------------------------------- S0 BH BH BH BH BH BH BH Real_F BH (a) (b + (c) (d) )* ( (c) ) BH BH BH BH BH EPS + (f) BH BH BH S2 BH g + (a) (b + (c) (d) )* (e) BH BH BH BH BH g BH S2 BH BH BH ( (f) (d) ) (b + (c) (d) )* (e) BH Real_F BH BH BH EPS + (f) + ( (f) (d) ) (b + (c) (d) )* ( (c) ) **** Eliminating state S0 **** {'Q': {'S2', 'BH', 'Real_I', 'Real_F'}, 'q0': 'Real_I', 'F': {'Real_F'}, 'Sigma': {' ( (a) (b + (c) (d) )* ( (c) ) ) ', 'EPS + (f) + ( (f) (d) ) (b + (c) (d) )* ( (c) ) ', ' (g + (a) (b + (c) (d) )* (e) ) ', ' ( (f) (d) ) (b + (c) (d) )* (e) '}, 'Delta': {('Real_I', ' ( (a) (b + (c) (d) )* ( (c) ) ) '): 'Real_F', ('BH', ' ( (f) (d) ) (b + (c) (d) )* (e) '): 'BH', ('Real_F', 'EPS + (f) + ( (f) (d) ) (b + (c) (d) )* ( (c) ) '): 'BH', ('BH', 'EPS + (f) + ( (f) (d) ) (b + (c) (d) )* ( (c) ) '): 'BH', ('Real_F', ' ( (f) (d) ) (b + (c) (d) )* (e) '): 'BH', ('Real_I', 'EPS + (f) + ( (f) (d) ) (b + (c) (d) )* ( (c) ) '): 'BH', ('Real_I', ' (g + (a) (b + (c) (d) )* (e) ) '): 'S2', ('Real_I', ' ( (f) (d) ) (b + (c) (d) )* (e) '): 'BH', ('S2', 'EPS + (f) + ( (f) (d) ) (b + (c) (d) )* ( (c) ) '): 'Real_F', ('S2', ' ( (f) (d) ) (b + (c) (d) )* (e) '): 'S2'}} Q: {'S2', 'BH', 'Real_I', 'Real_F'} Sigma: {' ( (a) (b + (c) (d) )* ( (c) ) ) ', 'EPS + (f) + ( (f) (d) ) (b + (c) (d) )* ( (c) ) ', ' (g + (a) (b + (c) (d) )* (e) ) ', ' ( (f) (d) ) (b + (c) (d) )* (e) '} q0: Real_I F: {'Real_F'} Delta: S2 BH Real_I Real_F ---------------------------------------------------------------------------------------- BH BH Real_F BH ( (a) (b + (c) (d) )* ( (c) ) ) Real_F BH BH BH EPS + (f) + ( (f) (d) ) (b + (c) (d) )* ( (c) ) BH BH S2 BH (g + (a) (b + (c) (d) )* (e) ) S2 BH BH BH ( (f) (d) ) (b + (c) (d) )* (e) **** Eliminating state S2 **** {'Q': {'Real_F', 'BH', 'Real_I'}, 'q0': 'Real_I', 'F': {'Real_F'}, 'Sigma': {' ( (a) (b + (c) (d) )* ( (c) ) ) + ( (g + (a) (b + (c) (d) )* (e) ) ) ( ( (f) (d) ) (b + (c) (d) )* (e) )* (EPS + (f) + ( (f) (d) ) (b + (c) (d) )* ( (c) ) ) ', ' ( (a) (b + (c) (d) )* ( (c) ) ) '}, 'Delta': {('BH', ' ( (a) (b + (c) (d) )* ( (c) ) ) '): 'BH', ('Real_I', ' ( (a) (b + (c) (d) )* ( (c) ) ) + ( (g + (a) (b + (c) (d) )* (e) ) ) ( ( (f) (d) ) (b + (c) (d) )* (e) )* (EPS + (f) + ( (f) (d) ) (b + (c) (d) )* ( (c) ) ) '): 'Real_F', ('Real_F', ' ( (a) (b + (c) (d) )* ( (c) ) ) '): 'BH'}} Q: {'Real_F', 'BH', 'Real_I'} Sigma: {' ( (a) (b + (c) (d) )* ( (c) ) ) + ( (g + (a) (b + (c) (d) )* (e) ) ) ( ( (f) (d) ) (b + (c) (d) )* (e) )* (EPS + (f) + ( (f) (d) ) (b + (c) (d) )* ( (c) ) ) ', ' ( (a) (b + (c) (d) )* ( (c) ) ) '} q0: Real_I F: {'Real_F'} Delta: Real_F BH Real_I ---------------------------------------------------------------------------------------- BH BH Real_F ( (a) (b + (c) (d) )* ( (c) ) ) + ( (g + (a) (b + (c) (d) )* (e) ) ) ( ( (f) (d) ) (b + (c) (d) )* (e) )* (EPS + (f) + ( (f) (d) ) (b + (c) (d) )* ( (c) ) ) BH BH BH ( (a) (b + (c) (d) )* ( (c) ) ) >>> DFA1 DFA1 {'Q': {'S1', 'S0'}, 'q0': 'S0', 'F': {'S1'}, 'Sigma': {'a', 'b'}, 'Delta': {('S0', 'a'): 'S0', ('S1', 'a'): 'S0', ('S1', 'b'): 'S1', ('S0', 'b'): 'S1'}} >>> prdfa(DFA1) prdfa(DFA1) Q: {'S1', 'S0', 'BH'} Sigma: {'a', 'b'} q0: S0 F: {'S1'} Delta: S1 S0 BH ---------------------------------------------------------------------------------------- S0 S0 BH a S1 S1 BH b >>>  Suspended [ganesh@line-ganesh concepts]$ [ganesh@line-ganesh concepts]$ ls *.py ls *.py calc.py nfa.py parsetab.py prauttest.py savehacks.py dfa.py nfa2dfa.py pascal.py re.py test.py dfatest.py old-DFA.py pascal_nopr.py redfac.py lang.py old-lang.py praut.py reg.py [ganesh@line-ganesh concepts]$ fg fg python3 >>> from praut import * from praut import * >>> dot_dfa(DFA1, "/tmp/DFA1.dot") dot_dfa(DFA1, "/tmp/DFA1.dot") >>>  Suspended [ganesh@line-ganesh concepts]$ [ganesh@line-ganesh concepts]$ cp /tmp/DFA1.dot . cp /tmp/DFA1.dot . overwrite ./DFA1.dot? (y/n [n]) y y [ganesh@line-ganesh concepts]$ dot -Tps DFA1.dot > DFA1.ps dot -Tps DFA1.dot > DFA1.ps [ganesh@line-ganesh concepts]$ fg fg python3 >>> Delta1 Delta1 {('S0', 'a'): 'S0', ('S1', 'a'): 'S0', ('S1', 'b'): 'S1', ('S0', 'b'): 'S1'} >>> Delta1.keys() Delta1.keys() dict_keys([('S0', 'a'), ('S1', 'a'), ('S1', 'b'), ('S0', 'b')]) >>> Delta1.values() Delta1.values() dict_values(['S0', 'S0', 'S1', 'S1']) >>> Delta1[('S0', 'a')] Delta1[('S0', 'a')] 'S0' >>> DFA1 DFA1 {'Q': {'S1', 'S0'}, 'q0': 'S0', 'F': {'S1'}, 'Sigma': {'a', 'b'}, 'Delta': {('S0', 'a'): 'S0', ('S1', 'a'): 'S0', ('S1', 'b'): 'S1', ('S0', 'b'): 'S1'}} >>> ^D