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<stdio.h>
  int no_of_char=0;
  int no_of_lines=0;
%}

%%
\n {++no_of_lines, ++no_of_char;}
. ++no_of_char;

%%

int main()
{
 yyin=fopen("abc.txt", "r"); 
 yylex();
 printf("n0 of characters: %d",no_of_char);
 printf("\n");
 printf("n0 of lines: %d",no_of_lines);
 printf("\n"); 
 return 0;
}

//abc.txt file
output:


No comments:

Post a Comment

Featured post

Amazon Interview Process

On July 5, 1994, Jeff Bezos started the world's most "customer-centric" firm out of his garage in Bellevue, Washington. The A...