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

📄 yufa.cpp

📁 自定义简单语法类C语言编译器
💻 CPP
📖 第 1 页 / 共 4 页
字号:
		curnode->child[0]=curnode1;
		curnode->child[1]=curnode2;
		rootnode=curnode;
		return rootnode;
	}
	else return curnode1;

}

//relop→ < | <= | > | >= | == | !=
NodeList  relop()
{
	NodeList curnode=CreateTreeNode();
	tok[yf_wordnum].str.copy(curnode->nodestr,tok[yf_wordnum].str.length());
	curnode->lineno=tok[yf_wordnum-1].line;
	switch(tok[yf_wordnum].ttype)
	{
	case LT:
		match(LT);
		curnode->ntype=RLT;
		break;
	case LTEQ:
		match(LTEQ);
		curnode->ntype=RNGT;
		break;
	case GT:
		match(GT);
		curnode->ntype=RGT;
		break;
	case GTEQ:
		match(GTEQ);
		curnode->ntype=RNLT;
		break;
	case EQ:
		match(EQ);
		curnode->ntype=REQ;
		break;
	case NEQ:
		match(NEQ);
		curnode->ntype=RNEQ;
		break;
	}
	return curnode;
}

//additive-expression->term { ( + | - ) term }
NodeList  additive_expression()
{
	NodeList curnode=CreateTreeNode();
	NodeList tempnode=CreateTreeNode();
	curnode=term();
	tempnode=curnode;
	while(PLUS==tok[yf_wordnum].ttype||MINUS==tok[yf_wordnum].ttype)
	{
		NodeList curnode1=CreateTreeNode();
		tok[yf_wordnum].str.copy(curnode1->nodestr,tok[yf_wordnum].str.length());
		curnode1->lineno=tok[yf_wordnum].line;
		if(PLUS==tok[yf_wordnum].ttype)
		{
			match(PLUS);
			curnode1->ntype=ADD;
		
		}
	    else if(MINUS==tok[yf_wordnum].ttype)
		{
			match(MINUS);
			curnode1->ntype=SUB;
		}
		NodeList curnode2=CreateTreeNode();
		curnode2=term();
		curnode1->child[0]=tempnode;
		curnode1->child[1]=curnode2;
		tempnode=curnode1;
	}
	return tempnode;
}

//term->factor { ( * | / ) factor }
NodeList term()
{
	NodeList curnode=CreateTreeNode();
	NodeList tempnode=CreateTreeNode();
	curnode=factor();
	tempnode=curnode;
	while(STAR==tok[yf_wordnum].ttype||SLASH==tok[yf_wordnum].ttype)
	{
		NodeList curnode1=CreateTreeNode();
		tok[yf_wordnum].str.copy(curnode1->nodestr,tok[yf_wordnum].str.length());
		curnode1->lineno=tok[yf_wordnum].line;
		if(STAR==tok[yf_wordnum].ttype)
		{
			match(STAR);
			curnode1->ntype=MUL;
		}
		else if(SLASH==tok[yf_wordnum].ttype)
		{
			match(SLASH);
			curnode1->ntype=DIV;
		}
		NodeList curnode2=CreateTreeNode();
		curnode2=factor();
		curnode1->child[0]=tempnode;
		curnode1->child[1]=curnode2;
		tempnode=curnode1;
	}
	return tempnode;
}

//factor→ ( expression )| var | call | NUM
NodeList factor()
{
	NodeList curnode=CreateTreeNode();
	if(tok[yf_wordnum].ttype==LPAREN)
	{
			match(LPAREN);
			curnode=exp();
			match(RPAREN);
	}
	else if(tok[yf_wordnum].ttype==NUMBER)
	{
		match(NUMBER);
		curnode->lineno=tok[yf_wordnum-1].line;
		curnode->ntype=ConstID;
		tok[yf_wordnum-1].str.copy(curnode->nodestr,tok[yf_wordnum-1].str.length());
		curnode->ntype=ConstID;
	}	
	else if(tok[yf_wordnum].ttype==ID&&tok[yf_wordnum+1].ttype==LPAREN)
	{
		curnode=call();
		curnode->ntype=FunCall;
	}
	else{
		curnode=var();
	}
	return curnode;
}
	
//call→ ID( args )
NodeList call()
{
	match(ID);
	NodeList curnode=CreateTreeNode();
	curnode->ntype=FunCall;
	tok[yf_wordnum-1].str.copy(curnode->nodestr,tok[yf_wordnum-1].str.length());
	curnode->lineno=tok[yf_wordnum-1].line;
	if(tok[yf_wordnum].ttype==LPAREN)
	{
		match(LPAREN);
		NodeList curnode1=CreateTreeNode();
		curnode1=args();
		match(RPAREN);
		curnode->child[0]=curnode1;
	}
	return curnode;
}

