词法分析#include<iostream>#include<cstdio>#include<cstring>using namespace std;#define MAXN 20000int 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;}bool check_pre_prog(){int i=p-1;while(i>=0){if(s[i]!=' ' && s[i]!='\t' && s[i]!='\n'){if(s[i]>='0' && s[i]<='9' || s[i]>='a' && s[i]<='z') return false;else return true;}i--;}return true;}void pot(){while(1){ch=s[++p];if(ch=='\n')row++;if(!(ch==' ' || ch=='\t' || ch=='\n'))break;}}void scaner(){memset(token,0,sizeof(token));pot();//cout<<p<<' '<<ch<<endl;if(is_letter(ch)){syn=10;identifier();else if(is_digtial(ch)){syn=11;digit(true);}else{if(ch=='<'){len=0,token[len++]=ch;if(s[p+1]=='='){syn=22;token[len++]=s[p+1];p++;}else syn=20;}else if(ch=='>'){len=0,token[len++]=ch;if(s[p+1]=='='){syn=24;token[len++]=s[p+1];p++;}else syn=23;}else if(ch=='='){len=0,token[len++]=ch;if(s[p+1]=='='){syn=25;token[len++]=s[p+1];p++;}else syn=18;}else if(ch=='!'){len=0,token[len++]=ch;if(s[p+1]=='='){syn=22;token[len++]=s[p+1];p++;}else syn=-1;}else if(ch=='+'){if(check_behind_digit() && check_pre_prog()) {++p;pot();digit(true);syn=12;}else{syn=13;token[0]=ch;}}else if(ch=='-'){if(check_behind_digit() && check_pre_prog()) {++p;pot();digit(false);syn=12;}else{syn=13;token[0]=ch;}}else if(ch=='*') syn=15,strcpy(token,"*");else if(ch=='/') syn=16,strcpy(token,"/");else if(ch==';') syn=26,strcpy(token,";");else if(ch=='(') syn=27,strcpy(token,"(");else if(ch==')') syn=28,strcpy(token,")");else if(ch=='#') syn=0,strcpy(token,"#");}//cout<<token<<' '<<sum<<' '<<syn<<endl;}int main(){int i,j;p=0;row=1;cout<<"请选择输入方式,按0表示键盘输入,按1表示文件输入"<<endl;scanf("%d",&i);if(i) freopen("1.txt","r",stdin);else printf("\nPlease input the string:\n");do{scanf("%c",&ch);s[++p]=ch;}while(ch!='#');s[0]='#';cout<<s<<endl;p=0;printf("种别码单词符号\n");do{scaner();switch(syn){case 11:printf("(%-3d ,%d)\n",syn,sum);break;case 12:{printf("\n(%d,%lf",syn,dsum);if(len){for(i=0;i<len;i++) printf("%c",index[i]);len=0;}printf(")\n");break;//实数输出}case -1:printf("(第%d行,error!)\n",row);break;default:printf("(%-3d ,%s)\n",syn,token);}}while(syn!=0);//printf("词法分析成功!请按任意键结束\n");return 0;}/*functiona=9;x=2*4;b=a+x;endfunc#x=a+b*c;endfunc#functionx=a+b*c;#functiona=(a+b)/c;x=(x+y)*(a+b)+c+d*(a+c)+(a+f)+(d-g);endfunc#functiona=(a+b)/c;x=(x+y)*(a+b)+c+d*(a+c)+(a+f)+(d-g);b=(a*c+4)*9;endfunc#*/语法分析#include<iostream>#include<cstdio>#include<cstring>#include<cmath>using namespace std;#define MAXN 20000int syn,p,sum,kk,m,n,row,error;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;syn=12;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')){//cout<<"Looooooooooook"<<endl;syn=-1;}else{while(ch>='0' && ch<='9'){index[len++]=ch;ch=s[++p];}}}if(syn==-1 || (ch>='a' && ch<='z') || ch=='.'){syn=-1;//对数字开头的标识符进行判错。