?? 新建 文本文檔.txt
字號:
{
token=getnext();
expression(token);
if(token==40)
{
token=getnext();
}
else
{
cout<<"error13"<<endl;
}
}
else if(token==36)
{
strcpy(stack[stack_num++],table1[table1_num2++]);
cout<<stack_num<<endl;
id(token);
}
else if(token==37)
{
strcpy(stack[stack_num++],table1[table1_num2++]);
interger(token);
}
else
{
cout<<"error14"<<endl;
}
}
void interger(int &token)
{
if(token==37)
{
token=getnext();
}
else
{
cout<<"error15"<<endl;
}
}
void b_expression(int &token)
{
b_1(token);
mid_b1(token);
}
void mid_b1(int &token)
{
if(token==20)
{
token=getnext();
b_1(token);
mid_b1(token);
}
else if(token!=40||token!=52||token!=29||token!=10||token!=12)
{
cout<<"error16"<<endl;
}
}
void b_1(int &token)
{
b_2(token);
mid_b2(token);
}
void mid_b2(int &token)
{
if(token==1)
{
token=getnext();
b_2(token);
mid_b2(token);
}
else if(token!=20||token!=40||token!=52||token!=29||token!=10||token!=12)
{
cout<<"error17"<<endl;
}
}
void b_2(int &token)
{
if(token==18)
{
token=getnext();
b_2(token);
}
else
{
b_3(token);
}
}
void b_3(int &token) ///////////////////////////
{
if(token==39)
{
token=getnext();
b_expression(token);
if(token==40)
{
token=getnext();
}
else
{
cout<<"error18"<<endl;
}
}
else if(token==36)
{
a_expression(token);
mid(token);
}
else if(token==13||token==31)
{
bool_digit(token);
}
else
{
cout<<"error"<<endl;
}
}
void bool_digit(int &token)
{
if(token==13||token==31)
{
token=getnext();
}
else
{
cout<<"error19"<<endl;
}
}
void relation(int &token)
{
if(token==53)
{
flag=1;
token=getnext();
}
else if(token==54)
{
flag=2;
token=getnext();
}
else if(token==55)
{
flag=3;
token=getnext();
}
else if(token==56)
{
flag=4;
token=getnext();
}
else if(token==57)
{
flag=5;
token=getnext();
}
else if(token==58)
{
flag=6;
token=getnext();
}
else
{
cout<<"error20"<<endl;
}
}
void mid(int &token) ////////////////////////////
{
relation(token);
a_expression(token);
if(flag==1){tet("j<",stack[stack_num-2],stack[stack_num-1],"T");}
if(flag==2){tet("j<=",stack[stack_num-2],stack[stack_num-1],"T");}
if(flag==3){tet("j<>",stack[stack_num-2],stack[stack_num-1],"T");}
if(flag==4){tet("j=",stack[stack_num-2],stack[stack_num-1],"T");}
if(flag==5){tet("j>",stack[stack_num-2],stack[stack_num-1],"T");}
if(flag==6){tet("j>=",stack[stack_num-2],stack[stack_num-1],"T");}
}
void tet(char a1[],char a2[],char a3[],char a4[])
{
if(tetrad_num>=500)
{
cout<<"error"<<endl;
return;
}
strcpy(tetrad[tetrad_num].opr,a1);
strcpy(tetrad[tetrad_num].p1,a2);
strcpy(tetrad[tetrad_num].p2,a3);
strcpy(tetrad[tetrad_num].p3,a4);
tetrad_num++;
}
void putout()
{
for(int i=0;i<tetrad_num;i++)
{
cout<<"("<<i<<")"<<"("<<tetrad[i].opr<<","<<tetrad[i].p1<<","<<tetrad[i].p2<<","<<tetrad[i].p3<<")"<<endl;
}
}
int getnext()
{
if(token_num>=500)
{
cout<<"error"<<endl;
return 0;
}
else
{
return c[token_num++]%100;
}
}
/*設置保留字名字*/
void init()
{
strcpy(&(word[1][0]),"and");
strcpy(&(word[2][0]),"array");
strcpy(&(word[3][0]),"begin");
strcpy(&(word[4][0]),"bool");
strcpy(&(word[5][0]),"call");
strcpy(&(word[6][0]),"case");
strcpy(&(word[7][0]),"char");
strcpy(&(word[8][0]),"constant");
strcpy(&(word[9][0]),"dim");
strcpy(&(word[10][0]),"do");
strcpy(&(word[11][0]),"else");
strcpy(&(word[12][0]),"end");
strcpy(&(word[13][0]),"false");
strcpy(&(word[14][0]),"for");
strcpy(&(word[15][0]),"if");
strcpy(&(word[16][0]),"input");
strcpy(&(word[17][0]),"integer");
strcpy(&(word[18][0]),"not");
strcpy(&(word[19][0]),"of");
strcpy(&(word[20][0]),"or");
strcpy(&(word[21][0]),"output");
strcpy(&(word[22][0]),"procedure");
strcpy(&(word[23][0]),"program");
strcpy(&(word[24][0]),"read");
strcpy(&(word[25][0]),"real");
strcpy(&(word[26][0]),"repeat");
strcpy(&(word[27][0]),"set");
strcpy(&(word[28][0]),"stop");
strcpy(&(word[29][0]),"then");
strcpy(&(word[30][0]),"to");
strcpy(&(word[31][0]),"true");
strcpy(&(word[32][0]),"until");
strcpy(&(word[33][0]),"var");
strcpy(&(word[34][0]),"while");
strcpy(&(word[35][0]),"write");
strcpy(&(word[36][0]),"標識符");
strcpy(&(word[37][0]),"整數");
strcpy(&(word[38][0]),"字符常數");
strcpy(&(word[39][0]),"(");
strcpy(&(word[40][0]),")");
strcpy(&(word[41][0]),"*");
strcpy(&(word[42][0]),"*/");
strcpy(&(word[43][0]),"+");
strcpy(&(word[44][0]),",");
strcpy(&(word[45][0]),"-");
strcpy(&(word[46][0]),".");
strcpy(&(word[47][0]),"..");
strcpy(&(word[48][0]),"/");
strcpy(&(word[49][0]),"/*");
strcpy(&(word[50][0]),":");
strcpy(&(word[51][0]),":=");
strcpy(&(word[52][0]),";");
strcpy(&(word[53][0]),"<");
strcpy(&(word[54][0]),"<=");
strcpy(&(word[55][0]),"<>");
strcpy(&(word[56][0]),"=");
strcpy(&(word[57][0]),">");
strcpy(&(word[58][0]),">=");
strcpy(&(word[59][0]),"[");
strcpy(&(word[60][0]),"]");
}
/*判斷ch是否為數字*/
int IsDigit(char ch)
{
if((ch>='0')&&(ch<='9'))return 1;
else return 0;
}
/*判斷ch是否為字母*/
int IsAlpha(char ch)
{
if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))return 1;
else return 0;
}
/*查表*/
int Find(char *A,char B[60][20])
{
for(int i=1; i<=60; i++)
{
if(!strcmp(A,B[i]))
{
return i ;
}
}
return 0;
}
/*詞法分析*/
void getsym(FILE *fin)
{
init();
char A[20];
int m=5;
n=0;
int k=1;
int i=0;
int j=0;
int p=0;
q=0;
int row=1;
int col=1;
char ch=' ';
while(!feof(fin))
{
if( ch==' '|| ch =='\t'){ch=fgetc(fin);col++;}
else if(ch=='\n'){row++;ch=fgetc(fin);col=1;}
else if( IsDigit(ch))
{
while(IsDigit(ch))
{
A[j] = ch;
j++;
ch=fgetc(fin);
col++;
}
A[j]='\0';
if(!Find(A,table1))
{
c[q]=(k++)*100+37;q++;
strcpy(table1[n++],A);
}
else
{
c[q]=Find(A,table1)*100+37;q++;
}
j=0;
}
else if(IsAlpha(ch))//是字母開頭的
{
while(IsAlpha(ch) || IsDigit(ch))
{
A[i] =ch;
i++;
ch=fgetc(fin);
col++;
}
A[i]='\0';
if(Find(A,word)) //查表
{
c[q]=Find(A,word);q++;
}
else
{
if(!Find(A,table1))
{
c[q]=100*(k++)+36;q++;
strcpy(table1[n++],A);
}
else
{
c[q]=100*Find(A,table1)+36;q++;
}
}
i=0;
}
//以下為字符常數
else if(ch=='\'')
{
ch=fgetc(fin);
col++;
while(ch!='\'')
{
if(ch=='\n')
{
cout<<"第"<<row<<"行,第"<<col<<"列錯誤:字符常數缺右邊的單引號(字符常數要求左、右邊用單引號界定,不能跨行)"<<endl;
break;
}
A[p] = ch;
p++;
ch=fgetc(fin);
col++;
}
A[p]='\0';
if(!Find(A,table1))
{
c[q]=100*(k++)+38;q++;
ch=fgetc(fin);
col++;
strcpy(table1[n++],A);
}
else
{
c[q]=100*Find(A,table1)+38;q++;
ch=fgetc(fin);
col++;
}
p=0;
}
//以下為2字節的運算符號
else if( ch=='/')
{
ch=fgetc(fin);
col++;
if(ch!='*')
{
c[q]=48;q++;
}
if(ch=='*')
{
do
{
ch=fgetc(fin);
col++;
if(ch=='\n'){cout<<"第"<<row<<"行,第"<<col<<"列錯誤:注釋部分缺右邊的界符*/(注釋要求左右邊分別用/*和*/界定,不能跨行)"<<endl;break;}
}
while(ch!='/');
ch=fgetc(fin);
col++;
}
}
else if( ch=='.')
{
ch=fgetc(fin);
col++;
if(ch!='.')
{
c[q]=46;q++;
}
if(ch=='.')
{
c[q]=47;q++;
ch=fgetc(fin);
}
}
else if( ch==':')
{
ch=fgetc(fin);
col++;
if(ch!='=')
{
c[q]=50;q++;
}
if(ch=='=')
{
c[q]=51;q++;
ch=fgetc(fin);
col++;
}
}
else if( ch=='<')
{
ch=fgetc(fin);
col++;
if(ch!='='&&ch!='>')
{
c[q]=53;q++;
}
if(ch=='=')
{
c[q]=54;q++;
ch=fgetc(fin);
col++;
}
if(ch=='>')
{
c[q]=55;q++;
ch=fgetc(fin);
col++;
}
}
else if( ch=='>')
{
ch=fgetc(fin);
col++;
if(ch!='=')
{
c[q]=57;q++;
}
if(ch=='=')
{
c[q]=58;q++;
ch=fgetc(fin);
col++;
}
}
//以下為一個字節的運算符號
else if(ch=='(') {c[q]=39;q++;ch=fgetc(fin);col++;}
else if(ch==')') {c[q]=40;q++;ch=fgetc(fin);col++;}
else if(ch=='*') {c[q]=41;q++;ch=fgetc(fin);col++;}
else if(ch=='+') {c[q]=43;q++;ch=fgetc(fin);col++;}
else if(ch==',') {c[q]=44;q++;ch=fgetc(fin);col++;}
else if(ch=='-') {c[q]=45;q++;ch=fgetc(fin);col++;}
else if(ch==';') {c[q]=52;q++;ch=fgetc(fin);col++;}
else if(ch=='=') {c[q]=56;q++;ch=fgetc(fin);col++;}
else if(ch=='[') {c[q]=59;q++;ch=fgetc(fin);col++;}
else if(ch==']') {c[q]=60;q++;ch=fgetc(fin);col++;}
else
{
cout<<"第"<<row<<"行,第"<<col<<"列錯誤:非法字符,即不是SAMPLE字符集的符號"<<endl;
ch=fgetc(fin);col++;
}
}
//輸出二元式
for(i=0;i<q;i++)
{
if(c[i]/100==0)
{
cout<<"("<<c[i]%100<<","<<"-)"<<'\t';
m--;if(m==0){cout<<'\n';m=5;}
}
else
{
cout<<"("<<c[i]%100<<","<<c[i]/100<<")"<<'\t';
m--;if(m==0){cout<<'\n';m=5;}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -