Program:
%{
%}
comment1 \/\*(.|\n)*\*\/
comment2 \/\/.*
%%
{comment1} ;
{comment2} ;
.|\n ECHO;
%%
main()
{
yylex();
return 0;
}
Output:
( input file: c1.c
#include<stdio.h>
main ()
{
int i,n,fact=1;
printf("Enter the number: ");
scanf("%d",&n); //inputing the number
for(i=1;i<=n;i++) /* finding factorial */
{
fact = fact*i;
}
printf("Factorial=%d\n",fact);
}
)
nn@linuxmint ~ $ lex l2.lex
nn@linuxmint ~ $ gcc lex.yy.c -lfl
nn@linuxmint ~ $ ./a.out <c1.c
#include<stdio.h>
main ()
{
int i,n,fact=1;
printf("Enter the number: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact = fact*i;
}
printf("Factorial=%d\n",fact);
}
nn@linuxmint ~ $
%{
%}
comment1 \/\*(.|\n)*\*\/
comment2 \/\/.*
%%
{comment1} ;
{comment2} ;
.|\n ECHO;
%%
main()
{
yylex();
return 0;
}
Output:
( input file: c1.c
#include<stdio.h>
main ()
{
int i,n,fact=1;
printf("Enter the number: ");
scanf("%d",&n); //inputing the number
for(i=1;i<=n;i++) /* finding factorial */
{
fact = fact*i;
}
printf("Factorial=%d\n",fact);
}
)
nn@linuxmint ~ $ lex l2.lex
nn@linuxmint ~ $ gcc lex.yy.c -lfl
nn@linuxmint ~ $ ./a.out <c1.c
#include<stdio.h>
main ()
{
int i,n,fact=1;
printf("Enter the number: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact = fact*i;
}
printf("Factorial=%d\n",fact);
}
nn@linuxmint ~ $
This will not work if c program contains more than one multiple line comments.It will consider code between first '/*' and last '*/' as comment.
ReplyDeletetry this it works:
comment1 \/\*[^ "*/"]*\*\/
for the above comment... it still wont work if any of the comment lines contain a * or / in them...
ReplyDeleteI have recently started a blog, the info you provide on this site has helped me greatly. Thanks for all of your time & work
ReplyDeleteOriginal Hoa Lu Ninh Binh
I really thank you for the valuable info on this great subject and look forward to more great posts
ReplyDeleteequipment
This is highly informatics, crisp and clear. I think that everything has been described in systematic manner so that reader could get maximum information and learn many things. blackmart apk .99.2.93b _ APKForAll.Com
ReplyDeleteWow! Such an amazing and helpful post this is. I really really love it. It's so good and so awesome. I am just amazed. I hope that you continue to do your work like this in the future also RARBG
ReplyDeleteWow! Such an amazing and helpful post this is. I really really love it. It's so good and so awesome. I am just amazed. I hope that you continue to do your work like this in the future also RARBG
ReplyDelete