Exemple #1
0
class FiniteState(object):
    """Using the Automata library for Finate State"""
    state = attr.ib(default={"S0", "S1", "S2"})
    input_symbols = attr.ib(default={"0", "1"})
    transitions = attr.ib(
        default={
            "S0": {
                "0": "S0",
                "1": "S1"
            },
            "S1": {
                "0": "S2",
                "1": "S0"
            },
            "S2": {
                "0": "S1",
                "1": "S2"
            },
        })
    initial_state = attr.ib(default="S0")
    final_states = attr.ib(default={"S0", "S1", "S2"})
    dfa = attr.ib(default=None)

    def __attrs_post_init__(self):
        """Initialization by creating the default object"""
        if self.dfa is None:
            self.dfa = DFA(
                states=self.state,
                input_symbols=self.input_symbols,
                transitions=self.transitions,
                initial_state=self.initial_state,
                final_states=self.final_states,
            )

    def get_final_value(self, input_string):
        """Returns the final value, rasies RejectionException if a problem is found"""
        value = self.dfa.read_input(input_string)[1:]
        return value

    def get_final_state(self, input_string):
        """Returns the final state, rasies RejectionException if a problem is found"""
        value = self.dfa.read_input(input_string)[:1]
        return value
        print(
            "Please enter STATE_STATE and ACCEPT_STATES that are in Q : {}.\nAccept states should be a valid subset of Q\n"
            .format(Q))

CURRENT_STATE = None
dfa = DFA(states=Q,
          input_symbols=SIGMA,
          transitions=transition_dict,
          initial_state=initial,
          final_states=set(final))
if dfa.validate():
    print("\nThe DFA is accepted.")
    s = input("Enter the Input String: ")
    if dfa.accepts_input(s):
        print('Accepted!')
        print("The final state that the DFA stopped on:", dfa.read_input(s))
    else:
        print('Rejected!')
else:
    print("\nThe DFA is rejected.")
""" Non-Deterministic Finite Automata (NFA) """

nfa = NFA(
    states={'q0', 'q1', 'q2'},
    input_symbols={'a', 'b'},
    transitions={
        'q0': {
            'a': {'q1'}
        },
        'q1': {
            'a': {'q1'},
#Q. Write a deterministic automata code for the language with ∑={0,1} accepts the set of all strings with three consecutive 1's.
#!pip install automata-lib


from automata.fa.dfa import DFA 

dfa = DFA( states={'q0', 'q1', 'q2','q3'}, input_symbols={'0', '1'}, transitions={ 'q0': {'1': 'q1','0':'q0'},
'q1': {'0': 'q2', '1': 'q1'}, 'q2': {'0': 'q2', '1': 'q3'},
'q3': {'0': 'q3', '1': 'q3'},
},initial_state='q0', final_states={'q3 '} )

dfa.read_input('111')

if dfa.accepts_input('111'): 
    print('accepted' ) 

else: 
    print('rejected')


#Q. Write a deterministic automata code for the language with ∑={0,1} accepts even Number of 0's and even number of 1's.
#pip install automata-lib

from automata.fa.dfa import DFA 

dfa = DFA(
states={'q0', 'q1', 'q2','q3'},
input_symbols={'0', '1'}, transitions={ 'q0': {'1': 'q1','0':'q0'}, 'q1': {'0': 'q2', '1': 'q1'},
'q2': {'0': 'q2', '1': 'q3'},
'q3': {'0': 'q3', '1': 'q3'},
 },