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)
def addTransition(self, transitionName, fromName, toName): # print("Adding Transition ", transitionName, " from ", fromName , " to ", toName) _from = None _to = None try: _from = self.filter(fromName)[0] except: raise UnknownStateError(fromName) try: _to = self.filter(toName)[0] except: raise UnknownStateError(toName) for c in re.compile(",|\/").split(transitionName): if c not in self.alphabet: raise UnknownCharError(c) if not (self.isDeterministic(_from, transitionName)): raise DeterminismError(fromName, transitionName) existTransFromTo = None try: existTransFromTo = list( filter(lambda x: x._to == toName, _from.transitions))[0] except: _from.addTransition(Transition(transitionName, fromName, toName))
def create_zero_or_one_fsm(afn, afn_s): si = afn.create_state() sf = afn.create_state() states = [si,sf] states += afn_s.states transitions = afn_s.transitions transitions.append(Transition(si, afn_s.initial_state,epsilon_symbol)) transitions.append(Transition(si, sf,epsilon_symbol)) for final_state in afn_s.final_states: transitions.append(Transition(final_state, sf,epsilon_symbol)) new_afn = AF(start_node_id=afn.current_node_id) new_afn.states = states new_afn.initial_state = si new_afn.final_states = [sf] new_afn.transitions = transitions return new_afn
def create_letter_fsm(afn, letter): si = afn.create_state() sf = afn.create_state() transitions = [] transitions.append(Transition(si,sf,letter)) new_afn = AF(start_node_id=afn.current_node_id) new_afn.states = [si,sf] new_afn.initial_state = si new_afn.final_states = [sf] new_afn.transitions = transitions return new_afn
def addTransition(self, transitionName, fromName, toName): _from = None _to = None try: _from = self.filter(fromName)[0] except: raise UnknownStateError(fromName) try: _to = self.filter(toName)[0] except: raise UnknownStateError(toName) for c in re.compile(", | \/").split(transitionName): if not c in self.alphabet and c != epsilon: raise UnknownCharError(c) _from.addTransition(Transition(transitionName, fromName, toName))
def recursive_mov(afn, afd, state, letters): for letter in letters: new_state = mov(afn, state, letter) print('mov({}, {}) = {}'.format( set(state.id), letter, set(new_state.id) if new_state else {})) if (new_state): new_state_2, _ = e_closure(afn, new_state) print('e-closure({}) = {}'.format(set(new_state.id), set(new_state_2.id))) new_transitions = Transition(state, new_state_2, letter) if (new_transitions not in afd.transitions): afd.transitions.append(new_transitions) if (new_state_2 not in afd.states): afd.states.append(new_state_2) recursive_mov(afn, afd, new_state_2, letters)
def addTransition(self, transitionName, fromName, toName): _from = self.findState( fromName ) #list( filter( lambda x: x.label == fromName, self.states ) )[0] # _to = self.findState(toName) if not _from: raise UnknownStateError(fromName) if not _to: raise UnknownStateError(toName) if _from == _to: transitionName = transitionName existTransFromTo = None for x in _from.transitions: if x._to == toName: existTransFromTo = x break if existTransFromTo: existTransFromTo.label = '(' + existTransFromTo.label + '+' + transitionName + ')' else: _from.addTransition(Transition(transitionName, fromName, toName))