/* parsing, recursive descent parsing.
有限状态机。找出各种可能的状态,分析各种状态之间的转换关系。
PRE_NUM: 数分析前的状态
SIGN1: 数值部分的符号状态
PRE_POINT: 数值部分小数点前状态(1位数字)
POINT: 数值部分小数点状态
FRACTION: 数值部分小数点后数值状态(1-多位数字)(合法实型常数)
E: 指数符号状态
SIGN2: 指数部分的符号状态
EXP: 指数部分的指数状态 (合法实型常数)
FOLLOWBLANK: 常数后空格状态 (合法实型常数)
ERROR: 错误状态
用cin给字符变量输入数据时,自动过滤头部空白
*/
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
enum State{PRE_NUM,SIGN1,PRE_POINT,POINT,FRACTION,E,SIGN2,EXP,FOLLOWBLANK,ERROR};
char ch;
char str[1000];//存放读入的数
State state;
while(cin>>ch && ch!='*')
{
int idx=0;//索引号
state=PRE_NUM;
while(ch!='\n')
{
switch(state)
{
case PRE_NUM: if(ch=='+'||ch=='-') { state=SIGN1; break; }
if(ch>='0'&&ch<='9') { state=PRE_POINT; break; }
state=ERROR; break;
case SIGN1: if(ch>='0'&&ch<='9') state=PRE_POINT;
else state=ERROR;
break;
case PRE_POINT: if(ch=='.') { state=POINT; break; }
if(ch=='e'||ch=='E') { state=E; break; }
if(!(ch>='0'&&ch<='9')) state=ERROR;
break;
case POINT: if(ch>='0'&&ch<='9') state=FRACTION;
else state=ERROR;
break;
case FRACTION: if(ch=='e'||ch=='E') { state=E; break; }
if(ch==' ') { state=FOLLOWBLANK; break; }
if(!(ch>='0'&&ch<='9')) state=ERROR;
break;
case E: if(ch=='+'||ch=='-') { state=SIGN2; break; }
if(ch>='0'&&ch<='9') { state=EXP; break; }
state=ERROR; break;
case SIGN2: if(ch>='0'&&ch<='9') state=EXP;
else state=ERROR;
break;
case EXP: if(ch==' ') { state=FOLLOWBLANK; break; }
if(!(ch>='0'&&ch<='9')) state=ERROR;
break;
case FOLLOWBLANK: if(ch!=' ') state=ERROR;
default: break;
}
str[idx++]=ch;
cin.get(ch);
}
str[idx]='\0';
while(str[--idx]==' ')//去除尾部空格
str[idx]='\0';
cout<<str<<" is ";
if(state==FRACTION||state==EXP||state==FOLLOWBLANK)
cout<<"legal.\n";
else
cout<<"illegal.\n";
memset(str,0,++idx*sizeof(char));//str数组清空
}
return 0;
}