fork download
  1. %{
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. int state = 0; // Initial state: (even, even)
  6. %}
  7.  
  8. %option noyywrap
  9.  
  10. %%
  11.  
  12. // Transition rules based on input characters
  13. a {
  14. if (state == 0) state = 1; // (even, even) -> (odd, even)
  15. else if (state == 1) state = 0; // (odd, even) -> (even, even)
  16. else if (state == 2) state = 3; // (even, odd) -> (odd, odd)
  17. else if (state == 3) state = 2; // (odd, odd) -> (even, odd)
  18. }
  19.  
  20. b {
  21. if (state == 0) state = 2; // (even, even) -> (even, odd)
  22. else if (state == 1) state = 3; // (odd, even) -> (odd, odd)
  23. else if (state == 2) state = 0; // (even, odd) -> (even, even)
  24. else if (state == 3) state = 1; // (odd, odd) -> (odd, even)
  25. }
  26.  
  27. [ \t\n]+ ; // Ignore whitespace
  28.  
  29. . { /* Ignore any other character */ }
  30.  
  31. %%
  32.  
  33. // Main function
  34. int main(int argc, char **argv) {
  35. yylex(); // Start the lexer
  36. if (state == 0) {
  37. printf("Accepted: The string has an even number of 'a's and 'b's.\n");
  38. } else {
  39. printf("Rejected: The string does not have an even number of 'a's and 'b's.\n");
  40. }
  41. return 0;
  42. }
Success #stdin #stdout #stderr 0.03s 6840KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/sM3rS2/prog:2:1: Syntax error: Operator expected
ERROR: /home/sM3rS2/prog:42:0: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit