Wednesday, September 9, 2020

Write a Lex program to count the number of comment lines in a given C program.

Count the number of comment lines in a given C program using Lex

EXAMPLE :-


%{ int com=0; %} %% "/*"[^\n]+"*/" {com++;fprintf(yyout, " ");} %% int yywrap(void){} int main() { printf("Write a C program find Comment line : \n"); yyout=fopen("Output", "w"); yylex(); printf("Comment=%d\n",com); return 0; }


OUTPUT


sunny@sunny-PC:~/Desktop$ flex comment.l sunny@sunny-PC:~/Desktop$ gcc lex.yy.c sunny@sunny-PC:~/Desktop$ ./a.out
Write a C program find Comment line :
/* Write Hello World in C*/ #include <stdio.h> /* Main function starts from here*/ int main() { /* This statement will print Hello World message */ printf(“Hello World!”); return 0; }
            Ctrl+d
Comment=3












No comments:

Post a Comment

Please do not any spam link in Comment Box