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

📄 parse.cpp

📁 c的简化编译器
💻 CPP
📖 第 1 页 / 共 2 页
字号:
				//添加ASSIGN属性;
		
				p->child[0]=t;
				if (q==NULL)
				{
					a=q=p;
				}
				else
				{
					q->child[1]=p;
					q=p;
				}
				match(ASSIGN);
				goto loop;
			}
		}
		break;
	case LPAREN:
		match(LPAREN);
		t=expression();
		match(RPAREN);
		break;
	case NUM:
		t=newExpNode(ConstK);
		//添加CONST属性,并添加值;
	
		t->attr.val=atoi(tokenString);//
		match(NUM);
		break;
	default:
		syntaxError("unexpected error at ->>");break;
	}
	//上面的SWITCH得到了一个T和A,Q;
	while (token==TIMES||token==OVER)
	{
		
		TreeNode *b=newExpNode(OpK);
		//添加MULOP属性;
	
		b->attr.op=token;//
		match(token);
		b->child[0]=t;
		b->child[1]=factor();
		t=b;
	}
	while (token==PLUS||token==MINUS)
	{
		
		TreeNode *b=newExpNode(OpK);
		//添加ADDOP属性;

		b->attr.op=token;//
		match(token);
		b->child[0]=t;
		b->child[1]=term();
		t=b;
	}
	if (token==MT||token==MET||token==LT||token==LET||token==EQ||token==NEQ)
	{
		TreeNode *b=newExpNode(OpK);
		//添加RELOP属性;

		b->attr.op=token;//
		match(token);
		b->child[0]=t;
		b->child[1]=additive_expression();
		t=b;
	}
	if (a!=NULL)
	{
		q->child[1]=t;
		return a;
	}
	return t;
	
	
}
/*TreeNode *expression(void)
{
	TreeNode *t;
	if(token=="ID")
	{
		//添加ASSIGN属性,属STMT;
		t->child[0]=var();
		match("=");
		TreeNode *q;
		q=t;
		while(token=="ID")
		{
			TreeNode *p=newtreenode();
			//添加ASSIGN	属性,属STMT属性;
			p->child[0]=var();
			q=p;
		}
		q->child[1]=simple_expression();
	}
	else
		t=simple_expression();
	return t;
}*/
/*19*/
TreeNode *var(void)
{
	TreeNode *t=newExpNode(IdK);
	//添加ID属性;

	t->attr.name=copyString(tokenString);//
	if(token==ZLPAREN)
	{
		match(ZLPAREN);
		t->child[0]=expression();
		match(ZRPAREN);
	}
	return t;
}
/*20*/
TreeNode *simple_expression(void)
{
	TreeNode *t1=additive_expression();
	TreeNode *t;
	if(token==MT||token==MET||token==LT||token==LET||token==EQ||token==NEQ)
	{
		t=relop();
		t->child[1]=additive_expression();
		t->child[0]=t1;
		return t;
	}
	return t1;
}
/*21*/
TreeNode *relop(void)
{
	TreeNode *t=newExpNode(OpK);
	t->attr.op=token;
	match(token);
	return t;
}
/*
TreeNode *relop(void)
{
	TreeNode *t=newExpNode(OpK);
	//添加OP属性;

	switch (token)
	{
	case "<":
		match(token);
		if (token=="=")
		{
			t->attr.op="<=";//
			match("=");

		}
		else
			t->attr.op="<";//
		break;
	case ">":
		match(token);
		if (token=="=")
		{
			//添加>=属性;
			t->attr.op=">=";
			match(token);
		}
		else
		{
			//添加>属性:
			t->attr.op=">";
		}
		break;
	case "=":
		match(token);
		match("=");
		t->attr.op="==";//
		break;
	case "!":
		match("!");
		match("=");
		t->attr.op="!=";//
		break;
	default:
		error();
		break;
	return t;
	}
}*/
/*22*/
TreeNode *additive_expression(void)
{
	TreeNode *t=term();
	if (token==PLUS||token==MINUS)
	{
		TreeNode *p=addop();
		p->child[0]=term();
		p->child[1]=t;
		while (token==PLUS||token==MINUS)
		{
			TreeNode *q=addop();
			q->child[0]=term();
			q->child[1]=p;
			p=q;
		}
		t=p;
	}
	return t;
}
/*23*/
TreeNode *addop(void)
{
	TreeNode *t=newExpNode(OpK);
	//添加OP属性;

	if (token==PLUS)
	{
		match(PLUS);
		t->attr.op=PLUS;//

	}
	else
	{
		match(MINUS);
		t->attr.op=MINUS;//
	}
	return t;
}
/*24*/
TreeNode *term(void)
{
	TreeNode *t=factor();
	if (token==TIMES||token==OVER)
	{
		TreeNode *p=mulop();
		p->child[1]=factor();
		p->child[0]=t;
		while (token==TIMES||token==OVER)
		{
			TreeNode *q=mulop();
			q->child[1]=factor();
			q->child[0]=p;
			p=q;
		}
		t=p;
	}
	return t;
}
/*25*/
TreeNode *mulop(void)
{
	TreeNode *t=newExpNode(OpK);
	//添加OP属性;

	if (token==TIMES)
	{
		match(TIMES);
		t->attr.op=TIMES;//

	}
	else
	{
		match(OVER);
		t->attr.op=OVER;//

	}
	return t;
}
/*26*/
TreeNode *factor(void)
{
	TreeNode *t;
	switch (token)
	{
	case LPAREN:
		match(LPAREN);
		t=expression();
		match(RPAREN);
		break;
	case ID:
		t=newExpNode(IdK);
	    //添加ID属性;并添加TOKENSTRING;
	
		t->attr.name=copyString(tokenString);//
		match(ID);
		if (token==LPAREN)
		{
			match(LPAREN);
			t->child[0]=args();
			match(RPAREN);
		}
		else
		{
			if (token==ZLPAREN)
			{
				match(ZLPAREN);
				t->child[1]=expression();
				match(ZRPAREN);
			}
			
		}
		break;
		
	/*case 2:
		t=var();
		break;
	case 3:
		t=call();
		break;*/
	case NUM:
		t=newExpNode(ConstK);

		t->attr.val=atoi(tokenString);//
		match(NUM);
		break;
	default:
		syntaxError("unexpected token ->>>");
		break;
	
	
	}
	return t;
}
/*27*/
TreeNode *call(void)
{
	TreeNode *t=newExpNode(IdK);
	//添加ID属性,并添加TOKENSTRING;

	t->attr.name=copyString(tokenString);//
	match(LPAREN);
	t->child[0]=args();
	match(RPAREN);
	return t;
}
/*28*/
TreeNode *args(void)
{
	TreeNode *t;
	if (token==LPAREN||token==ID||token==NUM)
	{
		t=arg_list();

	}
	else
		t=empty();
	return t;
}
/*29*/
TreeNode *arg_list(void)
{
	TreeNode *t=expression();
	TreeNode *p=t;
	while (token==DOUHAO)
	{
		TreeNode *q;
		match(DOUHAO);
		q=expression();
		if (q!=NULL)
		{
			if (t==NULL)
			{
				t=p=q;
			}
			else
			{
				p->sibling=q;
				p=q;
			}
		}
		

	}
	return t;
}
/*empty*/
TreeNode *empty(void)
{
	TreeNode *t=NULL;
	return t;
}
TreeNode *parse(void)
{
	TreeNode *t;
	token=getToken();
	t=program();
	if (token!=ENDFILE)
	{
		syntaxError("code ends before file\n");
	}
	return t;
}
/////////////////////////////////////////////////////////////////////////////////
static void match(TokenType t)
{
	if (token==t)
	{
		token=getToken();
	}
	else
	{
		syntaxError("unexpected token -> ");
		printToken(token,tokenString);
		fprintf(listing,"                 ");
	}
}
//////////////////////
static void syntaxError(char * message)
{
	fprintf(listing,"\n>>>>>>>   ");
	fprintf(listing,"Syntax error at line %d: %s",lineno,message);
	Error=TRUE;
}
/////////////////////////////////////////
/*
//创建节点的函数,分类创建;
//////////////////////////////////////////////////////////////////
TreeNode *newStmtNode(StmtKind kind)
{
	TreeNode *t=(TreeNode *)malloc(sizeof(TreeNode));
	int i;
	if (t==NULL)
	{
		fprintf(listing,"out of memory error at line %d\n",lineno);
	}
	else
	{
		for (i=0;i<3 ;i++ )
		{
			t->child[i]=NULL;
		}
		t->sibling=NULL;
		t->nodeKind=StmtK;
		t->Kind.stmt=kind;
		t->lineno=lineno;

	}
	return t;
}
///////////////////////
TreeNode *newExpNode(ExpKind kind)
{
	TreeNode *t=(TreeNode *) malloc(sizeof(TreeNode));
	int i;
	if (t==NULL)
	{
		fprintf(listing,"out of memory error at line %d\n",lineno);
	}
	else
	{
		for (i=0;i<3 ;i++ )
		{
			t->child[i]=NULL
		}
		t->sibling=NULL
		t->nodeKind=ExpK;
		t->Kind.exp=kind;
		t->lineno=lineno;
		t->type=Void;
		t->idtype1=VarK;
	
		t->is=no;
	}
	return t;
}
////////////////////////////////////////////////
//copyString function used to move the content of the tokenstring to a particular space;
char * copyString(char *s)
{
	int n;
	char *t;
	if (s==NULL)
	{
		return NULL;
	}
	n=strlen(s)+1;
	t=malloc(n);
	if (t==NULL)
	{
		fprintf(listing,"out of memory error at line %d\n",lineno);
	}
	else strcpy(t,s);
	return t;
}
*/

⌨️ 快捷键说明

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