fork download
  1. %{
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. const char *keywords[] = {"int", "float", "if", "else", "for", "while", "return", "void"};
  6. const int num_keywords = sizeof(keywords) / sizeof(keywords[0]);
  7.  
  8. void check_keyword(char *word) {
  9. for (int i = 0; i < num_keywords; i++) {
  10. if (strcmp(word, keywords[i]) == 0) {
  11. printf("Keyword: %s\n", word);
  12. return;
  13. }
  14. }
  15. printf("Identifier: %s\n", word);
  16. }
  17. %}
  18.  
  19. %%
  20. // Operators
  21. [+\-*/%=<>!] { printf("Operator: %s\n", yytext); }
  22. [ \t\n] { /* Ignore whitespace */ }
  23. [a-zA-Z_][a-zA-Z0-9_]* { check_keyword(yytext); }
  24. . { printf("Enter a valid input.\n"); }
  25.  
  26. %%
  27.  
  28. int main() {
  29. yylex(); // Start the scanning process
  30. return 0;
  31. }
  32.  
Success #stdin #stdout #stderr 0.03s 6836KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/8ktZO1/prog:2:1: Syntax error: Operator expected
ERROR: /home/8ktZO1/prog:31:1: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit