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

📄 midcode.h

📁 编译原理实验之中间代码生成(四元式)
💻 H
字号:
// File Name: MidCode.h

#include "CharStack.h"
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>

struct MIDCODE
{
	char Op;
	WORDNODE *pArg1;
	WORDNODE *pArg2;
	WORDNODE *pResult;
}	m_MidCode[MAX_DATA_LEN];
int	m_nMidCodeLen;

void InitMidCode()
{
	m_nMidCodeLen = -1;
}

void Gen(char Op, WORDNODE *pArg1, WORDNODE *pArg2, WORDNODE *pResult)
{
	++m_nMidCodeLen;
	m_MidCode[m_nMidCodeLen].Op = Op;
	m_MidCode[m_nMidCodeLen].pArg1 = pArg1;
	m_MidCode[m_nMidCodeLen].pArg2 = pArg2;
	m_MidCode[m_nMidCodeLen].pResult = pResult;
}

void GetCodeString(WORDNODE *pWord, char cResult[])
{
	if (pWord == NULL)
	{
		cResult[0] = '\0';
		return;
	}

	if (pWord->byType != WT_TEMP)
	{
		memcpy(cResult, pWord->Value, MAX_DATA_LEN);
		return;
	}

	cResult[0] = 'T';
	_itoa(pWord->Value[0], &cResult[1], 10);
}

void PrintMidCode()
{
	char c[3][MAX_DATA_LEN];
	printf("生成四元式为:\n");

	for (int i = 0; i <= m_nMidCodeLen; i++)
	{
		GetCodeString(m_MidCode[i].pArg1, c[0]);
		GetCodeString(m_MidCode[i].pArg2, c[1]);
		GetCodeString(m_MidCode[i].pResult, c[2]);
		printf("%2d  (%c,%s,%s,%s)\n",  i, m_MidCode[i].Op, c[0], c[1], c[2]);
	}
}

⌨️ 快捷键说明

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