Lex program to count the frequency of the given word in a file

Problem: Given a text file as input, the task is to count frequency of a given word in the file.

Explanation:
Lex is a computer program that generates lexical analyzers and was written by Mike Lesk and Eric Schmidt. Lex reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in the C programming language.

Approach:
As we know, yytext holds the value of current matched token, we can compare it with the word whose frequency is to be counted. if the value of yytext and the given word are same, increment the count variable.

Input File: input.txt

Below is the implementation of above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

/* LEX code to count the frequency
   of the given word in a file */
  
/* Definition section */
/* variable word indicates the word
   whose frequency is to be count */
/* variable count is used to store the 
   frequency of the given word */
  
%{
#include<stdio.h>
#include<string.h>
  
char word [] = "geeks";
int count = 0;
  
  
%}
  
/* Rule Section */
/* Rule 1 compares the matched token with the
   word to count and increments the count variable
   on successful match  */
/* Rule 2 matches everything other than string 
   (consists of albhabets only ) and do nothing */
  
%%
[a-zA-Z]+    { if(strcmp(yytext, word)==0)
                   count++; }
.  ;         
%%
  
  
int yywrap()
{
    return 1;
}
  
/* code section */
int main()
{
        extern FILE *yyin, *yyout;
          
        /* open the input file
           in read mode */
        yyin=fopen("input.txt", "r");
        yylex();
           
        printf("%d", count);
      
}

chevron_right


Output:



My Personal Notes arrow_drop_up

self motivated and passionate programmer

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.