提交 fbf026ab 创建 作者: 宋海霞's avatar 宋海霞

modify

上级 5f18bb2d
#ifndef T2P_H_
#define T2P_H_
//
// 在此处包含 C 标准库头文件
//
#include <stdio.h>
//
// 在此处包含其他头文件
//
......@@ -19,26 +16,28 @@
// 在此处定义数据结构
//
#define MAX_STR_LENGTH 64
#define MAX_CODE_COUNT 64
// 地址类型
typedef enum _AddrKind{
typedef enum _AddrKind
{
empty, // 空
intconst, // 整数常量
string // 字符串
}AddrKind;
// 地址
typedef struct _Address{
typedef struct _Address
{
AddrKind Kind; // 地址的类型。这个域是一个枚举类型
int Value; // 地址的值。地址类型为整数常量时,这个域有效
char Name[MAX_STR_LENGTH]; // 地址的值。地址类型为字符串时,这个域有效
}Address;
// 三地址码指令类型
typedef enum _TOpKind{
typedef enum _TOpKind
{
t_rd = 1, // 注意,从 1 开始
t_gt,
t_if_f,
......@@ -52,13 +51,15 @@ typedef enum _TOpKind{
}TOpKind;
// 三地址码
typedef struct _TCode{
typedef struct _TCode
{
TOpKind Kind; // 三地址码指令类型
Address Addr1, Addr2, Addr3; // 三个地址
}TCode;
// P-代码指令类型
typedef enum _POpKind{
typedef enum _POpKind
{
p_lda = 1, // 注意,从 1 开始
p_rdi,
p_lod,
......@@ -75,23 +76,21 @@ typedef enum _POpKind{
}POpKind;
// P-代码
typedef struct _PCode{
typedef struct _PCode
{
POpKind Kind; // P-代码指令类型
Address Addr; // 地址
}PCode;
//
// 在此处声明函数
//
void T2P(TCode* TCodeList, PCode* PCodeList);
void InitTCodeList(TCode* pTCodeList);
void InitTCodeList_CI(TCode* pTCodeList);
void OutputResult(PCode* PCodeList);
//
// 在此声明全局变量
//
......
......@@ -87,7 +87,7 @@ static const TCodeEntry TCodeTable[] =
void InitAddress(const AddressEntry* pEntry, Address* pAddr)
{
pAddr->Kind = pEntry->Kind;
switch(pAddr->Kind)
switch (pAddr->Kind)
{
case empty:
break;
......@@ -112,7 +112,7 @@ void InitTCodeList(TCode* pTCodeList)
int i;
int EntryCount = sizeof(TCodeTable) / sizeof(TCodeTable[0]);
for(i = 0; i < EntryCount; i++)
for (i = 0; i < EntryCount; i++)
{
pTCodeList[i].Kind = TCodeTable[i].Kind;
......@@ -131,108 +131,108 @@ void InitTCodeList(TCode* pTCodeList)
*/
void InitTCodeList_CI(TCode* pTCodeList)
{
for(int i = 0; i < MAX_CODE_COUNT; i++)
for (int i = 0; i < MAX_CODE_COUNT; i++)
{
gets(TCodeArray[i]);
int length = strlen(TCodeArray[i]);
if(length == 0)
if (length == 0)
{
break;
}
char unitArr[7][64] = {};
int k = 0, l = 0;
for(int j = 0; j < 256; j++)
int k = 0, w = 0;
for (int j = 0; j < 256; j++)
{
if(TCodeArray[i][j] == '\0')
if (TCodeArray[i][j] == '\0')
{
break;
}
if(TCodeArray[i][j] == 32)
if (TCodeArray[i][j] == 32)
{
k++;
l = 0;
w = 0;
continue;
}
unitArr[k][l++] = TCodeArray[i][j];
unitArr[k][w++] = TCodeArray[i][j];
}
char kind[64] = {0};
strcpy(kind, unitArr[0]);
if(strcmp(kind, "t_rd") == 0)
if (strcmp(kind, "t_rd") == 0)
{
pTCodeList[i].Kind = t_rd;
}
else if(strcmp(kind, "t_gt") == 0)
else if (strcmp(kind, "t_gt") == 0)
{
pTCodeList[i].Kind = t_gt;
}
else if(strcmp(kind, "t_if_f") == 0)
else if (strcmp(kind, "t_if_f") == 0)
{
pTCodeList[i].Kind = t_if_f;
}
else if(strcmp(kind, "t_asn") == 0)
else if (strcmp(kind, "t_asn") == 0)
{
pTCodeList[i].Kind = t_asn;
}
else if(strcmp(kind, "t_lab") == 0)
else if (strcmp(kind, "t_lab") == 0)
{
pTCodeList[i].Kind = t_lab;
}
else if(strcmp(kind, "t_mul") == 0)
else if (strcmp(kind, "t_mul") == 0)
{
pTCodeList[i].Kind = t_mul;
}
else if(strcmp(kind, "t_sub") == 0)
else if (strcmp(kind, "t_sub") == 0)
{
pTCodeList[i].Kind = t_sub;
}
else if(strcmp(kind, "t_eq") == 0)
else if (strcmp(kind, "t_eq") == 0)
{
pTCodeList[i].Kind = t_eq;
}
else if(strcmp(kind, "t_wri") == 0)
else if (strcmp(kind, "t_wri") == 0)
{
pTCodeList[i].Kind = t_wri;
}
else if(strcmp(kind, "t_halt") == 0)
else if (strcmp(kind, "t_halt") == 0)
{
pTCodeList[i].Kind = t_halt;
}
for(int j = 1; j < k; j += 2)
for (int j = 1; j < k; j += 2)
{
if(strcmp(unitArr[j], "string") == 0)
if (strcmp(unitArr[j], "string") == 0)
{
if(j == 1)
if (j == 1)
{
pTCodeList[i].Addr1.Kind = string;
strcpy(pTCodeList[i].Addr1.Name,unitArr[j + 1]);
strcpy(pTCodeList[i].Addr1.Name, unitArr[j + 1]);
}
else if(j == 3)
else if (j == 3)
{
pTCodeList[i].Addr2.Kind = string;
strcpy(pTCodeList[i].Addr2.Name,unitArr[j + 1]);
strcpy(pTCodeList[i].Addr2.Name, unitArr[j + 1]);
}
else if(j == 5)
else if (j == 5)
{
pTCodeList[i].Addr3.Kind = string;
strcpy(pTCodeList[i].Addr3.Name,unitArr[j + 1]);
strcpy(pTCodeList[i].Addr3.Name, unitArr[j + 1]);
}
}
else if(strcmp(unitArr[j], "intconst") == 0)
else if (strcmp(unitArr[j], "intconst") == 0)
{
if(j == 1)
if (j == 1)
{
pTCodeList[i].Addr1.Kind = intconst;
pTCodeList[i].Addr1.Value = atoi(unitArr[j + 1]);
}
else if(j == 3)
else if (j == 3)
{
pTCodeList[i].Addr2.Kind = intconst;
pTCodeList[i].Addr2.Value = atoi(unitArr[j + 1]);
}
else if(j == 5)
else if (j == 5)
{
pTCodeList[i].Addr3.Kind = intconst;
pTCodeList[i].Addr3.Value = atoi(unitArr[j + 1]);
......@@ -253,9 +253,9 @@ void InitTCodeList_CI(TCode* pTCodeList)
void OutputResult(PCode* PCodeList)
{
for(int PIndex = 0; PCodeList[PIndex].Kind != 0; PIndex++)
for (int PIndex = 0; PCodeList[PIndex].Kind != 0; PIndex++)
{
switch(PCodeList[PIndex].Kind)
switch (PCodeList[PIndex].Kind)
{
case p_lda:
printf("lda%c%c%s\n", 32, 32, PCodeList[PIndex].Addr.Name);
......
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论