⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cifafenxi.cpp

📁 分析C程序中的词法,如是否为关键字或变量等.
💻 CPP
字号:
/************************************************************
实现文件lex.cpp
作者:孔路明 20053796
日期:2007.10.12
*************************************************************/
//只能读取‘{’与‘}’相匹配的一个函数
#include <iostream.h>
#include "global.h"
#include <stdio.h>

void init()//初始化符号表
{
	struct  entry* p  = symtable;
	int j=0;
	for( j =0; j<32; j++)
	{	
		p->lexptr = str[j].c_str();
		p->token = j+3;
		p++;
	}
}
/******************************************************************
功能:判断一个字符是否为数字
*******************************************************************/
bool IsDigit(char ch)
{
	return (ch >='0' && ch <= '9');
}
/******************************************************************
功能:判断一个字符是否确为字母
*******************************************************************/
bool IsAlpha(char ch)
{
	return ((ch >='a' && ch <= 'z')|| (ch>='A' && ch<='Z')||(ch=='_'));
}
/******************************************************************
功能:在关键字表中查找与arr相同的关键字
找到就返回类别编码,否则返回0
*******************************************************************/
int  FindOK(char* arr)
{
	for(int i=0; i<sizeof(symtable)/sizeof(symtable[0]); ++i)
	{
		if(!strcmp(symtable[i].lexptr ,arr))
		{
			return  symtable[i].token;
		}
	}
 return 0;
}
/********************************************************************
以下为主分析函数
从输入文件里面读,把分析结果写到输出文件中
参数:fpin :输入文件指针  fpout: 输出文件指针
********************************************************************/
void parse(FILE* fpin,FILE* fpout)
{
	char arr[MAXBUF];
	int i=0;
	int j=0;   //数字常量
    
	while(1)
	{
		fscanf(fpin,"%c",&ch);
	
		if(feof(fpin))
			break;
		else if( ch==' '|| ch =='\t')
			;
		else if( ch=='\n')
			lineno++;
		else if( IsDigit(ch))
		{
			while(IsDigit(ch))
			{
				arr[j] = ch;
				j++;
				fscanf(fpin,"%c",&ch);
			}
			
		    fseek(fpin,-1L,SEEK_CUR);//文件指针后退一个字节

			char* temp1 =(char*)malloc(j+1);
			memcpy(temp1,arr,j);
			temp1[j] ='\0';//把数组里面的内容拷贝到连外一个数组里面,因为我定义的
			//arr为255个字节,实际上写不到那么多,所以只拷贝实际上有数据的

			j=0;//恢复初始状态,以备下次使用
			fprintf(fpout,"%s\t\t%d,数值常量\n",temp1,2);//常数
			cout<<temp1<<"   "<<"2  数值常量"<<endl;
			free(temp1);//释放内存	
		}

		else if(IsAlpha(ch))//是字母开头的
		{	
			while(IsAlpha(ch) || IsDigit(ch))
			{
				arr[i] =ch;
				i++;
				fscanf(fpin,"%c",&ch);
			}
			fseek(fpin,-1L,SEEK_CUR);
			
			char* temp = (char*)malloc(i+1) ;
			memcpy(temp,arr,i);
			temp[i] ='\0';

			i=0;
			/*基本思想同处理数字的*/
		
			if(FindOK(temp))//查表
			{	
				fprintf(fpout,"%s\t\t%d,关键字 \n",temp,FindOK(temp));
				cout<<temp<<"  "<<FindOK(temp)<<"   关键字"<<endl;
			}
			else
			{
				fprintf(fpout,"%s\t\t%d,变量\n",temp,1);//标示符号
				cout<<temp<<"   1    变量"<<endl;
			}
			free(temp);
		}
	
		//以下为2字节的运算符号
		else if(ch=='>')
		{  
			fscanf(fpin,"%c",&ch);
			if(ch=='=')
			{fprintf(fpout,"%s\t\t%d,运算符\n",">=",46);
			cout<<">=   46   运算符"<<endl;}
			else
			{
				fprintf(fpout,">\t\t44,运算符\n");
				cout<<">   44   运算符"<<endl;
				//fseek(fpin,-1L,SEEK_CUR);
			}
		}
		else if( ch=='<')
		{
			fscanf(fpin,"%c",&ch);
			if(ch=='=')
			{fprintf(fpout,"<=\t\t45,运算符\n");
			cout<<"<=   45   运算符"<<endl;}
			else 
			{fprintf(fpout,"<\t\t43,运算符\n");
			fseek(fpin,-1L,SEEK_CUR);
			cout<<"<   43   运算符"<<endl;
			}
		}
		else if(ch=='!')
		{
			fscanf(fpin,"%c",&ch);
			if(ch='=')
			{fprintf(fpout,"!=\t\t42,运算符\n");
			cout<<"!=   42   运算符\n";}
		}
		else if(ch=='=') 
		{
			fscanf(fpin,"%c",&ch);
			if(ch=='=')
			{
			fprintf(fpout,"==\t\t41,运算符\n");
			cout<<"==   41    运算符"<<endl;
			}
			else
			{
			fprintf(fpout,"=\t\t40,运算符\n");
			cout<<"=   40    运算符"<<endl;
			fseek(fpin,-1L,SEEK_CUR);
			}
		}
		else if(ch=='"')
		{//引号中的所有字符均为字符常量				
			
			fprintf(fpout,"\"\t\t53,符号\n");
			cout<<"\"   53   符号"<<endl;
			fscanf(fpin,"%c",&ch);
			j=0;
			while(ch!='"')
			{
				if(ch=='\\')
				{
					
					fscanf(fpin,"%c",&ch);
					if(ch=='n')
					{
						cout<<"\\n     62     格式说明符"<<endl;
						fprintf(fpout,"\\n\t\t62,格式说明符\n");
					}
					else if(ch=='t')
					{
						cout<<"\\t     63     格式说明符"<<endl;
						fprintf(fpout,"\\t\t\t63,格式说明符\n");
					}
					j--;
				}
				else
					arr[j] = ch;
				j++;
				fscanf(fpin,"%c",&ch);
			}
				
				char* temp1 =(char*)malloc(j+1);
				memcpy(temp1,arr,j);
				temp1[j] ='\0';

				j=0;//恢复初始状态,以备下次使用
				fprintf(fpout,"%s\t\t%d,字符常量\n",temp1,61);//常数
				cout<<temp1<<"   61   字符常量"<<endl;
				free(temp1);//释放内存
				fprintf(fpout,"\"\t\t37,符号\n");
				cout<<"\"   37   符号"<<endl;
		}
		else if(ch=='-') 
		{
			fscanf(fpin,"%c",&ch);
			if(ch='>')
			{
				fprintf(fpout,"->\t\t23,运算符\n");
				cout<<"->   23    运算符"<<endl;
			}
			else
			{
				fprintf(fpout,"-\t\t36,运算符\n");
				cout<<"-   36    运算符"<<endl;
				fseek(fpin,-1L,SEEK_CUR);
			}
		}
		else 
		{
	   		//以下为一个字节的运算符号
			
			if(ch==';') {fprintf(fpout,";\t\t56,界符\n");
			cout<<";   56   界符"<<endl;
			continue;}
			if(ch=='+') {fprintf(fpout,"+\t\t35,运算符\n");
			cout<<"+   35    运算符"<<endl;
			continue;}
			if(ch=='*') {fprintf(fpout,"*\t\t37,运算符\n");
			cout<<"*   37    运算符"<<endl;
			continue;}
			if(ch=='\\') {fprintf(fpout,"\\ \t\t559,运算符\n");
			cout<<"\\   59    运算符"<<endl;
			continue;}
			if(ch=='&') {fprintf(fpout,"&\t\t60,运算符\n");
			cout<<"&   60    运算符"<<endl;
			continue;}
			if(ch=='(') {fprintf(fpout,"(\t\t47,界符\n");
			cout<<"(   47    界符"<<endl;
			continue;}
			if(ch==')') {fprintf(fpout,")\t\t48,界符\n");
			cout<<")   48    界符"<<endl;
			continue;}
			if(ch=='[') {fprintf(fpout,"[\t\t49,界符\n");
			cout<<"[   49    界符"<<endl;
			continue;}
			if(ch==']') {fprintf(fpout,"]\t\t50,界符\n");
			cout<<"]   50    界符"<<endl;
			continue;}
			if(ch=='.') {fprintf(fpout,".\t\t39,运算符\n");
			cout<<".   39    运算符"<<endl;
			continue;}
			if(ch==',') {fprintf(fpout,",\t\t55,界符\n");
			cout<<",   55    界符"<<endl;
			continue;}
			if(ch=='#') {fprintf(fpout,"#\t\t58,界符\n");
			cout<<"#   58    界符"<<endl;
			continue;}
			if(ch=='%') {fprintf(fpout,"%\t\t57,界符\n");
			cout<<"%   57    界符"<<endl;
			continue;}
			if(ch=='\'') {fprintf(fpout,"'\t\t,界符54\n");
			cout<<"'   54    界符"<<endl;
			continue;}
			if(ch=='{') {fprintf(fpout,"{\t\t51,界符\n");
			cout<<"{   51   界符"<<endl;
			continue;}
			if(ch=='}') {fprintf(fpout,"}\t\t52,界符\n");
			cout<<"}   52   界符"<<endl;
			continue;}
			else fprintf(fpout,"error in compileing %d lines unknown character %c \n",lineno,ch);//出错了
		}	
	}
}
/***************************************************************************
主函数
****************************************************************************/
int main()
{
	char filenamein[14],filenameout[14];
	printf("please input the filename of your source code(the name length can not exceed 25: \n");
	printf("(eg.the filename can be c:\\test.txt)\n");
	scanf("%s",filenamein);
	printf("please input the filename of your output file(the name length can not exceed 25: \n");
	printf("(eg. c:\\output.txt)\n");
	scanf("%s",filenameout);

	FILE* fpin = fopen(filenamein,"r");
	FILE* fpout =fopen(filenameout,"w");
	
	init();
	parse(fpin,fpout);

    fclose(fpin);
	fclose(fpout);
	printf("press any key to exit..........\n");
	getchar();
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -