www.pudn.com > bianyi课程设计.rar > b.cpp


#include 
#include 
#include 
#include 
#include 
 
/*********************保留字表*************************/ 
struct reserve                          
{ 
	char name[10]; 
	int num; 
}reser[8]={"if",1,"then",2,"else",3,"end",4,"repeat",5,"until",6,"read",7,"write",8}; 
 
/*********************词法分析的结果链表结点****************/ 
typedef struct Lnode    
{ 
	int code; 
	int value; 
	Lnode *next; 
}Lnode; 
 
/**********************标识符表结点**********************/ 
typedef struct node    
{ 
	char id[10];                            
	int value; 
	node *next; 
}node; 
 
 
void Getchar(char &ch,int &i,char string[]) 
{ 
	ch=string[i]; 
	i++; 
} 
 
void getBC(char &ch,int &i,char string[])   //注意:该函数使用之前必须先用getchar()函数 
{ 
	while(ch==' ') 
		Getchar(ch,i,string); 
} 
 
void concat(char ch,int &j,char strtoken[]) 
{ 
	strtoken[j]=ch; 
	j++; 
} 
 
bool isletter(char ch) 
{ 
	if((ch<='z'&&ch>='a')||(ch<='Z'&&ch>='A')) 
		return true; 
	else return false; 
} 
 
bool isdigit(char ch) 
{ 
	if(ch<='9'&&ch>='0') return true; 
	else return false; 
} 
 
void Retract(int &i,char &ch) 
{ 
	i--; 
	ch=' '; 
} 
 
 
/**************在保留字表中查找strtoken中的字符串,判断是保留字还是标识符**************/ 
int Reserve(char strtoken[],reserve reser[])//在保留字表中查找strtoken中的字符串,判断是保留字还是标识符 
{ 
	int m=1; 
	int i=0;                              
	while(m==1&&i<8) 
	{ 
		if(strcmp(strtoken,reser[i].name)==0)  m=0; 
		else i++; 
	} 
	if(i==8) return 0; 
	else return(reser[i].num); 
} 
 
 
/*******************将扫描到的字符串表示的数字转换成int的数字*************************/ 
int change(char ch[])             
{ 
	int c=0; 
	int i=0; 
	while(ch[i]!=0) 
	{ 
		c=10*c+(ch[i]-48); 
		i++; 
	} 
	return c; 
} 
 
	 
/**************************创建标识符表头结点************************************/ 
void createsymlist(node* &symlist)          
{ 
	symlist=(node *)malloc(sizeof(node)); 
	symlist->next=NULL; 
	symlist->value=0; 
} 
 
 
/************************将新标识符结点插入标识符表中****************************/ 
int insertId(node* &symlist,node* &q,char strtoken[]) 
{                                           
	node *s; 
	s=(node *)malloc(sizeof(node)); 
	s->value=q->value+1; 
	strcpy(s->id,strtoken); 
	q->next=s; 
	s->next=NULL; 
	q=q->next; 
	return(s->value); 
} 
 
 
/************************创建词法分析结果的输出表********************************/ 
void createlist(Lnode* &List)            
{ 
	List=(Lnode*)malloc(sizeof(Lnode)); 
	List->next=NULL; 
} 
 
 
/***************************将新的单词插入结果链表***************************************/ 
void insertlist(Lnode* &List,Lnode* &p,int code,int value) 
{                                        
	Lnode *s; 
	s=(Lnode *)malloc(sizeof(Lnode)); 
	s->code=code; 
	s->value=value; 
	p->next=s; 
	s->next=NULL; 
	p=p->next; 
} 
 
 
/****************************词法分析构成结果链表***************************************/ 
void word(node* &symlist,node* &q,Lnode* &List,Lnode* &p,char string[],int &i) 
{ 
	char strtoken[10]=" "; 
	char ch; 
	int j=0; 
	int code=0; 
	int value=0; 
	Getchar(ch,i,string); 
	getBC(ch,i,string); 
	if(isletter(ch)) 
	{ 
		while(isletter(ch)||isdigit(ch)) 
		{ 
			concat(ch,j,strtoken); 
			Getchar(ch,i,string); 
		} 
		Retract(i,ch);         
		value=Reserve(strtoken,reser); 
		if(value==0) 
		{ 
			value=insertId(symlist,q,strtoken); 
			code=4; 
		} 
		else code=1; 
	} 
	else if(isdigit(ch)) 
	{ 
		while(isdigit(ch)) 
		{ 
			concat(ch,j,strtoken); 
			Getchar(ch,i,string); 
		} 
		Retract(i,ch); 
		code=3; 
		value=change(strtoken); 
	} 
	else if(ch==':') 
	{ 
		Getchar(ch,i,string); 
		if(ch=='=') 
		{ 
			code=2; 
			value=10; 
		} 
		else{ 
			Retract(i,ch); 
			cout<<"error"<next; 
	cout<id<next; 
	cout<code==1) 
		{ 
			cout<<"                 "<<"reserved word:"; 
		    reserout(q->value); 
		}  
		else if(q->code==2) 
		{ 
			cout<<"                 "<<"symbol:"; 
		    symout(q->value); 
		} 
		else if(q->code==3) cout<<"                 "<<"integer:"<value<