fork download
  1. %{
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. // List of C language keywords
  6. char *keywords[] = {
  7. "auto", "double", "int", "struct", "break", "else", "long", "switch",
  8. "case", "enum", "register", "typedef", "char", "extern", "return", "union",
  9. "continue", "for", "signed", "void", "do", "if", "static", "while",
  10. "default", "goto", "sizeof", "volatile", "const", "float", "short", "unsigned",
  11. NULL
  12. };
  13.  
  14. // Function to check if a token is a keyword
  15. int is_keyword(char *s) {
  16. for (int i = 0; keywords[i]; i++) {
  17. if (strcmp(s, keywords[i]) == 0)
  18. return 1;
  19. }
  20. return 0;
  21. }
  22. %}
  23.  
  24. %%
  25.  
  26. // Match valid identifiers (starting with letter or underscore)
  27. [a-zA-Z_][a-zA-Z0-9_]* {
  28. if (is_keyword(yytext))
  29. printf("Keyword: %s\n", yytext);
  30. else
  31. printf("Identifier: %s\n", yytext);
  32. }
  33.  
  34. // Match integers (not valid identifiers)
  35. [0-9]+ {
  36. printf("Not an Identifier: %s\n", yytext);
  37. }
  38.  
  39. // Ignore any other characters (punctuation, whitespace, etc.)
  40. .|\n {
  41. // do nothing (skip)
  42. }
  43.  
  44. %%
  45.  
  46. // Required function
  47. int yywrap() {
  48. return 1;
  49. }
  50.  
  51. // Main driver function
  52. int main(void) {
  53. yylex();
  54. return 0;
  55. }
  56.  
Success #stdin #stdout #stderr 0.03s 6784KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/ls29Ai/prog:55:1: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit