编译原理1

编译原理实验1—— 消去C语言中的注释
参考博客:https://blog.csdn.net/C__Monkey/article/details/8079580
@ c__monkey 对博主表示感谢!
仅在原程序上加入将原文件修改为去掉注释的文件的功能。

#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include<fcntl.h>
#include<io.h>
void remove_comment(char *buf,size_t size)
{
    char *p,*end,c;
    char *sq_start,*dp_start;
    char *lc_start,*bc_start;
    size_t len;

    p=buf;
    end=p+size;
    sq_start=NULL;
    dp_start=NULL;

    lc_start=NULL;
    bc_start=NULL;

    while(p<end)
    {
        c=*p;
        switch(c)
        {
        case '\'':
            if(dp_start||lc_start||bc_start)
            {
                p++;
                continue;
            }
            if(sq_start==NULL)
            {
                sq_start=p++;
            }
            else{
                len==p++-sq_start;
            }
            if(len==2&&*(sq_start+1)=='\\')
            {
                continue;
            }
            sq_start=NULL;
            break;
        case '\"':
            if(sq_start||lc_start||bc_start)
            {
                p++;
                continue;
            }
            if(dp_start==NULL)
            {
                dp_start=p++;
            }
            else{
                if(*(p++-1)=='\\')
                    continue;
            }
            dp_start=NULL;
            break;
        case '/':
            if(sq_start||dp_start||lc_start||bc_start)
            {
                p++;
                continue;
            }
            c=*(p+1);
            if(c=='/')
            {
                lc_start=p;
                p+=2;
            }
            else if(c=='*')
            {
                bc_start=p;
                p+=2;
            }
            else{
                p++;
            }
            break;
        case '*':
            if(sq_start||dp_start||lc_start||bc_start==NULL)
            {
                p++;
                continue;
            }
            if(*(p+1)!='/')
            {
                p++;
                continue;
            }
            p+=2;
            memset(bc_start,' ',p-bc_start);
            bc_start=NULL;
            break;
        case '\n':
            if(lc_start==NULL)
            {
                p++;
                continue;
            }
            c=*(p+1);
            memset(lc_start,' ',(c=='\r'?(p++-1):p++)-lc_start);
            lc_start=NULL;
            break;
        default:
            p++;
            break;
        }
    }
    if(lc_start)
    {
        memset(lc_start,' ',p-lc_start);
    }
}
int main(int argc,char *argv[])
{
    int fd,n;
    char buf[102400];
    char nname[1024];
    printf("请输入打开的文件名:\n");
    scanf("%s",nname);
    fd=open(nname,_O_RDONLY,0);
    if(fd==NULL)printf("打开失败!");
    if(fd==-1){
        return -1;
    }
    n=read(fd,buf,sizeof(buf));
    if(n==-1||n==0)
    {
        close(fd);
        return -1;
    }
    remove_comment(buf,n);
    *(buf+n)='\0';
    printf(buf);
    close(fd);
    FILE *fp;
    fp=fopen("Untitled1.c","w");
    for(int i=0;i<n;i++)
    {
        fprintf(fp,"%c",buf[i]);
    }
    fclose(fp);
    return 0;
}

运行程序前的原文件
这是运行前的原文件
在这里插入图片描述
运行程序
在这里插入图片描述
运行结果

猜你喜欢

转载自blog.csdn.net/qq_37507976/article/details/83057366