The Wayback Machine - https://web.archive.org/web/20241109174508/https://www.geeksforgeeks.org/lex-program-add-line-numbers-given-file/
Open In App

LEX program to add line numbers to a given file

Last Updated : 30 Apr, 2019
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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:



Similar Reads

Lex program to copy the content of one file to another file
Problem: Given a text file as input, the task is to copy the content of the given file to another 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 implementing the lexer in the C programming l
2 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 implementing the lexer in the C programming language
2 min read
Lex program to Count the Positive numbers, Negative numbers and Fractions
Problem : Write a Lex program to count the positive numbers, negative numbers and fractions Explanation : 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 i
2 min read
LEX program to print the longest string and to find average of given numbers
Lex :The Lex program has purpose of generating lexical analyzer. Lex analyzers are a program that transform input streams into sequence of tokens. A C program implements the lexical analyzer to read input stream and produce output as the source code. Commands to be used - lex file_name.l // To produce a c program cc lex.yy.c -lfl // To compile the
2 min read
Lex program to take input from file and remove multiple spaces, lines and tabs
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
2 min read
Lex Program to Identify and Count Positive and Negative Numbers
Given some numbers, task is to identify the positive and negative numbers and print the count of negative and positive numbers. Prerequisite: Flex (Fast lexical Analyzer Generator) Examples: Input : -52 Output : negative number=-52 number of positive numbers = 0 number of negative numbers = 1 Input : 63 Output : positive number = 63 number of posit
1 min read
LEX code to extract HTML tags from a 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. Prerequisite: Flex (Fast lexical Analyzer Generator) Approach: The extraction is based on the way tags are w
1 min read
LEX program to count the number of vowels and consonants in a given string
Prerequisite: Flex (Fast lexical Analyzer Generator) Given a string containing both vowels and consonants, write a LEX program to count the number of vowels and consonants in given string. Examples: Input: Hello everyone Output: Number of vowels are: 6 Number of consonants are: 7 Input: This is GeeksforGeeks Output: Number of vowels are: 7 Number o
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 divided by 2, it yields no remainder or 0 as rema
1 min read
Lex program to check whether given string is Palindrome or Not
Problem: Write a Lex program to check whether given string is Palindrome or Not. 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 implementing the lexer in the C programming language. Description: A
2 min read
Lex program to check whether given number is armstrong number or not
Problem: Lex program to check whether given number is armstrong number or not. 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 implementing the lexer in the C programming language. Description: An
2 min read
Lex Program to count number of words
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
1 min read
Lex Program For checking a valid URL
Problem: Write a Lex program for checking a valid URL. Explanation: Lex is a computer utility that generates some lexical analyzers. Lex reads a stream of characters as input specifying the lexical analyzer and give source code as the output implementing the lexer in the C programming language. Prerequisite: Flex (Fast Lexical Analyzer Generator) E
1 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 using Lex. Example: Input: Geeks for Geeks gfg gf
1 min read
Lex program to identify the identifier
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. In C, an identifier must begin with either an alphabet or underscore, it can not begin with a digit or any oth
1 min read
Lex program to check whether the input is digit or not
Lex is a computer program that generates lexical analyzers. 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). Given an input, the task is to check if the input is digit or not. Examples: Input: 28 Output: digit
1 min read
Lex program to check whether an year is a leap year or not
Problem: Write a Lex program to check whether an year is a leap year or not. 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 implementing the lexer in the C programming language. Note: Leap years a
1 min read
Lex Program to check whether a number is Prime or Not
Problem: Write a Lex Program to check whether a number is Prime or Not. 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 implementing the lexer in the C programming language. Definition: A prime num
1 min read
Lex program for Decimal to Binary Conversion
Problem: Write a Lex program for Decimal to Binary conversion. Explanation: 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
1 min read
Lex program to recognize valid arithmetic expression and identify the identifiers and operators
Problem: Write a Lex program to recognize valid arithmetic expression and identify the identifiers and operators. Explanation: 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
2 min read
Lex program to check if a Date is valid or not
Problem: Write a Lex program to check if a date is valid or not. Explanation: 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
1 min read
Lex program to count words that are less than 10 and greater than 5
Problem: Write a Lex program to count words that are less than 10 and greater than 5.Explanation: Lex is a computer program that generates lexical analyzers. Lex reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in the C programming language.The commands for executing the lex program are: lex abc.l
1 min read
Lex program to check valid Mobile Number
Problem: Write a Lex Program to check valid Mobile Number. Explanation: 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 pro
1 min read
Lex program to check if characters other than alphabets occur in a string
Problem :- Write a Lex program to find if a character apart from alphabets occurs in a string or not Explanation:- Flex (Fast Lexical Analyzer Generator ) is a tool or computer program which generates lexical analyzers (scanners or lexers) written by Vern Paxson in C around 1987.Lex reads an input stream specifying the lexical analyzer and outputs
2 min read
DFA in LEX code which accepts even number of zeros and even number of ones
Lex is a computer program that generates lexical analyzers, which is commonly used with the YACC parser generator. Lex, originally written by Mike Lesk and Eric Schmidt and described in 1975, is the standard lexical analyzer generator on many Unix systems, and an equivalent tool is specified as part of the POSIX standard. Lex reads an input stream
6 min read
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 atomic chunk of syntax i.e. token is a class of lexem
2 min read
DFA in LEX code which accepts strings ending with 11
Prerequisite: Designing Finite Automata Problem: Design a LEX code to construct a DFA which accepts the language: all the strings ending with "11" over inputs '0' and '1'. Examples: Input: 100011 Output: Accepted Input: 100101 Output: Not Accepted Input: asdf Output: Invalid Approach: LEX provides us with an INITIAL state by default. So in order to
2 min read
DFA in LEX code which accepts Odd number of 0’s and even number of 1’s
Prerequisite: Designing Finite Automata Problem: Design a LEX code to construct a DFA which accepts the language: all strings with Odd number of 0’s and even number of 1’s over inputs '0' and '1'. Examples: Input: 10001 Output: Accepted Input: 10011 Output: Not Accepted Input: asdf Output: Invalid Approach: LEX provides us with an INITIAL state by
2 min read
What is Amazon Lex?
Amazon Lex is a powerful service provided by AWS used for the development of conversational application interfaces. Its ability to understand various natural languages and automatic speech recognition makes it simpler for users to create interesting user interfaces that resemble real-world conversations. On using this service users can easily build
14 min read
Scan conversion of Line and Line Drawing algorithms
SCAN Conversion of Line : A line connects two points.It is a basic element in graphics.You'll need two spots between which to draw a line to draw a line . A line, or line segment, can be uniquely described by two points, according to geometry. We also know from algebra that a line can be defined by a slope, commonly denoted by the letter m, and a y
4 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg