📄 cffxq.cpp
字号:
// cffxq.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
class Word_analyzer
{
public:
ifstream infile;
ofstream outfile;
Word_analyzer();
int analyze(char &c);
int symbol_number_analyzer(char &c,int &count);
int number_analyzer(char &c,string &str,int &count);
int number_loop(char &c,string &str);
int exponent_analyzer(char &c,string &str);
int point_analyzer(char &c,string &str);
int id_analyzer(char &c,int &count);
int id_loop(char &c,string &str);
int id_compare(char &c,string &str);
int key_analyzer(char &c,int &count);
int note_analyzer(char &c,int &count);
int other_char_analyzer(char &c);
int exponent_analyzer_two(char &c,string &str);
private:
char a[10],b[4];
string key_table[6];
};
int main(int argc, char* argv[])
{
char c='\0';
Word_analyzer analyzer1;
while(analyzer1.infile.get(c))
analyzer1.analyze(c);
analyzer1.infile.close();
analyzer1.outfile.close();
return 0;
}
Word_analyzer::Word_analyzer()
{
a[0]='+';
a[1]='-';
a[2]=',';
a[3]=';';
a[4]=')';
a[5]='[';
a[6]=']';
a[7]='{';
a[8]='}';
a[9]='*';
b[0]='<';
b[1]='>';
b[2]='=';
b[3]='!';
key_table[0]="else";
key_table[1]="if";
key_table[2]="int";
key_table[3]="return";
key_table[4]="void";
key_table[5]="while";
infile.open("f1.txt",ios::in);
if(!infile)
{
cerr<<"open f1.txt error!"<<endl;
exit(1);
}
outfile.open("f2.txt",ios::out);
if(!outfile)
{
cerr<<"open f2.txt error!"<<endl;
exit(1);
}
}
int Word_analyzer::key_analyzer(char &c,int &count)
{
string str="";
count=0;
for(int i=0;i<10;i++)
{
if(c==a[i])
{
cout<<"(专用符号 "<<c<<')'<<endl;
outfile<<"(专用符号 "<<c<<')'<<endl;
return 0;
}
}
for(int j=0;j<4;j++)
{
if(c==b[j])
{
str+=c;
if(infile.get(c))
{
if(c=='=')
{
str+=c;
cout<<"(专用符号 "<<str<<')'<<endl;
outfile<<"(专用符号 "<<str<<')'<<endl;
return 0;
}
else
{
if(j==3)
{
cout<<"(不可识别 "<<str<<')'<<endl;
outfile<<"(不可识别 "<<str<<')'<<endl;
infile.seekg(-1,ios::cur);
return 0;
}
else
{
cout<<"(专用符号 "<<str<<')'<<endl;
outfile<<"(专用符号 "<<str<<')'<<endl;
infile.seekg(-1,ios::cur);
return 0;
}
}
}
else
{
if(j==3)
{
cout<<"(不可识别 "<<str<<')'<<endl;
outfile<<"(不可识别 "<<str<<')'<<endl;
infile.seekg(-1,ios::cur);
return 0;
}
else
{
cout<<"(专用符号 "<<str<<')'<<endl;
outfile<<"(专用符号 "<<str<<')'<<endl;
infile.seekg(-1,ios::cur);
return 0;
}
}
}
}
count=1;
return 0;
}
int Word_analyzer::number_loop(char &c,string &str)//数字循环
{
while(infile.get(c))
{
if('0'<=c&&c<='9')
str+=c;
else
return 0;
}
return 0;
}
int Word_analyzer::exponent_analyzer(char &c,string &str)//指数循环
{
if(c=='e')
{
str+=c;
if(infile.get(c))
{
if(c=='+'||c=='-')
{
str+=c;
if(infile.get(c))
{
if('0'<=c&&c<='9')
{
str+=c;
number_loop(c,str);
cout<<"(常数符号 "<<str<<')'<<endl;
outfile<<"(常数符号 "<<str<<')'<<endl;
infile.seekg(-1,ios::cur);
return 0;
}
else
{
cout<<"(错误常数 "<<str<<')'<<endl;
outfile<<"(错误常数 "<<str<<')'<<endl;
infile.seekg(-1,ios::cur);
return 0;
}
}
else
{
cout<<"(错误常数 "<<str<<')'<<endl;
outfile<<"(错误常数 "<<str<<')'<<endl;
return 0;
}
}
else
{
if('0'<=c&&c<='9')
{
str+=c;
number_loop(c,str);
cout<<"(常数符号 "<<str<<')'<<endl;
outfile<<"(常数符号 "<<str<<')'<<endl;
infile.seekg(-1,ios::cur);
return 0;
}
else
{
cout<<"(错误常数 "<<str<<')'<<endl;
outfile<<"(错误常数 "<<str<<')'<<endl;
infile.seekg(-1,ios::cur);
return 0;
}
}
}//718
else
{
cout<<"(错误常数 "<<str<<')'<<endl;
outfile<<"(错误常数 "<<str<<')'<<endl;
return 0;
}
}//715
else
{
cout<<"(常数符号 "<<str<<')'<<endl;
outfile<<"(常数符号 "<<str<<')'<<endl;
infile.seekg(-1,ios::cur);
return 0;///////////////////
}
}
int Word_analyzer::exponent_analyzer_two(char &c,string &str)
{
if(c=='e')
{
str+=c;
if(infile.get(c))
{
if(c=='+'||c=='-')
{
str+=c;
if(infile.get(c))
{
if('0'<=c&&c<='9')
{
str+=c;
number_loop(c,str);
cout<<"(常数符号 "<<str<<')'<<endl;
outfile<<"(常数符号 "<<str<<')'<<endl;
infile.seekg(-1,ios::cur);
return 0;
}
else
{
cout<<"(错误常数 "<<str<<')'<<endl;
outfile<<"(错误常数 "<<str<<')'<<endl;
infile.seekg(-1,ios::cur);
return 0;
}
}
else
{
cout<<"(错误常数 "<<str<<')'<<endl;
outfile<<"(错误常数 "<<str<<')'<<endl;
return 0;
}
}
else
{
if('0'<=c&&c<='9')
{
str+=c;
number_loop(c,str);
if(('A'<=c&&c<='Z')||('a'<=c&&c<='z'))
{
str+=c;
id_loop(c,str);
cout<<"(错误标识 "<<str<<')'<<endl;
outfile<<"(错误标识 "<<str<<')'<<endl;
infile.seekg(-1,ios::cur);
return 0;
}
else
{
cout<<"(常数符号 "<<str<<')'<<endl;
outfile<<"(常数符号 "<<str<<')'<<endl;
infile.seekg(-1,ios::cur);
return 0;
}
}
else
{
if(('A'<=c&&c<='Z')||('a'<=c&&c<='z'))
{
str+=c;
id_loop(c,str);
cout<<"(错误标识 "<<str<<')'<<endl;
outfile<<"(错误标识 "<<str<<')'<<endl;
infile.seekg(-1,ios::cur);
return 0;
}
else
{
cout<<"(错误常数 "<<str<<')'<<endl;
outfile<<"(错误常数 "<<str<<')'<<endl;
infile.seekg(-1,ios::cur);
return 0;
}
}
}
}//718
else
{
cout<<"(错误常数 "<<str<<')'<<endl;
outfile<<"(错误常数 "<<str<<')'<<endl;
return 0;
}
}//715
else
{
if(('A'<=c&&c<='Z')||('a'<=c&&c<='z'))
{
str+=c;
id_loop(c,str);
cout<<"(错误标识 "<<str<<')'<<endl;
outfile<<"(错误标识 "<<str<<')'<<endl;
infile.seekg(-1,ios::cur);
return 0;
}
else
{
cout<<"(常数符号 "<<str<<')'<<endl;
outfile<<"(常数符号 "<<str<<')'<<endl;
infile.seekg(-1,ios::cur);
return 0;///////////////////
}
}
}
int Word_analyzer::point_analyzer(char &c,string &str)//小数点后循环
{
if(c=='.')
{
str+=c;
if(infile.get(c))
{
if('0'<=c&&c<='9')
{
str+=c;
number_loop(c,str);
exponent_analyzer(c,str);
return 0;
}
else
{
cout<<"(错误常数 "<<str<<')'<<endl;
outfile<<"(错误常数 "<<str<<')'<<endl;
infile.seekg(-1,ios::cur);
return 0;
}
}
else
{
cout<<"(错误常数 "<<str<<')'<<endl;
outfile<<"(错误常数 "<<str<<')'<<endl;
return 0;
}
}
else
{
exponent_analyzer_two(c,str);/////////////////////////////////////////////////////
return 0;
}
}
int Word_analyzer::number_analyzer(char &c,string &str,int &count)
{
if('0'<=c&&c<='9')
{
str+=c;
number_loop(c,str);
point_analyzer(c,str);
return 0;
}//601
else
{
count=1;
return 0;
}
}//函数尾
int Word_analyzer::symbol_number_analyzer(char &c,int &count)
{
string str="";
count=0;
if(c=='(')
{
cout<<"(专用符号 "<<c<<')'<<endl;
outfile<<"(专用符号 "<<c<<')'<<endl;
if(infile.get(c))
{
if(c=='+'||c=='-')
{
str+=c;
if(infile.get(c))
{
if('0'<=c&&c<='9')
{
str+=c;
number_loop(c,str);
point_analyzer(c,str);
return 0;
}//567
else
{
cout<<"(专用符号 "<<str<<')'<<endl;
outfile<<"(专用符号 "<<str<<')'<<endl;
infile.seekg(-1,ios::cur);
return 0;
}
}//565
else
{
cout<<"(专用符号 "<<str<<')'<<endl;
outfile<<"(专用符号 "<<str<<')'<<endl;
return 0;
}
}//562
else
{
number_analyzer(c,str,count);
return 0;
}
}//560
else return 0;
}//557
else
{
number_analyzer(c,str,count);
return 0;
}
}//函数尾
int Word_analyzer::id_loop(char &c,string &str)
{
while(infile.get(c))
{
if(('0'<=c&&c<='9')||('A'<=c&&c<='Z')||('a'<=c&&c<='z'))
str+=c;
else
return 0;
}//620
return 0;
}//函数尾
int Word_analyzer::id_compare(char &c,string &str)
{
for(int k=0;k<6;k++)
{
if(str==key_table[k])
{
cout<<"(关键字符 "<<str<<')'<<endl;
outfile<<"(关键字符 "<<str<<')'<<endl;
infile.seekg(-1,ios::cur);
return 0;
}
}
cout<<"(标识字符 "<<str<<')'<<endl;
outfile<<"(标识字符 "<<str<<')'<<endl;
infile.seekg(-1,ios::cur);
return 0;
}//函数尾
int Word_analyzer::id_analyzer(char &c,int &count)
{
string str="";
count=0;
if(('A'<=c&&c<='Z')||('a'<=c&&c<='z'))
{
str+=c;
id_loop(c,str);
id_compare(c,str);
return 0;
}//655
else
{
if('0'<=c&&c<='9')
{
str+=c;
id_loop(c,str);
cout<<"(错误标识 "<<str<<')'<<endl;
outfile<<"(错误标识 "<<str<<')'<<endl;
infile.seekg(-1,ios::cur);
return 0;
}//664
else
{
count=1;
return 0;
}
}
}//函数尾*/
int Word_analyzer::note_analyzer(char &c,int &count)
{
string str="";
count=0;
if(c=='/')
{
str+=c;
if(infile.get(c))
{
if(c=='*')
{
str+=c;
cout<<"(注释符号 "<<str<<')'<<endl;
outfile<<"(注释符号 "<<str<<')'<<endl;
str="";
while(infile.get(c))
{
if(c=='*')
{
str+=c;
if(infile.get(c))
{
if(c=='/')
{
str+=c;
cout<<"(注释符号 "<<str<<')'<<endl;
outfile<<"(注释符号 "<<str<<')'<<endl;
return 0;
}
;
}//424
;
}//421
;
}//419
return 0;
}
else
{
cout<<"(专用符号 "<<str<<')'<<endl;
outfile<<"(专用符号 "<<str<<')'<<endl;
infile.seekg(-1,ios::cur);
return 0;
}
}//411
else
{
cout<<"(专用符号 "<<str<<')'<<endl;
outfile<<"(专用符号 "<<str<<')'<<endl;
return 0;
}
}//408
else
{
count=1;
return 0;
}
}
int Word_analyzer::other_char_analyzer(char &c)
{
if(c!=' '&&c!='\n'&&c!='\t')
{
cout<<"(不可识别 "<<c<<')'<<endl;
outfile<<"(不可识别 "<<c<<')'<<endl;
}
return 0;
}
int Word_analyzer::analyze(char &c)
{
int count=0;
symbol_number_analyzer(c,count);
if(count==0)
return 0;
else
key_analyzer(c,count);
if(count==0)
return 0;
else
id_analyzer(c,count);
if(count==0)
return 0;
else
note_analyzer(c,count);
if(count==0)
return 0;
else
other_char_analyzer(c);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -