Lex : Check Whether number is Even or Odd

%{
#include<stdio.h>
#include<stdlib.h>
int i;
%}
%%
[0-9]+ {i=atoi(yytext); if(i%2==0) printf("Even !"); else printf("Odd !");};
%%
int main()
{
yylex();
}
/*  To Compile :-
1) lex filename.l
2) gcc lex.yy.c -ll
3) ./a.out
Enter Input and press enter
*/

Output :-
145
Odd !
204
Even !

Leave a comment