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

📄 translate.h

📁 pascal编译器
💻 H
📖 第 1 页 / 共 2 页
字号:
				strcpy(str,"int");
				break;
			case 1:
				strcpy(str,"float");
				break;
			case 2:
				strcpy(str,"bool");
				break;
			case 3:
				strcpy(str,"int");
				break;
			}
            strcpy(dec1_str,genid01(str));
			id_list1_p=0;
			break;
		case 1:
            switch (p3){
			case 0:
				strcpy(str,"int");
				break;
			case 1:
				strcpy(str,"float");
				break;
			case 2:
				strcpy(str,"bool");
				break;
			case 3:
				strcpy(str,"int");
				break;
			}
            strcpy(dec1_str,genid11(str));
			id_list1_p=0;
			break;
		case 2:
			strcpy(str,"struct {\n");
			strcat(str,struct_str);
			strcat(str,"}");
            strcpy(dec1_str,genid01(str));
			id_list1_p=0;
			break;
		}
		}
		strcpy(str,"");
        if (!val_pop(2)) translateError();
		break;          //declaration->identifier_list:type

	case 10:
        p1=0;
		break;          //type->standard_type
	case 11:
		p1=1;
		cpy(p2,(val_stack[val_point-4]->enter)->name);
        if (!val_pop(8)) translateError();
		break;          //type->array [digits..digits] of standard_type
	case 12:
		p1=2;
		if (!val_pop(2)) translateError();
		strcpy(struct_str,dec1_str);
		strcpy(dec1_str,"");
		break;          //type->record declaration end
	case 13:
		p3=0;
		break;          //standard_type->integer
	case 14:
		p3=1;
		break;          //standard_type->real
	case 15:
		p3=2;      
		break;          //standard_type->boolean
	case 16:
		p3=0;
        if (!val_pop(3)) translateError();
		break;          //standard_type->num..num
    case 17:
        if (!val_pop(2)) translateError();
		break;          //subproc_declarations->subproc_declarations subproc_declaration ;
	case 18:
		val_push(n);
		break;          //subproc_declarations->empty
	case 19:
		wf(com_str);
		wf("}\n");
		if (!val_pop(2)) translateError();
		break;          //subproc_declaration->subproc_head declarations compound_statement
	case 20:
        switch (p3){
			case 0:
				strcpy(str,"int ");
				break;
			case 1:
				strcpy(str,"float ");
				break;
			case 2:
				strcpy(str,"bool ");
				break;
			case 3:
				strcpy(str,"int ");
				break;
			}
        cat(str,(val_stack[val_point-5]->enter)->name);//val_stack[val_point-5]为id
		strcat(str,arg_str);
		strcat(str,"{\n");
		wf(str);
		if (!val_pop(5)) translateError();
		strcpy(str,"");
		strcpy(arg_str,"");
		break;           //subproc_head->function id arguments:standard_type;          
	case 21:
        strcpy(str,"void ");
        cat(str,(val_stack[val_point-3]->enter)->name);//val_stack[val_point-3]为Id
		strcat(str,arg_str);
		strcat(str,"{\n");
		wf(str);
		if (!val_pop(3)) translateError();
		strcpy(str,"");
		strcpy(arg_str,"");
        break;           //subproc_head->procedure id arguments;
	case 22:
        strcpy(arg_str,"(");
		strcat(arg_str,para_str);
		strcat(arg_str,")");
        if (!val_pop(2)) translateError();
		strcpy(para_str,"");
		break;           //arguments->(parameter_list)
	case 23:
		strcpy(arg_str,"()");
		val_push(n);
		break;           //arguments->empty
	case 24:
        strcpy(aparastr,"");
        switch (p3){
			case 0:
				strcpy(str,"int");
				break;
			case 1:
				strcpy(str,"float");
				break;
			case 2:
				strcpy(str,"bool");
				break;
			case 3:
				strcpy(str,"int");
				break;
			}
        genparastr(aparastr);
		strcpy(str,"");
		strcat(para_str,",");
		strcat(para_str,aparastr);
        if (!val_pop(4)) translateError();
		id_list_p=0;
		break;           //parameter_list->parameter_list;identifier:type
	case 25:
		strcpy(para_str,"");
        switch (p3){
			case 0:
				strcpy(str,"int");
				break;
			case 1:
				strcpy(str,"float");
				break;
			case 2:
				strcpy(str,"bool");
				break;
			case 3:
				strcpy(str,"int");
				break;
			}
        genparastr(para_str);
		strcpy(str,"");
		if (!val_pop(2)) translateError();
		id_list_p=0;
		break;           //parameter_list->identifier_list:type
	case 26:
        strcpy(com_str,"");
		strcat(com_str,"{\n");
		strcat(com_str,opt_str);
		strcat(com_str,"}\n");
		strcpy(opt_str,"");
        if (!val_pop(2)) translateError();
		break;           //compound_statement->begin optional_statements end
	case 27:
		strcpy(opt_str,sta_str);
		strcpy(sta_str,"");
		break;           //optional_statements->statement_list
	case 28:
		strcpy(opt_str,"");
        val_push(n);
		break;           //optional_statements->empty
	case 29:
		strcat(sta_str,stat_stack[stat_point-1]);
		if (!stat_pop(1)) translateError();
        if (!val_pop(2)) translateError();
		break;           //statement_list->statement_list;statement
	case 30:
		strcpy(sta_str,stat_stack[stat_point-1]);
        if (!stat_pop(1)) translateError();
		break;           //statement_list->statement
	case 31:
        if (is_func != 1){
        strcpy(str,"");
		strcat(str,var_str);
		strcat(str,"=");
		strcat(str,exp_stack[exp_point-1]);
		exp_pop(1);
		strcat(str,";\n");}
		else {
		strcpy(str,"return (");
		strcat(str,exp_stack[exp_point-1]);
		exp_pop(1);
		strcat(str,");\n");
        }
		stat_push(str);
		strcpy(str,"");
        if (!val_pop(2)) translateError();
		break;           //statement->variable assignop expression
	case 32:
		strcpy(str,"");
		strcat(str,pro_str);
		strcat(str,";\n");
		stat_push(str);
		strcpy(str,"");
		break;           //statement->procedure_statement
	case 33:
        stat_push(com_str);
		break;           //statement->compound_statement
	case 34:
        strcpy(str,"");
		strcat(str,"if (");
		strcat(str,exp_stack[exp_point-1]);
		exp_pop(1);
		strcat(str,") ");
		strcat(str,stat_stack[stat_point-2]);
		strcat(str,"else ");
		strcat(str,stat_stack[stat_point-1]);
		if (stat_pop(2)) stat_push(str);
		else translateError();
		strcpy(str,"");
        if (!val_pop(5)) translateError();
        break;           //statement->if expression then statement else statement
	case 35:
		strcpy(str,"");
		strcat(str,"while (");
		strcat(str,exp_stack[exp_point-1]);
		exp_pop(1);
		strcat(str,") ");
        strcat(str,stat_stack[stat_point-1]);
        if (stat_pop(1)) stat_push(str);
		else translateError();
        strcpy(str,"");
        if (!val_pop(3)) translateError();
		break;           //statement->while expression do statement
	case 36:
        //只能读十进制整型^_^
        strcpy(str,"scanf(\"");
        for (i=0;i<exp_num;i++){
	    strcat(str,"%d");	
		}
		strcat(str,"\",");
		strcat(str,exl_str);
		strcat(str,");\n");
		stat_push(str);
		exp_num=0;
        if (!val_pop(3)) translateError();
		break;           //statement->read(expression_list)
	case 37:
        //只能写十进制整型^_^
        strcpy(str,"printf(\"");
        for (i=0;i<exp_num;i++){
	    strcat(str,"%d");	
		}
		strcat(str,"\\n");
		strcat(str,"\",");
		strcat(str,exl_str);
		strcat(str,");\n");
		stat_push(str);
		exp_num=0;
		if (!val_pop(3)) translateError();
		break;           //statement->write(expression_list)
	case 38:
		is_func=(val_stack[val_point-1]->enter)->function_type;
		cpy(var_str,(val_stack[val_point-1]->enter)->name);
		break;           //variable->id
	case 39:
		is_func=(val_stack[val_point-4]->enter)->function_type;
        cpy(var_str,(val_stack[val_point-4]->enter)->name);
		strcat(var_str,"[(");
		strcat(var_str,exp_stack[exp_point-1]);
		exp_pop(1);
		strcat(var_str,")-1]");
		strcpy(exp_str,"");
        if (!val_pop(3)) translateError();
		break;           //variable->id[expression]
	case 40:
		cpy(pro_str,(val_stack[val_point-1]->enter)->name);
		strcat(pro_str,"()");
		break;           //procedure_statement->id
	case 41:
		cpy(pro_str,(val_stack[val_point-4]->enter)->name);
		strcat(pro_str,"(");
		strcat(pro_str,exl_str);
		strcat(pro_str,")");
		strcpy(exl_str,"");
        if (!val_pop(3)) translateError();
		exp_num=0;
		break;           //procedure_statement->id(expression_list)
	case 42:
		if (!is_red){
		strcat(exl_str,",");
		strcat(exl_str,exp_stack[exp_point-1]);
		exp_pop(1);
		strcpy(exp_str,"");}
		else{
		strcat(exl_str,",&");
		strcat(exl_str,exp_stack[exp_point-1]);
		exp_pop(1);
		strcpy(exp_str,"");
		}
        if (!val_pop(2)) translateError();
		exp_num++;
		break;           //expression_list->expression_list,expression
	case 43:
		if (!is_red){
		strcpy(exl_str,exp_stack[exp_point-1]);
		exp_pop(1);
		strcpy(exp_str,"");}
		else{
		strcpy(exl_str,"&");
		strcat(exl_str,exp_stack[exp_point-1]);
		exp_pop(1);
		strcpy(exp_str,"");		
		}
		exp_num++;
		break;           //expression_list->expression
	case 44:
        strcpy(exp_str,"");
		strcat(exp_str,sim_stack[sim_point-2]);
		switch (val_stack[val_point-2]->label_type){
		case 31:
			strcpy(relop,"<");
			break;
		case 32:
			strcpy(relop,"<=");
			break;
		case 33:
			strcpy(relop,">");
			break;
		case 34:
			strcpy(relop,">=");
			break;
		case 35:
			strcpy(relop,"==");
			break;
		case 36:
			strcpy(relop,"!=");
			break;
		}
		strcat(exp_str,relop);
		strcat(exp_str,sim_stack[sim_point-1]);
		exp_push(exp_str);
		if (!sim_pop(2)) error;
		if (!val_pop(2)) translateError();
		break;           //expression->simple_expression relop simple_expression
	case 45:
        strcpy(exp_str,"");
        strcat(exp_str,sim_stack[sim_point-1]);
        if (!sim_pop(1)) error;
		exp_push(exp_str);
		break;           //expression->simple_expression
	case 46:
		sim_push(ter_str);
		break;           //simple_expression->term
	case 47:
		strcpy(str,"");
		strcat(str,sig_str);
		strcat(str,ter_str);
        sim_push(str);
        strcpy(str,"");
        if (!val_pop(1)) translateError();
		break;           //simple_expression->sign term
	case 48:
        strcpy(str,"");
		strcat(str,sim_stack[sim_point-1]);
		switch (val_stack[val_point-2]->label_type){
		case 27:
			strcpy(addop,"+");
			break;       //"+"
		case 28:
			strcpy(addop,"-");
			break;       //"-"
		case 16:
			strcpy(addop,"||");
			break;       //||
		}
		strcat(str,addop);
		strcat(str,ter_str);
		if (sim_pop(1)) sim_push(str);
		else translateError();
        if (!val_pop(2)) translateError();
        strcpy(str,"");
		strcpy(ter_str,"");
		break;           //simple_expression->simple_expression addop term
	case 49:
		switch (val_stack[val_point-2]->label_type){
		case 29:
			strcpy(mulop,"*");
			break;
		case 30:
			strcpy(mulop,"/");
			break;
		case 55:
			strcpy(mulop,"%");
			break;
		case 54:
			strcpy(mulop,"/");
			break;
		case 3:
			strcpy(mulop,"&&");
			break;       
		}
		strcat(ter_str,mulop);
		strcat(ter_str,fac_str);
		strcpy(fac_str,"");
        if (!val_pop(2)) translateError();
		break;           //term->term mulop factor
	case 50:
		strcpy(ter_str,fac_str);
		strcpy(fac_str,"");
		break;           //term->factor
	case 51:
		cpy(fac_str,(val_stack[val_point-1]->enter)->name);
		break;           //factor->id
	case 52:
		cpy(fac_str,(val_stack[val_point-4]->enter)->name);
		strcat(fac_str,"(");
		strcat(fac_str,exl_str);
		strcat(fac_str,")");
		strcpy(exl_str,"");
        if (!val_pop(3)) translateError();
		exp_num=0;
		break;           //factor->id(expression_list)
	case 53:
		cpy(fac_str,(val_stack[val_point-1]->enter)->name);
		break;           //factor->num
	case 54:
		strcpy(fac_str,"(");
		strcat(fac_str,exp_stack[exp_point-1]);
		exp_pop(1);
		strcat(fac_str,")");
		strcpy(exp_str,"");
        if (!val_pop(2)) translateError();
		break;           //factor->(expression)
	case 55:
		strcpy(str,"!");
		strcat(str,fac_str);
		strcpy(fac_str,str);
		strcpy(str,"");
        if (!val_pop(1)) translateError();
		break;           //factor->not factor
	case 56:
		strcpy(fac_str,"true");
		break;           //factor->true
	case 57:
		strcpy(fac_str,"false");
		break;           //factor->false
	case 58:
		strcpy(sig_str,"+");
		break;           //sign->+
	case 59:
		strcpy(sig_str,"-");
		break;           //sign->-
}
}

#endif

⌨️ 快捷键说明

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