请高手帮我编出一个c语言程序!急

这个程序要能读入任何一个c语言程序,并能打印出它里面的所有函数的函数名和函数名后面的参数。说起来很简单但做起来就感觉难,希望有高手能帮我给解决了。重谢!!!!!
3楼的一看就是高手啊!谢谢你的关注先!

不用判断无效代码,预处理里面的函数定义不用算。
不用算空格或者TAB,按最简单的方式来就行。
一行中也有可能出现多个函数。

还有什么需求方面的问题也可以提出来!

题有点大,先问一下什么需求吧!
1.无效代码用不用判断。(包括预处理部分和用注释的方式注掉的代码)还有,预处理里面的函数定义要不要算上?
2.要解析的文件中的函数定义有没有什么规范。比如int main( int a, char* b[] )这样,中间肯定有或者肯定没有空格或者TAB。
3.会不会出现一行中有多个函数的情况,比如:
int a(){return 0;}int b(){return 0;}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int strNoSpaceLen(char *strChkLen)
{
int iCount = 0;
int i;

for(i = 0; i < (int)strlen(strChkLen); i++)
{
if(strChkLen[i] == '\0')
{
break;
}
/*不计空格和TAB*/
else if(strChkLen[i] == ' '
|| strChkLen[i] == '\t' )
{
continue;
}
else
{
iCount++;
continue;
}
}

return iCount;
}

int sentenceChk(int *iCommendFlg, char *strSentence)
{
int i;
char *p;
int iNoSpaceLen;
int iCommendStart = -1;
int iQuotesStart = -1;

if(strlen(strSentence) == 0)
{
return 0;
}

/*去除块注释*/
if(strstr(strSentence,"/*")
|| strstr(strSentence,"*/"))
{
if(*iCommendFlg == 1)
{
iCommendStart = 0;
}
for(i = 1; strSentence[i] != '\0'; i++)
{
if(strSentence[i] == '*'
&& strSentence[i - 1] =='\/')
{
p = NULL;
p = strpbrk(strSentence, "/*");
*iCommendFlg = 1;
iCommendStart = i -1;
}
else if(strSentence[i] == '*'
&& strSentence[i + 1] =='\/')
{
if(iCommendStart >= 0)
{
strcpy(&strSentence[iCommendStart], &strSentence[i+2]);
*iCommendFlg = 0;
iCommendStart = -1;
}
}// end of if(p[i] == '*' && p[i + 1] =='\/')
}// end of for(...
}//end of if(strstr(strSentence...

/*去除行注释*/
p = strpbrk(strSentence, "//");
if(p != NULL)
{
if(p[1] == '\/')
{
*p = '\0';
}
}

/*去除引号里的内容*/
if(strstr(strSentence,"\"")
|| strstr(strSentence,"\'"))
{
for(i = 1; strSentence[i] != '\0'; i++)
{
if((strSentence[i] == '\"' || strSentence[i] == '\'') && strSentence[i-1] != '\\')
{
if(iQuotesStart == -1)
{
iQuotesStart = i;
}
else
{
strcpy(&strSentence[iQuotesStart], &strSentence[i+1]);
iQuotesStart = -1;
}//end of else
}//end of if(strSentence...
}//end of for(i = 1;...
}//end of if(strstr...

/*取得去除空格和TAB的字符串长度*/
iNoSpaceLen = strNoSpaceLen(strSentence);
if(iNoSpaceLen == 0)
{
return 0;
}
return 1;
}//end of int sentenceChk()

int strChk(int *iEnableFlg, int *iPretrLay, int *iCommendFlg, char *strSource)
{
int rc = 0;

/*头文件的引用和预处理的常数定义消除*/
if(strstr(strSource, "#include")
|| strstr(strSource, "#define"))
{
return 0;
}

/*条件编译的处理*/
/*预编译里面的#if 0的处理*/
else if(strstr(strSource, "#if 0"))
{
*iPretrLay = *iPretrLay +1;
*iEnableFlg = 0;
return 0;
}
/*预编译里面的#else的处理*/
else if(strstr(strSource, "#else"))
{
if(*iEnableFlg == 0)
{
if(*iPretrLay == 1)
{
*iEnableFlg = 1;
}
}
else
{
*iEnableFlg = 0;
}
return 0;
}
/*预编译里面的#if 1的处理*/
else if(strstr(strSource, "#if 1"))
{
*iPretrLay++;
return 0;
}

/*预编译里面的#endif的处理*/
else if(strstr(strSource, "#endif"))
{
*iPretrLay--;
if(iPretrLay == 0)
{
*iEnableFlg = 1;
}
return 0;
}

/*如果代码无效则返回0*/
else if(*iEnableFlg == 0)
{
return 0;
}

/*判定其他情况下代码是否有效*/
else if(*iEnableFlg == 1)
{
rc = sentenceChk(iCommendFlg, strSource);
if(rc != 1)
{
return 0;
}
}
return 1;
}

int delFun(int *iBracketLay, char *strFunSource)
{
int i;
int iBracketStart = -1;
int iNoSpaceLen;
char *p;

p = NULL;

if(strstr(strFunSource, "{") || strstr(strFunSource, "}"))
{
if(*iBracketLay > 0)
{
iBracketStart = 0;
}
for(i = 0; strFunSource[i] != '\0'; i++)
{
if(strFunSource[i] == '{')
{
*iBracketLay = *iBracketLay + 1;
strFunSource[i] = '\0';
iBracketStart = i;
}
if(strFunSource[i] == '}')
{
*iBracketLay = *iBracketLay - 1;
strFunSource[i] = '\0';
if(*iBracketLay == 0)
{
strcpy(&strFunSource[iBracketStart], &strFunSource[i+1]);
}
}
}
}
p = strpbrk(strFunSource, "}");

if(*iBracketLay > 0 && p == NULL)
{
strFunSource[0] = '\0';
}

iNoSpaceLen = strNoSpaceLen(strFunSource);
if(iNoSpaceLen == 0)
{
return 0;
}
return 1;
}

void main()
{
FILE *fp;

char strFileName[1024]; //文件名
char strTmp[1024]; //存储一行代码的内容
char ch;
int i = 0;
int ret; //0:不是函数 1:是函数
int bracketsCount = 0; //大括号的计数器
int iEnable = 1; //代码是否有效 0:无效 1:有效
int iPretrLay = 0; //条件编译的层数
int iCommendFlg = 0; //块注释 0:不是块注释 1:是块注释的一部分
int iBracketLay = 0; //大括号的层数
int iFunFlg = 1; //是否是函数的内容

/*字符串初始化*/
memset( strFileName, '\0', sizeof(char) * 1024 );
memset( strTmp, '\0', sizeof(char) * 1024 );

strcpy( strFileName, "D:\\test.txt" );

/*打开文件*/
if( NULL == (fp = fopen( strFileName, "r" ) ) )
{
printf( "%s cann't open.", strFileName );
}

while( !feof(fp) )
{

/*取得字符*/
ch = fgetc(fp);

if(ch == 10)
{
/*测试字符串是否符合输出要求*/
ret = strChk(&iEnable, &iPretrLay, &iCommendFlg, strTmp);
ret = ret * delFun(&iBracketLay, strTmp);
/*公共变量定义的消除*/
if(strstr(strTmp, "="))
{
ret = 0;
}
if(ret == 1)
{
printf("%s\n", strTmp);
}
memset(strTmp, '\0', sizeof(char) * 1024);
i = 0;
if(ret == 0)
{
continue;
}
}
else
{
strTmp[i] = ch;
i++;
}
}

printf("\n");

/*关闭文件*/
if(fp != NULL)
{
fclose(fp);
fp = NULL;
}
}

我只是简单测试了一下,你再看看吧!
有问题联系我!
温馨提示:答案为网友推荐,仅供参考
第1个回答  2008-03-21
你在写编译器吗?

相关了解……

你可能感兴趣的内容

大家正在搜

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网