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

📄 scaner.h

📁 编译原理课程试验
💻 H
字号:
struct libriry//编码表及初始化
{
	char name[20];
	int id;
};
libriry lib[30]=
{
	{"+",0},
	{"*",1},
	{"(",2},
	{")",3},
	{"{",4},
	{"}",5},
	{"<",6},
	{"=",7},
	{":",8},
	{";",9},
	{"and",10},
	{"int",11},
	{"true",12},
	{"while",13},
	{"if",14},
	{"else",15},
	{"id",16},
	{"num",17}
};
struct token//token字结构体
{
	int id;
    int name;
	struct token *next;
};
token *t_head=new token;
token *t1=t_head;
token *t2=NULL;
struct symbolist//符号表结构体定义
{
	int number;
	char name[20];
	int id;
	char type[5];
	int addr;
	struct symbolist *next;
};
symbolist *s_head=new symbolist;
symbolist *s1=s_head;
symbolist *s2=NULL;
//------------------------------------------------------------
char ch;//
int error_t=1;
int classes;//输入字符的类型
FILE *f1;
int k;//符号表入口
//---------------------------------------------------
void inital()
{
	for(k=0;k<13;k++)
	{
		s2=new symbolist;
		s2->id=lib[k].id;
		strcpy(s2->name,lib[k].name);
		s2->number=k+1;
		s1->next=s2;
		s1=s1->next;
		s2->next=NULL;
	}	
}
//-------------------------------------------------------
int lookup(char w[20])
{
	symbolist *i;
	i=s_head->next;
	while(i!=NULL)
	{
		if(!strcmp(w,i->name))
			return (i->number);
		i=i->next;
	}
	return (0);
}
//=-------------------------------------------------
void error()
{
	cout<<"error in line  "<<error_t<<endl;
	ch=fgetc(f1);
	return;
}
//---------------------------------------------------------------
void class_l()
{	
	if ((ch>='A')&&(ch<='Z')||(ch>='a'&&ch<='z')) //分类,识别字母
		classes=2;
	else if((ch>=48) && (ch<=57))
		classes=1;
	else if (((ch>=39) && (ch<=47)) || ((ch>57) && (ch<=62))||((ch>=123) && (ch<=125)))//符号
		classes=3;
	else if(ch=='\n') classes=5;
	else classes=4;//无效字符
	return;
}
//--------------------------------------------------------
void id_r()//识别标识符
{
	char word[20]="",clear[20]="";//装配单词
	int i=0,flag=0;
	while(ch!=' '&&ch!=EOF&&classes<=2)
	{
		word[i]=ch;
		i++;
		ch=fgetc(f1);
		class_l();
	}
	for(i=10;i<16;i++)//识别关键字
	{
		if(!strcmp(word,lib[i].name))
		{
			flag=1;
			i=lib[i].id;
			goto RE;
		}
	}
RE:t2=new token;//添token 字
   if(flag)//是关键字
   {
	   t2->id=i;
	   t2->name=' ';
   }
   else
   {
	   t2->id=16;
	   if(lookup(word)==0)
	   {
		   s2=new symbolist;
		   s2->id=16;
		   strcpy(s2->name,word);
		   s2->number=k+1;
		   t2->name=k+1;
		   k++;
		   s1->next=s2;
		   s1=s1->next;
		   s2->next=NULL;
	   }
	   else
		   t2->name=lookup(word);
   }
   t1->next=t2;
   t1=t1->next;
   t2->next=NULL;
   return;
}
//------------------------------------------------------------
void int_r()//数字
{
	char word[20]="";//装配单词
	int i=0;
	word[i]=ch;
	i++;
	ch=fgetc(f1);
	class_l();
	while(classes==1)
	{
		word[i]=ch;
		i++;
		ch=fgetc(f1);
		class_l();
	}
	t2=new token;//添token 字
	t2->id=17;
	if(lookup(word)==0)
	{
		s2=new symbolist;
		s2->id=17;
		strcpy(s2->name,word);
		s2->number=k+1;
		t2->name=k+1;
		k++;
		s1->next=s2;
		s1=s1->next;
		s2->next=NULL;
	}
	else
		t2->name=lookup(word);
	t1->next=t2;
	t1=t1->next;
	t2->next=NULL;
	return;
}
//-----------------------------------------------------------------------
void sla_r()//界符
{
	char word[20]="",clear[20]="";//装配单词
	int i=0;
	int j,flag=0;
	word[i]=ch;
	i++;
	if(ch=='/')//考虑注解
	{
		ch=fgetc(f1);
		if(ch=='*')
		{
			strcpy(word,clear);
			ch=fgetc(f1);
			while(1)
			{
				if(ch=='*')
					flag=1;//标志位
				ch=fgetc(f1);
				if(ch=='/'&&flag==1)
					break;
				else flag=0;
			}
			return;
		}
	}
	for(j=0;j<=9;j++)//识别界符
	{
		if(!strcmp(word,lib[j].name))//token
		{
			t2=new token;
			t2->id=lib[j].id;
			t2->name=' ';
			t1->next=t2;
			t1=t1->next;
			t2->next=NULL;
		}
		if(!strcmp(word,lib[j].name)) break;
	}
	clear[0]='=';
	clear[1]='\0';
	ch=fgetc(f1);
	return;
}
int scaner()//主程序--------------------------------------------------
{
	cout<<"***************************************************************************"<<endl;
	cout<<"**                       0203104班  张永亮(scaner)                       **"<<endl;
	cout<<"***************************************************************************"<<endl;
	char input[20]="";//输入文件名暂存
	int i=0;
	cout<<"please input the source file name,ending with # :";
	cin>>ch;
	while(i<20&&ch!='#')
	{
		input[i]=ch;
		i++;
		cin>>ch;
	}
	f1=fopen(input,"r");
	if (f1==NULL)//打开源文件失败
	{
		cerr<<"open source file error!"<<endl;
		exit(1);
	}
    inital();
	ch=fgetc(f1);
	while(ch==' ')
	{
		ch=fgetc(f1);
	}
	while(ch!=EOF)
	{
		while(ch==' ')
		{
			ch=fgetc(f1);
		}
		class_l();
		switch (classes)
		{
		case 1:int_r();break;
		case 2:id_r();break;
		case 3:sla_r();break;
		case 4:error();break;
        case 5:{error_t++;ch=fgetc(f1);break;}
		}
	}
	t2=new token;
	t2->id=18;
	t2->name='$';
	t1->next=t2;
	t1=t1->next;
	t2->next=NULL;
	fclose(f1);
	return 1;
}

⌨️ 快捷键说明

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