ADVERTISEMENT
ADVERTISEMENT

Home » Compiler Design

LEX Code to remove the comments from any C-Program given at run-time and store into ‘out.c’ file

In this article, we going to learn how to create LEX program to extract comment (single line and multiline) from c program?
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:
	    /*this in multiline comment
	    We are going to remove this*/

	    // this is single line comment
	    int main()
	    {
		    Code goes here
	    } 
    Output file:
	    int main()
	    {
		    Code goes here
	    } 
/*Definition Section*/
%{
#include<stdio.h>
%}


/*Rule Section*/

%%
/*Regular expression for single line comment*/
\/\/(.*) {};					
/*Regular expression for multi line comment*/
\/\*(.*\n)*.*\*\/  {};			
%%

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



/*Auxiliary function*/
/*Driver function*/

int main()
{
yyin=fopen("input6.c","r");
yyout=fopen("out.c","w");
/*call the yylex function.*/
yylex();
return 0;
}

Input

LEX Code to remove the comments from any C-Program given at run-time and store into ‘out.c’ file - Incput

Output

LEX Code to remove the comments from any C-Program given at run-time and store into ‘out.c’ 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.