comment.l
%{
#include<stdio.h>
int com=0;
%}
%s COMMENT
%% “/*” {BEGIN COMMENT;} <COMMENT>”*/” {BEGIN
0;com++;}
<COMMENT>\n {com++;}
<COMMENT>. {;}
\/\/.* {; com++;} .|\n
{fprintf(yyout,”%s”,yytext);}
%%
void main(intargc, char *argv[])
{
if(argc!=3)
{ printf(“usage : ./a.out in.txt
out.txt\n”);
exit(0);
} yyin=fopen(argv[1],”r”);
yyout=fopen(argv[2],”w”);
yylex(); printf(“\n number of comments are =
%d\n”,com);
}
intyywrap()
{
return 1;
}
OUTPUT :
Input :
lexcomment.l
cc lex.yy.c -ll
./a.out in.txt out.txt
Output :
number of comments are = 2
Content of in.txt file
#include<stdio.h>
int main()
{
inta,b,c; /*varible declaration*/ printf(“enter two
numbers”); scanf(“%d %d”,&a,&b);
c=a+b;//adding two numbers printf(“sum is %d”,c);
return 0;
}
Content of out.txt file
#include<stdio.h>
int main()
{
inta,b,c; printf(“enter two numbers”); scanf(“%d
%d”,&a,&b);
c=a+b; printf(“sum is %d”,c);
return 0; }