Lex Program to remove comments from C program
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 (abc is the file name) cc lex.yy.c -efl ./a.out
Let’s see LEX program to accept string starting with vowel.
Examples:
Input :
//testing
#include
int main()
{
/* multiline comment
continue....
*/
return 0;
}
Output :
#include
int main()
{
return 0;
}
Below is the implementation:
/% Lex Program to remove comments from C program and save it in a file %/ /*Definition Section*/ %{ %} /*Starting character sequence for multiline comment*/ start \/\* /*Ending character sequence for multiline comment*/ end \*\/ /*Rule Section*/ %% /*Regular expression for single line comment*/ \/\/(.*) ; /*Regular expression for multi line comment*/ {start}.*{end} ; %% /*Driver function*/ int main(int k,char **argcv) { yyin=fopen(argcv[1],"r"); yyout=fopen("out.c","w"); /*call the yylex function.*/ yylex(); return 0; } |
chevron_right
filter_none
Output:


Recommended Posts:
- Lex program to take input from file and remove multiple spaces, lines and tabs
- C Program for Program to find area of a circle
- C Program for Program for array rotation
- C program to detect tokens in a C program
- Why are there two folders - Program Files and Program Files (x86) in 64-bit Windows OS?
- Program Execution in the CPU
- C Program for Cutting a Rod | DP-13
- C Program for ID3 Tagging
- C Program to Add two Integers
- How does a C program executes?
- Program to print Hut
- Lex program to check whether a given number is even or odd
- C Program for simple interest
- Program to print the Cot Bed Pattern
- C program for pipe in Linux
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.
Improved By : psil123



