fork download
  1. lex 1
  2. %{
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. // Declare the state variable globally
  7. int state = 0; // Start state q0
  8. %}
  9.  
  10. %%
  11.  
  12. 0 {
  13. if (state == 0) state = 1; // From q0 to q1 on '0'
  14. else if (state == 1) state = 0; // From q1 to q0 on '0'
  15. else if (state == 2) state = 3; // From q2 to q3 on '0'
  16. else if (state == 3) state = 2; // From q3 to q2 on '0'
  17. }
  18.  
  19. 1 {
  20. if (state == 0) state = 3; // From q0 to q3 on '1'
  21. else if (state == 1) state = 2; // From q1 to q2 on '1'
  22. else if (state == 2) state = 1; // From q2 to q1 on '1'
  23. else if (state == 3) state = 0; // From q3 to q0 on '1'
  24. }
  25.  
  26. \n { /* Ignore newline characters */
  27. return 0;
  28. }
  29.  
  30. . { /* Ignore any other character */
  31. return 0;
  32. }
  33.  
  34. %%
  35.  
  36. int main(void) {
  37. // Start the lexer
  38. yylex();
  39.  
  40. // Check final state for acceptance
  41. if (state == 3) {
  42. printf("Accepted: Odd number of 1’s and even number of 0’s\n");
  43. } else {
  44. printf("Rejected\n");
  45. }
  46.  
  47. return 0;
  48. }
  49.  
  50.  
Success #stdin #stdout #stderr 0.02s 6964KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/7uMhXf/prog:1:4: Syntax error: Operator expected
ERROR: /home/7uMhXf/prog:49:0: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit