fork download
  1. %{
  2. #include <stdio.h>
  3.  
  4. // State definitions
  5. #define S0 0 // Even a, Even b (start)
  6. #define S1 1 // Odd a, Even b (accepting)
  7. #define S2 2 // Even a, Odd b
  8. #define S3 3 // Odd a, Odd b
  9.  
  10. int state = S0;
  11. %}
  12.  
  13. %%
  14. a {
  15. if (state == S0) state = S1;
  16. else if (state == S1) state = S0;
  17. else if (state == S2) state = S3;
  18. else if (state == S3) state = S2;
  19. }
  20. b {
  21. if (state == S0) state = S2;
  22. else if (state == S1) state = S3;
  23. else if (state == S2) state = S0;
  24. else if (state == S3) state = S1;
  25. }
  26. \n {
  27. if (state == S1)
  28. printf("Accepted\n");
  29. else
  30. printf("Rejected\n");
  31.  
  32. state = S0; // Reset for next string
  33. }
  34. . ; // Ignore other characters
  35. %%
  36.  
  37. int main() {
  38. printf("Enter strings (press Ctrl+D to stop):\n");
  39. yylex();
  40. return 0;
  41. }
  42.  
Success #stdin #stdout #stderr 0.03s 6836KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/2ctRJV/prog:2:1: Syntax error: Operator expected
ERROR: /home/2ctRJV/prog:41:1: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit