Skip to content
Related Articles

Related Articles

Lex Program to remove comments from C program

View Discussion
Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 11 Jul, 2022

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

Let’s see LEX program to accept string starting with vowel.

Input : 
int main()
 /* multiline comment
 return 0;

Output :
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)
/*call the yylex function.*/
return 0;


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!