资源描述:
《编译原理_词法分析器_c语言版_简单易懂》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、#include#includecharch,token[8],prog[80];char*a[6]={"标示符","整常数","if","else","while","for"};intp,i,sum,m,n;panduan();main(){p=0;printf("pleaseinputstring:");do{scanf("%c",&ch);prog[p++]=ch;}while(ch!='#');p=0;do{panduan();switch(i){case1:printf("(%s,%
2、d,指向%s在符号表的入口)",token,i,token);break;case2:printf("(%d,%d,指向%d在符号表的入口)",sum,i,sum);break;case-1:printf("error!");default:printf("(%s,%d,-)",token,i);break;}}while(i!=0);printf("词法分析成功!");}panduan(){sum=0;for(m=0;m<8;m++)token[m++]=NULL;ch=prog[p++];m=0;while((ch=
3、='')
4、
5、(ch==''))ch=prog[p++];if(((ch<='z')&&(ch>='a'))
6、
7、((ch<='Z')&&(ch>='A'))){while(((ch<='z')&&(ch>='a'))
8、
9、((ch<='Z')&&(ch>='A'))
10、
11、((ch>='0')&&(ch<='9'))){token[m++]=ch;ch=prog[p++];}p--;i=1;//如果是字母,输出的标示类别为for(n=0;n<6;n++)if(strcmp(token,a[n])==0){i=n+1;break;}}else
12、if((ch>='0')&&(ch<='9')){while((ch>='0')&&(ch<='9')){sum=sum*10+ch-'0';ch=prog[p++];}p--;i=2;}elseswitch(ch)//以下为自己定义符号类的标示类别号{case'<':i=39;token[m++]=ch;break;case'<=':i=40;token[m++]=ch;break;case'>':i=41;token[m++]=ch;break;case'+':i=35;token[m++]=ch;break;case'-':i=3
13、6;token[m++]=ch;break;case'=':i=37;token[m++]=ch;break;case'==':i=38;token[m++]=ch;break;case'*':i=15;token[m++]=ch;break;case'(':i=55;token[m++]=ch;break;case')':i=56;token[m++]=ch;break;case';':i=57;token[m++]=ch;break;case'#':i=0;token[m++]=ch;break;default:i=-1;break
14、;}token[m++]=' ';}