Create a Lexer to Take Tnput From Text File And Count no of Characters, no. of Lines & no. of Words.


Aim :- Create a Lexer to take input from text file and count no of characters, no. of lines & no. of words.


C Program Code :


%{
      int w=0,cc=0,n=1;
%}
%%
      [a-zA-Z]+ {w++;cc=cc+yyleng;}
      [\n]+ {n++;cc++;}
      [ ]+ {cc++;}
      [.]+ {cc++;}
%%
int main()
{
      FILE *fp;
      char file[20];
      printf("Enter File Name : ");
      scanf("%s",file);
      fp=fopen(file,"r");
      yyin=fp;
      yylex();
      printf("\nTotal Words : %d",w);
      printf("\nTotal Characters : %d",cc);
      printf("\nTotal Lines : %d",n);
}
int yywrap()
{
      return 1;
}


Input :









Output :


Post a Comment

0 Comments