//args->[ expression { , expression } ]
NodeList  args()
{
	NodeList curnode=CreateTreeNode();
	NodeList tempnode=curnode;
	if(tok[yf_wordnum].ttype==ID||tok[yf_wordnum].ttype==NUMBER)
	{
		tempnode->sibling=exp();
		while(tok[yf_wordnum].ttype==COMMA)
		{
			match(COMMA);
			NodeList curnode1=CreateTreeNode();
			curnode1=exp();
			while(tempnode->sibling!=0)
				tempnode=tempnode->sibling;
			tempnode->sibling=curnode1;
			tempnode=curnode1;
		}
	}
	curnode=curnode->sibling;
	return curnode;
}

//program->{var-declaration | fun-declaration }
NodeList  program()
{
	NodeList rootnode=CreateTreeNode();
	NodeList tempnode=rootnode;
	while(tok[yf_wordnum].ttype==INT||tok[yf_wordnum].ttype==VOID)
	{
		NodeList curnode=CreateTreeNode();
		if(tok[yf_wordnum].ttype==VOID)
		{
			curnode=fun_decla();
		}
		else if(yf_wordnum<=wordnum-2)
					if(tok[yf_wordnum+2].ttype==LPAREN)
					curnode=fun_decla();
					else  curnode=var_decla();
		tempnode->sibling=curnode;
		tempnode=curnode;
	}
	if(yf_wordnum<wordnum)
	{
		printerror();
	}
	return rootnode;
}

//插入全局变量
int insertgloablevar(NodeList root)
{
	int i=0;
	while(strlen(gloablevarlist[i].varname)!=0)
	{		if(strcmp(root->nodestr,gloablevarlist[i].varname)==0)
		{
			cout<<"line"<<root->lineno<<":"<<root->nodestr<<"重复定义"<<endl;
			exit(1);
		}
		i++;
	}
	strcpy(gloablevarlist[gloablevarnum].varname,root->nodestr);
	strcpy(gloablevarlist[gloablevarnum].vartype,root->vartype);
	gloablevarlist[gloablevarnum].isarray=root->isarray;
	if(root->isarray)
	{
		gloablevarlist[gloablevarnum].arraysize=atoi(root->child[0]->nodestr);
	}
		gloablevarlist[gloablevarnum].Rva=gloablevarnum;
	return 1;
}

//插入形参变量
int	insertpara(NodeList	root)
{
	int i=0;
	while(strlen(funlist[funnum].pvartable[i].varname)!=0)
	{
		if(strcmp(root->nodestr,funlist[i].pvartable[i].varname)==0)
		{
			cout<<"line"<<root->lineno<<":"<<root->nodestr<<"重复定义"<<endl;
			exit(1);
		}
		i++;
	}	
	funlist[funnum].size++;
	strcpy(funlist[funnum].pvartable[paranum].varname,root->nodestr);
	strcpy(funlist[funnum].pvartable[paranum].vartype,root->vartype);
	funlist[funnum].pvartable[paranum].isarray=root->isarray;
	if(root->isarray)
	{
		funlist[funnum].pvartable[paranum].arraysize=atoi(root->child[0]->nodestr);
		funlist[funnum].size+=atoi(root->child[0]->nodestr);
	}
	return 1;
}

//插入局部变量
int	insertvar(NodeList root)
{
	int i=0;
	while(strlen(funlist[funnum].pvartable[i].varname)!=0)
	{
		if(strcmp(root->nodestr,funlist[funnum].pvartable[i].varname)==0)
		{
			cout<<"line"<<root->lineno<<":"<<root->nodestr<<"重复定义"<<endl;
			exit(1);
		}
		i++;
	}
		funlist[funnum].size++;
		strcpy(funlist[funnum].pvartable[paranum+varnum].varname,root->nodestr);
		strcpy(funlist[funnum].pvartable[paranum+varnum].vartype,root->vartype);
		funlist[funnum].pvartable[paranum+varnum].isarray=root->isarray;
		funlist[funnum].pvartable[paranum+varnum].Rva=varnum;
		if(root->isarray)
		{
			funlist[funnum].pvartable[paranum+varnum].arraysize=atoi(root->child[0]->nodestr);
		}
	return 1;
}
//把函数信息插入函数表
int insertfun(NodeList root)
{
	int i=0;
	while(strlen(funlist[i].funname)!=0)
	{
		if(strcmp(root->nodestr,funlist[i].funname)==0)
		{
			cout<<"line"<<root->lineno<<":"<<root->nodestr<<"重复定义"<<endl;
			exit(1);
		}
		i++;
	}
	funlist[funnum].FunId=funnum;
	strcpy(funlist[funnum].funname,root->nodestr);
	funlist[funnum].localVarSize=varnum;
	funlist[funnum].paraVarSize=paranum;
	strcpy(	funlist[funnum].rettype,root->rettype);
	return 1;
}

