%{
#include <stdio.h>
#include <stdlib.h>
int state = 0; // Initial state: (even, even)
%}
%option noyywrap
%%
// Transition rules based on input characters
a {
if (state == 0) state = 1; // (even, even) -> (odd, even)
else if (state == 1) state = 0; // (odd, even) -> (even, even)
else if (state == 2) state = 3; // (even, odd) -> (odd, odd)
else if (state == 3) state = 2; // (odd, odd) -> (even, odd)
}
b {
if (state == 0) state = 2; // (even, even) -> (even, odd)
else if (state == 1) state = 3; // (odd, even) -> (odd, odd)
else if (state == 2) state = 0; // (even, odd) -> (even, even)
else if (state == 3) state = 1; // (odd, odd) -> (odd, even)
}
[ \t\n]+ ; // Ignore whitespace
. { /* Ignore any other character */ }
%%
// Main function
int main(int argc, char **argv) {
yylex(); // Start the lexer
if (state == 0) {
printf
("Accepted
: The string has an even
number of
'a's and
'b's
.\n"
); } else {
printf
("Rejected
: The string does not have an even
number of
'a's and
'b's
.\n"
); }
return 0;
}
JXsKI2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCmludCBzdGF0ZSA9IDA7IC8vIEluaXRpYWwgc3RhdGU6IChldmVuLCBldmVuKQolfQoKJW9wdGlvbiBub3l5d3JhcAoKJSUKCi8vIFRyYW5zaXRpb24gcnVsZXMgYmFzZWQgb24gaW5wdXQgY2hhcmFjdGVycwphICAgeyAKICAgICAgICBpZiAoc3RhdGUgPT0gMCkgc3RhdGUgPSAxOyAvLyAoZXZlbiwgZXZlbikgLT4gKG9kZCwgZXZlbikKICAgICAgICBlbHNlIGlmIChzdGF0ZSA9PSAxKSBzdGF0ZSA9IDA7IC8vIChvZGQsIGV2ZW4pIC0+IChldmVuLCBldmVuKQogICAgICAgIGVsc2UgaWYgKHN0YXRlID09IDIpIHN0YXRlID0gMzsgLy8gKGV2ZW4sIG9kZCkgLT4gKG9kZCwgb2RkKQogICAgICAgIGVsc2UgaWYgKHN0YXRlID09IDMpIHN0YXRlID0gMjsgLy8gKG9kZCwgb2RkKSAtPiAoZXZlbiwgb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