Exemplo n.º 1
0
def get_states_cross_product(automaton1: Automaton, automaton2: Automaton) -> set:
    states = set()
    for s1 in sorted(automaton1.states):
        for s2 in sorted(automaton2.states):         
            state = State(f"{s1.name}{s2.name}")
            for trans1 in s1.transitions:
                for trans2 in s2.transitions:
                    if trans1.symbol == trans2.symbol:
                        target_name = f"{trans1.target}{trans2.target}"
                        state.add_transition(target_name, trans1.symbol)
            states.add(state)
    return states
Exemplo n.º 2
0
def kleene_star(automaton1: Automaton) -> Automaton:
    states = automaton1.states
    newInitial = State("kleen_initial")
    states.add(newInitial)

    for state in automaton1.states:
        if state.name in automaton1.accept:
            state.add_transition(newInitial.name, EPSILON_SYMBOL)

    automaton1.accept.add(newInitial.name)
    newInitial.add_transition(automaton1.initial, EPSILON_SYMBOL)
    return Automaton(states, newInitial.name, automaton1.accept, automaton1.alphabet)
Exemplo n.º 3
0
def recursive(afd_direct, current_state, alphabet, symbol_ids,
              follow_pos_table, final_state_symbol_id):
    for letter in alphabet:
        new_state_id = set()

        for symbol_id in symbol_ids:
            if (symbol_id['symbol'] == letter
                    and symbol_id['id'] in current_state.id):
                new_state_id.update(follow_pos_table[symbol_id['id']])
                #print('S:{}   I:{}   IDS:{}'.format(current_state.id, symbol_id['symbol'], new_state_id))

        if (len(new_state_id) == 0):
            continue

        found_state = None
        for state in afd_direct.states:
            if (state.id == new_state_id):
                found_state = state
                break

        if (found_state):
            afd_direct.transitions.append(
                Transition(current_state, found_state, letter))
        else:
            new_state = State(new_state_id)
            afd_direct.states.append(new_state)
            afd_direct.transitions.append(
                Transition(current_state, new_state, letter))

            if (final_state_symbol_id in new_state_id):
                afd_direct.final_states.append(new_state)

            recursive(afd_direct, new_state, alphabet, symbol_ids,
                      follow_pos_table, final_state_symbol_id)
Exemplo n.º 4
0
def create_direct_afd(tree, symbol_ids, alphabet):
    follow_pos_table = {}

    final_state_symbol_id = -1

    for symbol_id in symbol_ids:
        follow_pos_table[symbol_id['id']] = set()
        if (symbol_id['symbol'] == '#'):
            final_state_symbol_id = symbol_id['id']
            break

    recursive_follow_pos(tree, symbol_ids, follow_pos_table)

    print('follow_pos: {}'.format(follow_pos_table))

    afd_direct = AF()

    initial_state = State(tree.first_pos)
    afd_direct.states.append(initial_state)
    afd_direct.initial_state = initial_state

    if (final_state_symbol_id in initial_state.id):
        afd_direct.final_states.append(initial_state)

    current_state = initial_state

    recursive(afd_direct, current_state, alphabet, symbol_ids,
              follow_pos_table, final_state_symbol_id)

    return afd_direct
Exemplo n.º 5
0
def concat(automaton1: Automaton, automaton2: Automaton) -> Automaton:
    states = set()
    for state in sorted(automaton1.states):
        new_state = State(f"A1{state.name}")
        for trans in state.transitions:
            new_state.add_transition(f"A1{trans.target}", trans.symbol)
        if state.name in automaton1.accept:
            new_state.add_transition(f"A2{automaton2.initial}", EPSILON_SYMBOL)
        states.add(new_state)
    for state in sorted(automaton2.states):
        new_state = State(f"A2{state.name}")
        for trans in state.transitions:
            new_state.add_transition(f"A2{trans.target}", trans.symbol)
        states.add(new_state)
    initial = f"A1{automaton1.initial}"
    accept = set()
    for state_name in automaton2.accept:
        accept.add(f"A2{state_name}")
    alphabet = automaton1.alphabet | automaton2.alphabet
    return Automaton(states, initial, accept, alphabet)
Exemplo n.º 6
0
    def test_State(self):
        s1 = State()
        self.assertFalse(s1.is_start())
        self.assertFalse(s1.is_accept())

        s1.set_accept(True)
        self.assertTrue(s1.is_accept())
Exemplo n.º 7
0
def e_closure(afn, state, past_states_ids=set()):
    if (type(state.id) is set):
        state_id_set = state.id.copy()
    else:
        state_id_set = {state.id}

    for transition in afn.transitions:
        if (transition.symbol == epsilon_symbol
                and transition.current_state.id in state_id_set
                and transition.next_state.id not in state_id_set
                and transition.next_state.id not in past_states_ids):
            new_past_states_ids = past_states_ids.copy()
            new_past_states_ids.add(transition.next_state.id)
            new_state, _ = e_closure(afn, transition.next_state,
                                     new_past_states_ids)
            state_id_set.update(new_state.id)

    return State(state_id_set), past_states_ids
Exemplo n.º 8
0
def mov(afn, state, letter):
    if (type(state.id) is set):
        state_id_set = state.id.copy()
    else:
        state_id_set = {state.id}

    state_transitions = [
        transition for transition in afn.transitions
        if (transition.current_state.id in state_id_set
            and transition.symbol == letter)
    ]
    new_subset = set()
    for transition in state_transitions:
        new_subset.add(transition.next_state.id)

    if (len(new_subset) > 0):
        return State(new_subset)
    else:
        return None
Exemplo n.º 9
0
def create_direct_afd(tree, symbol_ids, alphabet, tokens):
    follow_pos_table = {}

    final_state_symbol_ids = {}

    for symbol_id in symbol_ids:
        follow_pos_table[symbol_id['id']] = set()
        if(type(symbol_id['symbol']) is str and len(symbol_id['symbol'])>1 and symbol_id['symbol'].startswith('#')):
            final_state_symbol_ids[str(symbol_id['id'])] = symbol_id['symbol'][1:]

    #print('FINAL: {}'.format(final_state_symbol_ids))

    recursive_follow_pos(tree, symbol_ids, follow_pos_table)

    #print('follow_pos: {}'.format(follow_pos_table))

    afd_direct = AF()

    initial_state = State(tree.first_pos)
    afd_direct.states.append(initial_state)
    afd_direct.initial_state = initial_state

    flag = False
    for id in initial_state.id:
        if(str(id) in final_state_symbol_ids):
            flag = True
            for token in tokens:
                if(token.id == final_state_symbol_ids[str(id)]):
                    initial_state.tokens.append(token)
                    break
    if(flag):
        afd_direct.final_states.append(initial_state)

    current_state = initial_state

    recursive(afd_direct,current_state,alphabet,symbol_ids,follow_pos_table,final_state_symbol_ids, tokens)

    return afd_direct
Exemplo n.º 10
0
 def __init__(self, name):
     self.name = name
     self.states = {}
     self.initial_state = State()
Exemplo n.º 11
0
 def add_state(self, label, is_final):
     self.states[label] = State(
         name=label,
         accepting=is_final
     )
Exemplo n.º 12
0
 def addState(self, stateName, isInitial=False, isFinal=False):
     if not self.findState(stateName):
         self.states.append(State(stateName, isInitial, isFinal))
     else:
         raise StateAlreadyExistError(stateName)
Exemplo n.º 13
0
 def addState(self, stateName, isInitial=False, isFinal=False):
     # print("adding state ", stateName, isInitial, isFinal, self.findState(stateName))
     if not self.findState(stateName):
         self.states.append(State(stateName, isInitial, isFinal))
     else:
         return