实验三语义分析 编译原理
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
/*
输入(0124.3+0x35a.4f)*12
输出:
运算式语法正确!
运算结果 11331.75000
输入 12a+45*013468-0x23a3
输出:
第一个单词是错误数据
第五个单词是错误数据
运算式语法正确!
输入 (1-44))/4
输出:
第六个单词‘)’出错
输入 (1-4(4))/4
输出:
第五个单词‘(’出错
第八个单词‘)’出错
输入 0x11+07
输出:
运算式语法正确!
运算结果 24.00000
输入 0x11.88+07
输出:
运算式语法正确!
运算结果 24.75000
输入 0x11+07.44
输出:
运算式语法正确!
运算结果 24.75000
输入 10*(2+3)/100
输出:
运算式语法正确!
运算结果 0.50000
*/
#include
#include
#include
#include
#include
#include
using namespace std;
bool newstringXC(string &strsor);
bool newstringJJ(string &strsor);
bool resetstringJia1(string &strsor);
bool resetstringJia2(string &strsor);
bool resetstringJia3(string &strsor);
bool resetstringJia4(string &strsor);
bool newstringKH(string &strsor)
{
int index1 = strsor.rfind("(", strsor.length()-1);
if(index1 == string::npos)
return false;
int index2 = strsor.find(")", index1);
string childstr;
childstr.assign(strsor, index1+1, index2-index1-1);
while(newstringXC(childstr))
{
while((resetstringJia1(childstr))){}
while((resetstringJia2(childstr))){}
while((resetstringJia3(childstr))){}
while((resetstringJia4(childstr))){}
}
while(newstringJJ(childstr))
{
while((resetstringJia1(childstr))){}
while((resetstringJia2(childstr))){}
while((resetstringJia3(childstr))){}
while((resetstringJia4(childstr))){}
}
strsor.replace(index1, index2-index1+1, childstr.c_str(), childstr.length());
while((resetstringJia1(strsor))){}
while((resetstringJia2(strsor))){}
while((resetstringJia3(strsor))){}
while((resetstringJia4(strsor))){}
return true;
};
bool newstringXC(string &strsor)
{
int index1 = strsor.find_first_of("*/", 0);
if(index1 == string::npos)
return false;
int index2 = strsor.find_first_of("+-*/", index1+2);
if(index2 == string::npos)
index2 = strsor.length();
int index3 = strsor.find_last_of("+-*/", index1-1);
if(index3 == string::npos)
index3 = -1;
string str1;
str1.assign(strsor, index3+1, index1-index3-1);
string str2;
str2.assign(strsor, index1+1, index2-index1-1);
double dob1 = atof(str1.c_str());
double dob2 = atof(str2.c_str());
double dob3;
if(strsor.at(index1) == '*')
dob3 = dob1 * dob2;
else if(strsor.at(index1) == '/')
dob3 = dob1 / dob2;
char c_str3[50];
memset(c_str3, 0, 50);
sprintf(c_str3, "%.5f", dob3);
strsor.replace(index3 +1, index2-index3-1, c_str3, strlen(c_str3));
return true;
};
bool newstringJJ(string &strsor)
{
int index1 = strsor.find_first_of("+-", 1);
if(index1 == string::npos)
return f
alse;
int index2 = strsor.find_first_of("+-*/", index1+1);
if(index2 == string::npos)
index2 = strsor.length();
int index3 = strsor.find_last_of("+-*/", index1-1);
if((index3 == string::npos) || (index3 == 0))
index3 = -1;
string str1;
str1.assign(strsor, index3+1, index1-index3-1);
string str2;
str2.assign(strsor, index1+1, index2-index1-1);
double dob1 = atof(str1.c_str());
double dob2 = atof(str2.c_str());
double dob3;
if(strsor.at(index1) == '+')
dob3 = dob1 + dob2;
else if(strsor.at(index1) == '-')
dob3 = dob1 - dob2;
char c_str3[50];
memset(c_str3, 0, 50);
sprintf(c_str3, "%.5f", dob3);
strsor.replace(index3 +1, index2-index3-1, c_str3, strlen(c_str3));
return true;
};
bool resetstringJia1(string &strsor)
{
int index1 = strsor.find("++");
if(index1 == string::npos)
return false;
strsor.replace(index1, 2, "+", 1);
return true;
};
bool resetstringJia2(string &strsor)
{
int index1 = strsor.find("+-");
if(index1 == string::npos)
return false;
strsor.replace(index1, 2, "-", 1);
return true;
};
bool resetstringJia3(string &strsor)
{
int index1 = strsor.find("-+");
if(index1 == string::npos)
return false;
strsor.replace(index1, 2, "-", 1);
return true;
};
bool resetstringJia4(string &strsor)
{
int index1 = strsor.find("--");
if(index1 == string::npos)
return false;
strsor.replace(index1, 2, "+", 1);
return true;
};
char yuyi[100]={'\0'};
int f(char *t)
{
int j;
bool ss=0,is8=0,is10=0,is16=0;
for(j=0;j
if(t[j]=='.')
ss=1;
}
if(t[0]=='0'&&t[1]=='x')
{
int c=0;
for(j=2;j
c++;
if(ss&&c==strlen(t)-3)
is16=1;
if(!ss&&c==strlen(t)-2)
is16=1;
if(is16&&ss)
{
char *s;
int i=(int)strtol(t,&s,16);
char ss[100];
//sprintf(ss,"%d",i);
//strcat(yuyi,ss);
double ans=(double)i;
i=0;
double sum=0;
int quan=16;
while(t[i]!='.')i++;
for(int k=i+1;k
sum=sum+(double)(t[k]-'0')/quan;
quan=quan*2;
}
ans=ans+sum;
sprintf(ss,"%lf",ans);
strcat(yuyi,ss);
return 1;
}
//cout<<"十六进制实数 "<
{
char *s;
int i=(int)strtol(t,&s,16);
char ss[100];
sprintf(ss,"%d",i);
strcat(yuyi,ss);
return 1;
}
//cout<<"十六进制整数 "<
return 0;
//cout<<"错误数据 "<
}
if(t[0]=='0')
{
int cc=0;
for(j=1;j
cc++;
if(ss&&cc==strlen(t)-2)
is8=1;
if(!ss&&cc==strlen(t)-1)
is8=1;
if(is8&&ss)
{
char *s;
int i=(int)strtol(t,&s,8);
char ss[100];
double ans=(double)i;
i=0;
double sum=0;
int quan=8;
while(t[i]!='.')i
++;
for(int k=i+1;k
sum=sum+(double)(t[k]-'0')/quan;
quan=quan*2;
}
ans=ans+sum;
sprintf(ss,"%lf",ans);
strcat(yuyi,ss);
return 1;
}
//cout<<"八进制实数 "<
{
char *s;
int i=(int)strtol(t,&s,8);
char ss[100];
sprintf(ss,"%d",i);
strcat(yuyi,ss);
return 1;
}
//cout<<"八进制整数 "<
return 0;
//cout<<"错误数据 "<
}
if(t[0]-'0'>0&&t[j]-'0'<10)
{
int ccc=0;
for(j=1;j
ccc++;
if(ss&&ccc==strlen(t)-2)
is10=1;
if(!ss&&ccc==strlen(t)-1)
is10=1;
if(is10&&ss)
{
strcat(yuyi,t);
return 1;
}
//cout<<"十进制实数 "<
{
strcat(yuyi,t);
return 1;
}
//cout<<"十进制整数 "<
return 0;
//cout<<"错误数据 "<
}
// if(t[0]=='')
return 0;
//cout<<"错误数据 "<
}
void yyfx(char *yy)
{
string m_str=(string)yy;
while(newstringKH(m_str))
{
while((resetstringJia1(m_str))){}
while((resetstringJia2(m_str))){}
while((resetstringJia3(m_str))){}
while((resetstringJia4(m_str))){}
}
while(newstringXC(m_str))
{
while((resetstringJia1(m_str))){}
while((resetstringJia2(m_str))){}
while((resetstringJia3(m_str))){}
while((resetstringJia4(m_str))){}
}
while(newstringJJ(m_str))
{
while((resetstringJia1(m_str))){}
while((resetstringJia2(m_str))){}
while((resetstringJia3(m_str))){}
while((resetstringJia4(m_str))){}
}
cout<<"运算结果 " << m_str << endl;
}
int main()
{
char *m[50]={"零","一","二","三","四","五","六","七","八","九","十","十一","十二","十三","十四","十五","十六","十七","十八","十九","二十","二一","二二","二三","二四","二五","二六","二七","二八","二九","三十"};
char s[100],temp[100],yf[100];
//char yuyi[100];
while(gets(s))
{
yuyi[0]='\0';
//strcpy(yuyi,s);
int i;
int k=0;
int q=0;
for(i=0;i
if(s[i]==' ')
continue;
if(s[i]!='+' && s[i]!='-'&& s[i]!='*'&& s[i]!='/'&& s[i]!='('&& s[i]!=')')
{
temp[k++]=s[i];
}
else
{
temp[k]='\0';
if(k>0)
{
if(f(temp))
{
yf[q++]='N';
//cout<<"1 "<
}
else
{
yf[q++]='E';
//cout<<"2 "<
if(i!=strlen(s))
{
yf[q++]=s[i];
char s2[100];
strcpy(s2,s);
char *sss=s2+i;
sss[1]='\0';
strcat(yuyi,sss);
//cout<<"3 "<
//cout<<"运算符 "<k=0;
}
else
{
yf[q++]=s[i];
char s2[100];
strcpy
(s2,s);
char *sss=s2+i;
sss[1]='\0';
strcat(yuyi,sss);
//cout<<"4 "<}
}
}
if(k>0)
{
temp[k]='\0';
if(f(temp))
{
yf[q++]='N';
//cout<<"5 "<
}
else
{
yf[q++]='E';
//cout<<"6 "<
}
yf[q++]='#';
yf[q++]='\0';
char tyf[100];
strcpy(tyf,yf);
int h[100]={0};
char cc[100];
int kuohao=0;
if(yf[0]=='+'||yf[0]=='-'||yf[0]=='*'||yf[0]=='/'||yf[0]==')')
{
h[0]=1;
cc[0]=yf[0];
yf[0]='#';
}
if(yf[0]=='(')
kuohao++;
if(yf[0]=='E')
h[0]=2;
for(i=1;i
if(yf[i]=='+'||yf[i]=='-'||yf[i]=='*'||yf[i]=='/')
{
if(yf[i-1]=='#')
{
h[i]=1;
cc[i]=yf[i];
yf[i]='#';
}
if(yf[i-1]=='+'||yf[i-1]=='-'||yf[i-1]=='*'||yf[i-1]=='/'||yf[i-1]=='(')
{
h[i]=1;
cc[i]=yf[i];
yf[i]=yf[i-1];
}
}
if(yf[i]=='(')
{
if(yf[i-1]=='N'||yf[i-1]=='E')
{
h[i]=1;
cc[i]=yf[i];
yf[i]=yf[i-1];
}
else
kuohao++;
}
if(yf[i]==')')
{
if(yf[i-1]=='+'||yf[i-1]=='-'||yf[i-1]=='*'||yf[i-1]=='/'||yf[i-1]=='(')
{
h[i]=1;
cc[i]=yf[i];
yf[i]=yf[i-1];
}
else
kuohao--;
}
if(yf[i]=='N'||yf[i]=='E')
{
if(yf[i-1]==')')
{
h[i]=1;
cc[i]=yf[i];
yf[i]=yf[i-1];
}
if(yf[i]=='E')
h[i]=2;
}
if(yf[i]=='#')
{
if(tyf[i-1]=='+'||tyf[i-1]=='-'||tyf[i-1]=='*'||tyf[i-1]=='/'||tyf[i-1]=='(')
{
h[i-1]=1;
cc[i-1]=tyf[i-1];
}
}
}
if(kuohao>0)
{
i=0;
while(kuohao>0)
{
if(tyf[i]=='(')
{
h[i]=1;
cc[i]=tyf[i];
kuohao--;
}
i++;
}
}
else if(kuohao<0)
{
i=strlen(yf);
while(kuohao<0)
{
if(yf[i]==')')
{
h[i]=1;
cc[i]=yf[i];
kuohao++;
}
i--;
}
}
i=strlen(yf)-2;
while(tyf[i]!='N'&&tyf[i]!='E')
{
if(tyf[i]==')')
{
i--;
continue;
}
else
{
h[i]=1;
cc[i]=tyf[i];
i--;
}
}
int flag=0;
int flag2=0;
for(i=0;i<100;i++)
if(h[i])
{
if(h[i]==1)
{
cout<<"第"<
}
else
{
cout<<"第"<
flag2=1;
}
}
if(!flag)
{
cout<<"运算式语法正确!"<
{
yyfx(yuyi);
}
}
}
return 0;
}
/*
输入(0124.3+0x35a.4f)*12
输出:
运算式语法正确!
运算结果 11331.75000
输入 12a+45*013468-0x23a3
输出:
第一个单词是错误数据
第五个单词是错误数据
运算式语
法正确!
输入 (1-44))/4
输出:
第六个单词‘)’出错
输入 (1-4(4))/4
输出:
第五个单词‘(’出错
第八个单词‘)’出错
输入 0x11+07
输出:
运算式语法正确!
运算结果 24.00000
输入 0x11.88+07
输出:
运算式语法正确!
运算结果 24.75000
输入 0x11+07.44
输出:
运算式语法正确!
运算结果 24.75000
输入 10*(2+3)/100
输出:
运算式语法正确!
运算结果 0.50000
*/