📄 布尔表达式的翻译.cpp
字号:
s[j] = s[i];
s[i] = tem;
--i;
++j;
}
temp=s;
}
int printerror()
{
cout << "这不是一句合法的句子,无法推导!" <<endl;
return 1;
}
//-----------------------------语法分析变量及函数定义结束---------------------------
//-----------------------------输出四元式序列变量及函数定义--------------------------
int pos1;
char expr[100];
int pos;
char newt;
char newval[5];
char title[7]={'+','*','!','O','(',')','#'};
char oo[7][7]={
{'>','<','<','<','<','>','>'},
{'>','>','<','<','<','>','>'},
{'>','>','<','>','<','>','>'},
{'>','>','>','>','<','>','>'},
{'<','<','<','<','<','=','!'},
{'>','>','>','!','!','>','>'},
{'<','<','<','<','<','!','='}};
char optr[100];//操作符
char opnd[100];
int tr;
int nd;
char s1;
//char token[100];
int num[100];
int mber;
void pushoptr(char sym)
{
optr[tr]=sym;
tr++;
}
void pushopnd(char sym)
{
opnd[nd]=sym;
//printf("%c",opnd[nd]);
nd++;
}
char popoptr()
{
char i;
int j;
j=tr;
j=j-1;
i=optr[j];
tr--;
return i;
}
char popopnd()
{
char i;
int j;
j=nd;
j=j-1;
i=opnd[j];
//printf("%c",i);
nd--;
return i;
}
char gettopoptr()
{
char i;
int j;
j=tr;
j=j-1;
i=optr[j];
//printf("%c",optr[j]);
return i;
}
int isopnd(char sym)
{
if (sym!='+'&&sym!='*'&&sym!='O'&&sym!='!'&&sym!='('&&sym!=')'&&sym!='K')
return 1;
else
return 0;
}
int isoptr(char sym)
{
if (sym=='+'||sym=='*'||sym=='#'||sym=='O'||sym=='!'||sym=='('||sym==')')
return 1;
else
return 0;
}
void pushnum(int i)
{
num[mber]=i;
mber++;
}
int popnum()
{
int i;
int j;
j=mber;
j=j-1;
i=num[j];
mber--;
return i;
}
int Position(char c)
{
int i;
for(i=0;i<7;i++)
if(c==title[i])
{ //printf("%d",i);
return i;}
return -1;
}
char Precede(char c1,char c2)
{
int i,j;
i=0;
j=0;
i=Position(c1);
j=Position(c2);
if(i!=-1 && j!=-1)
{
return oo[i][j];}
}
char operate(char a,char b,char o)
{
char c;
c='T';
return c;
}
void added()
{
pos1++;
strcpy(token_list[pos1].value,newval);
token_list[pos1].type ='K';
token_list[pos1].pose =pos1;
pushnum(pos1);
// printf("%d",pos1);
//pos1++;
token_list[pos1+1].type ='#';
newt++;
newval[0]=newt;
}
void first()
{
char a;
a='#';
char b;
int i=1;
char e;
int f,g,h;//fang shu xu hao
pushoptr(a);
//s=expr[pos++];
pos=pos+1;
s1=token_list[pos].type ;
// do{
while(s1!='#')
{
if(isopnd(s1))
{
pushopnd(s1);
//printf("%c",s);
//s=expr[pos++];
pushnum(token_list[pos].pose);
pos=pos+1;
s1=token_list[pos].type ;
}
if(isoptr(s1))
{
e=gettopoptr();
b=Precede(e,s1);
switch(b)
{
case'<':
if(s1=='(')
{
pushoptr(s1);
//s=expr[pos++];
pos=pos+1;
s1=token_list[pos].type ;
}
else{
pushoptr(s1);
//s=expr[pos++];
pushnum(token_list[pos].pose);
pos=pos+1;
s1=token_list[pos].type ;}
break;
case '=':
popoptr();
pos=pos+1;
s1=token_list[pos].type ;
printf("%c",s1);
break;
case '>':
/*d=popopnd();
c=popopnd();
r=operate(c,d,e);
pushopnd(r);
printf("%c,%c,%c,%c",popoptr(),c,d,r);
printf("[");
printf("%d",i);
printf("]\n");
i++;*/
// j=pos;
//j=j+1;
//pushnum(token_list[j].pose);
if(e=='!')
{ popoptr();
f=popnum();
g=popnum();
printf("\n");
printf("\n%s,%s ,%s",token_list[g].value,token_list[f].value,newval);
added();
}
else{
popoptr();
f=popnum();
//printf("%d",f);
g=popnum();
//printf("%d",g);
h=popnum();
//printf("%d",h);
printf("\n%s,%s,%s,%s",token_list[g].value,token_list[h].value,token_list[f].value,newval);
added();
}
break;
default:
break;
}
}
else
{
pos=pos+1;
s1=token_list[pos].type ;
}
}
while (popoptr()!='#')
{
/*d=popopnd();
c=popopnd();
r=operate(c,d,e);
pushopnd(r);
printf("%c,%c,%c,%c",e,c,d,r);
printf("[");
printf("%d",i);
printf("]\n");
i++;*/
f=popnum();
g=popnum();
h=popnum();
printf("\n%s,%s,%s,%s",token_list[g].value,token_list[h].value,token_list[f].value,newval);
added();
getchar();
}
}
//-------------------------输出四元式过程变量及函数定义结束---------------------------------------
//------------------------------主函数-------------------------------------------
void main()
{
FILE *fp;
char filename[20];
int i=0;
char string[20],ch;
for(i=0;i<20;i++)string[i]='\0';
i=0;
cout<<"Input the filename:\n"<<endl;
scanf("%s",filename);
while((fp=fopen(filename,"r"))==NULL){
printf("Cannot open file,please input the filename again:\n");
scanf("%s",filename);
}
ch=fgetc(fp);
putchar(ch);
while(!feof(fp)){
string[i++]=ch;
ch=fgetc(fp);
putchar(ch);
}
pos1=0;
strcpy(Str,string);
getch();//从文件中取出第一个字符中,并存在ch变量中
Fbuffer=1;//初始化定义,认为缓冲区中还有字母
printf("\n-------------------词法分析阶段------------------\n");
while(Fbuffer!=-1)
{
ty=getw();//调用getw()函数,得到一个单词,并返回此单词的类型
if(ty==Errtype)//单词不合法,长度没超过WMaxlen,显示其具体内容
cout<<"错误 "<<"\""<<Word<<"\""<<endl;
else if (ty==Longtype)//单词不合法,给出模糊描述
cout<<"id is too long "<<"\""<<Word<<"......"<<"\""<<endl;
else if (ty<=Maxtype)//单词合法,输出其类型,以及内容
{
cout<<"("<<ty<<",";
cout<<"\""<<Word<<"\"";
cout<<")"<<endl;
switch (ty)
{
case '1':
if(Word[0]=='o'&& Word[1]=='r')par_list[pos1]='+';
if(Word[0]=='a' && Word[1]=='n' && Word[2]=='d')par_list[pos1]='*';
if(Word[0]=='n' && Word[1]=='o'&& Word[2]=='t')par_list[pos1]='!';
break;
case '2':
par_list[pos1]='I';break;
case '3':
par_list[pos1]='D';break;
case '4':
par_list[pos1]='O';break;
case '5':
if(Word[0]=='(')par_list[pos1]='(';
if(Word[0]==')')par_list[pos1]=')';
break;
default:;
}
token_list[pos1+1]=pp(Word,pos1+1,par_list[pos1]);
pos1++;
}
}
token_list[pos1+1].type='#';//增加末尾的结束#
par_list[pos1]='#';
par_list[pos1+1]='\0';
cout<<'\n';
for(i=1;i<=15;i++)
printf("%d,%s,%c\n",token_list[i].pose, token_list[i].value,token_list[i].type );
cout<<"-------------------语法分析阶段------------------\n";
input();
cout<<"analyzed_sentence"<<analyzed_sentence<<endl;
init_stack(); //初始化栈,将'#'和开始符进栈
newt='A';
newval[0]='A';
newval[1]='\0';
pos=0;
tr=0;
nd=0;
mber=0;
if(ll1_analyzing()){cout<<"\n\n---------------四元式输出部分------------------\n";first();}
getchar();
exit(0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -