lex program to count alphanumeric words , alphabets, numbers and lines

 

 

%{
#include<stdio.h>
#include<string.h>
int al=0,aln=0,n=0,ln=0;
%}
%%
([a-z]|[A-Z])*    {al++;}
(” “)*.(\n)    {}
[0-9]*    {n++;}
(“\n”)*(” “)*(“\n”)     {ln++;}
([a-z]|[A-Z]|[0-9])*    {aln++;}
%%
main()
{
yyin=fopen(“inp.txt”,”r”);
yylex();
printf(“Alphanumeric:%d\n alphabets:%d\n numbers:%d\n line:%d\n”,aln,al,n,ln);
return 0;
}
int yywrap()
{
}

Sample output

inp.txt

abc123
abcd
efgh567 dfdfdgdf
1234

Alphanumeric:2
alphabets:2
numbers:1
line:4

Leave a Reply

Your email address will not be published. Required fields are marked *