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.
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.
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
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.
Below is the implementation of above approach: