Friday, April 12, 2019

Lex program to count number of comments (single line or multiple lines) in given C source file


%{ 
  #include<stdio.h>
  int count,cont;
%}           
%%
"//" count++;
"/*"([^*]|[*][^/])*"*/" {cont++;}
.
\n
%%
int main()
{
  FILE *fp;
  fp=fopen("test.c","r");
  if(fp==NULL)
  {
       printf("File cant oprn");
       return 0;
  }
  yyin=fp;
  yylex();
  printf("single line comments is=%d",count);
  printf("\nMultiple line commentis=%d",cont);
}

ScreenShot:



No comments:

Post a Comment