class TuringMachine: '''Class to represent a Turing machine''' def __init__(self, alphabet, initialstate, initialtape, finalstates, blank): '''Construct a Turing machine Args: alphabet (str): The alphabet initialstate (str): The name of the initial state initialtape (str): The initial contents of the state finalstates (List): A list of the names of the final states blank (str): The blank symbol for this TM ''' self.alphabet = alphabet self.initialstate = initialstate self.states = {} self.currentstate = self.initialstate self.tape = Tape(initialtape, blank) self.finalstates = finalstates self.halted = '' self.finaltape = '' def addstate(self, statename, state): '''Add a state to the TM Args: statename(str) : name of the state state(State) : the state ''' self.states[statename] = state def set_alphabet_in_TM(self,alphabet): self.alphabet = alphabet def set_tape_in_TM(self, tape, blank): self.tape = Tape(tape, blank) def set_initialstate(self, _state): self.initialstate = _state self.currentstate = str(self.initialstate) def set_finalstates(self, _final): self.finalstates.add(_final) # def set_alphabet(self, alphabet): # self.alphabet = alphabet # # def set_initialstate(self, initialstate): # self.initialstate = initialstate # # def add_finalstate(self, statename): # self.finalstates.append() def removestate(self, statename): '''Removes a state from the TM Args: statename(str) : name of the state ''' del self.states[statename] def getstate(self): '''Get the current state Returns: str: the name of the current state ''' return self.currentstate def gettape(self): '''Get the current tape as a string Returns: str: the current tape ''' return self.tape.gettape() def step(self): '''Executes one execution step on the TM Returns: bool: False if there was no transition defined, True otherwise ''' cursym = self.tape.getsym() state= self.states[self.currentstate] transition = state.get_transition(cursym) # print transition if transition is None: return False self.currentstate = transition.get_next_state() if (transition.get_next_direction() == "R"): self.tape.writeright(transition.get_write_sym()) else: self.tape.writeleft(transition.get_write_sym()) return True
class TuringMachine: '''Class to represent a Turing machine''' def __init__(self, alphabet, initialstate, initialtape, finalstates, blank): '''Construct a Turing machine Args: alphabet (str): The alphabet initialstate (str): The name of the initial state initialtape (str): The initial contents of the state finalstates (List): A list of the names of the final states blank (str): The blank symbol for this TM ''' self.alphabet = alphabet self.initialstate = initialstate self.states = {} self.currentstate = self.initialstate self.tape = Tape(initialtape, blank) self.finalstates = finalstates def addstate(self,statename, state): '''Add a state to the TM Args: statename(str) : name of the state state(State) : the state ''' self.states[statename] = state def getstate(self): '''Get the current state Returns: str: the name of the current state ''' return self.currentstate def gettape(self): '''Get the current tape as a string Returns: str: the current tape ''' return self.tape.gettape() def step(self): '''Executes one execution step on the TM Returns: bool: False if there was no transition defined, True otherwise ''' cursym = self.tape.getsym() state= self.states[self.currentstate] transition = state.get_transition(cursym) # print transition if transition is None: return False self.currentstate = transition.get_next_state() if (transition.get_next_direction() == "R"): self.tape.writeright(transition.get_write_sym()) else: self.tape.writeleft(transition.get_write_sym()) return True def runtohalt(self): '''Run the machine to completion. Prints an execution trace Returns: nothing ''' print "initial state=", self.currentstate print "initial tape=", self.gettape() print " " steps = 0 while self.step(): steps += 1 print "steps = ", steps print "state = ", self.currentstate print "tape = ", self.gettape() print " " if self.currentstate in self.finalstates: print "halted with answer yes" else: print "halted with answer no"