Showing posts with label Comment Removal. Show all posts
Showing posts with label Comment Removal. Show all posts

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

Comment Removal Program - Compiler Lab - C Program

Program:

#include<stdio.h>
main()
{
    FILE *ip,*op;
    char ipname[10],opname[10];
    char c;
    int flag=0;
    printf("\nEnter the input file name : ");
    scanf("%s",ipname);
    printf("\nEnter the output file name : ");
    scanf("%s",opname);
    if((ip=fopen(ipname,"r"))==NULL)
        printf("Input file does not exist !\n");
    else
    {   
        op=fopen(opname,"w");
        while((c=getc(ip))!=EOF)
        {
            if(c=='/')
            {
                c=getc(ip);
                if(c=='*')
                    flag=1;
                if(c=='/')
                    flag=2;
            }
   
            if((flag==2)&&(c=='\n'))
                flag=0;
            if(flag==0)
                putc(c,op);
            if((flag==1)&&(c=='*'))
            {
                c=getc(ip);
                if(c=='/')
                    flag=0;
            }
        }
        fclose(ip);
        fclose(op);
    }
}


Output:

nn@ubuntu:~$ gcc cmt.c
nn@ubuntu:~$ ./a.out

Enter the input file name : a.c

Enter the output file name : b.c
nn@ubuntu:~$

a.c: (Input File)
#include<stdio.h>
main()
{
prntf("hai...");//comment removal program in c
printf("hello...");/*www.2k8618.blogspot.com */
}


b.c:(Output File)
#include<stdio.h>
main()
{
prntf("hai...");
printf("hello...");
}
Related Posts Plugin for WordPress, Blogger...