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

3 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

Related Posts Plugin for WordPress, Blogger...