ADVERTISEMENT
ADVERTISEMENT

Home » Compiler Design

LEX Code to replace white spaces of ‘Input.txt’ file by a single blank character into ‘Output.txt’ file

In this article, we going to learn how to create LEX program to analysis how many characters, white space (means \n \t “”), and words are present in given input file?
Submitted by Ashish Varshney, on March 25, 2018

For achieve to this task we create some rules in rule section in the code.

    Input file:
	    Include       help
    Output file:
	    Include help
/*Definition Section*/
%{
#include<stdio.h>
%}

%%
/*Whenever encounter an whitespace in input file 
it place with single whitespace*/
[\t" "]+ fprintf(yyout," ");		
/*print other character as it is in yyout file*/
.|\n fprintf(yyout,"%s",yytext);	
%%

/*call the yywrap function*/
int yywrap()
{
return 1;
}

int main(void)
{
yyin=fopen("input.txt","r");		
yyout=fopen("output.txt","w");
/*call the yylex function.*/
yylex();
return 0;
}

Input

LEX Code to replace white spaces of ‘Input.txt’ file by a single blank character into ‘Output.txt’ file - Incput

Output

LEX Code to replace white spaces of ‘Input.txt’ file by a single blank character into ‘Output.txt’ file - Output

ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.