//对一串字符串进行分类。
#include
#include
using namespace std;
#define N 256
void getsym(char STR[N],string SYM,string *ID,int *NUM){
char *s;
for(int i=0;i<N;i++) //去空格
{
if(STR[i]!=' ')
{
*s=STR[i];
s++;
}
}
for(i=0;*s!=NULL;i++) //保留字,符号,数字,标识符的识别
{
//识别保留字:var、、const、、begin、end、、while、、do、、read、、write、if、、then、、procedure、、call、、
if((*s=='v')&&((s+1)=='a')&&(*(s+2)=='r'))
{
SYM++="var";
cout<<"VARSYM:var"<<endl;
s=s-3;
}
else if((*s=='c')&&((s+1)=='o')&&(*(s+2)=='n')&&(*(s+3)=='s')&&(*(s+4)='t'))
{
*SYM++="const";
cout<<"CONSTSYM:const"<<endl;
s=s-5;
}
else if((*s=='b')&&(*(s+1)=='e')&&(*(s+2)=='g')&&(*(s+3)=='i')&&(*(s+4)='n'))
{
*SYM++="begin";
cout<<"BEGINSYM:begin"<<endl;
s=s-5;
}
else if((*s=='e')&&(*(s+1)=='n')&&(*(s+2)=='d'))
{
*SYM++="end";
cout<<"ENDSYM:end"<<endl;
s=s-3;
}
else if((*s=='w')&&(*(s+1)=='h')&&(*(s+2)=='i')&&(*(s+3)=='l')&&(*(s+4)='e'))
{
*SYM++="while";
cout<<"WHILESYM:while"<<endl;
s=s-5;
}
else if((*s=='d')&&(*(s+1)=='o'))
{
*SYM++="do";
cout<<"DOSYM:do"<<endl;
s=s-2;
}
else if((*s=='r')&&(*(s+1)=='e')&&(*(s+2)=='a')&&(*(s+3)=='d'))
{
*SYM++="read";
cout<<"READSYM:read"<<endl;
s=s-5;
}
else if((*s=='w')&&(*(s+1)=='r')&&(*(s+2)=='i')&&(*(s+3)=='t')&&(*(s+4)='e'))
{
*SYM++="write";
cout<<"WRITESYM:write"<<endl;
s=s-5;
}
else if((*s=='i')&&(*(s+1)=='f'))
{
*SYM++="if";
cout<<"IFSYM:if"<<endl;
s=s-2;
}
else if((*s=='t')&&(*(s+1)=='h')&&(*(s+2)=='e')&&(*(s+3)=='n'))
{
*SYM++="then";
cout<<"THENSYM:then"<<endl;
s=s-4;
}
else if((*s=='p')&&(*(s+1)=='r')&&(*(s+2)=='o')&&(*(s+3)=='c')&&(*(s+4)='e')&&(*(s+5)='d')&&(*(s+6)='u')&&(*(s+7)='r')&&(*(s+8)='e'))
{
*SYM++="procedure";
cout<<"PROCEDURESYM:procedure"<<endl;
s=s-9;
}
else if((*s=='c')&&(*(s+1)=='a')&&(*(s+2)=='l')&&(*(s+3)='l'))
{
*SYM++="call";
cout<<"CALLSYM:call"<<endl;
s=s-4;
}
//识别符号
else if((*s==',')||(*s==';')||(*s==':')||(*s=='=')||(*s=='>')||(*s=='<')||(*s=='!')||(*s=='(')||(*s==')')||(*s=='.'))
{
cout<<"symbol:"<<*s<<endl;
s=s-1;
}
//识别数字
else if((*s<58)&&(*s>48))
{
*NUM++=*s;
cout<<"number:"<<*s<<endl;
s=s-1;
}
//识别标识符
else
{
cout<<*s;
s=s-1;
}
}
}
int main()
{
char str[N];
string *sym=0,*id=0;
int *num=0;
cout<<"please input the code:\n";
for(int i=0;i
{
cin>>str[i];
if(str[i]=='.')
break;
}
getsym(str,sym,id,num);
return 0;
}