/*
无符号数的识别
编译原理与实践 --张菁
P59
*/
#include<stdio.h>
int M(int S,int t)
{
switch(S)
{
case 0:
switch(t)
{
case '0': case '1':
case '2': case '3':
case '4': case '5':
case '6': case '7':
case '8': case '9': return 1;
case '.': return 2;
case 'E': return 3;
}
break;
case 1:
switch(t)
{
case '0': case '1':
case '2': case '3':
case '4': case '5':
case '6': case '7':
case '8': case '9': return 1;
case '.': return 2;
case 'E': return 3;
}
break;
case 2:
switch(t)
{
case '0': case '1':
case '2': case '3':
case '4': case '5':
case '6': case '7':
case '8': case '9': return 4;
}
break;
case 3:
switch(t)
{
case '+': case '-': return 5;
}
break;
case 4:
switch(t)
{
case '0': case '1':
case '2': case '3':
case '4': case '5':
case '6': case '7':
case '8': case '9': return 4;
case 'E': return 3;
}
break;
case 5:
switch(t)
{
case '0': case '1':
case '2': case '3':
case '4': case '5':
case '6': case '7':
case '8': case '9': return 6;
}
break;
case 6:
switch(t)
{
case '0': case '1':
case '2': case '3':
case '4': case '5':
case '6': case '7':
case '8': case '9': return 6;
}
break;
}
return -1;
}
bool Realize(char* buf)
{
char S=0; //S=S0
char c=buf[0];
int i=1;
while(c!='\0')
{
printf("M(%d,%c)=",S,c); //
S=M(S,c);
printf("%d\n",S); //
if(S==-1) return false;
c=buf[i++];
}
return ( (S==1||S==4||S==6)?true:false );
}
int main()
{
char buf[50]={"3.1415928E+10"};
printf("FA=( {0,1,2,3,4,5,6},{0,1,2,3,4,5,6,7,8,9,.,+,-,E},M,0,{1,4,6} )\n");
printf("M:\n");
printf(" M(0,0)=1 ... M(0,9)=1 M(0,.)=2 M(0,E)=3\n");
printf(" M(1,0)=1 ... M(1,9)=1 M(1,.)=2 M(1,E)=3\n");
printf(" M(2,0)=4 ... M(2,9)=4\n");
printf(" M(3,+)=5 M(3,-)=5\n");
printf(" M(4,0)=4 ... M(4,9)=4 M(4,E)=3\n");
printf(" M(5,0)=6 ... M(5,9)=6\n");
printf(" M(6,0)=6 ... M(6,9)=6\n");
printf("the string is \"%s\"\n",buf);
if(Realize(buf)) printf("Yes.\n");
else printf("No.\n");
return 0;
}