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

📄 parse.cpp

📁 编译原理课程设计 词,语法分析器 用C++手工编写
💻 CPP
字号:
#include "globals.h"
#include "util.h"
#include "scan.h"
#include "parse.h"

static TokenType token;

static TreeNode * declaration_list(void);
static TreeNode * declaration(void);
static TreeNode * param_sequence(void);
static TreeNode * stmt_sequence(void);


static TreeNode * param(void);

static TreeNode * compound_stmt(void);
static TreeNode * statement(void);

static TreeNode * expression_stmt(void);
static TreeNode * selection_stmt(void);
static TreeNode * iteration_stmt(void);
static TreeNode * return_stmt(void);

static TreeNode * expression(void);
static TreeNode * var(void);
static TreeNode * simple_expression(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 * message)
{
	fprintf(listing, "\n>>> ");
	fprintf(listing, "Syntax error at line %d: %s", lineno, message);
	Error = TRUE;
}


static void match(TokenType expected)
{
	if(token == expected) token = getToken();
	else
	{
		syntaxError("Unexpected token -> ");
		printToken(token, tokenString);
		fprintf(listing, "		");
	}
}

static TreeNode * declaration_list(void)
{
	TreeNode * t = NULL;
	while (token == INT || token == VOID) 
	{
		t  = declaration();
		TreeNode * p = t;
		while(token!=ENDFILE && (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;	
}


static TreeNode * declaration(void)
{
	TreeNode * t = newDeclarNode();
 //   while ((token != ENDFILE) && (token == INT|| token == VOID))  
//	{  
		if(token == VOID)
		{
			strcpy(t->type, "void");		
		}
		else if(token == INT)
		{
			strcpy(t->type, "int");
		}
		token = getToken();
		t->attr.name = copyString(tokenString);
		token = getToken();
		switch (token) 
		{
		case LPAREN : 
			match(LPAREN);
			t->kind.declar = FuncK;
			if (t != NULL) 
				t->child[0] = param_sequence();
			match(RPAREN);
			if (t != NULL)
				t->child[1] = compound_stmt();
			break; 
		case SEMI :
			match(SEMI);
			t->kind.declar = VarK;
			break;
		case LSPAREN : 
			match(LSPAREN);
			t->kind.declar = ArrayK;
			t->child[0] = factor();
			match(RSPAREN);
			match(SEMI);
			break;
		default : 
			syntaxError("unexpected token -> ");
			printToken(token, tokenString);
			token = getToken();
			break;
		}
//	}
    
    return t;
}

TreeNode * compound_stmt(void)
{  
	TreeNode * t = newStmtNode(CompoundK);
	match(LBPAREN);
	if (t != NULL) 
		t->child[0] = declaration_list();
	if (t != NULL)
		t->child[1] = stmt_sequence();
	
	match(RBPAREN);
	return t;
}


TreeNode * param_sequence(void)
{   
	TreeNode * t = param();
	TreeNode * p = t;
	int maxPramNum = 5;
	while (token != RPAREN && maxPramNum > 0)       
	{ 
		maxPramNum--;
		match(COMMA);
		TreeNode * q;
		q = param();
		if (q != NULL)
		{
			if (t == NULL) t = p = q;
			else 
			{ 
				p->sibling = q;
				p = q;
			}
		}
	}
	
	return t;
}


TreeNode * param(void)
{ 
	TreeNode * t = newParamNode();
//	while ((token!=ENDFILE) && (token==INT || token==VOID)) 
//	{
	if(token==VOID )//|| token == RPAREN)
	{
		match(VOID);
		t->kind.param = Null;
	}
	else if(token == INT)
	{
		strcpy(t->type, "int");
		token = getToken();
		t->attr.name = copyString(tokenString);
		token = getToken();
		switch (token) 
		{
		case COMMA:
		case RPAREN:
			t->kind.param = Var;
			break; 
		case LSPAREN : 
			match(LSPAREN);
			t->kind.param = Array;
			match(RSPAREN);
			return t;
			break;
		default : 
			syntaxError("unexpected token -> ");
			printToken(token,tokenString);
			token = getToken();
			break;
		} 	
//	}
	}
   
	return t;
}




TreeNode * stmt_sequence(void)
{ 
	TreeNode * t = NULL;
	if((token != ENDFILE) && (token!=RBPAREN))
	{
		t = statement();
		TreeNode * p = t;
		while ((token!=ENDFILE) && (token!=RBPAREN))
		{ 
			TreeNode * q;
		
			q = statement();
			if (q != NULL) 
			{
				if (t == NULL) t = p = q;
				else 
				{ 
					p->sibling = q;
					p = q;
				}
			}
		}
		
	}
	
	return t;
}


TreeNode * statement(void)
{ 
	TreeNode * t=NULL;
//	if(token!=ENDFILE)
//	{
		switch(token)
		{ 
		case LBPAREN: t = compound_stmt(); break;
		case IF: t = selection_stmt(); break;
		case WHILE: t = iteration_stmt(); break;
		case RETURN: t = return_stmt(); break;
		case ID:
		case NUM: t = expression_stmt(); break;
		default: 
			syntaxError("unexpected token-> ");
		    printToken(token, tokenString);
			token = getToken();
			break;
	
//		}
	}
  
  return t;
}


TreeNode * expression_stmt(void)
{
	
	TreeNode * t = newStmtNode(ExpressionK);
	if(token == SEMI)
		return t;
    else if (t != NULL) 
	{
		t->child[0] = expression();
	}
	match(SEMI);
    return t;
}


TreeNode * selection_stmt(void)
{ 
	TreeNode * t = newStmtNode(IfK);
	match(IF);
//	match(LPAREN);
	if(t!=NULL) t->child[0] = expression();
//	match(RPAREN);
	if(t!=NULL) t->child[1] = statement();
	if(token == ELSE)
	{ 
		match(ELSE);
		if(t != NULL) t->child[2] = statement();
	}
  return t;
}


TreeNode * iteration_stmt(void)
{ 
	TreeNode * t = newStmtNode(WhileK);
	match(WHILE);
//	match(LPAREN);
	if(t!=NULL) t->child[0] = expression();
//	match(RPAREN);
	if(t!=NULL) t->child[1] = statement();
	return t;
}



TreeNode * return_stmt(void)
{
	TreeNode * t = newStmtNode(ReturnK);
	match(RETURN);
	if (t!=NULL && token!=SEMI) 
		t->child[0] = expression();
	match(SEMI);
	return t;
}


TreeNode * expression(void)
{ 
	TreeNode * t = simple_expression();
	if(token == ASSIGN)
	{
		TreeNode * p = newExpNode(OpK);
		if (p != NULL)
		{
			p->child[0] = t;
			p->attr.op = token;
			t = p;
			match(token);
			t->child[1] = expression();
		}
		
	}
	return t;
}



TreeNode * simple_expression(void)
{ 
	TreeNode * t = additive_expression();
	while((token==LEQ) || (token==LT) || (token==GT)
			|| (token==GEQ) || (token==EQ) || (token==NEQ))
	{ 
		TreeNode * p = newExpNode(OpK);
		if (p != NULL)
		{ 
			p->child[0] = t;
			p->attr.op = token;
			t = p;
		}
		match(token);
		if (t != NULL)
			t->child[1] = additive_expression();
	}
  return t;
}


TreeNode * additive_expression(void)
{ 
	TreeNode * t = term();
	while((token==PLUS) || (token==MINUS))
	{ 
		TreeNode * p = newExpNode(OpK);
		if (p != NULL)
		{ 
			p->child[0] = t;
			p->attr.op = token;
			t = p;
			match(token);
			t->child[1] = term();
		}
	}
  return t;
}


TreeNode * term(void)
{ 
	TreeNode * t = factor();
	while((token==TIMES) || (token==DIV))
	{	
		TreeNode * p = newExpNode(OpK);
		if (p != NULL)
		{ 
			p->child[0] = t;
			p->attr.op = token;
			t = p;
			match(token);
			t->child[1] = factor();
		}
	}
  return t;
}


TreeNode * factor(void)
{
	TreeNode * t = NULL;
    if(token == NUM) 
	{	
		t = newExpNode(ConstK);
		if ((t!=NULL) && (token==NUM))
			t->attr.val = atoi(tokenString);
		match(NUM);
	}
	else if (token == LPAREN )
	{
		match(LPAREN);
		t = expression();
		match(RPAREN);
	}
    else if(token == ID)
	{
		t = newExpNode(IdVarK);
		if ((t!=NULL) && (token==ID))
			t->attr.name = copyString(tokenString);
		token = getToken();
		if(token == LSPAREN)
		{
			match(LSPAREN);
			t->kind.exp = IdArrayK;
			t->child[0] = expression();
			match(RSPAREN);
		}
		else if(token == LPAREN)
		{ 
			match(LPAREN);
			t->kind.exp = CallK;
			t->child[0] = args();
			match(RPAREN);
		}
		
	}
	else 
	{  
		syntaxError("unexpected token -> ");
		printToken(token,tokenString);
		token = getToken();
		
	}
	return t;
}



TreeNode * arg_list(void)
{
	TreeNode * t = expression();
	TreeNode * p = t;
	int maxArgNum = 5;
	while(token != RPAREN && maxArgNum > 0)
	{
		maxArgNum--;
		match(COMMA);
		TreeNode * q = expression();
		p->sibling = q;
		p = q;
	}
    return t;
}
TreeNode * args(void)
{ 
	TreeNode * t = NULL;
	if(token != RPAREN)
	{
		t = arg_list();	
	}
	return t;
}





TreeNode * parse(void)
{ 
	TreeNode * t;
	token = getToken();
	t = declaration_list();
	if (token != ENDFILE)
		syntaxError("Code ends before file\n");
	return t;
}


⌨️ 快捷键说明

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