//输出节点信息(语法分析)
void printtree(NodeList node)
{
	//int i;
	cout<<node->nodestr<<endl;
//	for(i=0;i<=treeheight;i++)
//		yfoutfile<<"  ";
	switch(node->ntype)
	{
	case VarID:
		yfoutfile<<"<tree line=\""<<node->lineno
			<<"\" nodetype=\""<<nodetype[node->ntype]<<"\" nodestring=\""<<node->nodestr
			<<"\" isArray=\""<<node->isarray<<"\" />"<<"\n	";
		break;
	case VarDecl:
		yfoutfile<<"<tree line=\""<<node->lineno
			<<"\" nodetype=\""<<nodetype[node->ntype]<<"\" nodestring=\""<<node->nodestr
			<<"\" vartype=\""<<node->vartype<<"\" isArray=\""<<node->isarray<<"\" />"<<"\n	";
		break;
	case Para:
		yfoutfile<<"<tree line=\""<<node->lineno
			<<"\" nodetype=\""<<nodetype[node->ntype]<<"\" nodestring=\""<<node->nodestr
			<<"\" vartype=\""<<node->vartype<<"\" isArray=\""<<node->isarray<<"\" />"<<"\n	";
		break;
	case FunDecl:
		yfoutfile<<"<tree line=\""<<node->lineno
			<<"\" nodetype=\""<<nodetype[node->ntype]<<"\" nodestring=\""<<node->nodestr
			<<"\" rettype=\""<<node->rettype<<"\" >"<<"\n	";
		break;
	case ConstID:
		yfoutfile<<"<tree line=\""<<node->lineno
			<<"\" nodetype=\""<<nodetype[node->ntype]<<"\" nodestring=\""<<node->nodestr
			<<"\" />"<<"\n	";
		break;
	case OTHER:
		cout<<node;
		break;
	default:	
		yfoutfile<<"<tree line=\""<<node->lineno
			<<"\" nodetype=\""<<nodetype[node->ntype]<<"\" nodestring=\""<<node->nodestr
			<<"\" >"<<"\n	";
	}
}

//输出符号表
void printsymtable()
{
	int i=0;
	if(strlen(gloablevarlist[0].varname)!=0)
		sym_table<<"<tree>";
	while(strlen(gloablevarlist[i].varname)!=0)
	{
		sym_table<<"<child varName=\""<<gloablevarlist[i].varname
			<<"\" varType=\""<<gloablevarlist[i].vartype<<"\" isArray=\""<<gloablevarlist[i].isarray
			<<"\" arraysize=\""<<gloablevarlist[i].arraysize<<"\" rva=\""<<gloablevarlist[i].Rva<<"\" />"<<"\n	";
		i++;
	}
	if(strlen(gloablevarlist[0].varname)!=0)
		sym_table<<"</tree>";
	i=0;
	//输出函数信息
	while(strlen(funlist[i].funname)!=0)
	{
		sym_table<<"<tree>\n";
		sym_table<<"<tree funID=\""<<funlist[i].FunId
			<<"\" funName=\""<<funlist[i].funname<<"\" retType=\""<<funlist[i].rettype
			<<"\" paraVarSize=\""<<funlist[i].paraVarSize
			<<"\" localVarSize=\""<<funlist[i].localVarSize<<"\" />"<<"\n	";
		//输出参数变量
		int j=0;
		if(strlen(funlist[i].pvartable[0].varname)!=0)
		sym_table<<"<paraVar>";
		while(j<funlist[i].paraVarSize)
		{
			sym_table<<"<child varName=\""<<funlist[i].pvartable[j].varname
				<<"\" varType=\""<<funlist[i].pvartable[j].vartype<<"\" isArray=\""
				<<funlist[i].pvartable[j].isarray	<<"\" arraysize=\""<<funlist[i].pvartable[j].arraysize
				<<"\" rva=\""<<funlist[i].pvartable[j].Rva<<"\" />"<<"\n	";
			j++;
		}
		if(strlen(funlist[i].pvartable[0].varname)!=0)
		sym_table<<"</paraVar>";
		//输出局部变量
		int k=0;
		if(strlen(funlist[i].pvartable[funlist[i].paraVarSize].varname)!=0)
			sym_table<<"<localVar>";
		while(strlen(funlist[i].pvartable[k+funlist[i].paraVarSize].varname)!=0)
		{
				sym_table<<"<child varName=\""
				<<funlist[i].pvartable[k+funlist[i].paraVarSize].varname	<<"\" varType=\""
				<<funlist[i].pvartable[k+funlist[i].paraVarSize].vartype<<"\" isArray=\""
				<<funlist[i].pvartable[k+funlist[i].paraVarSize].isarray<<"\" arraysize=\""
				<<funlist[i].pvartable[k+funlist[i].paraVarSize].arraysize<<"\" rva=\""
				<<funlist[i].pvartable[k+funlist[i].paraVarSize].Rva<<"\" />"<<"\n	";
			k++;
	}
		if(strlen(funlist[i].pvartable[funlist[i].paraVarSize].varname)!=0)
		sym_table<<"</localVar>";
		sym_table<<"</tree>";
		i++;
	}
}

//查找变量索引值,若不存在,指出错误并退出程序
int searchvar(NodeList root,int funnum)
	{
		int i=0;
		while(strlen(funlist[funnum].pvartable[i].varname)!=0)
		{
			if(strcmp(root->nodestr,funlist[funnum].pvartable[i].varname)==0)
			{
				return i;
			}
			i++;
		}
		i=0;
		while(strlen(gloablevarlist[i].varname)!=0)
		{
			if(strcmp(root->nodestr,gloablevarlist[i].varname)==0)
			{
				return 100+i;//为说明是否全局变量
			}
			i++;
		}
	cout<<"line"<<root->lineno<<":"<<root->nodestr<<"没有定义"<<endl;
	//exit(1);
	}

//查找函数索引值,若不存在,指出错误并退出程序
int searchfun(NodeList root)
{
	int i=0;
	if(strcmp(root->nodestr,curfunname)==0)
		return funnum;
	while(strlen(funlist[i].funname)!=0)
	{
		if(strcmp(root->nodestr,funlist[i].funname)==0)
		{
			return i;
		}
		i++;
	}
	cout<<"line"<<root->lineno<<":"<<root->nodestr<<"没有定义"<<endl;
	exit(1);
}

//生成代码时查找函数索引值
int geningsearchfun(NodeList root)
{
	int i=0;
	while(strlen(funlist[i].funname)!=0)
	{
	if(strcmp(root->nodestr,funlist[i].funname)==0)
	{
		return i;
	}
	i++;
	}
}
//先序遍历抽象语法树
void ScanTree(NodeList root)
{
	if(root)
	{
		if(root->ntype==IfStm||root->ntype==WhileStm||root->ntype==ADD||root->ntype==SUB
			||root->ntype==MUL||root->ntype==DIV||root->ntype==REQ||root->ntype==RLT||
			root->ntype==RGT||root->ntype==RNEQ||root->ntype==RNGT||root->ntype== RNLT||
			root->ntype==AssignStm)
		{
			printtree(root);
			if(root->child[0]!=0)
				{	yfoutfile<<"<child>\n";
			ScanTree(root->child[0]);
			yfoutfile<<"</child>\n";
			}
			if(root->child[1]!=0)
				{
			yfoutfile<<"<child>\n";
			ScanTree(root->child[1]);
			yfoutfile<<"</child>\n";
			}
			yfoutfile<<"</tree>\n";
			if(root->sibling!=0)
				ScanTree(root->sibling);
			}
		if(root->ntype==IfElseStm)
		{
			printtree(root);
			if(root->child[0]!=0)
			{
				yfoutfile<<"<child>\n";
				ScanTree(root->child[0]);
				yfoutfile<<"</child>\n";
			}
			if(root->child[1]!=0)
			{
				yfoutfile<<"<child>\n";
				ScanTree(root->child[1]);
				yfoutfile<<"</child>\n";
			}
			if(root->child[2]!=0)
			{
				yfoutfile<<"<child>\n";
				ScanTree(root->child[2]);
				yfoutfile<<"</child>\n";
			}
			yfoutfile<<"</tree>\n";
			if(root->sibling!=0)
				ScanTree(root->sibling);
		}
		if(root->ntype==ConstID)
		{
			printtree(root);
			if(root->child[0]!=0)
			{
				yfoutfile<<"<child>\n";
				ScanTree(root->child[0]);
				yfoutfile<<"</child>\n";
			}
			if(root->sibling!=0)
				ScanTree(root->sibling);
		}
		if(root->ntype==VarID)
		{
			printtree(root);
			searchvar(root,funnum);
			if(root->child[0]!=0)
			{
				yfoutfile<<"<child>\n";
				ScanTree(root->child[0]);
				yfoutfile<<"</child>\n";
			}
				if(root->sibling!=0)
				ScanTree(root->sibling);
		}
		if(root->ntype==Para)
		{
			printtree(root);

⌨️ 快捷键说明

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