fork download
  1. %{
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. #define MAX_KEYWORDS 10
  6.  
  7. // Array of keywords
  8. const char *keywords[] = {
  9. "if", "else", "while", "for", "return",
  10. "int", "float", "char", "void", "break"
  11. };
  12.  
  13. // Function to check if the string is a keyword
  14. int is_keyword(const char *str) {
  15. for (int i = 0; i < MAX_KEYWORDS; i++) {
  16. if (strcmp(str, keywords[i]) == 0) {
  17. return 1; // It's a keyword
  18. }
  19. }
  20. return 0; // It's not a keyword
  21. }
  22. %}
  23.  
  24. %%
  25.  
  26. // Match keywords or identifiers (words starting with a letter or underscore)
  27. [a-zA-Z_][a-zA-Z0-9_]* {
  28. if (is_keyword(yytext)) {
  29. printf("'%s' is a keyword.\n", yytext);
  30. } else {
  31. printf("'%s' is not a keyword.\n", yytext);
  32. }
  33. }
  34.  
  35. // Match anything that is not a word
  36. [ \t\n]+ { /* Ignore whitespace */ }
  37.  
  38. . { /* Ignore any other characters */ }
  39.  
  40. %%
  41.  
  42. int main() {
  43. // Read input string from the user
  44. printf("Enter a string: ");
  45. yylex(); // Run Lex to start processing input
  46. return 0;
  47. }
  48.  
Success #stdin #stdout #stderr 0.03s 6940KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/22zqF1/prog:2:1: Syntax error: Operator expected
ERROR: /home/22zqF1/prog:47:1: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit