fork download
  1. import ply.lex as lex
  2.  
  3. # List of token names
  4. tokens = (
  5. 'NUMBER',
  6. 'PLUS',
  7. 'MINUS',
  8. 'TIMES',
  9. 'DIVIDE',
  10. 'IDENTIFIER',
  11. 'KEYWORD',
  12. )
  13.  
  14. # Regular expression rules for simple tokens
  15. t_PLUS = r'\+'
  16. t_MINUS = r'-'
  17. t_TIMES = r'\*'
  18. t_DIVIDE = r'/'
  19.  
  20. # Define a regular expression for identifying numbers
  21. t_NUMBER = r'\d+'
  22.  
  23. # Define a regular expression for identifiers (names starting with a letter, followed by letters or digits)
  24. t_IDENTIFIER = r'[a-zA-Z_][a-zA-Z0-9_]*'
  25.  
  26. # Define a rule for keywords
  27. def t_KEYWORD(t):
  28. r'\b(if|else|while|for)\b'
  29. return t
  30.  
  31. # Define a rule for ignoring spaces and newlines
  32. t_ignore = ' \t\n'
  33.  
  34. # Define a rule for handling errors
  35. def t_error(t):
  36. print(f"Illegal character '{t.value[0]}'")
  37. t.lexer.skip(1)
  38.  
  39. # Build the lexer
  40. lexer = lex.lex()
  41.  
  42. # Sample input string
  43. data = '''
  44. if x + 10 > 20:
  45. y = x * 2
  46. '''
  47.  
  48. # Input the string to the lexer
  49. lexer.input(data)
  50.  
  51. # Tokenize the input
  52. while True:
  53. tok = lexer.token()
  54. if not tok:
  55. break
  56. print(f'Token: {tok.type}, Value: {tok.value}')
Success #stdin #stdout #stderr 0.02s 6960KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/kh8VpJ/prog:56:50: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit