fork download
  1. %{
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. int is_keyword(char *str); // Function to check keywords
  6. %}
  7.  
  8. %%
  9.  
  10. [a-zA-Z_][a-zA-Z0-9_]* {
  11. if (is_keyword(yytext))
  12. printf("\"%s\" is a keyword, not a valid identifier.\n", yytext);
  13. else
  14. printf("\"%s\" is a valid identifier.\n", yytext);
  15. }
  16.  
  17. [^a-zA-Z0-9_] {
  18. printf("\"%s\" is not a valid identifier.\n", yytext);
  19. }
  20.  
  21. %%
  22.  
  23. int is_keyword(char *str) {
  24. // List of some common C keywords
  25. char *keywords[] = {"int", "return", "void", "if", "else", "while", "for", "char", "float", "double", "switch", "case", "break", "continue", "struct", "typedef", "static", "const", "goto", NULL};
  26.  
  27. for (int i = 0; keywords[i] != NULL; i++) {
  28. if (strcmp(str, keywords[i]) == 0)
  29. return 1;
  30. }
  31. return 0;
  32. }
  33.  
  34. int main() {
  35. printf("Enter an identifier: ");
  36. yylex();
  37. return 0;
  38. }
  39.  
Success #stdin #stdout #stderr 0.03s 6924KB
stdin
hello123
stdout
Standard output is empty
stderr
ERROR: /home/IclAZ3/prog:38:1: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit