Flex/Lex program to detect white-space, tab, single line comments, multiple line comment and Remove it | Tech With Code
Flex/Lex program to detect white-space, tab, single-line comments, multiple line comments, and Remove it:
Today I will show you how to write a Lex/Flex program that detects white-space, tab, single-line comments, multiple line comments, and after that remove all of these and print that as an output.
Also Read:- LEX-FLEX-program-to-check-for-Even-Odd-Decimal
Example:
Input:-
This is TechWithCode.comHere You will get all programming Source Code
//this is first single line comment
//this is second single line comment
/*this
is
multiple
line
comments
*/
OutPut:-
No. of spaces=8No. of tabs=1
No. of Single Line Comments=2
No. of Multiple Line Comments=1
After Removal:-
ThisisTechWithCode.comHereYouwillgetallprogrammingSourceCode
Source Code of Flex program:-
%{#include<stdio.h>
#include<stdlib.h>
int slc=0, sc=0, tc=0, flag =0, mlc=0;
%}
%%
" " {sc++;} //space counter
\t {tc++;} //tab counter
"//".*\n {slc++;} //single line comment
"/*"[^*/]*"*/" {mlc++;} // multiple line comment
%%
int yywrap(){return 1;}
main()
{
yyin=fopen("input.txt","r");
printf("After Execution:-\n\n");
yylex();
printf("\n\nIn the Input File,");
printf("\nNo. of spaces=%d",sc);
printf("\nNo. of tabs=%d",tc);
printf("\nNo. of Single Line Comments=%d",slc);
printf("\nNo. of Multiple Line Comments=%d",mlc);
}
OutPut of Flex Program:-
Must Read:- TCS CodeVita Question With Solution
Final Word:-
In this article, We have explained to you how to write a Flex/Lex program to detect white-space, tab, single-line comments, multiple line comments, and Remove it. We hope you like it
If This post helps you a little bit then please share this post with your friends. For more Computer and programming Related Tips and tricks, please
follow us on Telegram
Don't forget to share how you like this Content in the comment below. If you have any doubt or suggestion then please write in the comment section below, we will love to hear from you.
No comments: