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 atomic chunk of syntax i.e. token is a class of lexemes that matches a pattern. Eg – Keywords, identifier, operator, separator.
Example:
Input: int p=0, d=1, c=2; Output: total no. of tokens = 13
Below is the implementation of the above explanation:
/*Lex code to count total number of tokens */ %{ int n = 0 ; %} // rule section %% //count number of keywords "while"|"if"|"else" {n++;printf("\t keywords : %s", yytext);} // count number of keywords "int"|"float" {n++;printf("\t keywords : %s", yytext);} // count number of identifiers [a-zA-Z_][a-zA-Z0-9_]* {n++;printf("\t identifier : %s", yytext);} // count number of operators "<="|"=="|"="|"++"|"-"|"*"|"+" {n++;printf("\t operator : %s", yytext);} // count number of separators [(){}|, ;] {n++;printf("\t separator : %s", yytext);} // count number of floats [0-9]*"."[0-9]+ {n++;printf("\t float : %s", yytext);} // count number of integers [0-9]+ {n++;printf("\t integer : %s", yytext);} . ; %% int main() { yylex(); printf("\n total no. of token = %d\n", n); } |
Output:

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.
Recommended Posts:
- DFA in LEX code which accepts even number of zeros and even number of ones
- DFA in LEX code which accepts Odd number of 0’s and even number of 1’s
- Lex Program to print the total characters, white spaces, tabs in the given input file
- LEX code to extract HTML tags from a file
- Lex code to replace a word with another word in a file
- DFA in LEX code which accepts strings ending with 11
- Lex Program to count number of words
- Lex program to count the number of lines, spaces and tabs
- LEX program to count the number of vowels and consonants in a given string
- Lex program to check whether given number is armstrong number or not
- Lex Program to Identify and Count Positive and Negative Numbers
- Lex program to Count the Positive numbers, Negative numbers and Fractions
- Lex program to count the frequency of the given word in a file
- Lex program to count words that are less than 10 and greater than 5
- Lex program to check whether a given number is even or odd
- Lex program to check whether input number is odd or even
- Lex Program to check whether a number is Prime or Not
- Lex program to check valid Mobile Number
- LEX program to add line numbers to a given file
- Lex Program For checking a valid URL
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.
Improved By : Ravimaurya2

