📄 yuyi.cpp
字号:
#include<stdio.h>
#include<string.h>
#define END 1024
//符号表
typedef struct Symbol
{
char idname[10]; //标识符名称
char type[10]; //标识符类型
}Symbol;
//四元式
struct sentence
{
char op[8];
char argu1[8];
char argu2[8];
char result[8];
int flag; //用来区分jmp指令和非jmp指令
int jmpforth; //jmp指令比较特殊,它的第四个域是整型
};
struct sentence sen[256];
typedef struct DigitTable
{
char digitlist[16][4];
int quantity;
}DigitTable;
DigitTable DT;
typedef struct SymbolTable
{
Symbol SB[32];
int quantity;
}SymbolTable;
SymbolTable ST;
typedef struct DStack
{
int entry[64];
int top;
}DStack;
DStack DS;
FILE* p;
FILE* q;
FILE* r;
int currword, entry;
int varicount = 0;
int sencount = 0;
char newtemp[64][4] = {"t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7", "t8", "t9", "t10", "t11"};
char E_place[4], E1_p[4];
char relop_op[4], relop_id[4];
int B_truelist, B_falselist, B1_tlist, B1_flist;
int U_nextlist, U_head;
void K();
int L();
int L1(int L1_nextlist);
int M();
int S();
void S1();
void U();
int W();
int C();
char* T();
void D(int entry);
void D1();
int N();
int P();
void B();
void B1(int B1_truelist, int B1_falselist);
void relop(char* idname);
void E(int entrance);
void E1(char* Eplace1);
void InitStack();
void ReadidTable();
void ReaddigTable();
void Pop();
void Push(int entry);
void FillTable(char* type, int i);
void write(char* op, char* argu1, char* argu2, char* result);
void backpatch(int head, int quad);
int merge(int list1, int list2);
int main()
{
if ((p = fopen("token.txt", "r")) == NULL)
{
printf( "Cannot open output file.\n");
return 0;
}
if((q = fopen("id.txt", "r")) == NULL)
{
printf( "Cannot open output file.\n");
return 0;
}
if((r = fopen("digit.txt", "r")) == NULL)
{
printf( "Cannot open output file.\n");
return 0;
}
InitStack();
ReadidTable();
ReaddigTable();
K(); //进入第一个产生式
printf("本程序的四元式如下:\n");
for(int i = 0; i < sencount; ++i)
{
if(sen[i].flag) //jmp产生式
printf("(%d)(%s,%s,%s,%d)\n", i, sen[i].op, sen[i].argu1, sen[i].argu2, sen[i].jmpforth);
else
printf("(%d)(%s,%s,%s,%s)\n", i, sen[i].op, sen[i].argu1, sen[i].argu2, sen[i].result);
}
printf("本程序的符号表如下:\n");
for(int i = 0; i < ST.quantity; ++i)
{
if(strcmp(ST.SB[i].type, "$"))
printf("%d %s %s\n", i, ST.SB[i].idname, ST.SB[i].type);
else
printf("%d %s " "\n", i, ST.SB[i].idname);
}
return 0;
}
void K()
{
int L_nextlist;
fscanf(p, "%d %d", &currword, &entry);
if(currword == 1) //关键字func
{
fscanf(p, "%d %d", &currword, &entry);
if(currword == 11) //标识符
{
fscanf(p, "%d %d", &currword, &entry);
if(currword == 12) //{
{
fscanf(p, "%d %d", &currword, &entry);
L_nextlist = L();
}
else
{
printf("Error! { lost\n");
}
if(currword == 13) //}
{
//执行语义动作
//{backpatch(L.nextlist, nextquad);
// emit("end"," "," ",0);}
//*********************************
backpatch(L_nextlist, sencount);
write("end"," "," ","0");
//*********************************
fscanf(p, "%d %d", &currword, &entry);
}
else
{
printf("Error! } lost\n");
}
}
else
{
printf("Error! There should be a id here\n");
}
}
else
{
printf("Error! There should be a string func here\n");
}
}
int L()
{
int S_nextlist, L1_nlist, L_nextlist, L1_nextlist;
S_nextlist = S();
//执行语义动作
//{L1.nextlist = S.nextlist}
//**************************
L1_nextlist = S_nextlist;
//**************************
L1_nlist = L1(L1_nextlist);
//执行语义动作
//{L.nextlist = L1_nlist}
//************************
L_nextlist = L1_nlist;
//************************
return L_nextlist;
}
int L1(int L1_nextlist)
{
int M_quad, S_nextlist, L11_nextlist, L1_nlist, L11_nlist;
M_quad = M();
if(currword == 12 || currword == 2 || currword == 4 || currword == 9 || currword == 11)
{
S_nextlist = S();
//执行语义动作
//{backpacth(L1.nextlist, M_quad);
// L11.nextlist = S.nextlist;}
//****************************
backpatch(L1_nextlist, M_quad);
L11_nextlist = S_nextlist;
//****************************
L11_nlist = L1(L11_nextlist);
L1_nlist = L11_nlist;
//printf("L'' -> SL''\n");
}
else if(currword != 13 && currword != 6)
{
printf("Error! “}” or “else” lost");
}
else
{
L1_nlist = L1_nextlist;
}
return L1_nlist;
}
int M()
{
int M_quad;
//执行语义动作
//{M.quad = nextquad;}
//***********************
M_quad = sencount;
//***********************
return M_quad;
}
int S()
{
char type[16];
char idname[8];
int S_nextlist, S1_nextlist, C_nextlist, L_nextlist;
if(currword == 12) //S-〉{L}
{
fscanf(p, "%d %d", &currword, &entry);
L_nextlist = L();
if(currword == 13)
{
//执行语义动作
//{S.nextlist = L.nextlist}
//**************************
S_nextlist = L_nextlist;
//**************************
fscanf(p, "%d %d", &currword, &entry);
}
else
{
printf("Error! } lost");
}
}
else if(currword == 2) //S -> US
{
fscanf(p, "%d %d", &currword, &entry);
U();
S1_nextlist = S();
//执行语义动作
//{backpatch(S(后).nextlist, U.head);
// emit(j, , ,U.head)
// S(前).nextlist = U.nextlist;
//************************************
backpatch(S1_nextlist, U_head);
write("j"," "," "," ");
sen[sencount - 1].jmpforth = U_head;
S_nextlist = U_nextlist;
//************************************
}
else if(currword == 4) //S -> CSS'
{
fscanf(p, "%d %d", &currword, &entry);
//执行语义动作
//{S_nextlist = merge(C_nextlist, S1_nextlist);}
//*************************************************
C_nextlist = C();
S1_nextlist = S();
S_nextlist = merge(C_nextlist, S1_nextlist);
//*************************************************
//S1();
}
else if(currword == 9) //S -> TD
{
fscanf(p, "%d %d", &currword, &entry);
strcpy(type, T());
D(entry);
//执行语义动作
//*******************************
for(int i = 0; i < DS.top; ++i)
FillTable(type, i);
//*******************************
S_nextlist = 0;
/* for(i = 0; i < ST.quantity; ++i)
printf("%d %s %s\n", i, ST.SB[i].idname, ST.SB[i].type);*/
if(currword == 14)
{
fscanf(p, "%d %d", &currword, &entry);
}
else
{
printf("Error! “;” lost\n");
}
}
else if(currword == 11)
{
strcpy(idname, ST.SB[entry].idname);
fscanf(p, "%d %d", &currword, &entry);
if(currword == 16)
{
fscanf(p, "%d %d", &currword, &entry);
E(entry);
if(currword == 14)
{
//执行语义动作emit("=", E.place, ,idname)
//*******************************
write("=", E_place," ", idname);
//*******************************
S_nextlist = 0;
fscanf(p, "%d %d", &currword, &entry);
}
}
}
return S_nextlist;
}
void U()
{
int W_head;
W_head = W();
B();
if(currword == 3)
{
//执行语义动作
//{backpacth(B.truelist, nextquad);
// U.nextlist = B.falselist;
// U.head = W.head;
//**************************************
backpatch(B_truelist, sencount);
U_nextlist = B_falselist;
U_head = W_head;
//**************************************
fscanf(p, "%d %d", &currword, &entry);
}
else
{
printf("Error! There should be a “do” matched with “while”\n");
}
}
int W()
{
int W_head;
//执行语义动作
//{ W.head = nextquad}
//***********************
W_head = sencount;
//***********************
return W_head;
}
int C()
{
int C_nextlist;
B(); //注意:之前已经将if读了
if(currword == 5)
{
//执行语义动作
//{C.nextlist = B.falselist;
// backpatch(B.truelist, nextquad)}
//***********************************
C_nextlist = B_falselist;
backpatch(B_truelist, sencount);
//***********************************
fscanf(p, "%d %d", &currword, &entry);
return C_nextlist;
}
else
{
printf("Error! There should be a “then” matched with “if”\n");
}
}
char* T()
{
char type[16];
strcpy(type, "int");
return 0;
}
void D(int entry)
{
if(currword == 11)
{
//执行语义动作
//***************************************************
while(DS.top != 0)
Pop();
Push(entry); //将标识符在符号表中的入口地址压栈
//***************************************************
fscanf(p, "%d %d", &currword, &entry);
D1();
}
else
{
printf("There should be a “id” here\n");
}
}
void D1()
{
if(currword == 15)
{
fscanf(p, "%d %d", &currword, &entry);
if(currword == 11)
{
//执行语义动作
//************
Push(entry);
//************
fscanf(p, "%d %d", &currword, &entry);
D1();
}
}
else if(currword != 6 && currword != 13 &&currword != 14)
{
printf("Error! “,” lost\n");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -