fork download
  1. %{
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. // List of keywords
  6. char *keywords[] = {
  7. "int", "float", "char", "double", "if", "else",
  8. "while", "for", "return", "void", "break", "continue"
  9. };
  10.  
  11. int isKeyword(char *word) {
  12. int i;
  13. for(i = 0; i < 12; i++) {
  14. if(strcmp(word, keywords[i]) == 0)
  15. return 1;
  16. }
  17. return 0;
  18. }
  19. %}
  20.  
  21. %%
  22. [a-zA-Z_][a-zA-Z0-9_]* {
  23. if(isKeyword(yytext))
  24. printf("%s is a KEYWORD\n", yytext);
  25. else
  26. printf("%s is an IDENTIFIER\n", yytext);
  27. }
  28.  
  29. \n { return 0; }
  30. . ;
  31. %%
  32.  
  33. int main() {
  34. printf("Enter a word: ");
  35. yylex();
  36. return 0;
  37. }
  38.  
  39. int yywrap() {
  40. return 1;
  41. }
Success #stdin #stdout #stderr 0.02s 6960KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/4bhIXl/prog:2:1: Syntax error: Operator expected
ERROR: /home/4bhIXl/prog:41:0: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit