删除源文件中的注释

网上代码中大部分用的状态机,以下程序比较简陋,未考虑源文件有错误等情况。
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;

#define LEN		1000000		//MAX LENGTH of a source file

char src[LEN];
char dst[LEN];

void Input()
{
	cin.getline(src, LEN, EOF);
	cout<<"source code length: "<<strlen(src)<<endl;
}

void Delete_Comments()
{
	int src_len = strlen(src);
	int i=0, j=0;
	for(; i<src_len-1; i++)
	{
		if('/' == src[i])
		{
			if('/' == src[i+1])
			{
				while(i<src_len && src[i]!='\n') ++i;
				if(i < src_len) --i; //keep \n
			}
			else if('*' == src[i+1])
			{
				++ i;
				while(i<src_len-1 && !(src[i]=='*' && src[i+1]=='/')) ++i;
				if(i < src_len-1) ++i;
			}
		}
		else if('"' == src[i])
		{
			dst[j++] = src[i++];
			while(i < src_len)
			{
				if('"' == src[i] && '\\'!=src[i-1])
				{
					dst[j++] = src[i];
					break;
				}
				dst[j++] = src[i++];
			}
			
		}
		else if('\'' == src[i])
		{
			dst[j++] = src[i++];
			while(i < src_len)
			{
				if('\'' == src[i] && '\\'!=src[i-1])
				{
					dst[j++] = src[i];
					break;
				}
				dst[j++] = src[i++];
			}
		}
		else
		{
			dst[j++] = src[i];
		}
	}
	if(i == src_len-1)
	{
		dst[j++] = src[i];
	}
}

void Output()
{
	cout<<dst;
}

int main(int argc, char *argv[])
{
	Input();
	Delete_Comments();
	Output();
	return 0;
}

以下是shell脚本处理注释,还未考虑字符串中含注释符号的情况

#! /bin/bash

sed -e 's;//.*$;;g' -e 's;/\*.*\*/;;g' $1 |
	sed -e 's;\(/\*\);\n\1\n;g' -e 's;\(\*/\);\n\1\n;g' |
		sed -e '/\(\/\*\)/,/\(\*\/\)/d'
示例源文件如下
#include <iostream>
#include <cstdio>
using namespace std;

#define MACRO_NOTHING 

//asdfadfasdf

     
MACRO_NOTHING/*
//adfadf
 */MACRO_NOTHING

int main(void)
{
	char q1 = '\'';
	char q2 = '"';

	// this is where
	printf("//hello world\n");
	cout<<"hwat/*what is that*/as\"df"<<endl;
	/*
	 * shen ma
	 */
	cout<<"helo*/"<<endl;	
	return 0;
}


你可能感兴趣的:(删除源文件中的注释)