📄 translate.h
字号:
#ifndef _TANSLATE_H
#define _TANSLATE_H
#define error 3
//TRANSLATE
FILE *fp;
//--------------------------
#define MAX1 2000
char *id_list[50];
int id_list_p=0; //用于暂存identifier_list中的ID的指针
char *id_list1[50];
int id_list1_p=0;
int is_func=0;
bool is_rec=false;
bool is_red=false;
int p1,p3; //p1=0为standard_type;p1=1为array_type;p1=2为record_type;
int exp_num=0; //expression_list中的expression个数
char p2[5]=""; //p2为数组长度的字符表示(上界-下届+1)
char relop[2]="";
char addop[2]="";
char mulop[3]="";
char strc[MAX1]="";
char *str=strc; //临时string
char para_str[MAX1]=""; //parameter_list
char aparastr[MAX1]=""; //for parameter_list
char dec_str[MAX1]=""; //declaration
char dec1_str[MAX1]="";
char struct_str[MAX1]=""; //for type->record declarations end
char arg_str[MAX1]=""; //arguments
char com_str[MAX1]=""; //compound_statement
char opt_str[MAX1]=""; //optional_statements
char sta_str[MAX1]=""; //statement_list
char exp_str[MAX1]=""; //expression
char var_str[MAX1]=""; //variable
char pro_str[MAX1]=""; //procedure_statement
char exl_str[MAX1]=""; //expression_list
char ter_str[MAX1]=""; //term
char fac_str[MAX1]=""; //factor
char sig_str[1]=""; //sign
//------------------------------
//以下为simple_expression栈
char sim_stack[50][MAX1];
int sim_point=0;
int sim_push(char * x){ //简单表达式入栈;
if (sim_point == MAX1){
return 0;
}else{
strcpy(sim_stack[sim_point],x);
sim_point++;
return 1;
}
}
int sim_pop(int x){ //简单表达式出栈;
sim_point = sim_point-x;
if (sim_point<0){
sim_point = sim_point+x;
return 0;
}else{
return 1;
}
}
//------------------------------
//以下为expression栈
char exp_stack[50][MAX1];
int exp_point=0;
int exp_push(char * x){ //表达式入栈
if (exp_point == MAX1){
return 0;
}else{
strcpy(exp_stack[exp_point],x);
exp_point++;
return 1;
}
}
int exp_pop(int x){ //表达式出栈
exp_point = exp_point-x;
if (exp_point<0){
exp_point = exp_point+x;
return 0;
}else{
return 1;
}
}
//------------------------------
//以下为statement(语句)栈
char stat_stack[50][MAX1];
int stat_point=0;
int stat_push(char * x){ //语句入栈
if (stat_point == MAX1){
return 0;
}else{
strcpy(stat_stack[stat_point],x);
stat_point++;
return 1;
}
}
int stat_pop(int x){ //语句出栈
stat_point = stat_point-x;
if (stat_point<0){
stat_point = stat_point+x;
return 0;
}else{
return 1;
}
}
//---------------------------
//出错处理
void translateError(){
printf("Error when translating!\n");
}
//---------------------------
//分析栈中val域的定义
//---------------------------
//分析栈
L_ListNodePtr n=0; //空指针
L_ListNodePtr val_stack[MAX1];
int val_point = 0; //栈顶指针
//---------------------------
//入栈操作
int val_push(L_ListNodePtr x){
if (val_point == MAX1){
return 0;
}else{
val_stack[val_point] = x;
val_point++;
return 1;
}
}
//---------------------------
//出栈操作
int val_pop(int x){
val_point = val_point-x;
if (val_point<0){
val_point = val_point+x;
return 0;
}else{
return 1;
}
}
//----------------------------
//求栈顶
int val_top(int x){
int i=val_point-x;
if (i<0){
printf("Not so many element in the val_stack!");
return -1;
}else{
return i;
}
}
//-----------------------------
//写文件
void wf(char * str){
int i;
for (i=0;str[i]!='\0';i++) {
fputc(str[i],fp);
}
}
//------------------------------
char *cpy(char *str1,char *str2){ //处理关键字函数
int j=0;
while (str2[j]!='#'){
str1[j]=str2[j];
j++;
}
str1[j]='\0';
return str1;
}
char *cat(char *str1,char *str2){
char s[64]="";
cpy(s,str2);
strcat(str1,s);
return str1;
}
//连接生成standard_type的identifier_list
char *genid0(char *str){ //生成id_list函数
int j=0;
while (id_list_p!=j){
if (j==0) strcat(str," ");
else strcat(str,",");
cat(str,id_list[j]);
j++;
}
strcat(str,";\n");
return str;
}
//-------------------------------
char *genid1(char *str){ //生成数组类型的id_list函数
int j=0;
while (id_list_p!=j){
if (j==0) strcat(str," ");
else strcat(str,",");
cat(str,id_list[j]);
strcat(str,"[");
strcat(str,p2);
strcat(str,"]");
j++;
}
strcat(str,";\n");
return str;
}
//连接生成standard_type的identifier_list
char *genid01(char *str){ //生成记录类型id_list
int j=0;
while (id_list1_p!=j){
if (j==0) strcat(str," ");
else strcat(str,",");
cat(str,id_list1[j]);
j++;
}
strcat(str,";\n");
return str;
}
//-------------------------------
char *genid11(char *str){ //生成记录内数组类型id_list
int j=0;
while (id_list1_p!=j){
if (j==0) strcat(str," ");
else strcat(str,",");
cat(str,id_list1[j]);
strcat(str,"[");
strcat(str,p2);
strcat(str,"]");
j++;
}
strcat(str,";\n");
return str;
}
//-------------------------------
void genparastr(char *para_str){ //生成参数list
int j=0;
while (id_list_p!=j){
if (j==0) {strcat(para_str,str);strcat(para_str," ");cat(para_str,id_list[j]);}
else {
strcat(para_str,",");
strcat(para_str,str);
strcat(para_str," ");
cat(para_str,id_list[j]);
}
j++;
}
}
//-------------------------------
void shifthandle(L_ListNodePtr x){ //与语法分析移进时的接口
val_push(x);
if (x->label_type==21) is_rec=true;
if ((x->label_type==9)&&(is_rec)) is_rec=false;
if (x->label_type==19) is_red=true;
if ((x->label_type==37)&&(is_red)) is_red=false;
}
//-------------------------------
//翻译
void translate(int x){ //翻译函数
int i;
switch (x){
case 0:
break;
case 1:
if (!val_pop(2)) translateError();
break; //program->program_head program_body.
case 2:
id_list_p=0;
strcpy(str,"#include <stdio.h>\n");
wf(str);
if (!val_pop(5)) translateError();
strcpy(str,"");
break; //program_head->program id (identifier_list);
case 3:
wf("void main()\n");
wf(com_str);
if (!val_pop(2)) translateError();
break; //program_body->declarations subproc_declarations compound_statement
case 4:
if (!is_rec){
id_list[id_list_p]=(val_stack[val_point-1]->enter)->name;
id_list_p++;}
else {
id_list1[id_list1_p]=(val_stack[val_point-1]->enter)->name;
id_list1_p++;}
if (!val_pop(2)) translateError();
break; //identifier_list->identifier_list,id
case 5:
if (!is_rec){
id_list[id_list_p]=(val_stack[val_point-1]->enter)->name;
id_list_p++;}
else {
id_list1[id_list1_p]=(val_stack[val_point-1]->enter)->name;
id_list1_p++;
}
break; //identifier_list->id
case 6:
wf(dec_str);
if (!val_pop(2)) translateError();
strcpy(dec_str,"");
break; //declarations->VAR declaration;
case 7:
val_push(n);
break; //declarations->empty
case 8:
if (!is_rec){
switch (p1){
case 0:
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;
}
str=genid0(str);
id_list_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;
}
str=genid1(str);
id_list_p=0;
break;
case 2:
strcpy(str,"struct {\n");
strcat(str,struct_str);
strcat(str,"}");
str=genid0(str);
id_list_p=0;
break;
}
strcat(dec_str,str);
}
else {
switch (p1){
case 0:
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;
}
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;
}
str=genid11(str);
id_list1_p=0;
break;
case 2:
strcpy(str,"struct {\n");
strcat(str,struct_str);
strcat(str,"}");
str=genid01(str);
id_list1_p=0;
break;
}
strcat(dec1_str,str);
}
strcpy(str,"");
if (!val_pop(4)) translateError();
break; //declaration->declaration;identifier_list:type
case 9:
if (!is_rec){
switch (p1){
case 0:
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(dec_str,genid0(str));
id_list_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(dec_str,genid1(str));
id_list_p=0;
break;
case 2:
strcpy(str,"struct {\n");
strcat(str,struct_str);
strcat(str,"}");
strcpy(dec_str,genid0(str));
id_list_p=0;
break;
}
}
else {
switch (p1){
case 0:
switch (p3){
case 0:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -