Advertisement
Guest User

Untitled

a guest
May 5th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.56 KB | None | 0 0
  1. class State(object):
  2.     def __init__(self, state_type, next_state=None):
  3.         """If state_type='Start', then a next_state must be provided."""
  4.         if state_type not in ('Accept', 'Push', 'Pop', 'Read', 'Start'):
  5.             raise SystemExit(state_type + " is not a valid State type!")
  6.         if state_type == 'Start' and not next_state:
  7.             raise SystemExit("Start states must be provided a next_state!")
  8.         self.state_type = state_type
  9.         self.transitions = list()
  10.         self.next_state = next_state
  11.  
  12.     def add_transition(self, next_state, character=None):
  13.         """Adds a transition to the list of the state's acceptable
  14.        transitions.  A transition consists of at least a  next state, and a
  15.        character depending on the type of the state."""
  16.         transition = {'char': character, 'next': next_state}
  17.         self.transitions.append(transition)
  18.  
  19.     def get_next_state(self, character=None):
  20.         """Retrieves the next state that the PDA should go to, based on the
  21.        current state's type, and an input from either the tape or stack."""      
  22.         if self.state_type == "Start":
  23.             return self.next_state
  24.  
  25.         if self.state_type == "Read":
  26.             if character == "":
  27.                 return self.transitions[0]['next']
  28.  
  29.             for transition in self.transitions:
  30.                 if transition['char'] == character:
  31.                     return transition['next']
  32.  
  33.         if self.state_type == "Push":
  34.             return self.transitions[0]['next']
  35.  
  36.         if self.state_type == "Pop":
  37.             for transition in self.transitions:
  38.                 if transition['char'] == character:
  39.                     return transition['next']
  40.  
  41.     def action(self, tape, stack, current_state="q-0",rule_num=0, verbose=True):
  42.         """If state_type is Read, Push, or Pop, the respective action is taken
  43.        and the get_next_state() retrieves the next state in the PDA.  The
  44.        action() function of the next state is then called.
  45.        If state_type is Start, next state's action() function is called.
  46.        If state_type is Accept, the state simply prints a message."""
  47.         if verbose:
  48.             print (current_state,end="    ")
  49.             print (rule_num,end="    ")
  50.             if current_state == "q-2":
  51.                 if tape[0] == "b":
  52.                     rule_num=4
  53.                     current_state = "q-2"
  54.                 else:
  55.                     current_state = "q-3"
  56.                     rule_num=5
  57.             elif current_state == "q-1":
  58.                 if tape[0] == "a":
  59.                     rule_num=2
  60.                     current_state = "q-1"
  61.                 elif tape[0] == "b":
  62.                     rule_num=3
  63.                     current_state = "q-2"
  64.             elif current_state == "q-0":
  65.                 current_state = "q-1"
  66.                 rule_num=1
  67.             print("TAPE: ", tape)
  68.             if stack:
  69.                 print("STACK: ", stack[-1],end="    ")
  70.             else:
  71.                 print("STACK: ", stack, end="    ")
  72.         try:
  73.            
  74.             if self.state_type == "Start":
  75.                 current_state="q-0"
  76.                 rule_num=0
  77.                 #if verbose:
  78.                     #print("START -> next_state")
  79.                 self.get_next_state().action(tape, stack, current_state,rule_num, verbose)
  80.             elif self.state_type == "Read":
  81.                 #if verbose:
  82.                     #print("READ " + tape[0] + "")
  83.                 self.get_next_state(character=tape[0]).action(tape[1:], stack, current_state,rule_num, verbose)
  84.             elif self.state_type == "Push":
  85.                 char = self.transitions[0]['char']
  86.                 stack.append(char)
  87.                 #if verbose:
  88.                  #  print("PUSH " + char + "")
  89.                 self.get_next_state().action(tape, stack, current_state,rule_num, verbose)
  90.             elif self.state_type == "Pop":
  91.                 char = stack.pop()
  92.                 #if verbose:
  93.                  #   print("POP " + char + "")
  94.                 self.get_next_state(character=char).action(tape, stack, current_state,rule_num, verbose)
  95.             elif self.state_type == "Accept":
  96.                 print("Tape accepted!")
  97.                 if verbose:
  98.                     print("FINAL TAPE: ", tape)
  99.                     print("FINAL STACK: ", stack)
  100.             else:
  101.                 pass
  102.         except:
  103.             print("Tape NOT accepted!")
  104.             if verbose:
  105.                 print("FINAL TAPE: ", tape)
  106.                 print("FINAL STACK: ", stack)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement