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

📄 code.c

📁 编译原理词法分析器,生成二元式的代码!适合于大学计算机科学与技术实验设计。
💻 C
字号:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define NULL 0
FILE *fp;//文件指针
//char ch;
char str;
char *keyword[8]={"if","else","break","for","while","do","return","continue"};//建立关键字指针数组
//char *opratornum[4]={"-","+","*","/"};
//char *comparison[5]={">","<","=","&&","||"};
//char *interpounction[6]={"{","}",";","(",")",","};


int search(char searchstr[],int wordtype) //标识符匹配
{ 

 int p;
 int i=0; 
 switch (wordtype) 
 { 
 case 1:
	 for(i=0;i<=7;i++) 
	 { 
		 if(strcmp(keyword[i],searchstr)==0) //字符串比较
		 {p=i+1; break;}
		 else p=0;
	 }	   
 }
 return(p);
} 
		/*
 case 2:
	 for(i=0;i<=3;i++) 
	 { 
		 if(strcmp(opratornum[i],searchstr)==0) 
			 return(1);
	 } 
	 break; 
 case 3: 
	 for(i=0;i<=5;i++) 
	 { 
		 if(strcmp(comparison[i],searchstr)==0) 
			 return(1); 
	 } 
		 break;
 case 4:
	 for(i=0;i<=5;i++) 
	 { 
		 if(strcmp(interpounction[i],searchstr)==0) 
			 return(1); 
	 } 
		break;
 */


char letterprocess(char ch)//字母处理函数
{
	int i=-1;
	char letter[20];
	while(isalnum(ch)!=0||ch=='_')//读取到的为数字或字母或下划线
	{
		letter[++i]=ch;
		ch=fgetc(fp);
	};
	letter[i+1]='\0';
	if(search(letter,1))//查关键字表,找到为关键字,否则为标识符
	{
		printf("(%s,1)\n",letter);
	}else{printf("(%s,2)\n",letter);}
	return(ch);
}

char numberprocess(char ch)//数字处理函数
{
	int i=-1;
	char number[20];
	while(isdigit(ch)||ch=='.')//读取为数字或小数点
	{
		number[++i]=ch;
		ch=fgetc(fp);
	}
	number[i+1]='\0';
	printf("(%s,3)\n",number);
	return (ch);


/*	if(ch!='.')//如果当前读入的不为小数点,则常数读完
	{
		
	    printf("(%s,3)\n",number);
	}
	else//如果是小数点则继续往下读
	{
		number[i++]=ch;
		ch=fgetc(fp);
		while(isdigit(ch)!=0)
		{
			number[i++]=ch;
			ch=fgetc(fp);
		}
		number[i++]='\0';
		printf("(%s,3)\n",number);
		//while//...........
	return(ch);
	}*/
}
	

char otherprocess(char ch){//处理非数字和字母函数
	char other[20];
	other[0]=ch;
	other[1]='\0';
	if(ch=='{'||ch=='}'||ch=='('||ch==')'||ch==','||ch==';'||ch=='#'||ch=='"'||ch=='.')//分界符识别
	{
		printf("(%s,5)\n",other);
		ch=fgetc(fp);
		return(ch);
	}
	else if(ch=='+'||ch=='*'||ch=='/'||ch=='%')//操作运算符识别
	{
		printf("(%s,4)\n",other);
		ch=fgetc(fp);
		return(ch);
	}else if(ch=='<'||ch=='>'||ch=='='||ch=='!')//比较运算符识别
	{
		ch=fgetc(fp);//超前阅读,取其下一个字符,如为‘=’则将其写入数组中,否则只输出当前读取的字符
		if(ch=='=')
		{
			other[1]=ch;
			other[2]='\0';
			printf("(%s,4)\n",other);
			return(ch);
		}
		else
		{
			printf("(%s,4)\n",other);
			return ch;
		}

	}else if(ch=='&')//识别‘&’与‘&&’
	{
		ch=fgetc(fp);//超前阅读
		if(ch=='&')//如果其后一个字符为‘&’,则将其后一个字符也写入数组中并输出
		{
			other[1]=ch;
			other[2]='\0';
			printf("(%s,4)\n",other);
			return(ch);
		}else
		{
			printf("(%s,4)\n",other);
			return(ch);
		}
	}else if(ch=='\\')//识别‘\n’
	{
		ch=fgetc(fp);
		if(ch=='n')
		{
			other[1]=ch;
			other[2]='\0';
			printf("(%s,5)\n",other);
			return(ch);
		}
	}else if(ch=='-')//判断‘-’是负号还是减号
	{
		ch=fgetc((fp-1));//取其前一个字符,如果为数字则为减号,否则为负号
		if(isdigit(ch))
		{
			ch=fgetc((fp+1));//指针回指到下一个字符,取下一个字符
			other[1]=ch;
			other[2]='\0';
			printf("(%s,4)\n",other);
			return(ch);
		}else
		{
			other[1]=ch;
			ch=fgetc(fp);
			other[2]=ch;
			other[3]='\0';
			printf("(%s,3)\n",other);
			ch=fgetc(fp);
			return(ch);
		}
	}


	return ch;
}


void main()
{

	
	//char letterprocess(char ch);
	//char numberprocess(char ch);
	//bool search(char searchstr[],int wordtype);

	if((fp=fopen("source.c","r"))==NULL)//以读取的方式打开文件
	{
		printf("cannot open file\n");//打开失败时直接结束
		exit(0);
	}
	else
	{
		
		str = fgetc(fp);
		while(str!=EOF)
		{
			if(str==' '||str=='\n')//掠过空格和换行
	          str =fgetc(fp);
			else if (isalpha(str)){//如为数字或字母对调用字母处理函数
				str =letterprocess(str);
				//printf("fdfd");
				}
			else if (isdigit(str))
				str =numberprocess(str);//调用数字处理函数
			else str =otherprocess(str);//调用其他非数字和字母函数
		}
	}
	fclose(fp);
}

⌨️ 快捷键说明

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