📄 稿.txt
字号:
/**********************SAMPLE语言的语法、语义分析器******************************/
#include<iostream.h>
#include<string.h>
#include <stdio.h>
char word[60][20]; //用来存放初使符号表
int n; //用来表示table1的元素个数
int q; //用来表示数组c的元素个数
char table1[100][20]; //表里存放标识符,整数,字符常数
int c[500]; //表里存放词法分析结果
int tetrad_num=0; //记录四元式个数
int token_num=0; //对token的内容记数
int token=0;
int table1_num=0; //
int table1_num2=0;
char stack[100][20];
int stack_num=0;
int flag=0;
typedef struct //四元组类说明
{
char opr[20];
char p1[20];
char p2[20];
char p3[20];
}sys;
sys tetrad[500]; //四元组
void tet(char a1[],char a2[],char a3[],char a4[]); //四元组赋值
void init();
int IsDigit(char ch); //数字?
int IsAlpha(char ch); //字母?
void getsym(FILE *fin); //词法分析
int getnext();
void putout(); //输出四元组
//以下为各产生式对应程序
void program();
void id(int &token);
void var_explain(int &token);
void var_definition(int &token);
void mid_var(int &token);
void kind(int &token);
void id_table(int &token);
void mid_id(int &token);
void expression(int &token);
void a_expression(int &token);
void mid_a1(int &token);
void a_1(int &token);
void mid_a2(int &token);
void a_2(int &token);
void a_3(int &token);
void interger(int &token);
void b_expression(int &token);
void mid_b1(int &token);
void b_1(int &token);
void mid_b2(int &token);
void b_2(int &token);
void b_3(int &token);
void bool_digit(int &token);
void relation(int &token);
void mid(int &token);
void main()
{
cout<<"* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n"
<<"* 姓名 薛江 *\n"
<<"* 班级 05计算机联合班 *\n"
<<"* 学号 200530612154 *\n"
<<"* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n";
while(1)
{
cout<<"请输入要测试的程序:";
char filename[10];
cin>>filename;
FILE* fin = fopen(filename,"r");
getsym(fin);
cout<<endl;
expression(token);
cout<<table1[0]<<table1[1]<<table1[2]<<table1[3]<<table1[4]<<table1[5]<<table1[6]<<table1[7]<<endl;
cout<<stack[0]<<stack[1]<<stack[2]<<stack[3]<<stack[4]<<stack[5]<<stack[6]<<stack[7]<<endl;
putout();
fclose(fin);
}
}
void program()
{
token=getnext();
if(token==23)
{
token=getnext();
id(token);
if(token==52)
{
tet("program",table1[table1_num++],"-","-");
token=getnext();
var_explain(token);
//complex(token);
if(token==46)
{
tet("sys","-","-","-");
}
else
{
cout<<"error1"<<endl;
}
}
else
{
cout<<"error2"<<endl;
}
}
else
{
cout<<"error3"<<endl;
}
}
void id(int &token)
{
if(token==36)
{
token=getnext();
}
else
{
cout<<"error4"<<endl;
}
}
void var_explain(int &token)
{
if(token==33)
{
token=getnext();
var_definition(token);
}
else if(token!=3)
{
cout<<"error5"<<endl;
}
}
void var_definition(int &token)
{
id_table(token);
if(token==50)
{
token=getnext();
kind(token);
if(token==52)
{
token=getnext();
}
else
{
cout<<"error6"<<endl;
}
mid_var(token);
}
else
{
cout<<"error7"<<endl;
}
}
void mid_var(int &token)
{
var_definition(token);
if(token!=3)
{
cout<<"error8"<<endl;
}
}
void kind(int &token)
{
if(token==17||token==4||token==25)
{
token=getnext();
}
else
{
cout<<"error9"<<endl;
}
}
void id_table(int &token)
{
id(token);
mid_id(token);
}
void mid_id(int &token)
{
if(token==44)
{
token=getnext();
id_table(token);
}
else if(token!=3)
{
cout<<"error10"<<endl;
}
}
void expression(int &token)
{
token=getnext();
a_expression(token);
b_expression(token);
//c_expression(token);
}
void a_expression(int &token)
{
a_1(token);
mid_a1(token);
}
void mid_a1(int &token)
{
if(token==43)
{
token=getnext();
a_1(token);
mid_a1(token);
tet("+",stack[stack_num-2],stack[stack_num-1],"T");
}
else if(token==45)
{
token=getnext();
a_1(token);
mid_a1(token);
tet("-",stack[stack_num-2],stack[stack_num-1],"T");
}
else if(token!=40||token!=20||token!=1||
token!=56||token!=53||token!=55||
token!=54||token!=58||token!=57||
token!=52||token!=11||token!=32||
token!=12||token!=10||token!=29)
{
cout<<"error11"<<endl;
}
}
void a_1(int &token)
{
a_2(token);
mid_a2(token);
}
void mid_a2(int &token)
{
if(token==41)
{
token=getnext();
a_2(token);
mid_a2(token);
tet("*",stack[stack_num-2],stack[stack_num-1],"T");
}
else if(token==48)
{
token=getnext();
a_2(token);
mid_a2(token);
tet("/",stack[stack_num-2],stack[stack_num-1],"T");
}
else if(token!=43||token!=45||token!=40||
token!=56||token!=1||token!=20||
token!=53||token!=54||token!=55||
token!=57||token!=58||token!=52||
token!=11||token!=32||token!=12||
token!=10||token!=29)
{
cout<<"error12"<<endl;
}
}
void a_2(int &token) //////////////////////////////
{
if(token==45)
{
token=getnext();
a_2(token);
}
else
{
a_3(token);
}
}
void a_3(int &token)
{
if(token==39)
{
token=getnext();
expression(token);
if(token==40)
{
token=getnext();
}
else
{
cout<<"error13"<<endl;
}
}
else if(token==36)
{
strcpy(stack[stack_num++],table1[table1_num2++]);
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
{
a_expression(token);
mid(token);
}
}
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 + -