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

📄 myparser.c

📁 编译原理的课程设计,输出汇编,很好的 南开大学的同学做的
💻 C
📖 第 1 页 / 共 3 页
字号:
	{ 40, 56 },
	{ 35, 54 },
	{ 32, 51 },
	{ 31, 50 },
	{ 30, 49 },
	{ 29, 48 },
	{ 28, 42 },
	{ 27, 37 },
	{ 4, 6 },
	{ 0, 2 }
};

YYCONST yystategoto_t YYNEARFAR YYBASED_CODE YYDCDECL yystategoto[] = {
	{ 44, -1 },
	{ 0, -1 },
	{ 0, -1 },
	{ 0, -1 },
	{ 33, -1 },
	{ 16, 71 },
	{ 0, -1 },
	{ 0, -1 },
	{ 0, -1 },
	{ 0, -1 },
	{ 0, -1 },
	{ 0, -1 },
	{ 0, -1 },
	{ 0, -1 },
	{ 0, -1 },
	{ 0, -1 },
	{ 0, -1 },
	{ 0, -1 },
	{ 0, -1 },
	{ 5, -1 },
	{ 0, -1 },
	{ 0, -1 },
	{ 0, -1 },
	{ 0, -1 },
	{ 0, -1 },
	{ 0, -1 },
	{ 0, -1 },
	{ 34, 71 },
	{ 30, 39 },
	{ 29, 39 },
	{ 28, 39 },
	{ 27, 39 },
	{ 21, -1 },
	{ 0, -1 },
	{ 0, -1 },
	{ 25, 39 },
	{ 0, -1 },
	{ 0, -1 },
	{ 0, -1 },
	{ 11, 59 },
	{ 22, 67 },
	{ 0, -1 },
	{ 0, -1 },
	{ 0, -1 },
	{ 0, -1 },
	{ 0, -1 },
	{ 0, -1 },
	{ 0, -1 },
	{ 0, -1 },
	{ 0, -1 },
	{ 0, -1 },
	{ 0, -1 },
	{ 0, -1 },
	{ 18, -1 },
	{ 0, -1 },
	{ 0, -1 },
	{ 0, -1 },
	{ 25, 71 },
	{ 20, 59 },
	{ 7, 67 },
	{ 18, 67 },
	{ 16, 62 },
	{ 3, 69 },
	{ 16, 67 },
	{ 15, 67 },
	{ 14, 67 },
	{ 13, 67 },
	{ -1, 62 },
	{ 0, 69 },
	{ -6, -1 },
	{ -8, 71 },
	{ -2, -1 },
	{ 0, -1 },
	{ 0, -1 },
	{ 0, -1 },
	{ 0, -1 },
	{ 0, -1 },
	{ 0, -1 },
	{ 0, -1 },
	{ 0, -1 },
	{ 0, -1 },
	{ 0, -1 },
	{ 0, -1 },
	{ 0, -1 },
	{ 0, -1 },
	{ 0, -1 },
	{ 0, -1 },
	{ 0, -1 },
	{ 0, -1 },
	{ 0, -1 },
	{ 0, -1 },
	{ 0, -1 },
	{ 0, -1 },
	{ 0, -1 }
};

YYCONST yydestructor_t YYNEARFAR *YYNEAR YYDCDECL yydestructorptr = NULL;

YYCONST yytokendest_t YYNEARFAR *YYNEAR YYDCDECL yytokendestptr = NULL;
int YYNEAR YYDCDECL yytokendest_size = 0;

YYCONST yytokendestbase_t YYNEARFAR *YYNEAR YYDCDECL yytokendestbaseptr = NULL;
int YYNEAR YYDCDECL yytokendestbase_size = 0;
#line 354 ".\\myparser.y"


/////////////////////////////////////////////////////////////////////////////
// programs section

YYSTYPE newStmNode(int kind,int count)
{ 
	YYSTYPE t = (YYSTYPE ) malloc(sizeof(TreeNode));
	int i;
    for (i=0;i<3;i++) 

	t->child[i] = NULL;
    t->sibling = NULL;
    t->kind=StmtKind;
	//t->nodekind = ExpK;
	t->nodekind= kind;
//	t->lineno = lineno;
	t->count = count;
  return t;
}
/*
YYSTYPE newIDNode(int kind,int count,char* string)
{ 
	YYSTYPE t = (YYSTYPE ) malloc(sizeof(TreeNode));
	int i;
    for (i=0;i<3;i++) 
		t->child[i] = NULL;
    t->sibling = NULL;
	t->nodekind= kind;
	t->count = count;
	strcpy(t->name,string);
  return t;
}
YYSTYPE newNUMNode(int kind,int count,int val)
{
	YYSTYPE t = (YYSTYPE ) malloc(sizeof(TreeNode));
	int i;
    for (i=0;i<3;i++) 
		t->child[i] = NULL;
    t->sibling = NULL;
	t->nodekind= 8;
	t->count = count;
	t->val=val;
  return t;
}*/

