Skip to main content

lex program for count character ,the word, space and new line in the given text file



/* deceleration section in this sections we will declared the different  value and include the header file which we are using in this program to run this program */

%{
#include<stdio.h>
int nc_count=0;      // for count the character
int nw_count=0;     // for count the word
int ns_count=0;      // for count the space
int nl_count=0;      // count the line
%}

/* this sections is known as defined section in which we defined the rule and regulation of regular expression which will going to accept or not */

%%
[ ]  {ns_count++;nw_count++;}
[\t]  {ns_count++;nw_count++;}
[\n]  {nl_count++;nw_count++;}
[a-zA-Z] {nc_count++;}

%%

/* main section for calling function */

int main()
{
FILE *fp;
char file[10];
printf("enter the filename\n");
scanf("%s",file);
fp=fopen(file,"r");
yyin=fp;
yylex();
printf("character=%d\nword=%d\nSpace=%d\nLine=%d\n",nc_count,nw_count-1,ns_count,nl_count-1);
return 0;
}


How to run this program 
          open terminal  and run    " flex  ass1.2.l "
          it generates c file run      " cc lex.yy.c -o count   -ll  "  as out put  in file count
          now run   for output       " ./count "


                           

Comments

  1. Casinos for Canadians - DMC
    The casinos have the lowest deposit bonus, 제천 출장샵 and you can use the code 'GDSR' on top of that, then try it 태백 출장샵 out for yourself at 보령 출장마사지 the 김해 출장마사지 casino. 부천 출장마사지

    ReplyDelete

Post a Comment

Popular posts from this blog

YACC Program to check whether the arithmetic expression is valid or not

validexp.l //LEX Program /* declaration section in this sections we will declare the different value and include the header file which we are using in this program to run this program */ %{ #include"y.tab.h" extern yylval; %} /* defined section */ %% [0-9]+ {yylval=atoi(yytext); return NUMBER;}    //this is send to the yacc code as token INTEGER [a-zA-Z]+ {return ID;}                                          //this is send to the yacc code as token  ID [\t]+ ; \n {return 0;} . {return yytext[0];} %%    validexp.y  //yacc program //decelration section %{ #include<stdio.h> %} //definition section %token NUMBER ID                        // token from lex file %left '+' '-'                    ...

LEX to count the no of "scanf" and "printf" statement in a c program. Replace them with "writf" and "readf" respectively.

/* declaration section in this sections we will declare the different value and include the header file which we are using in this program to run this program */ %{ #include<stdio.h> int sf=0,pf=0; %} /* defined section */ %% "scanf" { sf++; fprintf(yyout,"readf");}            // replace scanf  with readf "printf" { pf++; fprintf(yyout,"writef");}            // replace printf  with writef %% int main() { yyin=fopen("open.c","r");                            // input file open.c yyout=fopen("new.c","w");                            // output file new.c with replace  yylex();      //no of printf and scanf in the file printf("Number of scanfs=%d\nNumber of Printf's=%d\n",sf,pf); return 0; } download the file  ...

lex program to count the number of Positive and negative integer

/* decleration section in this sections we will decleared the different  value and include the header file which we are using in this program to run this program */ %{ #include<stdio.h> int p_count=0; int n_count=0; %} /* this sections is known as defined section in which we defined the rule and regulation of regular expression which will going to accept or not */ %% [+]?([0-9])+  { /* this is for increment and print the positive integer eg: 96, +36 */ p_count++; printf("Positive Integer:%s\n",yytext);} [-]([0-9])+   { /* this is for increment and print the negative integer eg: -96, -36 */ n_count++; printf("negative Integer:%s\n",yytext);} %% /* main function for calling the function */ void main(int argc[],char *argv[]) { yylex(); printf("the total number of Postive Integer are: %d\n" ,p_count); printf("the total number of Negative  Integer are:%d\n",n_count); }                ...