📄 词法分析器.zip.txt
字号:
b->bword = ch;
b->sign = 29;
b_outfile.open("saveb.dat",ios::binary|ios::app);
b_outfile.write((char *)b,sizeof(*b));
b_outfile.close();
i++;
break;
case '}':
b->bword = ch;
b->sign = 30;
b_outfile.open("saveb.dat",ios::binary|ios::app);
b_outfile.write((char *)b,sizeof(*b));
b_outfile.close();
i++;
break;
case '\"':
b->bword = ch;
b->sign = 31;
b_outfile.open("saveb.dat",ios::binary|ios::app);
b_outfile.write((char *)b,sizeof(*b));
b_outfile.close();
i++;
break;
case'(':
b->bword = ch;
b->sign = 23;
b_outfile.open("saveb.dat",ios::binary|ios::app);
b_outfile.write((char *)b,sizeof(*b));
b_outfile.close();
i++;
break;
case ')':
b->bword = ch;
b->sign = 24;
b_outfile.open("saveb.dat",ios::binary|ios::app);
b_outfile.write((char *)b,sizeof(*b));
b_outfile.close();
i++;
break;
case '[':
b->bword = ch;
b->sign = 25;
b_outfile.open("saveb.dat",ios::binary|ios::app);
b_outfile.write((char *)b,sizeof(*b));
b_outfile.close();
i++;
break;
case ']':
b->bword = ch;
b->sign = 26;
b_outfile.open("saveb.dat",ios::binary|ios::app);
b_outfile.write((char *)b,sizeof(*b));
b_outfile.close();
i++;
break;
default:
cout << "ERROR:不能识别"
<< ch << endl;
exit(0);
}//end of switch
}//end of else
}//end of else
}//end of while
if(cpp[i] != '#')
return 1;
else return 0;
}
//字符变数字函数
float Analysis::Change_to_float(char save_ch[])
{
int len,i,j,temp_len;
float Result = 0,temp_Result;
len = strlen(save_ch);
for(i = 0;i<len;i++)
{
if(save_ch[i] == '.') { temp_len = i; break;}//寻找小数点
}
if(i ==len)
{
for(i=0;i<len;i++)
{
temp_Result = save_ch[i] - '0';//无小数点时将字符转化为数字
for(j = len-1;j>i;j--)
temp_Result *= 10;
Result += temp_Result;
}
return Result;
}
else//有小数点时
{
for(i=0;i<temp_len;i++)
{
temp_Result = save_ch[i] - '0';//计算整数部分
for(j = temp_len-1;j>i;j--)
temp_Result *= 10;
Result += temp_Result;
}
for(i=temp_len+1;i<len;i++)//计算小数部分
{
temp_Result = (float)(save_ch[i] - '0');
for(j=temp_len;j<i;j++)
temp_Result /= 10;
Result += temp_Result;
}
return Result;
}
}
void Analysis::show_Result()
{
ifstream k_infile;
ifstream s_infile;
ifstream d_infile;
ifstream o_infile;
ifstream b_infile;
int i,j;
i = j = 0;
k_infile.open("savek.dat",ios::binary);//读关键字
if(!k_infile)
{
cout << "关键字文件不能读!"
<< endl;
exit(0);
}
k_infile.seekg(0,ios::beg);//以下为判空
int len_a = k_infile.tellg();
k_infile.seekg(0,ios::end);
int len_b = k_infile.tellg();
if(len_a == len_b)
cout << "ERROR:关键字为空!"
<< endl;
else
{
cout << "关键字:" << endl;
k_infile.seekg(0);
k_infile.read((char *)k,sizeof(*k));
while(!k_infile.eof())
{
cout << "( Sign: "
<< k->sign
<< " "
<< "Value: "
<< k->kword
<< " )";
cout << "\t";
k_infile.read((char *)k,sizeof(*k));
}
cout << endl;
}
k_infile.close();
s_infile.open("saves.dat",ios::binary);//读标识符
if(!s_infile)
{
cout << "标识符文件不能读!"
<< endl;
exit(0);
}
s_infile.seekg(0,ios::beg);//以下为判空
len_a = s_infile.tellg();
s_infile.seekg(0,ios::end);
len_b = s_infile.tellg();
if(len_a == len_b)
cout << "标识符为空!"
<< endl;
else
{
cout << "标识符:" << endl;
s_infile.seekg(0);
s_infile.read((char *)s,sizeof(*s));
while(!s_infile.eof())
{
cout << "( Sign: "
<< s->sign
<< " "
<< "Value: "
<< s->sword
<< " "
<< "Loc: "
<< s->sloc
<< " )";
cout << "\t\t";
s_infile.read((char *)s,sizeof(*s));
}
cout << endl;
}
s_infile.close();
d_infile.open("saved.dat",ios::binary);//读数字
if(!d_infile)
{
cout << "数字文件不能读!"
<< endl;
exit(0);
}
d_infile.seekg(0,ios::beg);//以下为判空
len_a = d_infile.tellg();
d_infile.seekg(0,ios::end);
len_b = d_infile.tellg();
if(len_a ==len_b)
cout << "数字为空!"
<< endl;
else
{
cout << "数字:" ;
d_infile.seekg(0);
d_infile.read((char *)d,sizeof(*d));
while(!d_infile.eof())
{
cout << "\n( Sign: "
<< d->sign
<< " "
<< "Value: "
<< d->data;
cout << " Binary:";
if(d->flag == 1)
cout << "1";
else cout << "0";
i = d->len_int + 1;
while(i < 15)
{
cout <<"0";
i++;
}
i = 0;
while(d->len_int >=0)
cout << d->int_Binary[d->len_int--];
cout << ".";
i = 0;
while(i < d->len_flo)
cout << d->flo_Binary[i++];
while(d->len_flo <= 16)
{
cout << "0";
d->len_flo++;
}
cout << ")";
d_infile.read((char *)d,sizeof(*d));
}
}
cout << endl;
d_infile.close();
o_infile.open("saveo.dat",ios::binary);//读运算符
if(!o_infile)
{
cout << "运算符文件不能读!"<< endl;
exit(0);
}
o_infile.seekg(0,ios::beg);//以下为判空
len_a = o_infile.tellg();
o_infile.seekg(0,ios::end);
len_b = o_infile.tellg();
if(len_a == len_b)
cout << "运算符为空!" << endl;
else
{
cout << "运算符:" << endl;
o_infile.seekg(0);
o_infile.read((char *)o,sizeof(*o));
while(!o_infile.eof())
{
cout<< "( Sign: "
<<o->sign
<< " "
<< "Value: "
<< o->oword
<< " )";
cout << "\t\t";
o_infile.read((char *)o,sizeof(*o));
}
}
cout << endl;
o_infile.close();
b_infile.open("saveb.dat",ios::binary);//读分界符
if(!b_infile)
{
cout << "界符文件不能读!" << endl;
exit(0);
}
b_infile.seekg(0,ios::beg);//以下为判空
len_a = b_infile.tellg();
b_infile.seekg(0,ios::end);
len_b = b_infile.tellg();
if(len_a == len_b)
cout << "ERROR:分界符为空!" << endl;
else
{
cout << "分界符:" << endl;
b_infile.seekg(0);
b_infile.read((char *)b,sizeof(*b));
while(!b_infile.eof())
{
cout << "( Sign: "
<<b->sign
<< " "
<< " Value: "
<< b->bword
<< " )";
cout << "\t\t";
b_infile.read((char *)b,sizeof(*b));
}
}
b_infile.close();
cout << endl;
}
int main()
{
Analysis Ana;
kwf.Getin();
const int MAX = 81;
cout <<"********************************************************************************"
<< "\t\t\t类C计算语言(CCL)"
<< "\n\t\t\t(使用前请阅读说明)"
<< "\n\t\t\t提示:请以#号结束你的源程序!"
<< "\n\t\t\t 一.词法分析"
<< "\n\t\t\t\t\t 姚 2005.11.5
<< endl
<<"********************************************************************************"
<< "\n请输入源程序:\n";
char CPP[MAX];//保存一行程序
while(true)
{
cin.get(CPP,MAX,'\n');
cin.ignore(1,'\n');
if( !Ana.Search_word(CPP))
{
Ana.show_Result();
break;
}
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -