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