Advertisement
vinocastro

CS 150 / LAB 1 / Item 1

Oct 25th, 2021
822
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.93 KB | None | 0 0
  1. from enum import Enum
  2.  
  3. class NumberBase(Enum):
  4.     DECIMAL = 'DECIMAL'
  5.     HEX = 'HEX'
  6.     OCTAL = 'OCTAL'
  7.     BINARY = 'BINARY'
  8.  
  9. def parse_number_base(str):
  10.     conditions = [
  11.     {'curr_state': 'START', 'input': ['0'], 'next_state': 'ZERO', 'accept': 0},
  12.     {'curr_state': 'START', 'input': [chr(i) for i in range(49,56)],'next_state': 'DEC_OCT_HEX', 'accept': 0},
  13.     {'curr_state': 'START', 'input': ['8','9'], 'next_state': 'DEC_HEX', 'accept': 0},
  14.     {'curr_state': 'ZERO', 'input': ['b','B'], 'next_state': 'BIN_START', 'accept': 1},
  15.     {'curr_state': 'ZERO', 'input': [chr(i) for i in range(48,56)], 'next_state': 'OCT', 'accept': 1},
  16.     {'curr_state': 'ZERO', 'input': ['x','X'], 'next_state': 'HEX_START', 'accept': 1},
  17.     {'curr_state': 'BIN_START','input': ['0','1'], 'next_state': 'BIN', 'accept': 0},
  18.     {'curr_state': 'HEX_START', 'input': [chr(i) for i in range(48,58)] + [chr(j) for j in range(97,103)] + [chr(k) for k in range(65,71)], 'next_state': 'HEX', 'accept': 0},
  19.     {'curr_state': 'BIN', 'input': ['0','1'], 'next_state': 'BIN', 'accept': 1},
  20.     {'curr_state': 'OCT', 'input': [chr(i) for i in range(48,56)], 'next_state': 'OCT', 'accept': 1},  
  21.     {'curr_state': 'DEC_OCT_HEX', 'input': [chr(i) for i in range(48,56)], 'next_state': 'DEC_OCT_HEX', 'accept': 1},
  22.     {'curr_state': 'DEC_OCT_HEX', 'input': ['8','9'], 'next_state': 'DEC_HEX', 'accept': 1},
  23.     {'curr_state': 'DEC_OCT_HEX', 'input': [chr(j) for j in range(97,103)] + [chr(k) for k in range(65,71)], 'next_state': 'HEX', 'accept': 1},
  24.     {'curr_state': 'DEC_HEX', 'input': [chr(i) for i in range(48,58)], 'next_state': 'DEC_HEX', 'accept': 1},
  25.     {'curr_state': 'DEC_HEX', 'input': [chr(j) for j in range(97,103)] + [chr(k) for k in range(65,71)], 'next_state': 'HEX', 'accept': 1},
  26.     {'curr_state': 'HEX', 'input': [chr(i) for i in range(48,58)] + [chr(j) for j in range(97,103)] + [chr(k) for k in range(65,71)], 'next_state': 'HEX', 'accept': 1}]
  27.  
  28.     res = [
  29.     {'final_state': 'ZERO', 'result': NumberBase.DECIMAL},
  30.     {'final_state': 'DEC_OCT_HEX', 'result': NumberBase.DECIMAL},
  31.     {'final_state': 'DEC_HEX', 'result': NumberBase.DECIMAL},
  32.     {'final_state': 'BIN', 'result': NumberBase.BINARY},
  33.     {'final_state': 'OCT', 'result': NumberBase.OCTAL},
  34.     {'final_state': 'HEX', 'result': NumberBase.HEX}]
  35.  
  36.     current_state = 'START'
  37.     current_char = str[0]
  38.     while(1):
  39.         print("str:",str)
  40.         print("char:",current_char)
  41.         if len(str) == 0:
  42.             for condition in conditions:
  43.                 if condition.get('current_state') == current_state and condition.get('accept') == 1:
  44.                     for item in res:
  45.                         if item.get('final_state') == current_state:
  46.                             return item.get('result')
  47.                 else:
  48.                     return None
  49.         else:
  50.             current_char = str[0]
  51.             print("current_char",current_char)
  52.             for condition in conditions:
  53.                 if (condition.get('curr_state') == current_state) and (current_char in condition.get('input')):
  54.                     current_state = condition.get('next_state')
  55.                 str = str[1:]
  56.  
  57.  
  58.  
  59. print(parse_number_base("12345"))
  60.  
  61.  
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement