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

📄 ifrename.cpp

📁 该程序是一个MiniPascal语言的编译器
💻 CPP
字号:
#include<string.h>
#include<iostream.h>

#include "list.h"
#include "global.h"
#include "keyword.h"
//**********************************************************************************************//

//					在语义处理过程中进行相应的判断
//包括同名检查,向前看符号的类型判断等

//**********************************************************************************************//

//#include"lex.cpp"
//#include"search.cpp"
void match(int);
int search(char name[SIZE],struct proc_list* pro ,struct var_list* var);
//char to int
int AsciiToInt(char c){
	return int(c)-48;
}
//convert token to int
int CharToInt(){
    int count,i,sum,bit,multi;
	for(count=0;token[count]!=0;count++);
	for(sum=0,i=count-1,multi=1;i>=0;i--){
		bit=AsciiToInt(token[i]);
        sum=sum+bit*multi;
		multi*=10;
	}
	return sum;
}
//判断是否关系符
bool isrelational_op(int lookahead){
	if((lookahead<=relop_GE)&&(lookahead>=relop_LT))
		return true;
	return false;
}
//检查是否为常数,ConstType携带相应信息返回
bool isconst(int& ConstType){ //      < 常数 > number/ Const
	var_list* var=new var_list;
	proc_list* pro=new proc_list;
	int flag=0;
	if(lookahead==minus_op)
	{
		flag=1;
		match(minus_op);
	}
	if(lookahead==number)
	{
		if(flag==0)
			ConstVal=CharToInt();//token
		else ConstVal=-CharToInt();
		match(number);            //
		ConstType=1;//数字
		return true;
	}
	if(lookahead==True)
	{
		match(True);
		ConstType=0;//of bool
		ConstVal=1;
		return true;
	}
	if(lookahead==False){
		match(False);
		ConstType=0;//
		ConstVal=0;
		return true;
	}
	if(search(token,pro,var)==1)
	{//const/bool/int/new type
		if(var->type==Const)
		{
			match(id);//
			ConstType=var->typeofconst;//0:bool   1:int
			ConstVal=var->ConstVal;
			return true;
		}
	}
	return false;
}
bool type_rename(char Typename[SIZE]){//在同一过程中检查新类型是否同名
	int numofnewtype=cur_proc->numofnewtype;
	int i;
	for(i=0;i<numofnewtype;i++){
		if(strcmp(Typename,typelist[cur_proc->NewType[i]].name)==0)
			return true;
	}
	return false;
}
bool field_rename(char fieldname[SIZE]){//在同一个域中检查变量名是否同名
	field_table* temp=new field_table;
	for(temp=typelist[typecount].fieldlist;temp->type>0;temp=temp->next)
		if(strcmp(temp->name,fieldname)==0)
			return true;
	return false;
}
bool var_rename(char varname[SIZE]){//在同一过程中检查变量是否同名
	var_list* tempc=new var_list;
	for(tempc=cur_proc->next;tempc->type>=0;tempc=tempc->next){
		if(strcmp(tempc->name,varname)==0){
			return true;//var_rename
		}
	}
	return false;
}
bool proc_rename(char name[SIZE]){//检查过程是否重名
	int i;
	proc_list* tempf=new proc_list;

	tempf=cur_proc;
	proc_list* tempc=new proc_list;
	if(!strcmp(tempf->name,name))
		return true;
	for(i=0,tempc=tempf->child[0];i<tempf->numofchild;i++,tempc=tempf->child[i])
	{//递归检查子过程

		if(proc_rename(name))
			return true;
	}
	return false;
}

⌨️ 快捷键说明

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