Commit 464f164f authored by cwzsquare's avatar cwzsquare
Browse files

"continue实现"(连带修改while、if、&&)Fixed

parent 66a50bca
......@@ -163,6 +163,7 @@ static int FoundKeyword()
if (strcompare(tokenStr,"else")) return(SYN_ELSE);
if (strcompare(tokenStr,"while")) return(SYN_WHILE);
if (strcompare(tokenStr,"show")) return(SYN_SHOW);
if (strcompare(tokenStr,"continue")) return(SYN_CONTINUE);
return(SYN_ID);
}
......
......@@ -33,7 +33,7 @@ static TERMINAL lookahead;
static int curtoken_num;
static char curtoken_str[MAXTOKENLEN];
static IDTABLE *IDTHead=NULL;
static int run_status=1; //0;程序不执行;1:程序正常执行;2:跳过当前结构后继续执行
static int run_status=1; //0;程序不执行;1:程序正常执行;2:跳过当前结构后继续执行;3:continue跳过当前结构
void SyntaxAnalysis()
{
......@@ -221,6 +221,7 @@ static int Prod_S()
#if defined(AnaTypeSyn)
printf("SYN: S-->if (B) {S} [else {S}] S");
#endif
int temp_status=run_status; //if功能也是残缺的,存储if语句前run_status的状态
match(SYN_IF);
match(SYN_PAREN_L);
bval=Prod_B();
......@@ -239,6 +240,7 @@ static int Prod_S()
match(SYN_BRACE_R);
if (run_status==2) run_status=1;
}
if (run_status==1 || run_status==2) run_status=temp_status;
Prod_S();
}
else if (lookahead.token==SYN_WHILE)
......@@ -247,7 +249,7 @@ static int Prod_S()
printf("SYN: S-->while(B) {S} S\n");
#endif
match(SYN_WHILE);
file_index=ftell(sFile)-6;
file_index=ftell(sFile)-1; //修改指针回退到'('
match(SYN_PAREN_L);
bval=Prod_B();
match(SYN_PAREN_R);
......@@ -255,14 +257,27 @@ static int Prod_S()
match(SYN_BRACE_L);
Prod_S();
match(SYN_BRACE_R);
if(run_status==3 && bval==0) run_status=2; //continue
else if(run_status==3 && bval!=0) run_status=1; //continue
if (run_status==1)
{ fseek(sFile,file_index,SEEK_SET);
{ lookahead.token=SYN_WHILE;
fseek(sFile,file_index,SEEK_SET);
renewLex();
}
else if (run_status==2)
run_status=1;
Prod_S();
}
else if(lookahead.token==SYN_CONTINUE)
{
#if defined(AnaTypeSyn)
printf("SYN: S-->continue; \n");
#endif
match(SYN_CONTINUE);
match(SYN_SEMIC);
if (run_status==1) run_status=3;
Prod_S();
}
else
{
#if defined(AnaTypeSyn)
......@@ -433,7 +448,7 @@ static int Prod_TB1(int bval1)
printf("SYN: TB1-->&& TB1\n");
#endif
match(SYN_AND);
bval1=Prod_FB();
bval2=Prod_FB(); //针对lab_5.2.txt,if也是错的,修改bval2=Prod_FB();
bval1=(run_status==1 && (bval1==1 && bval2==1)) ? 1 : 0;
bval2=Prod_TB1(bval1);
return(bval2);
......
......@@ -72,6 +72,7 @@ static int LexTable[6][8]=
#define SYN_ELSE 59 // else
#define SYN_WHILE 60 // while
#define SYN_SHOW 61 // show
#define SYN_CONTINUE 62 // continue
//用于符号表中类型
#define ID_FUN 1 // 函数类型
......
No preview for this file type
main()
{ int a,b;
b=5;
a=(3*b>11)+(b>9||b<=6);
show(a);
{ int i=1,j=1;
while(i<6&&j<25){
j=j+i;
if(i==4||j==7){
continue;
}
i=i+1;
}
show(j);
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment