词法分析 #include #include #include using namespace std; #define MAXN 20000 int syn,p,sum,kk,m,n,row; double dsum,pos; char index[800],len;//记录指数形式的浮点数 char r[6][10]={"function","if","then","while","do","endfunc"} ; char token[MAXN],s[MAXN]; char ch; bool is_letter(char c) { return c>='a' && c<='z' || c>='A' && c<='Z'; } bool is_digtial(char c) { return c>='0' && c<='9'; } bool is_dot(char c) { return c==',' || c==';'; } void identifier()//标示符的判断 { m=0; while(ch>='a' && ch<='z' || ch>='0' && ch<='9') { token[m++]=ch; ch=s[++p]; } token[m]='\0'; ch=s[--p]; syn=10; for(n=0;n<6;n++) if(strcmp(token,r[n])==0) { syn=n+1; break; } } void digit(bool positive)//数字的判断 { len=sum=0; ch=s[p]; while(ch>='0' && ch<='9') { sum=sum*10+ch-'0'; ch=s[++p]; } if(ch=='.') { dsum=sum; ch=s[++p]; pos=0.1; while(ch>='0' && ch<='9') { dsum=dsum+(ch-'0')*pos; pos=pos*0.1; ch=s[++p]; } if(ch=='e') { index[len++]=ch; ch=s[++p]; if(ch=='-' || ch=='+') { index[len++]=ch; ch=s[++p]; } if(!(ch>='0' && ch<='9')) { syn=-1; } else { while(ch>='0' && ch<='9') { index[len++]=ch; ch=s[++p]; } } } if(syn==-1 || (ch>='a' && ch<='z') || ch=='.') { syn=-1;//对数字开头的标识符进行判错。 while(ch>='0' && ch<='9' || ch>='a' && ch<='z' || ch=='.') ch=s[++p];// 找到下一次要判断的开头 ch=s[--p]; } else { ch=s[--p]; syn=12; if(!positive) dsum*=-1.0; } } else { if(ch>='a' && ch<='z') { syn=-1;//对数字开头的标识符进行判错。 while(ch>='0' && ch<='9' || ch>='a' && ch<='z') ch=s[++p];//找到下一次要判断的开头 ch=s[--p]; } else { ch=s[--p]; syn=11; if(!positive) sum*=-1; } } } bool check_behind_digit() { int i=p+1; while(s[i]!='\0') { if(s[i]>='0' && s[i]<='9') return true; else if(s[i]!=' ' && s[i]!='\t' && s[i]!='\n') return false; i++; } return false; } boo...