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. # Input C code as a string
  12. c_code = '''
  13. #include<stdio.h>
  14. int main() {
  15. printf("Welcome");
  16. return 0;
  17. }
  18. '''
  19.  
  20. # Tokenize the input string
  21. tokens = re.findall(r'\w+|[^\w\s]', c_code)
  22.  
  23. # Classify and print each token
  24. for token in tokens:
  25. print(f"Token: {classify_token(token)}")
  26.  
Success #stdin #stdout #stderr 0.02s 6856KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/Uv7FZr/prog:25:44: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit