我研究编译器设计在我的大学这个程序是用来识别每一个word,number,operation,separator在进入行用户,当你进入像int i = 0 ; (每个单词之间的空格是必要的),
它识别int为关键字,i如ID,=作为平等的,0因为数量和;作为输出中的分隔符,我的老师给了我一项任务,将本节中的“ if else”更改为switch
for(int j=0;j<=k;j++){
if(token[j]=="int"|| token[j]=="string"||token[j]=="float")
{
cout<<"keyword : "<<token[j]<<endl;
num_token++;
}
else if(token[j]=="*"||token[j]=="+"||token[j]=="-"||token[j]=="/")
{
cout<<"operation : "<<token[j]<<endl;
num_token++;
}
else if(token[j]==","||token[j]==";")
{
cout<<"separator : "<<token[j]<<endl;
num_token++;
}
else if(token[j]=="=")
{
cout<<"equal : "<<token[j]<<endl;
num_token++;
}
else if(token[j]>="0"&&token[j]<="9")
{
cout<<"Number : "<<token[j]<<endl;
num_token++;
}
else if(token[j]>="a"&&token[j]<="z")
{
cout<<"ID : "<<token[j]<<endl; …Run Code Online (Sandbox Code Playgroud)