YYSTYPE newExprNode(int kind,int count)
{
	YYSTYPE t = (YYSTYPE ) malloc(sizeof(TreeNode));
	int i;
    for (i=0;i<3;i++) 
		t->child[i] = NULL;
    t->sibling = NULL;
    t->kind=ExprKind;
	t->nodekind= kind;
	t->count = count;
	if(kind!=IDk && kind!=Constk) 
	fprintf(out,"	_t%d DWORD 0\n",count);
//	t->op=op;
  return t;
}
output(TreeNode* root){
	
	TreeNode* t;   
	int i;
	if(root==NULL) return ;
	if(root->child[0]!=NULL) printf("%d ",root->child[0]->count);
	if(root->child[1]!=NULL) printf("%d ",root->child[1]->count);
	if(root->child[2]!=NULL) printf("%d ",root->child[2]->count);
	
	for(i=0;i<3;i++){
		if(root->child[i]==NULL) continue;
		t=root->child[i]->sibling;
		//if(t==NULL) continue;
		while(t!=NULL)
		{
//			printf("%d ",t->count);
			t=t->sibling;
		}       
	}
}

find(TreeNode* root){
//	printf("%d\n",root->nodekind);
	if(root->child[0]!=NULL) find(root->child[0]);
	if(root->child[1]!=NULL) find(root->child[1]);
	if(root->child[2]!=NULL) find(root->child[2]);
	if(root->nodekind==IfK) {
//		printf("%d If Statement               ",root->count);
	}
	if(root->nodekind==Repk) {
//		printf("%d Repeat Statement           ",root->count);
	}	
	else if(root->nodekind==AssignK){
//		printf("%d  Assign Statement           ",root->count);
	}
	else if(root->nodekind==IDk){
//		printf("%d  ID Declaration symbol %s ",root->count,root->name);
	}
	else if(root->nodekind==8){
//		printf("%d  Const Declaration value %d ",root->count,root->val);
	}	                                                               
	else if(root->nodekind==Exprk) {
//		printf("%d  Expr          op%d         ",root->count,root->op);			
	}
	else if(root->nodekind==Com){
//		printf("%d  Compound Statement         ",root->count);			
	}                         
	else if(root->nodekind==Vark){
//		printf("%d  Var Declaration            ",root->count);			
	}
	else if(root->nodekind==Typek){
//		printf("%d  Type Specifier %s ",root->count,root->name);
	}
	else if(root->nodekind==Prink){
//		printf("%d Print Statement           ",root->count);
	}
//	printf("Children :");       
	output(root);
//	printf("\n");
	if(root->sibling!=NULL) {
		find(root->sibling);
//		output(root->sibling);
	}
		
}
void header()
{
    fprintf(out,"Welcome to GLY's complier. StudentID:0510615   Computer Science\n");  
    fprintf(out,".586\n.model flat, stdcall\noption casemap :none\ninclude \\masm32\\include\\windows.inc\ninclude \\masm32\\include\\user32.inc\ninclude \\masm32\\include\\kernel32.inc\n");
	fprintf(out,"include \\masm32\\include\\masm32.inc\nincludelib \\masm32\\lib\\user32.lib\nincludelib \\masm32\\lib\\kernel32.lib\n	includelib \\masm32\\lib\\masm32.lib\n");
	fprintf(out,".data\n");
	fprintf(out,"buffer BYTE 128 dup(0)\n");
	fprintf(out,"LF BYTE 13, 10, 0\n");

}
void genVari(TreeNode *root){
	if(root->nodekind==IDk)
 	fprintf(out,"%s",root->name);
	else if(root->nodekind==Constk)
	fprintf(out,"%d",root->val);
	else 
	fprintf(out,"_t%d",root->count);
}
void genStmt(TreeNode * root){
//	switch(root->nodekind){}
	int l1,l2;
	if(root->nodekind==Com){
		cGen(root->child[0]);
	}
	else if(root->nodekind==Prink){
		genExp(root->child[0]);
//		if(root->child[0]->nodekind==IDk)
//		fprintf(out,"mov eax,%s\n",root->child[0]->name);
//		else 
//		fprintf(out,"mov eax,_t%d\n",root->child[0]->count);

		fprintf(out,"	mov eax,");
		genVari(root->child[0]);
		fprintf(out,"\n");
		
		fprintf(out,"	invoke dwtoa, eax, ADDR buffer\n");
		fprintf(out,"	invoke StdOut, ADDR buffer\n");
		fprintf(out,"	invoke StdOut, ADDR LF\n");		
	}
	else if(root->nodekind==Inputk){
		fprintf(out,"	invoke StdIn, ADDR buffer, 127\n");
		fprintf(out,"	invoke StripLF, ADDR buffer\n");
		fprintf(out,"	invoke atodw, ADDR buffer\n");
		fprintf(out,"	mov %s, eax\n",root->child[0]->name);
	}	
	else if(root->nodekind==AssignK)
	{
		//cGen(root->root[0]);
		genExp(root->child[1]);
		
//		if(root->child[1]->nodekind==IDk)
//		fprintf(out,"mov eax,%s\n",root->child[1]->name);
//		else 
//		fprintf(out,"mov eax,_t%d\n",root->child[1]->count);
//		genVari(root->child[1],"eax");

		fprintf(out,"	mov eax,");
		genVari(root->child[1]);
		fprintf(out,"\n");
		
	
		fprintf(out,"	mov %s,eax\n",root->child[0]->name);		
	}
	else if(root->nodekind==Repk){
		//*********************************************
		l1=label;l2=label+1;
		label=label+2;
		
		fprintf(out,"@%d:\n",l1);
		genExp(root->child[0]);
		
/*		if(root->child[0]->nodekind==IDk)
		fprintf(out,"cmp %s,0\n",root->child[0]->name);
		else if(root->child[0]->nodekind==Constk)
		fprintf(out,"cmp %d,0\n",root->child[0]->val);
		else 
		fprintf(out,"cmp _t%d,0\n",root->child[0]->count);
*/
		fprintf(out,"	cmp ");
		genVari(root->child[0]);
		fprintf(out,",0\n");
		
		
		fprintf(out,"	jz @%d\n",l2);
		cGen(root->child[1]);
		fprintf(out,"	jmp @%d\n",l1);
		fprintf(out,"@%d:\n",l2);
	}
	else if(root->nodekind==IfK){
		//*********************************************
		l1=label;l2=label+1;
		label=label+2;
		
		fprintf(out,"@%d:\n",l1);
		genExp(root->child[0]);
		
		fprintf(out,"	cmp ");
		genVari(root->child[0]);
		fprintf(out,",0\n");
		
/*		if(root->child[0]->nodekind==IDk)
		fprintf(out,"cmp %s,0\n",root->child[0]->name);
		else if(root->child[0]->nodekind==Constk)
		fprintf(out,"cmp %d,0\n",root->child[0]->val);
		else 
		fprintf(out,"cmp _t%d,0\n",root->child[0]->count);*/
		
		
		fprintf(out,"	jz @%d\n",l2);
		cGen(root->child[1]);
//		fprintf(out,"jmp @%d\n",l1);
		fprintf(out,"@%d:\n",l2);
	}	
	return ;

}
void genExp(TreeNode * root){
//	switch(root->nodekind){}
	int l1,l2;
//	printf("oooooooooooooooooooops   %d\n",root->nodekind);
	if(root->nodekind==Constk){
//		printf("oooooooooooooooooooops\n");
		//fprintf(out,"mov _t%d ,%d\n",root->count,root->val);
	}
	else if(root->nodekind==IDk){
	}
	else if(root->nodekind==Exprk){
		cGen(root->child[0]);
///////////////////////////////////		
//		if(root->child[0]->nodekind==IDk)
//			fprintf(out,"mov eax , %s\n",root->child[0]->name);
//		else 
//			fprintf(out,"mov eax , _t%d\n",root->child[0]->count);
		fprintf(out,"	mov eax,");
		genVari(root->child[0]);
		fprintf(out,"\n");		
		
		fprintf(out,"	mov _t%d , eax\n",root->count);
		if(root->child[1]!=NULL){
			cGen(root->child[1]);
			
			fprintf(out,"	mov ebx,");
			genVari(root->child[1]);
			fprintf(out,"\n");		
			
//			if(root->child[1]->nodekind==IDk)
//				fprintf(out,"mov ebx , %s\n",root->child[1]->name);
//			else 
//				fprintf(out,"mov ebx , _t%d\n",root->child[1]->count);		
		}
			
//////////////////////////////////
		if(root->op==PLUS){
			fprintf(out,"	add _t%d ,ebx\n",root->count);
		}
		else if(root->op==MINUS){
			fprintf(out,"	sub _t%d ,ebx\n",root->count);
		}
		else if(root->op==TIMES){
			fprintf(out,"	mul ebx\n");
		
			fprintf(out,"	mov ");
			genVari(root);
			fprintf(out,",eax\n");
			
/*			if(root->nodekind==IDk)
				fprintf(out,"mov %s,eax\n",root->name);
			else 
				fprintf(out,"mov _t%d,eax\n",root->count);			*/
		}
		else if(root->op==OVER){
			if(root->child[0]->nodekind==IDk)
				fprintf(out,"	mov eax, %s\n",root->child[0]->name);
			else 
				fprintf(out,"mov eax, _t%d\n",root->child[0]->count);
				
			if(root->child[1]->nodekind==IDk)
				fprintf(out,"div %s\n",root->child[1]->name);
			else 
				fprintf(out,"div _t%d\n",root->child[1]->count);
			
			if(root->nodekind==IDk)
				fprintf(out,"mov %s,eax\n",root->name);
			else 
				fprintf(out,"mov _t%d,eax\n",root->count);			
		}
		else if(root->op==LT){
//			printf("*******************************\n");
			l1=label;l2=label+1;label+=2;
			fprintf(out,"	cmp eax,ebx\n");
			fprintf(out,"	jl @%d\n",l1);
			fprintf(out,"	mov eax,0\n");
			fprintf(out,"	jmp @%d\n",l2);
			fprintf(out,"@%d:\n",l1);
			fprintf(out,"	mov eax,1\n");						
			fprintf(out,"@%d:\n",l2);
			
			fprintf(out,"	mov ");
			genVari(root);
			fprintf(out,",eax\n");		
			
			/*if(root->nodekind==IDk)
			
			fprintf(out,"mov %s,eax\n",root->name);
			else 
			fprintf(out,"mov _t%d,eax\n",root->count);			*/
		}
		
		else if(root->op==EQ_OP){
//			printf("*******************************\n");
			l1=label;l2=label+1;label+=2;
			fprintf(out,"	cmp eax,ebx\n");
			fprintf(out,"	jz @%d\n",l1);
			fprintf(out,"	mov eax,0\n");
			fprintf(out,"	jmp @%d\n",l2);
			fprintf(out,"@%d:\n",l1);
			fprintf(out,"	mov eax,1\n");						
			fprintf(out,"@%d:\n",l2);
			
			fprintf(out,"	mov ");
			genVari(root);
			fprintf(out,",eax\n");		
/*			if(root->nodekind==IDk)
			fprintf(out,"mov %s,eax\n",root->name);
			else 
			fprintf(out,"mov _t%d,eax\n",root->count);			
			*/
		}		
		else if(root->op==GT){
//			printf("*******************************\n");
			l1=label;l2=label+1;label+=2;
			fprintf(out,"	cmp eax,ebx\n");
			fprintf(out,"	jg @%d\n",l1);
			fprintf(out,"	mov eax,0\n");
			fprintf(out,"	jmp @%d\n",l2);
			fprintf(out,"@%d:\n",l1);
			fprintf(out,"	mov eax,1\n");						
			fprintf(out,"@%d:\n",l2);
			
/*			if(root->nodekind==IDk)
			fprintf(out,"mov %s,eax\n",root->name);
			else 
			fprintf(out,"mov _t%d,eax\n",root->count);			*/
			fprintf(out,"	mov ");
			genVari(root);
			fprintf(out,",eax\n");				
		}		
		else if(root->op==LE){
//			printf("*******************************\n");
			l1=label;l2=label+1;label+=2;
			fprintf(out,"	cmp eax,ebx\n");
			fprintf(out,"	jle @%d\n",l1);
			fprintf(out,"	mov eax,0\n");
			fprintf(out,"	jmp @%d\n",l2);
			fprintf(out,"@%d:\n",l1);
			fprintf(out,"	mov eax,1\n");						
			fprintf(out,"@%d:\n",l2);

			fprintf(out,"	mov ");
			genVari(root)	;
			fprintf(out,",eax\n");		
/*						
			if(root->nodekind==IDk)
			fprintf(out,"mov %s,eax\n",root->name);
			else 
			fprintf(out,"mov _t%d,eax\n",root->count);			*/
		}
		else if(root->op==GE){
//			printf("*******************************\n");
			l1=label;l2=label+1;label+=2;
			fprintf(out,"	cmp eax,ebx\n");
			fprintf(out,"	jge @%d\n",l1);
			fprintf(out,"	mov eax,0\n");
			fprintf(out,"	jmp @%d\n",l2);
			fprintf(out,"@%d:\n",l1);
			fprintf(out,"	mov eax,1\n");						
			fprintf(out,"@%d:\n",l2);
			
			fprintf(out,"	mov ");
			genVari(root);
			fprintf(out,",eax\n");
			
/*			if(root->nodekind==IDk)
			fprintf(out,"mov %s,eax\n",root->name);
			else 
			fprintf(out,"mov _t%d,eax\n",root->count);			*/
		}		
		else if(root->op==NEQ){
//			printf("*******************************\n");
			l1=label;l2=label+1;label+=2;
			fprintf(out,"	cmp eax,ebx\n");
			fprintf(out,"	jne @%d\n",l1);
			fprintf(out,"	mov eax,0\n");
			fprintf(out,"	jmp @%d\n",l2);
			fprintf(out,"@%d:\n",l1);
			fprintf(out,"	mov eax,1\n");						
			fprintf(out,"@%d:\n",l2);
			
			fprintf(out,"	mov ");
			genVari(root);
			fprintf(out,",eax\n");		
/*			if(root->nodekind==IDk)
			fprintf(out,"mov %s,eax\n",root->name);
			else 
			fprintf(out,"mov _t%d,eax\n",root->count);*/
		}		
		else if(root->op==NOT){
//			printf("***********zzzzzzzzzzz******\n");
			l1=label;l2=label+1;label+=2;
			fprintf(out,"	cmp eax,0\n");
			fprintf(out,"	jz @%d\n",l1);
			fprintf(out,"	mov eax,0\n");
			fprintf(out,"	jmp @%d\n",l2);
			fprintf(out,"@%d:\n",l1);
			fprintf(out,"	mov eax,1\n");						
			fprintf(out,"@%d:\n",l2);
			
			fprintf(out,"	mov ");
			genVari(root);
			fprintf(out,",eax\n");		
/*			if(root->nodekind==IDk)
			fprintf(out,"mov %s,eax\n",root->name);
			else 
			fprintf(out,"mov _t%d,eax\n",root->count);		*/
		}
		else if(root->op==AND){
//			printf("*******************************\n");
			l1=label;l2=label+1;label+=2;
			fprintf(out,"	mov ecx,0\n");
			fprintf(out,"	cmp eax,0\n");
			fprintf(out,"	jz @%d\n",l1);
			fprintf(out,"	cmp ebx,0\n");
			fprintf(out,"	jz @%d\n",l1);
			fprintf(out,"	mov ecx,1\n");
			fprintf(out,"@%d:\n",l1);
			
			fprintf(out,"	mov ");
			genVari(root);
			fprintf(out,",ecx\n");	
			
/*			if(root->nodekind==IDk)
			fprintf(out,"mov %s,ecx\n",root->name);
			else 
			fprintf(out,"mov _t%d,ecx\n",root->count);*/
		}
		
		else if(root->op==OR){
//			printf("*******************************\n");
			l1=label;l2=label+1;label+=2;
			fprintf(out,"	mov ecx,1\n");
			fprintf(out,"	cmp eax,1\n");
			fprintf(out,"	jz @%d\n",l1);
			fprintf(out,"	cmp ebx,1\n");
			fprintf(out,"	jz @%d\n",l1);
			fprintf(out,"	mov ecx,0\n");
			fprintf(out,"@%d:\n",l1);
			
			fprintf(out,"	mov ");
			genVari(root);
			fprintf(out,",ecx\n");	
/*			if(root->nodekind==IDk)
			fprintf(out,"mov %s,ecx\n",root->name);
			else 
			fprintf(out,"mov _t%d,ecx\n",root->count);*/
		}		
	}
	return ;
	
}
static void cGen( TreeNode * tree)
{ if (tree != NULL)
  { switch (tree->kind) {
      case StmtKind:
//		printf("opoooooooooooooooo!\n");
//		printf("%d\n",tree->nodekind);
        genStmt(tree);
        break;
      case ExprKind:
        genExp(tree);
        break;
      default:
        break;
    }
    cGen(tree->sibling);
  }
}

int main(void)
{
    yyin=fopen("tmp.c","r");
    out= fopen( "code.asm", "w" );
    header();
    printf("欢迎来到高连永的编译器,编译器正在编译tmp.c文件,请稍后......\n");
	yyparse();
	
//	printTab();
	fprintf(out,".code\n");
//	find(savedTree);
	fprintf(out,"_start:\n");
	cGen(savedTree);
	fprintf(out,"invoke ExitProcess, 0\n");
	fprintf(out,"end _start\n");
	printf("编译完成,请打开code.asm文件查看相应的汇编代码......\n");
	return 0;
}

⌨️ 快捷键说明

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