Advertisement
Guest User

python is

a guest
Feb 27th, 2020
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. def get_char():
  2. global character_class
  3. global next_char
  4. global fp
  5.  
  6. next_char = fp.read(1)
  7. if next_char.isalpha():
  8. character_class = LETTER
  9. elif next_char.isdigit():
  10. character_class = DIGIT
  11. else:
  12. character_class = UNKNOWN
  13.  
  14.  
  15. def get_non_blank():
  16. while next_char.isspace():
  17. get_char()
  18.  
  19. # Constants
  20. # Character Classes
  21. LETTER = 0
  22. DIGIT = 0
  23. UNKNOWN = 99
  24.  
  25. # Token Codes
  26. INT_LIT = 10
  27. IDENT = 11
  28. ASSIGN_OP = 20
  29. ADD_OP = 21
  30. SUB_OP = 22
  31. MULT_OP = 23
  32. DIV_OP = 24
  33. LEFT_PAREN = 25
  34. RIGHT_PAREN = 26
  35.  
  36. # Initialize Global Variables
  37. character_class = -1
  38. lexeme = -1
  39. next_char = -1
  40. lex_length = -1
  41. token = -1
  42. next_token = -1
  43.  
  44. # Get the name of the input file from the user.
  45. file_name = input("Input file name: ")
  46.  
  47. # Open the file and read the first char.
  48. # NOTE: error handling needs to be added later.
  49. try:
  50. with open(file_name, "r") as fp:
  51. get_char()
  52. while next_char:
  53. print(next_char)
  54. get_non_blank()
  55. except OSError:
  56. print("Unable to open file")
  57. quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement