/*
* 该自动机能够判断输入的字符串是关键字(保留字),还是标示符,或者不是标示符
* 这个自动机还不能识别数字
* 关键字用一个二维数组给出,标示符的命名规则是:由数字、下划线和字母组成,数字不能开头,最多只有8个字符,超过八个时前8个有效。
* iszm函数的作用是判断一个字符是否为字母
* sign表示标示符,blz表示保留字
*/
#include "stdio.h"
#include "string.h"
int iszm(char n);
int isblz(char s[]);
main()
{
int i=0;
char ch,c[9],temp;
printf("Please input the string :");
temp=getchar();
c[i++]=temp;
while((ch=getchar())!='\n')
{
if(iszm(ch)==0 && ch!='_' && (ch>'9' || ch<'0'))
break;
if(i<8) //当标示符的字符个数超过8个时,只有前8个有效
c[i++]=ch;
}
c[i]='\0';
if(iszm(temp)==0 && temp!='_')
printf("The string you entered isn't a sign or a blz,you can't define a sign like this.\n");
else if(ch!='\n')
printf("The string you entered isn't a right sign!\n");
else if (isblz(c)==1)
printf("The string you entered is a blz, It's %s\n",c);
else if (isblz(c)==0)
printf("The string you entered is a right sign,It's %s\n",c);
}
int iszm(char n)
{
if((n>='A' && n<='Z') || (n>='a' && n<='z'))
return 1; //是字母
else
return 0; //不是字母
}
int isblz(char s[])
{
char blz[32][9]={"auto","break","case","char","const","continue","default","do","double","else","enum","extern","float","for","goto","if","int","long","register","return","short","signed","sizeof","static","struct","switch","typedef","unsigned","union","void","volatile","while"};
int i=0;
for(i=0;i<32;i++)
{
if(strcmp(s,blz[i])==0)
break;
}
if(i==32)
return 0; //不是保留字
else
return 1; //是保留字
}