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:
#include<stdio.h>
// Driver Code
int main()
{
int a;
/* we have to delete comments */
return 0;
}
Output:
1 #include<stdio.h>
2 // Driver Code
3 int main()
4 {
5 int a;
6 /* we have to delete comments */
7 return 0;
8 }
Approach:
A new line is encountered because of the /n. To count the number of lines, count the number of /n occurred with initial value as 1 as there exists an initial single line. All the other things can be ignored as focus is on number of /n. Take a counter initially set to 1 and increment it whenever a new line (/n) occurs.
Input File: testtest.c

Below is the implementation to add line numbers to a given file.
/* Program to add line numbers to a given file*/%{ int line_number = 1; // initializing line number to 1 %} /* simple name definitions to simplify the scanner specification name definition of line*/line .*\n %% {line} { printf("%10d %s", line_number++, yytext); } /* whenever a line is encountered increment count*/ /* 10 specifies the padding from left side to present the line numbers*/ /* yytext The text of the matched pattern is stored in this variable (char*)*/%% int yywrap(){} int main(int argc, char*argv[]) { extern FILE *yyin; // yyin as pointer of File type yyin = fopen("testtest.c","r"); /* yyin points to the file testtest.c and opens it in read mode.*/ yylex(); // The function that starts the analysis. return 0; } |
Output:

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Lex program to copy the content of one file to another file
- Lex Program to print the total characters, white spaces, tabs in the given input file
- Lex program to count the frequency of the given word in a file
- Lex program to take input from file and remove multiple spaces, lines and tabs
- Lex program to search a word in a file
- C program to print odd line contents of a File followed by even line content
- Lex program to Count the Positive numbers, Negative numbers and Fractions
- LEX code to extract HTML tags from a file
- Lex code to replace a word with another word in a file
- Lex Program to Identify and Count Positive and Negative Numbers
- Lex program to check perfect numbers
- Lex Program to remove comments from C program
- LEX program to count the number of vowels and consonants in a given string
- Lex program to check whether a given number is even or odd
- Lex program to check whether given string is Palindrome or Not
- Lex program to check whether given number is armstrong number or not
- Lex Program to count number of words
- Lex Program For checking a valid URL
- Lex program to count the number of lines, spaces and tabs
- Lex program to identify the identifier
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.

