Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- '''
- This simple module allow you to generate DOT graph descriptions using the
- fa_to_dot() function. So the function would return something like the following:
- digraph fi {
- "x-" -> "y" [label="a"];
- "x-" -> "z+" [label="b"];
- "y" -> "x-" [label="a"];
- "y" -> "z+" [label="b"];
- "z+" -> "z+" [label="a"];
- "z+" -> "z+" [label="b"];
- }
- Please have a look at the source of the main() function of the module to see how
- to use fa_to_dot().
- '''
- def check_state(state, start_state, final_states):
- '''
- Return the state as a begin (-) or end (+) state if the state is a begin
- or end state.
- '''
- result_state = state
- if state == start_state:
- result_state += '-'
- if state in final_states:
- result_state += '+'
- return result_state
- def fa_to_dot(states, alphabet, transistions, start_state, final_states):
- '''
- Builds a DOT graph description using a FA Machine.
- TODO: Check that the states and alphabet that is in use by the transistions
- match the parameters.
- '''
- dot = 'digraph fa {\n'
- for transition in transistions:
- from_state, input_letter = transition
- to_state = transistions[transition]
- from_node = check_state(from_state, start_state, final_states)
- to_node = check_state(to_state, start_state, final_states)
- label = input_letter
- dot += '\t"' + from_node + '" -> "' + to_node + '" [label="' \
- + label + '"]\n'
- dot += '}'
- return dot
- def main():
- # S - Set of the states
- states = set('xyz')
- # Σ (Sigma) - Alphabet of the input letters
- alphabet = set('ab')
- # T: SxΣ -> S - Set of transistions
- # Because we want it to resebmle a set of functions we use a dict:
- # T(S,Σ) = S is (S, Σ): S
- # Where (S, Σ) on the LHS is the key and S on the RHS is the value of
- # the dict item
- transistions = {('x', 'a'): 'y',
- ('x', 'b'): 'z',
- ('y', 'a'): 'x',
- ('y', 'b'): 'z',
- ('z', 'a'): 'z',
- ('z', 'b'): 'z'}
- # s0 - Start state
- start_state = 'x'
- # F - Set of Final States
- final_states = set('z')
- print fa_to_dot(states, alphabet, transistions, start_state, final_states)
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement