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

📄 parse.cpp

📁 c的简化编译器
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/****************************************************/
/* File: parse.c                                    */
/* Main program for c- compiler                     */
/* Compiler Construction: Principles and Practice   */
/* Maker:Liukai                                     */
/****************************************************/
#include"globals.h"
#include"util.h"
#include"scan.h"
#include"parse.h"
static  TokenType token ;
static  TreeNode *  mulop(void);
static  TreeNode *  addop(void);
static  TreeNode *  empty(void);
static  TreeNode *  program(void);
static  TreeNode *  declaration_list(void);
static  TreeNode *  declaration(void);
static  TreeNode *  var_declaration(void);
static  TreeNode *  type_specifier(void);
static  TreeNode *  fun_declaration(void);
static  TreeNode *  params(void);
static  TreeNode *  para_list(void);
static  TreeNode *  param(void);
static  TreeNode *  compound_stmt(void);
static  TreeNode *  local_declaration(void);
static  TreeNode *  statement_list(void);
static  TreeNode *  statement(void);
static  TreeNode *  expression_stmt(void);
static  TreeNode *  iteration_stmt(void);
static  TreeNode *  return_stmt(void);
static  TreeNode *  expression(void);
static  TreeNode *  selection_stmt(void);
static  TreeNode *  var(void);
static  TreeNode *  simple_expression(void);
static  TreeNode *  relop(void);
static  TreeNode *  additive_expression(void);

static  TreeNode *  term(void);

static  TreeNode *  factor(void);
static  TreeNode *  call(void);
static  TreeNode *  args(void);
static  TreeNode *  arg_list(void);
static void syntaxError(char * );
static void match(TokenType);
/*1*/
TreeNode *program(void)
{
	TreeNode *t;
	t=declaration_list();
	return t;
}
/*2*/
TreeNode *declaration_list(void)
{
	TreeNode *t=declaration();
    TreeNode *p=t;
	while(token==INT||token==VOID)
	{
		TreeNode *q;
		q=declaration();
		if (q!=NULL)
		{
			if (t==NULL)
			{
				t=p=q;
			}
			else
			{
				p->sibling=q;
				p=q;
			}
		}
		
	}
	return t;
}
/*3*/
TreeNode *declaration(void)
{
	TreeNode *t=newExpNode(IdK);
	
	//加入IDK属性;
	
	if (token==INT)
	{
		match(INT);
		//加入INT类型属性;
		t->type=Int;//
	}
	else 
	{
		match(VOID);
		//加入VOID类型属性;
		t->type=Void;
	}
	if (token==ID)
	{
		//加入TOKENSTRING属性;
		t->attr.name=copyString(tokenString);
	}
	match(ID);
	if (token==LPAREN)
	{
		//加入FUN属性;
		t->idtype1=FunK;
		match(LPAREN);
		t->child[0]=params();
		match(RPAREN);
		t->child[1]=compound_stmt();
	}
	else 
	{
		//添加VAR属性;
		t->idtype1=VarK;
		if (token==SEMI)
		{
			//说明没有数组属性;
			t->is=no;
		}
		else 
		{
			match(ZLPAREN);
			if (token==NUM)
			{
				//说明有数组属性;
				t->is=yes;
				//添加NUM属性;数组中元素个数;
				t->arryno=atoi(tokenString);
			}
			match(NUM);
			match(ZRPAREN);
		}
		match(SEMI);
	}

	/*if()
		t=var_declaration();
	else
		t=fun_declaration();*/
	return t;
}
/*4*/
TreeNode *var_declaration(void)
{
	TreeNode *t=newExpNode(IdK);
	//添加ID属性;

	//添加VAR属性;
	t->idtype1=VarK;
	if (token==INT)
	{
		match(INT);
		//添加INT属性;
		t->type=Int;
	}
	else
	{
		match(VOID);
		//添加VOID属性;
		t->type=Void;
	}
	if (token==ID)
	{
		//添加TOKENSTRING属性;
		t->attr.name=copyString(tokenString);
	}
	match(ID);
	if (token==SEMI)
	{
		//说明没有数组属性;
		t->is=no;
	}
	else
	{
		match(ZLPAREN);
		if (token==NUM)
		{	
			//说明有数组属性;
			t->is=yes;
			//添加NUM属性;
			t->arryno=atoi(tokenString);
		}
		match(NUM);
		match(ZRPAREN);
	}
	match(SEMI);
	return t;
}

/*5*/
TreeNode *fun_declaration(void)
{
	TreeNode *t=newExpNode(IdK);
	//添加ID属性;


	//添加FUM属性;
	t->idtype1=FunK;
	if(token==INT)
	{
		match(INT);
		t->type=Int;
	}
	else
	{
		match(VOID);
		t->type=Void;
	}
	if (token==ID)
	{
		// 添加TOKENSTRING属性;
		t->attr.name=copyString(tokenString);//
	}
	match(ID);
	match(LPAREN);
	t->child[0]=params();
	match(RPAREN);
	t->child[1]=compound_stmt();
	return t;
}
/*6*/
TreeNode *params(void)
{
	TreeNode *t;
	if(token==VOID)
	{
		t=NULL;
		match(token);

	}
	else
		t=para_list();
	return t;
}
/*7*/
TreeNode *para_list(void)
{
	TreeNode *t=param();
	TreeNode *p=t;
	while(token==DOUHAO)
	{
		TreeNode *q;
		match(DOUHAO);
		q=param();
		if (q!=NULL)
		{
			if (t==NULL)
			{
				t=p=q;
			}
			else
			{
				p->sibling=q;
				p=q;
			}
		}
		
	}
	return t;
}
/*8*/
TreeNode *param(void)
{
	TreeNode *t=newExpNode(IdK);
	//添加ID属性;


	match(INT);
	t->type=Int;//
	t->attr.name=copyString(tokenString);//
	match(ID);
	if(token==ZLPAREN)
	{
		match(ZLPAREN);
		match(ZRPAREN);
		//说明是数组类型;
		t->is=yes;

	}
	return t;
}
/*9*/
TreeNode *compound_stmt(void)
{
	TreeNode *t=newStmtNode(CompoundK);
	//添加COMPOUND类型;COMPOUND类型为一个STMT;

	match(DLPAREN);
	t->child[0]=local_declaration();
	t->child[1]=statement_list();
	match(DRPAREN);
	return t;
}
/*10*/
TreeNode *local_declaration(void)
{
	TreeNode *t=empty();
	TreeNode *p=t;
	while(token==INT||token==VOID)
	{
		TreeNode *q;
		q=var_declaration();
		if (q!=NULL)
		{
			if (t==NULL)
			{
				t=p=q;
			}
			else 
			{
				p->sibling=q;
				p=q;
			}
		}
		

	}
	return t;
}
/*11*/
TreeNode *statement_list(void)
{
	TreeNode *t=empty();
	TreeNode *p=t;
	while(token==ID||token==DLPAREN||token==IF||token==WHILE||token==RETURN||token==LPAREN||token==NUM)
	{
		TreeNode *q;
		q=statement();
		if (q!=NULL)
		{
			if (t==NULL)
			{
				t=p=q;
			}
			else 
			{
				p->sibling=q;
				p=q;
			}
		}
		

	}
	return t;
}
/*TreeNode *statement_list(void)
{
	TreeNode *t;
	t=empty();
	TreeNode *p;
	p=t;
	while()//
	{
		TreeNode *q;
		q=statement();
		p->sibling=q;
		p=q;
	}
	return t;
}*/
/*12*/
TreeNode *statement(void)
{
	TreeNode *t;
	switch (token)
	{
	case ID:t=expression_stmt();break;
	case SEMI:t=expression_stmt();break;
	case LPAREN:t=expression_stmt();break;
	case NUM:t=expression_stmt();break;
	case DLPAREN:t=compound_stmt();break;
	case IF:t=selection_stmt();break;
	case WHILE:t=iteration_stmt();break;
	case RETURN:t=return_stmt();break;
	default:
		syntaxError("unexpected error at ->>");break;
	}
	return t;
	
	
}
/*14*/
TreeNode *expression_stmt(void)
{
	TreeNode *t;
	
	if(token!=SEMI)
	{
		t=expression();
	}
	else 
	{
		t=NULL;
	}
	match(SEMI);
	return t;

}
/*17*/
TreeNode *return_stmt(void)
{
	TreeNode *t=newStmtNode(ReturnK);
	//添加RETURN属性;

	match(RETURN);
	if(token==SEMI)
	{
		match(token);

	}
	else
	{
		t->child[0]=expression();
		match(SEMI);
	}
	return t;
}
/*15*/
TreeNode *selection_stmt(void)
{
	TreeNode *t=newStmtNode(IfK);
	//添加IF属性;

	match(IF);
	match(LPAREN);
	t->child[0]=expression();
	match(RPAREN);
	t->child[1]=statement();
	if(token==ELSE)
	{
		match(ELSE);
		t->child[2]=statement();
	}
	return t;
}
/*16*/
TreeNode *iteration_stmt(void)
{
	TreeNode *t=newStmtNode(WhileK);
	//添加WHILE属性;

	match(WHILE);
	match(LPAREN);
	t->child[0]=expression();
	match(RPAREN);
	t->child[1]=statement();
	return t;
}
/*18*/
TreeNode *expression(void)
{
	TreeNode *t;
	TreeNode *a=NULL;
	TreeNode *q=NULL;
loop:	switch (token)
	{
	case ID:
		t=newExpNode(IdK);
		//添加ID属性;并添加TOKENSTRING;

		t->attr.name=copyString(tokenString);//
		match(ID);
		if (token==LPAREN)
		{
			t->idtype1=FunK;
			match(LPAREN);
			t->child[0]=args();
			match(RPAREN);
		}
		else
		{
			t->idtype1=VarK;
			if (token==ZLPAREN)
			{
				match(ZLPAREN);
				t->is=yes;
				t->child[0]=expression();
				match(ZRPAREN);	
			}

			if (token==ASSIGN)
			{
				TreeNode *p=newStmtNode(AssignK);

⌨️ 快捷键说明

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