Sunday, 16 November 2014

LEX PROGRAM to count the no of characters, words, and lines in a given input file



Program to count the no of characters, words, and lines in a given input file.
%{
#include<stdio.h>
int c=0,w=0,l=0,b=0;
%}
%%
[a-zA-Z0-9]+ {w++;c+=yyleng;}
[\n] {l++;}
[ ] {b++;}
%%
main(int argc,char*argv[ ])
{
yyin=fopen(argv[1],"r");
yylex();
printf("words %d\n char %d\n spaces %d\n lines %d\n",w,c,b,l);
}

No comments:

Post a Comment

Total Pageviews