Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.41 KB | None | 0 0
  1. #!/usr/bin/python -tt
  2.  
  3. import sys
  4.  
  5. class Turing:
  6. state = '1' # The current state of the machine
  7. cp = 0 # current position of the head
  8. transistion_count = 0 # number of times the head executed a command
  9. running = False # flag to stop the machine
  10.  
  11. def __init__(self):
  12. self.running = True
  13.  
  14. def IsRunning(self):
  15. return self.running
  16.  
  17. def QuitWithError( self, error, tape, rule ):
  18. print error
  19. print "Rule :", rule
  20. print "State :", self.state
  21. print "Head position :", self.cp
  22. print "Transistion Count :", self.transistion_count
  23. print "Tape :", tape
  24.  
  25. sys.exit(0)
  26.  
  27. def GetTransistionCount(self):
  28. return self.transistion_count
  29.  
  30. def Step (self, tape, transistion_table ):
  31. state = self.state
  32. cp = self.cp
  33.  
  34. # get the character under the the current position on the tape
  35. head = tape[cp]
  36.  
  37. # the '#' character is a signal to stop the machine
  38. if state == "#":
  39. self.running = False
  40.  
  41. # loop through the program table, and determine what action needs to be done
  42. for t in transistion_table:
  43. rules = t.split()
  44. if rules[0] == state:
  45. if rules[1] == head:
  46. self.state = rules[2]
  47.  
  48. self.transistion_count += 1
  49.  
  50. # this simple replaces a character in the tape string with what ever is under
  51. # rules[3] - yes its ugly, but im ashamed to admit I don't know how else to do it
  52. tmp = list(tape)
  53. tmp[cp] = rules[3]
  54. tape = ''.join(tmp)
  55.  
  56. # determine which direction to move the head
  57. if rules[4] == "R": self.cp+=1
  58. if rules[4] == "L": self.cp-=1
  59.  
  60.  
  61. # simple error checking that stop the program if the current position is out
  62. # of bounds of the string
  63. if self.cp == -1:
  64. self.QuitWithError( 'Head moved out of bounds of tape < Start', tape, t )
  65.  
  66. if self.cp == len(tape):
  67. self.QuitWithError( 'Head moved out of bounds of tape > End', tape, t )
  68.  
  69.  
  70. return tape
  71.  
  72. #self.QuitWithError( 'Can not find a valid rule' , tape, transistion_table ) # This is on the wrong line... TODO FIXME
  73. return tape
  74.  
  75. def MakeTable(self):
  76. # program syntax ( "State Character NewState NewCharacter Movement")
  77. # Program commands are seperated by a blank whitespace character to make a 5 tuple command string
  78.  
  79. # In english, the turing Step code performs this:
  80.  
  81. # If current state is 'State' and character under head is 'Character' then
  82. # change current state to 'NewState' and print 'NewCharacter' under the head,
  83. # then move the head position across by 'Movement'
  84. table = []
  85. table.append ("1 _ 2 : R")
  86. table.append ("2 A 3 _ R")
  87. table.append ("2 B 4 _ R")
  88. table.append ("2 _ 7 _ L")
  89. table.append ("3 A 3 A R")
  90. table.append ("3 B 3 B R")
  91. table.append ("3 _ 5 _ L")
  92. table.append ("4 A 4 A R")
  93. table.append ("4 B 4 B R")
  94. table.append ("4 _ 6 _ L")
  95. table.append("5 A 11 _ L")
  96. table.append("5 B 12 _ L")
  97. table.append("5 _ 7 _ L")
  98. table.append("6 A 12 _ L")
  99. table.append("6 B 11 _ L")
  100. table.append("6 _ 7 _ L")
  101. table.append("7 _ 7 _ L")
  102. table.append("7 : 8 _ R")
  103. table.append("8 _ 9 Y R")
  104. table.append("9 _ 10 E R")
  105. table.append("10 _ # S R")
  106. table.append("11 A 11 A L")
  107. table.append("11 B 11 B L")
  108. table.append("11 _ 2 _ R")
  109. table.append("12 A 12 _ L")
  110. table.append("12 B 12 _ L")
  111. table.append("12 _ 12 _ L")
  112. table.append("12 : 13 _ R")
  113. table.append("13 _ 14 N R")
  114. table.append("14 _ # O R")
  115. return table
  116.  
  117. def main():
  118.  
  119. turing = Turing()
  120.  
  121. # the tape is simple a string, using the underscore character to represent a blank space
  122. tape = '_ABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBAABBBA__'
  123. # load the program
  124. table = turing.MakeTable()
  125.  
  126. print "Running Program..."
  127. while turing.IsRunning():
  128. tape = turing.Step( tape, table )
  129. #print tape # warning this is really slow
  130.  
  131. print tape
  132. print "Transistion Count :", turing.GetTransistionCount()
  133.  
  134.  
  135.  
  136. if __name__ == "__main__":
  137. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement