fork download
  1. import re
  2.  
  3. def classify_token(token):
  4. if re.match(r'^[a-zA-Z_]\w*$', token): return "Identifier"
  5. if token in {"int", "float", "char"}: return "Keyword"
  6. if re.match(r'^\d+$', token): return "Integer"
  7. if re.match(r'^\d+\.\d+$', token): return "Float"
  8. if token in {'+', '-', '*', '/', '=', '<', '>'}: return "Operator"
  9. return "Unknown"
  10.  
  11. statement = input("Enter a C statement: ")
  12. tokens = re.findall(r'\w+|[^\w\s]', statement)
  13.  
  14. for token in tokens:
  15. print(f"Token: {token}, Type: {classify_token(token)}")
  16.  
Success #stdin #stdout #stderr 0.03s 6972KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/gBNKcy/prog:15:59: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit