DFA wrapper Demo

Example usage case of dfa_parser is given below


In [1]:
from dfa_parser import dfa_from_dict, dfa_to_dict

In [2]:
dfa_dict = {'accepts': {1},
 'start': 0,
 'transitions': {0: {'a': 0, 'b': 1}, 1: {'a': 1, 'b': 2}, 2: {'a': 2, 'b': 0}}}

In [3]:
dfa = dfa_from_dict(dfa_dict)

In [4]:
dfa.pretty_print()


This DFA has 3 states
States: {0, 1, 2}
Alphabet: {'a', 'b'}
Starting state: 0
Accepting states: {1}
Transition function:
	 0	1	2
a 	 0	1	2
b 	 1	2	0
Current state: 0
Currently accepting: False


In [5]:
dfa_to_dict(dfa)


Out[5]:
{'accepts': {1},
 'start': 0,
 'transitions': {0: {'a': 0, 'b': 1},
  1: {'a': 1, 'b': 2},
  2: {'a': 2, 'b': 0}}}

In [6]:
dfa.current_state


Out[6]:
0

In [7]:
dfa.input('a')
dfa.current_state


Out[7]:
0

In [8]:
dfa.pretty_print()


This DFA has 3 states
States: {0, 1, 2}
Alphabet: {'a', 'b'}
Starting state: 0
Accepting states: {1}
Transition function:
	 0	1	2
a 	 0	1	2
b 	 1	2	0
Current state: 0
Currently accepting: False


In [9]:
import dfa_grapher as dfg

In [14]:
dfg.draw_dfa(dfa, 'dfa_graph.png')

DFA sampler usage is given below, the class generates random strings until it finds an accepting or rejecting string.


In [11]:
from dfa_sampler import get_negative_input, get_positive_input

In [12]:
get_positive_input(dfa, 5)


Out[12]:
'aaaab'

In [13]:
get_negative_input(dfa,5)


Out[13]:
'abaab'

In [ ]: