fork download
  1. %{
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. int isKeyword(char *str) {
  6. return strcmp(str, "if") == 0 ||
  7. strcmp(str, "else") == 0 ||
  8. strcmp(str, "while") == 0 ||
  9. strcmp(str, "int") == 0 ||
  10. strcmp(str, "float") == 0;
  11. }
  12. %}
  13.  
  14. %%
  15. "if" { printf("Keyword: %s\n", yytext); }
  16. "else" { printf("Keyword: %s\n", yytext); }
  17. "while" { printf("Keyword: %s\n", yytext); }
  18. "int" { printf("Keyword: %s\n", yytext); }
  19. "float" { printf("Keyword: %s\n", yytext); }
  20.  
  21. [ \t\n]+ ; // Ignore whitespace
  22.  
  23. "(" { printf("Symbol: %s\n", yytext); }
  24. ")" { printf("Symbol: %s\n", yytext); }
  25. "{" { printf("Symbol: %s\n", yytext); }
  26. "}" { printf("Symbol: %s\n", yytext); }
  27. ";" { printf("Delimiter: %s\n", yytext); }
  28.  
  29. ">="|"<="|"=="|"!="|">"|"<" { printf("Operator: %s\n", yytext); }
  30. "=" { printf("Assignment Operator: %s\n", yytext); }
  31. "*" { printf("Arithmetic Operator: %s\n", yytext); }
  32. "/" { printf("Arithmetic Operator: %s\n", yytext); }
  33.  
  34. [0-9]+ { printf("Number: %s\n", yytext); }
  35.  
  36. [a-zA-Z_][a-zA-Z0-9_]* {
  37. if (isKeyword(yytext))
  38. printf("Keyword: %s\n", yytext);
  39. else
  40. printf("Identifier: %s\n", yytext);
  41. }
  42.  
  43. . { printf("Unknown: %s\n", yytext); }
  44. %%
  45.  
  46. int main() {
  47. yylex();
  48. return 0;
  49. }
  50.  
Success #stdin #stdout #stderr 0.03s 6944KB
stdin
if(x>0){y=x*2;}else{y=x/2;}
stdout
Standard output is empty
stderr
ERROR: /home/3pSS85/prog:49:1: Syntax error: end_of_file_in_quasi_quotation
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? ERROR: Can't ignore goal at this port
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit