上课没听啥,一边看书,一边做的。基本上,老师的要求是实现了,能把一个句子(也可以是一整个程序,但还不完善,由于时间急,很多情况来不及考虑了)中的单词项分析出它是保留字,还是标识符,运算符等等。
开始是在控制台下做得,代码也比较纯正:
//lex.h
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/1327ab569c1ae82736693a50b8e33378.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/0196c3df5ea9e936f21e9932cca91014.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/1327ab569c1ae82736693a50b8e33378.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/0196c3df5ea9e936f21e9932cca91014.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
//lex.cpp
#include "lex.h"
#include <iostream>
#include <fstream>
#include <iostream>
#include <fstream>
Clex::Clex(string str):strindex(0),instr(str)
{
SetRevstr();
SetOper();
SetFrontier();
}
{
SetRevstr();
SetOper();
SetFrontier();
}
bool Clex::IsLetter(char& chr) const
{
if((chr>64 && chr<91)||(chr>96 && chr<123))
return true;
return false;
}
{
if((chr>64 && chr<91)||(chr>96 && chr<123))
return true;
return false;
}
bool Clex::IsDigit(char& chr) const
{
if(chr>47 && chr<58)
return true;
return false;
}
{
if(chr>47 && chr<58)
return true;
return false;
}
size_t Clex::Retract()
{
if(strindex>0) return --strindex;
return 0;
}
{
if(strindex>0) return --strindex;
return 0;
}
void Clex::SetRevstr()
{
string tempstr[]={"if","else","switch","case","default",
"break","continue","return","while","do","for","register",
"void","int","float","double","bool","char","long",
"enum","union","sizeof","const","static"};
size_t num=sizeof tempstr/sizeof tempstr[0];
for(size_t i=0;i<num;i++)
revstr.push_back(tempstr[i]);
revstr.sort();
}
{
string tempstr[]={"if","else","switch","case","default",
"break","continue","return","while","do","for","register",
"void","int","float","double","bool","char","long",
"enum","union","sizeof","const","static"};
size_t num=sizeof tempstr/sizeof tempstr[0];
for(size_t i=0;i<num;i++)
revstr.push_back(tempstr[i]);
revstr.sort();
}
bool Clex::IsReserve(string& wstr)
{
for(list<string>::iterator ite=revstr.begin();ite!=revstr.end();++ite)
if(wstr==(*ite)) return true;
return false;
}
{
for(list<string>::iterator ite=revstr.begin();ite!=revstr.end();++ite)
if(wstr==(*ite)) return true;
return false;
}
bool Clex::IsIdentifier(string& wstr) //should put behind IsReserve
{
if(wstr!="" && IsLetter(wstr.at(0))) return true;
return false;
}
{
if(wstr!="" && IsLetter(wstr.at(0))) return true;
return false;
}
bool Clex::IsConst(string& wstr)
{
if(wstr!="" && wstr.at(0)=='"' && wstr.at(wstr.length()-1)=='"') return true;
else if(IsDigit(wstr.at(0)))
{
for(size_t index=1;index<wstr.length();++index)
if(!IsDigit(wstr.at(index)) && wstr.at(index)!='.') return false;
return true;
}
return false;
}
{
if(wstr!="" && wstr.at(0)=='"' && wstr.at(wstr.length()-1)=='"') return true;
else if(IsDigit(wstr.at(0)))
{
for(size_t index=1;index<wstr.length();++index)
if(!IsDigit(wstr.at(index)) && wstr.at(index)!='.') return false;
return true;
}
return false;
}
void Clex::SetOper()
{
string tempstr[]={"+","-","*","/","&&",
"||","&","|",">",">=","<","<=",
"=","==",">>","<<"};
size_t num=sizeof tempstr/sizeof tempstr[0];
for(size_t i=0;i<num;i++)
oper.push_back(tempstr[i]);
}
{
string tempstr[]={"+","-","*","/","&&",
"||","&","|",">",">=","<","<=",
"=","==",">>","<<"};
size_t num=sizeof tempstr/sizeof tempstr[0];
for(size_t i=0;i<num;i++)
oper.push_back(tempstr[i]);
}
bool Clex::IsOper(string& wstr)
{
for(list<string>::iterator ite=oper.begin();ite!=oper.end();++ite)
if(wstr==*ite) return true;
return false;
}
{
for(list<string>::iterator ite=oper.begin();ite!=oper.end();++ite)
if(wstr==*ite) return true;
return false;
}
void Clex::SetFrontier()
{
string tempstr[]={"(", ")", ";", ","};
size_t num=sizeof tempstr/sizeof tempstr[0];
for(size_t i=0;i<num;i++)
frontier.push_back(tempstr[i]);
}
{
string tempstr[]={"(", ")", ";", ","};
size_t num=sizeof tempstr/sizeof tempstr[0];
for(size_t i=0;i<num;i++)
frontier.push_back(tempstr[i]);
}
bool Clex::IsFrontier(string& wstr)
{
for(list<string>::iterator ite=frontier.begin();ite!=oper.end();++ite)
if(wstr==*ite) return true;
return false;
}
{
for(list<string>::iterator ite=frontier.begin();ite!=oper.end();++ite)
if(wstr==*ite) return true;
return false;
}
void Clex::SetAtt(word& tw)
{
if(IsReserve(tw.content)) tw.att=rev;
else if(IsIdentifier(tw.content)) tw.att=idf;
else if(IsConst(tw.content)) tw.att=con;
else if(IsOper(tw.content)) tw.att=opr;
else if(IsFrontier(tw.content)) tw.att=fon;
}
{
if(IsReserve(tw.content)) tw.att=rev;
else if(IsIdentifier(tw.content)) tw.att=idf;
else if(IsConst(tw.content)) tw.att=con;
else if(IsOper(tw.content)) tw.att=opr;
else if(IsFrontier(tw.content)) tw.att=fon;
}
void Clex::GetBC()
{
size_t tempt=strindex;
while(GetChar()==' ' && strindex<instr.length()) GetChar();
if(tempt!=strindex) Retract();
}
{
size_t tempt=strindex;
while(GetChar()==' ' && strindex<instr.length()) GetChar();
if(tempt!=strindex) Retract();
}
char Clex::GetChar()
{
if(strindex<instr.length()) ++strindex;
return instr.at(strindex-1);
}
{
if(strindex<instr.length()) ++strindex;
return instr.at(strindex-1);
}
void Clex::Predo()
{
GetBC();
while(strindex<instr.length())
{
word tw;
char tchr;
while(strindex<instr.length() && (tchr=GetChar())!=' ')
{
tw.content+=tchr;
}
SetAtt(tw);
words.push_back(tw);
GetBC();
}
cout<<"words.size()="<<words.size()<<endl;
}
{
GetBC();
while(strindex<instr.length())
{
word tw;
char tchr;
while(strindex<instr.length() && (tchr=GetChar())!=' ')
{
tw.content+=tchr;
}
SetAtt(tw);
words.push_back(tw);
GetBC();
}
cout<<"words.size()="<<words.size()<<endl;
}
string Clex::temp(int t)
{
switch(t)
{
case 1:return "保留字";break;
case 2:return "标识符";break;
case 3:return "常数";break;
case 4:return "运算符";break;
case 5:return "界符";break;
default:return "其他东东";
}
}
{
switch(t)
{
case 1:return "保留字";break;
case 2:return "标识符";break;
case 3:return "常数";break;
case 4:return "运算符";break;
case 5:return "界符";break;
default:return "其他东东";
}
}
void Clex::Output()
{
Predo();
for(list<word>::iterator ite=words.begin();ite!=words.end();++ite)
cout<<(*ite).content<<""<<"-----is----"<<temp((*ite).att)<<endl;
}
{
Predo();
for(list<word>::iterator ite=words.begin();ite!=words.end();++ite)
cout<<(*ite).content<<""<<"-----is----"<<temp((*ite).att)<<endl;
}
int main()
{
string str;
cout<<"Please input a string:";
getline(cin,str);
Clex yanlex(str);
yanlex.Output();
}
{
string str;
cout<<"Please input a string:";
getline(cin,str);
Clex yanlex(str);
yanlex.Output();
}
注:getline(cin,str);读取标准输入的一行并赋值给str,单纯的cin>>str由于空格优先限定原则每次至多只能读取一个单词。如输入"if ( i = 1 )",str将为"if"而不是"if ( i = 1 )"。