Comment Removal - Lex Program - Compiler Design

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 ~ $

7 comments:

  1. This will not work if c program contains more than one multiple line comments.It will consider code between first '/*' and last '*/' as comment.
    try this it works:
    comment1 \/\*[^ "*/"]*\*\/

    ReplyDelete
  2. for the above comment... it still wont work if any of the comment lines contain a * or / in them...

    ReplyDelete
  3. I have recently started a blog, the info you provide on this site has helped me greatly. Thanks for all of your time & work
    Original Hoa Lu Ninh Binh

    ReplyDelete
  4. I really thank you for the valuable info on this great subject and look forward to more great posts
    equipment

    ReplyDelete
  5. 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

    ReplyDelete
  6. Wow! 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
  7. Wow! 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

Related Posts Plugin for WordPress, Blogger...