📄 new7-6.c
字号:
fprintf(exp,"\nSelection statement\n");selection_stmt();break;
case 32: /*--while--*/
fprintf(exp,"\nIteration statement\n");iteration_stmt();break;
case ID:case NUM: case Lcircle: /*--expression--*/
fprintf(exp,"\nExpression statement\n");expression_stmt();break;
case SEMI: /*--NULL expression--*/
fprintf(exp,"\nNULL Expression statement\n");expression_stmt();break;
case 20: /*--return--*/
fprintf(exp,"\nReturn statement\n");return_stmt();break;
default:
printf("%d",lookahead);
error("Statement syntax error!\n");
getch();
exit(1);
}
return;
}
expression_stmt() /*--expression表达式调用--*/
{
if(lookahead!=SEMI)
{fprintf(exp,"\nExpression;\n");expression();emit(SEMI,NONE);
match(SEMI);}
else
{fprintf(exp,"\nNULL ;\n");emit(SEMI,NONE);
match(SEMI);}
return;
}
selection_stmt() /*--selection表达式--*/
{
int k;
fprintf(exp,"\nif(simple_expression)statement_list \n");
emit(16,NONE);fprintf(yuyip,"#%d= ",w);k=w++;match(16);
emit(Lcircle,NONE);match(Lcircle);
simple_expression();
if(lookahead!=Rcircle) /*--识别if()选择语句少)--*/
{error("Selection_stmt miss match )\n");goto x;}
emit(Rcircle,NONE);fprintf(yuyip,"\nif(#%d) goto true\ngoto false\n",k);match(Rcircle);
x: if(lookahead==LH)
{
emit(LH,NONE);match(LH);
fprintf(yuyip,"true: ");
statement_list();
fprintf(yuyip,"\ngoto add\n");
emit(RH,NONE);match(RH);
}
else
{fprintf(yuyip,"true: ");statement();fprintf(yuyip,"\ngoto add\n");}
if(lookahead==10)
{ fprintf(exp,"\nChange::if(simple_expression)statement_list else statement_list\n");
emit(10,NONE);match(10);
if(lookahead==LH)
{
emit(LH,NONE);match(LH);fprintf(yuyip,"false:");
statement_list();
fprintf(yuyip,"\nadd:\n");
if(lookahead!=RH)
{error("Selection_comp miss match }\n");goto z;} /*--识别else选择语句少}--*/
emit(RH,NONE);match(RH);
}
else
{fprintf(yuyip,"false:");statement();fprintf(yuyip,"\nadd:\n");}
}
z: return;
}
iteration_stmt() /*--iteration表达式--*/
{
int k;
fprintf(exp,"\nwhile(simple_expression)statement_list\n");
emit(32,NONE);fprintf(yuyip,"self: #%d= ",w);k=w++;match(32);
emit(Lcircle,NONE);match(Lcircle);
simple_expression();
if(lookahead!=Rcircle) /*--识别while()选择语句少)--*/
{error("Iteration_stmt miss match )\n");goto x;}
emit(Rcircle,NONE);fprintf(yuyip,"\nif(# %d)goto true\ngoto false\n",k);match(Rcircle);
x: if(lookahead==LH)
{
emit(LH,NONE);match(LH);
fprintf(yuyip,"true: ");
statement_list();
fprintf(yuyip,"\ngoto self \nfalse:\n");
emit(RH,NONE);match(RH);
}
else
{fprintf(yuyip,"true: ");statement();fprintf(yuyip,"\ngoto self \nfalse:\n");}
return;
}
return_stmt() /*--return表达式--*/
{
fprintf(exp,"\nreturn;\n");
emit(20,NONE);fprintf(yuyip,"return ");match(20);
if(lookahead!=SEMI)
simple_expression();
emit(SEMI,NONE);fprintf(yuyip,"\n");match(SEMI);
return;
}
expression() /*--包括附值表达式的表达式--*/
{ int k,q=lastentry;
if(lookahead==ID)
{
/* fprintf(yuyip,"expression:\n"); */
simple_expression();
if(lookahead==EQ)
{fprintf(exp,"\nvar = expression \n");
emit(EQ,NONE);
match(EQ);
fprintf(yuyip,"#%d= ",w);
k=w++;
simple_expression();
fprintf(yuyip,"%s=#%d\n",stringi,k);
}
while(lookahead==COMMA)
{
emit(COMMA,NONE);
match(COMMA);
simple_expression();
}
}
if(lookahead==NUM||lookahead==Lcircle)
{fprintf(exp,"\nsimple_expression\n");
if(lookahead==Lcircle)
{fprintf(yuyip,"()\n");simple_expression();}
}
return;
}
simple_expression() /*--简单表达式--*/
{
fprintf(exp,"\nadditive_expression\n");
additive_expression();
if(lookahead==LT||lookahead==RT||lookahead==LE||lookahead==RE||
lookahead==NOTEQUEL||lookahead==EQA||lookahead==PEQ||lookahead==MEQ||lookahead==PP||lookahead==MM)
{
fprintf(exp,"\nChange::additive_expression relop additive_expression\n");
if(lookahead==LT)
{emit(LT,NONE);match(LT);}
if(lookahead==RT)
{emit(RT,NONE);match(RT);}
if(lookahead==LE)
{emit(LE,NONE);match(LE);}
if(lookahead==RE)
{emit(RE,NONE);match(RE);}
if(lookahead==NOTEQUEL)
{emit(NOTEQUEL,NONE);match(NOTEQUEL);}
if(lookahead==EQA)
{emit(EQA,NONE);match(EQA);}
if(lookahead==PEQ)
{emit(PEQ,NONE);match(PEQ);}
if(lookahead==MEQ)
{emit(MEQ,NONE);match(MEQ);}
if(lookahead==PP)
{emit(PP,NONE);match(PP);}
if(lookahead==MM)
{emit(MM,NONE);match(MM);}
additive_expression();
}
return;
}
additive_expression()
{
int t;
fprintf(exp,"\nadditive_expression addop term | term\n");
term();
while(1)
switch(lookahead)
{
case PLUS: case MINUS:
t=lookahead;
emit(t,NONE); match(lookahead); term();
continue;
default:
return;
}
}
term()
{
int t;
fprintf(exp,"\nterm -> term mulop factor | factor\n");
factor();
while(1)
{
switch(lookahead)
{
case TIMES: case OVER:
t=lookahead;
emit(t,NONE); match(lookahead); factor();
continue;
default:
return;
}
}
}
factor()
{
int p,q;
switch(lookahead)
{
case Lcircle:
fprintf(exp,"\nfactor -> (expression)\n");
emit(Lcircle,NONE); match(Lcircle);
if(lookahead==Rcircle){emit(Rcircle,NONE);match(Rcircle); break;}
else
{expression();
emit(Rcircle,NONE);match(Rcircle); break;}
case NUM:
fprintf(exp,"\nfactor -> NUM\n");emit(NUM,tokenval);fprintf(yuyip,"%s ",symtable[tokenval].lexptr);match(NUM); break;
case ID :
p=alookup(symtable[tokenval].lexptr);
if(p==0)
fprintf(errorp,"line %d: This ID %s doesn't declaration!\n",lineno,symtable[tokenval].lexptr);
/*--是否声明的检查--*/
fprintf(exp,"\nfactor -> var\n");emit(ID,tokenval);fprintf(yuyip,"%s ",symtable[tokenval].lexptr);strcpy(stringi,symtable[tokenval].lexptr);match(ID);
if(lookahead==LBcircle)
{
emit(LBcircle,NONE);
fprintf(yuyip,"[");
match(LBcircle);
if(lookahead==NUM)
{
emit(NUM,tokenval);
fprintf(yuyip,"%s",symtable[tokenval].lexptr);
/*--数组是否越界的检查--*/
if(yuyi[p+2].lexptr>=symtable[tokenval].lexptr);
error("This array is over flow!\n");
match(NUM);
}
else
{
emit(ID,tokenval);fprintf(yuyip,"%s",symtable[tokenval].lexptr);match(ID);
}
emit(RBcircle,NONE);
fprintf(yuyip,"]");
match(RBcircle);
}
break;
/*
p=alookup(symtable[tokenval].lexptr);
if(p==0)
fprintf(errorp,"This ID %s doesn't declaration!\n",symtable[tokenval].lexptr);
*/
case SEMI:
break;
default:
error("Factor syntax error!\n");
}
}
/***************************匹配函数*******************************************/
match(int t)
{
if(lookahead==t)
lookahead=lexan();
else
error("Match syntax error!\n");
}
/***************************所有信息的输出函数*********************************/
emit(int t, int tval) /*--函数符号输出函数--*/
{
switch(t)
{
case LH:
printf("{\n");fprintf(exp,"{ ");break;
case RH:
printf("}\n");fprintf(exp,"} ");break;
case Lcircle:
printf("(\n");fprintf(exp,"( ");break;
case Rcircle:
printf(")\n");fprintf(exp,") ");break;
case SEMI:
printf(";\n");fprintf(exp,"; ");break;
case LBcircle:
printf("[\n");fprintf(exp,"[ ");break;
case RBcircle:
printf("]\n");fprintf(exp,"] ");break;
case 16:
printf("if\n");fprintf(exp,"if ");break;
case 10:
printf("else\n");fprintf(exp,"else ");break;
case 32:
printf("while\n");fprintf(exp,"while ");break;
case 20:
printf("return\n");fprintf(exp,"return ");break;
case 17:
printf("int\n");fprintf(exp,"int ");break;
case 30:
printf("void\n");fprintf(exp,"void ");break;
case LT:
printf("<\n");fprintf(exp,"< ");fprintf(yuyip,"< ");break;
case RT:
printf(">\n");fprintf(exp,"> ");fprintf(yuyip,"> ");break;
case RE:
printf(">=\n");fprintf(exp,">= ");fprintf(yuyip,">= ");break;
case LE:
printf("<=\n");fprintf(exp,"<= ");fprintf(yuyip,"<= ");break;
case NOTEQUEL:
printf("!=\n");fprintf(exp,"!= ");fprintf(yuyip,"!= ");break;
case COMMA:
printf(",\n");fprintf(exp,", ");break;
case EQA:
printf("==\n");fprintf(exp,"== ");fprintf(yuyip,"== ");break;
case EQ:
printf("=\n");fprintf(exp,"= ");break;
case PEQ:
printf("+=\n");fprintf(exp,"+= ");fprintf(yuyip,"+= ");break;
case MEQ:
printf("-=\n");fprintf(exp,"-= ");fprintf(yuyip,"-= ");break;
case PP:
printf("++\n");fprintf(exp,"++ ");fprintf(yuyip,"++ ");break;
case MM:
printf("--\n");fprintf(exp,"-- ");fprintf(yuyip,"-- ");break;
case PLUS:
printf("+\n");fprintf(exp,"+ ");fprintf(yuyip,"+ ");break;
case MINUS:
printf("-\n");fprintf(exp,"- ");fprintf(yuyip,"- ");break;
case TIMES:
printf("*\n");fprintf(exp,"* ");fprintf(yuyip,"* ");break;
case OVER:
printf("/\n");fprintf(exp,"/ ");fprintf(yuyip,"/ ");break;
case NUM:
printf("%s\n",symtable[tval].lexptr);
fprintf(exp,"%s ",symtable[tval].lexptr);break;
case ID:
printf("%s\n",symtable[tval].lexptr);
fprintf(exp,"%s ",symtable[tval].lexptr);break;
default:
printf("token:%d, tokenval:%d\n",t,tval);
}
}
/***************************错误信息处理函数***********************************/
error(char *m) /*--错误信息返回--*/
{
fprintf(stderr,"line %d:%s",lineno,m);
fprintf(errorp,"line %d:%s",lineno,m);
}
/***************************主函数*********************************************/
main()
{
yuyip=fopen("yuyi.txt","a+"); /*--输出语义文件--*/
exp=fopen("program.txt","a+"); /*--产生式文件-*/
IDp=fopen("IDput.txt","a+"); /*--打开单词序列文件--*/
errorp=fopen("error.txt","a+"); /*--打开错误输出文件--*/
outp=fopen("output.txt","a+"); /*--打开词法分析输出文件--*/
if((fp=fopen("input.txt","r"))==NULL) /*--打开待分析文件--*/
printf("Can not open input.txt! \n");
else
{printf("File open success!\n");
init(); /*--字母表初始化--*/
prase(); /*--语法分析--*/
printf("Lex over!\n");
}
printf("Press any key to out!\n");
getch();
fclose(fp);
}
/***************************程序结束*******************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -