1. Lex Program to Count the Number of Lines and Characters in the Input File

 

Lex Program to Count the Number of Lines and Characters in the Input File

//Lex Program to Count the Number of Lines and Characters in the Input File
 
%{ #include<studio.h>
int n_char=0;
int n_lines=0;
%}

%%
\n {++n_lines, ++n_char;}
. ++n_char;

%%

int main(int argc[],char *argv[])
{
 yyin=fopen("abc.txt", "r"); 
 yylex();
 printf("n# of characters: %d",n_char);
 printf("\n");
 printf("n# of lines: %d",n_lines);
 printf("\n"); 
 return 0;
}
 
// file abc.txt 
Lex Program to Count the Number of Lines and Characters in the Input File
Lex Program to Count the Number of Lines and Characters in the Input File
 
// Output of the above program  
Lex Program to Count the Number of Lines and Characters in the Input File
Lex Program to Count the Number of Lines and Characters in the Input File

Comments

Popular posts from this blog

Compiler Design

3. Lex program to find the length of the longest word

6. Lex program to count the number of words,small and capital letters, digits and special characters in a C file