编译原理扫描程序,判断当前输入的程序中程序调用的函数是那个函数原型

l 任务描述
–分析C语言源程序的片段,判断程序末尾是否为一个函数调用语句。如果是函数调用,则输出被调用函数的原型,并判断程序的末尾正在输出第几个参数。
l 输入
–int foo(int a, unsigned char b) {
–  return 0;
–}
–Voidbar() {
–  foo(10,
l 输出:
–int foo(int, unsigned char)
–2
说明:
写的不是很好,因为是纯C写的,但是由于想偷个懒所以用了#include<iostream>请各路挑剔的大神原谅!
#include<string.h>
#include<stdlib.h>
#include<iostream>
using namespace std;
char *keyword[6]={"int","double","char","float","void","bool"};

struct parameter
{
	char *type[10];  //the type of parameters
	int no;          //the sum of parameters
};

struct function
{
	char *ftype;   //the type of function
	char *name;    //the name of function
	struct parameter PP;  //each function's parameters list
};

//create a table store the function in the Programme
struct function table[20];

FILE *fp;
bool Dflag=false;
bool Fflag=false;
bool Tflag=false;   //call f
char word[20];
char fname[25];  
char *funcname[20];  //each name of function
char *functype[20];  //each function's type
char *ptype[10];   //the type of parameters

int mm=0;  //the ptr of func
int nn=0;  //the ptr of parameters
int funnum=0;  

void isFunction(char ch)
{
	int k=0;
	//get keyword
	while((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
	{
		word[k++]=ch;
		ch=fgetc(fp);
	}
	word[k]='\0';
	fseek(fp,-1,1);

	int i;
	for(i=0;i<6;i++)
	{
		if(strcmp(word,keyword[i])==0)
		{
			Dflag=true;   
			break;
		}
	}

	//get definition name
	if(Dflag==true)
	{
		Dflag=false;
		ch=fgetc(fp);
		while(ch==' ')
		{
			ch=fgetc(fp);
		}
		i=0;
		while(ch!='(')
		{
			fname[i++]=ch;
			ch=fgetc(fp);
		}
		fname[i]='\0';
		if(ch=='(')
		{
			Fflag=true;
			funnum++;
			functype[mm]=(char *)malloc(10);
			memset(functype[mm],0,10);
			strcpy(functype[mm],word);
			funcname[mm]=(char *)malloc(25);
			memset(funcname[mm],0,25);
			strcpy(funcname[mm],fname);
			mm++;
		}
		else
		{
			Fflag=false;
		}
	}
	else
	{
	    //call f
		int k;
		for(k=0;k<funnum;k++)
		{
			if(strcmp(word,funcname[k])==0)
			{
				Tflag=true;
				//output this function
				cout<<table[k].ftype<<" "<<table[k].name<<"(";
				int m=0;
				for( m=0;m<table[k].PP.no-1;m++)
				{
					cout<<table[k].PP.type[m]<<",";
				}
				cout<<table[k].PP.type[m]<<")"<<endl;
				cout<<table[k].PP.no<<endl;
				break;
			}
		}
	 }
}
int  parameterScanner()
{
	char ch=fgetc(fp);
	int  i=0;
	char paratype[10];
	int pnum=0;
	while(ch!=')')
	{
		i=0;
		while(ch==' ')
		{
			ch=fgetc(fp);
		}
		while(ch!=' ')
		{
			paratype[i++]=ch;
			ch=fgetc(fp);
		}
		pnum++;
		paratype[i]='\0';
		ptype[nn]=(char *)malloc(10);
		memset(ptype[nn],0,10);
		strcpy(ptype[nn++],paratype);
		while( ch!=' ' )
		{
			ch=fgetc(fp);
		}
		while( ch!=',' )
		{
			ch=fgetc(fp);
			if( ch==')' )
				break;
		}
		
		if( ch==')' )
		{
			return pnum ;
		}
		else
		{
			ch=fgetc(fp);
		}
	}
	return pnum;
}
int main(int argc,char *argv[])
{
	char ch;
	int  i=0;
	if((fp=fopen("Pro.c","r"))==NULL)
	{
		cout<<"Can not open the file!"<<endl;
		exit(1);
	}
	else
	{
		ch=fgetc(fp);
		while(ch!=EOF)
		{
			if(ch=='i'||ch=='d'||ch=='c'||ch=='f'||ch=='v'||ch=='b')
			{
				isFunction(ch);
				if(Fflag=true)
				{
                     Fflag=false;
					 table[mm-1].ftype=(char *)malloc(10);
					 memset(table[mm-1].ftype,0,10);
					 strcpy(table[mm-1].ftype,functype[mm-1]);
					 table[mm-1].name=(char *)malloc(20);
					 memset(table[mm-1].name,0,20);
					 strcpy(table[mm-1].name,funcname[mm-1]);
					 table[mm-1].PP.no=parameterScanner();
					 for(i=0;i<table[mm-1].PP.no;i++)
					 {
						  table[mm-1].PP.type[i]=(char *)malloc(20);
						  memset(table[mm-1].PP.type[i],0,20);
						  strcpy(table[mm-1].PP.type[i],ptype[i]);
					 }	
				}
			}
			ch=fgetc(fp);
		}
	}
	system("Pause");
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值