fork download
  1. %{
  2. #include <stdio.h>
  3. #include <string.h>
  4. // Define an array of C reserved words
  5. char *keywords[] = {
  6. "auto", "break", "case", "char", "const", "continue", "default", "do", "double", "else", "enum",
  7. "extern", "float", "for", "goto", "if", "inline", "int", "long", "register", "restrict", "return",
  8. "short", "signed", "sizeof", "static", "struct", "switch", "typedef", "union", "unsigned", "void",
  9. "volatile", "while"
  10. };
  11. #define NUM_KEYWORDS 32
  12. // Function to check if a word is a keyword
  13. int is_keyword(char *word) {
  14. for (int i = 0; i < NUM_KEYWORDS; i++) {
  15. if (strcmp(word, keywords[i]) == 0) {
  16. return 1; // It's a keyword
  17. }
  18. }
  19. return 0; // It's not a keyword
  20. }
  21. %}
  22. %%
  23. [ \t\n]+ ; // Ignore spaces, tabs, and newlines
  24. "int"|"float"|"char"|"void"|"long"|"short"|"double"|"return"|"if"|"else"|"for"|"while"|"do"|"break"|"continue" {
  25. printf("Keyword: %s\n", yytext); // Print matched keywords
  26. }
  27. [A-Za-z_][A-Za-z0-9_]* {
  28. if (is_keyword(yytext)) {
  29. printf("Keyword: %s\n", yytext); // If it's a reserved word
  30. } else {
  31. printf("Identifier: %s\n", yytext); // If it's an identifier
  32. }
  33. }
  34. . { ECHO; } // Print unmatched characters
  35. %%
  36. int main() {
  37. printf("Enter C code:\n");
  38. yylex(); // Start the lexical analysis
  39. return 0;
  40. }
  41.  
Success #stdin #stdout #stderr 0.02s 6980KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/seiqBx/prog:2:1: Syntax error: Operator expected
ERROR: /home/seiqBx/prog:40:1: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit