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 runs the Rule Section.
Prerequisite: FLEX (Fast Lexical Analyzer Generator)
Example:
Input:
hello how
are
you?
Output:
hellohowareyou?
Input:
Welcome to
Geeks for
Geeks
Output:
WelcometoGeeksforGeeks
Approach:
Open input file in read mode and whenever parser encounters newline (\n), space ( ) or tab (\t) remove it and write all the other characters in output file.
Input File: Input.txt (Input File used in this program)

Below is the implementation program:
/*Lex program to take input from file and remove multiple spaces, newline and tab and write output in a separate file*/ % { /*Definition section */ % } /* Rule: whenever space, tab or newline is encounterd, remove it*/% % [ \n\t]+ {fprintf(yyout, "");} . { fprintf(yyout, "%s", yytext); } % % int yywrap(){} // driver code int main() { /* yyin and yyout as pointer of File type */ extern FILE *yyin, *yyout; /* yyin points to the file input.txt and opens it in read mode*/ yyin = fopen("Input.txt", "r"); /* yyout points to the file output.txt and opens it in write mode*/ yyout = fopen("Output.txt", "w"); yylex(); return 0; } |
Output:


Recommended Posts:
- Lex Program to print the total characters, white spaces, tabs in the given input file
- Lex program to count the number of lines, spaces and tabs
- C Program to count number of lines in a file
- Input an integer array without spaces in C
- Implement your own tail (Read last n lines of a huge file)
- How to use getline() in C++ when there are blank lines in input?
- C program to copy contents of one file to another file
- Lex program to copy the content of one file to another file
- Program to print last 10 lines
- C program to delete a file
- Lex Program to remove comments from C program
- Lex program to search a word in a file
- LEX program to add line numbers to a given file
- C Program to find size of a File
- C Program to print contents of file
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.


