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. with open("input.c", "r") as file:
  12. content = file.read()
  13. tokens = re.findall(r'\w+|[^\w\s]', content)
  14. for token in tokens:
  15. print(f"Token: {classify_token(token)}")
  16.  
Success #stdin #stdout #stderr 0.03s 6964KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/8Gn2YI/prog:15:48: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit