Lex Program to count number of words Last Updated : 13 Mar, 2023 Comments Improve Suggest changes Like Article Like Report 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. Prerequisite: Flex (Fast lexical Analyzer Generator) Example: Input: Hello everyone Output: 2 Input: This is GeeksforGeeks Output: 3 Note: The words can consist of lowercase characters, uppercase characters and digits. Below is the implementation to count the number of words. C /*lex program to count number of words*/ %{ #include<stdio.h> #include<string.h> int i = 0; %} /* Rules Section*/ %% ([a-zA-Z0-9])* {i++;} /* Rule for counting number of words*/ "\n" {printf("%d\n", i); i = 0;} %% int yywrap(void){} int main() { // The function that starts the analysis yylex(); return 0; } Output: Comment More infoAdvertise with us Next Article Lex Program to count number of words K Kanishk_Verma Follow Improve Article Tags : Misc DSA Lex program Practice Tags : Misc Similar Reads Lex code to count total number of tokens 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 lex in the C programming language. Tokens: A token is a group of characters forming a basic ato 2 min read Counting the number of words in a Trie A Trie is used to store dictionary words so that they can be searched efficiently and prefix search can be done. The task is to write a function to count the number of words. Example : Input : root / \ \ t a b | | | h n y | | \ | e s y e / | | i r w | | | r e e | r Output : 8 Explanation : Words for 7 min read Lex program to count the number of lines, spaces and tabs 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.Let's the how to count the number of lines, spaces and tabs 1 min read Lex program to check whether a given number is even or odd Given a number n, the task is to check whether the given n is even or odd using Lex program. Examples: Input : 10 Output : Even Input : 5 Output : Odd Prerequisite: FLEX (Fast Lexical Analyzer Generator) An even number is an integer which is "evenly divisible" by 2. This means that if the integer is 1 min read 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 im 2 min read Like