fork download
  1. %{
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. // To store keywords
  6. char *keywords[] = {"int", "float", "if", "else", "while", "return", "for", "char"};
  7. int keyword_count = 8;
  8.  
  9. int isKeyword(char *word) {
  10. for(int i = 0; i < keyword_count; i++) {
  11. if(strcmp(keywords[i], word) == 0)
  12. return 1;
  13. }
  14. return 0;
  15. }
  16. %}
  17.  
  18. %%
  19.  
  20. "int"|"float"|"if"|"else"|"while"|"return"|"for"|"char" { printf("Keyword: %s\n", yytext); }
  21. [a-zA-Z_][a-zA-Z0-9_]* {
  22. if(isKeyword(yytext))
  23. printf("Keyword: %s\n", yytext);
  24. else
  25. printf("Identifier: %s\n", yytext);
  26. }
  27. [0-9]+ { printf("Number: %s\n", yytext); }
  28. "=="|"="|"+"|"-"|"*"|"/" { printf("Operator: %s\n", yytext); }
  29. "(" { printf("Left Parenthesis: %s\n", yytext); }
  30. ")" { printf("Right Parenthesis: %s\n", yytext); }
  31. "{" { printf("Left Brace: %s\n", yytext); }
  32. "}" { printf("Right Brace: %s\n", yytext); }
  33. ";" { printf("Semicolon: %s\n", yytext); }
  34. [ \t\n]+ { /* Ignore whitespace */ }
  35. . { printf("Unknown token: %s\n", yytext); }
  36.  
  37. %%
  38.  
  39. int main(int argc, char **argv) {
  40. yylex();
  41. return 0;
  42. }
  43.  
  44. int yywrap() {
  45. return 1;
  46. }
  47.  
Success #stdin #stdout #stderr 0.03s 6864KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/2614FG/prog:2:1: Syntax error: Operator expected
ERROR: /home/2614FG/prog:46:1: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit