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

📄 parser.cpp

📁 生成类汇编代码的编译器
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include<fstream.h>
#include<iostream.h>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#define PLUS		0
#define MINUS		1
#define STAR		2	
#define SLASH		3
#define LT	     	4
#define LTEQ		5
#define GT	    	6
#define GTEQ		7
#define EQ	    	8
#define NEQ		    9
#define ASSIGN		10
#define SEMI		11
#define COMMA		12
#define COLON		13
#define DOT	    	14
#define WAVE		15
#define LPAREN		16
#define RPAREN		17
#define LSQUAR		18
#define RSQUAR		19
#define LBRACE		20
#define RBRACE		21
#define LCOMMENT	22
#define RCOMMENT	23
#define ID	    	24
#define NUMBER		25


char * operaterType[] = {"PLUS","MINUS","STAR","SLASH","LT","LTEQ","GT","GTEQ","EQ",
		     "NEQ","ASSIGN","SEMI","COMMA","COLON","DOT","WAVE","LPAREN",
		     "RPAREN","LSQUAR","RSQUAR","LBRACE","RBRACE","LCOMMENT","RCOMMENT",
		     "ID","NUMBER"};
char * operater[]={"+","-","*","/","<","<=",">",">=","==","!=","=",";",",",":",".","~",
                  "(",")","[","]","{","}","/*","*/"};
char * keyWordType[] = {"EXP","THROW","CATCH","TRY","VIRTUAL","THIS","DELETE",
		        "NEW","PROTECTED","PRIVATE","PUBLIC","CLASS","RETURN",
		        "WHILE","IF","ELSE","INT","VOID"};

char * keyWord[] = {"exp","throw","catch","try","virtual","this","delete",
		    "new","protected","private","public","class","return",
		    "while","if","else","int","void"};

ifstream in;
ofstream out;
int line=2;

struct Node
{
	char line[3];
	int type;
	char string[10];
};
struct stmtNode
{
	char line[3];
	char nodetype[15];
	char nodestring[10];
	char modifier[10];
	char  isArray;
	char retype[10];
	char vartype[10];
	struct stmtNode *next[3];
	struct stmtNode *link;
};


int Reserve(char *strToken)             //*********************
{
	int i;
	for(i=0;i<44;i++)
	{
		if(i<24 && !strcmp(strToken,operater[i]))
		{
			return i;
			break;
		}
		else if(i>25 && !strcmp(strToken,keyWord[i-26]))
		{
			return i;
			break;
		}
	}
    if(strToken[0]<58) 
		return NUMBER;
	else 
		return ID;
}


struct Node GetToken()                 //********************
{
	struct Node Token;
	int i;
	char ch,str[10];
	Token.type=22;
	while(Token.type==22 || Token.type==23)
	{

	in.get(ch);
	while(ch!='\"')
		in.get(ch);
	i=0;
	in.get(ch);
	while(ch!='\"')
	{
		Token.line[i]=ch;
		i++;
		in.get(ch);
	}
	Token.line[i]='\0';
	in.get(ch);
	for(i=0;i<3;i++)
	{
		in.get(ch);
		while(ch!='\"')
			in.get(ch);
	}
	in.get(ch);
	i=0;
	while(ch!='\"')
	{
		str[i]=ch;
		i++;
		in.get(ch);
	}
	str[i]='\0';
	Token.type=Reserve(str);
	strcpy(Token.string,str);
	while(ch!='\n')
		in.get(ch);
	line++;
	}
	return Token;
}

void BackGet()        //***************
{
	char ch;
	int i=0;
	in.seekg(0,ios::beg);
	while(i!=line-1)
	{
		in.get(ch);
		if(ch=='\n') i++;
	}
	line--;
}


void error(struct Node token)          //////////////////////////
{
	cout<<"! error in line :" <<token.line<<'\n'; 
}





struct stmtNode *fun_declaration() ;
struct stmtNode *exter_declaration();
struct stmtNode *program()         //*******************
{
	struct stmtNode *node=0;
	struct Node token1,token2,token3;
	node=(struct stmtNode *) malloc(sizeof(struct stmtNode));
	token1=GetToken();
	while(token1.type!=42 && token1.type!=43)
		token1=GetToken();
	token2=GetToken();
	token3=GetToken();
	if(token3.type==16)
	{
		BackGet();
		BackGet();
		BackGet();
		int i=0;
		while(token1.line[i])
		{
			node->line[i]=token1.line[i];
			i++;
		}node->line[i]='\0';
		strcpy(node->nodetype,"program");
		strcpy(node->nodestring,"program");
		strcpy(node->modifier,"$");
		node->isArray='$';
		strcpy(node->retype,"$");
		strcpy(node->vartype,"$");
		node->next[0]=fun_declaration();
		node->next[1]=node->next[2]=0;
		node->link=0;
	}
	else 
	{
		BackGet();
		BackGet();
		BackGet();
		int i=0;
		while(token1.line[i])
		{
			node->line[i]=token1.line[i];
			i++;
		}node->line[i]='\0';
		strcpy(node->nodetype,"program");
		strcpy(node->nodestring,"program");
		strcpy(node->modifier,"$");
		node->isArray='$';
		strcpy(node->retype,"$");
		strcpy(node->vartype,"$");
		node->next[0]=exter_declaration();
		node->next[1]=fun_declaration();
		node->next[2]=0;
		node->link=0;
	}
	char ch;
	in.get(ch);
	if(ch!='<')
		node->link=program();

	return node;
}




struct stmtNode *var_declaration()      //************************
{
	struct Node token1;
	struct stmtNode *node=0;
	node=(struct stmtNode *) malloc(sizeof(struct stmtNode));
	int i;
	token1=GetToken();
	struct Node token2;
	if(token1.type==42)
	{
		token2=token1=GetToken();
		if(token1.type==ID)
		{
			token1=GetToken();
			i=0;
			while(token2.line[i])
			{
				node->line[i]=token2.line[i];
				i++;
			}node->line[i]='\0';
            if(token1.type==11)
			{
				strcpy(node->nodetype,"var_decl");
				strcpy(node->nodestring,token2.string);
				strcpy(node->modifier,"$");
				node->isArray='0';
				strcpy(node->retype,"$");
				strcpy(node->vartype,"int");
				node->next[0]=node->next[1]=node->next[2]=0;
				node->link=0;
				return node;
			}
			else if(token1.type==18)
			{
				node->isArray ='1';
				token1=GetToken();
				if(token1.type==NUMBER)
				{
					token1=GetToken();
					if(token1.type==19)
					{
						token1=GetToken();
						if(token1.type==11)
						{							
							strcpy(node->nodetype,"var_decl");
							strcpy(node->nodestring,token2.string);
							strcpy(node->modifier,"$");
							strcpy(node->retype,"$");
							strcpy(node->vartype,"int");
							node->next[0]=node->next[1]=node->next[2]=0;
							node->link=0;
							return node;
						}
						else  error(token1);
					}
					else  error(token1);
				}
				else  error(token1);
			}
			else  error(token1);
		}
		else  error(token1);
	}
	return 0;
}

struct stmtNode *compund_stmt();
struct stmtNode *params(); 
struct stmtNode *fun_declaration()                  ////////////////////////
{
	struct Node token1,token2;
	struct stmtNode *node=0;
	node=(struct stmtNode *) malloc(sizeof(struct stmtNode));
	int i=0;
	token1=GetToken();
	if(token1.type==42 || token1.type==43)
	{
		if(token1.type==42)
			strcpy(node->retype,"int");
		else 
			strcpy(node->retype,"void");
		token2=token1=GetToken();
		if(token1.type==ID)
		{
			strcpy(node->nodetype,"fun_decl");
			strcpy(node->nodestring,token2.string);
			token1=GetToken();
			if(token1.type==16)
			{
				node->next[0]=params();
				token1=GetToken();
				if(token1.type==17)
				{
					node->next[1]=compund_stmt();
					node->next[2]=0;
					node->link=0;
					node->isArray='$';
					i=0;
					while(token1.line[i])
					{
						node->line[i]=token1.line[i];
						i++;
					}node->line[i]='\0';
					strcpy(node->modifier,"$");
					strcpy(node->vartype,"$");
					return node;
				}
				else error(token1);
			}
			else error(token1);
		}
		else error(token1);
	}
	else error(token1);
	return 0;
}

struct stmtNode *param_list();
struct stmtNode *params()                    //////////////////////////
{
	struct Node token;
	struct stmtNode *node=0;
	node=(struct stmtNode *) malloc(sizeof(struct stmtNode));
	int i=0;
	token=GetToken();
	if(token.type==43)
	{
		i=0;
		while(token.line[i])
		{
			node->line[i]=token.line[i];
			i++;
		}node->line[i]='\0';
		strcpy(node->nodetype,"params");
		strcpy(node->nodestring,"void");
		strcpy(node->modifier,"$");
		strcpy(node->retype,"$");
		strcpy(node->vartype,"$");
		node->isArray='$';
		node->next[0]=node->next[1]=node->next[2]=0;
		node->link=0;
		return node;
	}
	else
	{
		BackGet();
		node=param_list();
		return node;
	}
}

struct stmtNode *param();
struct stmtNode *param_list()                 ////////////////////////////
{
	struct stmtNode *node;
	struct Node token;
	node=(struct stmtNode *) malloc(sizeof(struct stmtNode));
	node=param();
	token=GetToken();
	if(token.type==12)
		node->link=param_list();
	else BackGet();
	return node;
}

struct stmtNode *param()                       /////////////////////////////
{
	struct stmtNode *node=0;
	struct Node token,token1;
	node=(struct stmtNode *) malloc(sizeof(struct stmtNode));
	int i=0;
	token=GetToken();
	if(token.type==42)
	{
		i=0;
		while(token.line[i])
		{
			node->line[i]=token.line[i];
			i++;
		}node->line[i]='\0';
		strcpy(node->nodetype,"param");
		strcpy(node->retype,"$");
		strcpy(node->vartype,"int");
		strcpy(node->modifier,"$");
		token1=token=GetToken();
		if(token.type==ID)
		{
			strcpy(node->nodestring,token1.string);
			token=GetToken();
			if(token.type==18)
			{
				node->isArray='1';
				node->next[0]=node->next[1]=node->next[2]=0;
				node->link=0;
				token=GetToken();
				if(token.type==19)
					return node;
				else error(token);
			}
			else 
			{
				node->isArray='0';
				node->next[0]=node->next[1]=node->next[2]=0;
				node->link=0;
				BackGet();
				return node;
			}
		}
		else error(token);
	}
	else error(token);
	return 0;
}





struct stmtNode *factor();
struct stmtNode *term()              ///////////////////////////////////
{
	struct Node token;
	struct stmtNode *node1=0;
	struct stmtNode *node2=0;
	node1=(struct stmtNode *) malloc(sizeof(struct stmtNode));
	node2=(struct stmtNode *) malloc(sizeof(struct stmtNode));
	node2=factor();
	token=GetToken();
	if(token.type==2 || token.type==3)
	{
		if(token.type==2)
			strcpy(node1->nodestring,"*");
		else strcpy(node1->nodestring,"\\");
		strcpy(node1->nodetype,"term");
		int i=0;
		while(token.line[i])
		{
			node1->line[i]=token.line[i];
			i++;
		}node1->line[i]='\0';
		node1->isArray='$';
		strcpy(node1->modifier,"$");
		strcpy(node1->retype,"$");
		strcpy(node1->vartype,"$");
		node1->next[0]=node2;
		node1->next[1]=term();
		node1->next[2]=0;
		node1->link=0;
		return node1;
	}
	else
	{
		node1=node2;
		BackGet();
		return node1;
	}
}



struct stmtNode *additive_expression()               ///////////////////////////
{
	struct stmtNode *node1=0;
	struct stmtNode *node2=0;
	node1=(struct stmtNode *) malloc(sizeof(struct stmtNode));
	node2=(struct stmtNode *) malloc(sizeof(struct stmtNode));
	struct Node token;
	node2=term();
	token=GetToken();
	if(token.type==0 || token.type==1)
	{
		if(token.type==0)
			strcpy(node1->nodestring,"+");
		else strcpy(node1->nodestring,"-");
		strcpy(node1->nodetype,"aditive-exp");
		int i=0;
		while(token.line[i])
		{
			node1->line[i]=token.line[i];
			i++;
		}node1->line[i]='\0';
		node1->isArray='$';
		strcpy(node1->modifier,"$");
		strcpy(node1->retype,"$");
		strcpy(node1->vartype,"$");
		node1->next[0]=node2;
		node1->next[1]=additive_expression();
		node1->next[2]=0;
		node1->link=0;
		return node1;
	}
	else
	{
		node1=node2;
		BackGet();
		return node1;
	}
}


struct stmtNode *simple_expression()      //////////////////////////////
{
	struct stmtNode *node1=0;
	struct stmtNode *node2=0;
	node1=(struct stmtNode *) malloc(sizeof(struct stmtNode));
	node2=(struct stmtNode *) malloc(sizeof(struct stmtNode));
	struct Node token;
	node2=additive_expression();
	token=GetToken();
	if(token.type<=9 && token.type>=4)
	{
		if(token.type==4)
			strcpy(node1->nodestring,"<");
		else if(token.type==5)
			strcpy(node1->nodestring,"<=");
		else if(token.type==6)
			strcpy(node1->nodestring,">");
		else if(token.type=7)
			strcpy(node1->nodestring,">=");
		else if(token.type==7)
			strcpy(node1->nodestring,"==");
		else strcpy(node1->nodestring,"!=");
		strcpy(node1->nodetype,"simple-exp");
		int i=0;
		while(token.line[i])
		{
			node1->line[i]=token.line[i];
			i++;
		}node1->line[i]='\0';
		node1->isArray='$';
		strcpy(node1->modifier,"$");
		strcpy(node1->retype,"$");
		strcpy(node1->vartype,"$");
		node1->next[0]=node2;
		node1->next[1]=simple_expression();
		node1->next[2]=0;
		node1->link=0;
		return node1;
	}
	else
	{
		node1=node2;
		BackGet();
		return node1;
	}
}




struct stmtNode *var() ;
struct stmtNode *expression()                 ////////////////////////////////////////////
{
	struct stmtNode *node1=0;
	struct stmtNode *node2=0;
	node1=(struct stmtNode *) malloc(sizeof(struct stmtNode));
	node2=(struct stmtNode *) malloc(sizeof(struct stmtNode));
	struct Node token;
	node2=var();
	token=GetToken();
	if(token.type==10)
	{
		node1->isArray='$';
		int i=0;
		while(token.line[i])
		{
			node1->line[i]=token.line[i];
			i++;
		}node1->line[i]='\0';
		strcpy(node1->modifier,"$");
		strcpy(node1->nodetype,"Assign-expre");
		strcpy(node1->retype,"$");
		strcpy(node1->vartype,"$");
		strcpy(node1->nodestring,"=");
        node1->next[0]=node2;
		node1->next[1]=simple_expression();
		node1->next[2]=0;
		node1->link=0;
		return node1;	 
	}
	else error(token);
	return 0;
}

⌨️ 快捷键说明

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