fork download
  1. %{
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. %}
  5.  
  6. %token NUM
  7.  
  8. %%
  9.  
  10. [0-9]+ { yylval = atoi(yytext); return NUM; }
  11.  
  12. "+" { return '+'; }
  13. "-" { return '-'; }
  14. "*" { return '*'; }
  15. "/" { return '/'; }
  16. "\n" { return 0; } // end of expression
  17.  
  18. [ \t] { /* Ignore whitespace */ }
  19.  
  20. %%
  21.  
  22. int main(void) {
  23. int token;
  24. while ((token = yylex()) != 0) {
  25. switch (token) {
  26. case NUM:
  27. printf("Number: %d\n", yylval);
  28. break;
  29. case '+':
  30. printf("Addition\n");
  31. break;
  32. case '-':
  33. printf("Subtraction\n");
  34. break;
  35. case '*':
  36. printf("Multiplication\n");
  37. break;
  38. case '/':
  39. printf("Division\n");
  40. break;
  41. default:
  42. printf("Unknown token\n");
  43. break;
  44. }
  45. }
  46. return 0;
  47. }
  48.  
Success #stdin #stdout #stderr 0.03s 7020KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/nD94Bu/prog:47:1: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit