lex 1
%{
#include <stdio.h>
#include <stdlib.h>
// Declare the state variable globally
int state = 0; // Start state q0
%}
%%
0 {
if (state == 0) state = 1; // From q0 to q1 on '0'
else if (state == 1) state = 0; // From q1 to q0 on '0'
else if (state == 2) state = 3; // From q2 to q3 on '0'
else if (state == 3) state = 2; // From q3 to q2 on '0'
}
1 {
if (state == 0) state = 3; // From q0 to q3 on '1'
else if (state == 1) state = 2; // From q1 to q2 on '1'
else if (state == 2) state = 1; // From q2 to q1 on '1'
else if (state == 3) state = 0; // From q3 to q0 on '1'
}
\n { /* Ignore newline characters */
return 0;
}
. { /* Ignore any other character */
return 0;
}
%%
int main(void) {
// Start the lexer
yylex();
// Check final state for acceptance
if (state == 3) {
printf
("Accepted
: Odd
number of
1’s and even
number of
0’s\n"
); } else {
printf("Rejected\n");
}
return 0;
}
bGV4IDEKJXsKI2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCi8vIERlY2xhcmUgdGhlIHN0YXRlIHZhcmlhYmxlIGdsb2JhbGx5CmludCBzdGF0ZSA9IDA7ICAvLyBTdGFydCBzdGF0ZSBxMAolfQoKJSUKCjAgICAgIHsgCiAgICAgICAgaWYgKHN0YXRlID09IDApIHN0YXRlID0gMTsgIC8vIEZyb20gcTAgdG8gcTEgb24gJzAnCiAgICAgICAgZWxzZSBpZiAoc3RhdGUgPT0gMSkgc3RhdGUgPSAwOyAgLy8gRnJvbSBxMSB0byBxMCBvbiAnMCcKICAgICAgICBlbHNlIGlmIChzdGF0ZSA9PSAyKSBzdGF0ZSA9IDM7ICAvLyBGcm9tIHEyIHRvIHEzIG9uICcwJwogICAgICAgIGVsc2UgaWYgKHN0YXRlID09IDMpIHN0YXRlID0gMjsgIC8vIEZyb20gcTMgdG8gcTIgb24gJzAnCiAgICAgIH0KCjEgICAgIHsgCiAgICAgICAgaWYgKHN0YXRlID09IDApIHN0YXRlID0gMzsgIC8vIEZyb20gcTAgdG8gcTMgb24gJzEnCiA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