Problem: Write a Lex program to search a word in a file.
FLEX (Fast Lexical Analyzer Generator) is a tool/computer program for generating lexical analyzers (scanners or lexers) written by Vern Paxson in C around 1987. Lex reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in the C programming language. The function yylex() is the main flex function which runs the Rule Section.
Input: welcome to geeksforgeeks Output: welcome FOUND for NOT FOUND geeksforgeeks FOUND
To search a word, I have stored words in file named “input.txt” and used lex to receive words from keyboard. On receiving each word, I check it with the words in the file. If the word is found, the program prints “FOUND” else “NOT FOUND”.
Input File: input.txt (Input File used in this program)
Below is the implementation of program:
Want to learn from the best curated videos and practice problems, check out the C Foundation Course for Basic to Advanced C.