词法分析是指将我们编写的文本代码流解析为一个一个的记号,分析得到的记号以供后续语法分析使用。词法分析的工作是低级别的分析:将字符或者字符序列转化成记号.
#include<stdio.h> #include<string.h> #define t 10 #define num2 11 void analyse(FILE *fp,char ch); int main() { FILE *fp; char ch; if((fp=fopen("demo.txt","w"))==NULL) { printf("Fileure to open demo.txt!\n"); } printf("请输入源程序(以$结束):"); ch=getchar(); while(ch!='$') { fputc(ch,fp); ch=getchar(); } fclose(fp); if((fp=fopen("demo.txt","r"))==NULL) { printf("Fileure to open demo.txt!\n"); } ch=fgetc(fp); analyse(fp,ch); } void analyse(FILE *fp,char ch) { int m,j,i=0,n,flag=0; FILE *fp1,*fp2; char s; char gjz[34][20]={"short","if","do","while","end","auto","double","int","struct","break"," "," ","else","long","switch","case","enum","register","typedef","char","extern","union","const","float","unsigned","continue","signed","for","default", "void","goto","sizeof","volatile","static"}; char num[5]; char bsf[10]; char alp[15]; while(!feof(fp)) { m=0,j=0; i=0; if(ch=='+') printf("%c\t%d\n",ch,35); else if(ch=='-') printf("%c\t%d\n",ch,36); else if(ch=='*') printf("%c\t%d\n",ch,37); else if(ch=='/') printf("%c\t%d\n",ch,38); else if(ch=='=') printf("%c\t%d\n",ch,39); else if(ch=='#') printf("%c\t%d\n",ch,40); else if(ch=='>') { s=fgetc(fp); if(s=='=') printf("%c=\t%d\n",ch,43); else { printf("%c\t%d\n",ch,41); analyse(fp,s); } } else if(ch=='<') { s=fgetc(fp); if(s=='=') printf("%c=\t%d\n",ch,44); else { printf("%c\t%d\n",ch,42); analyse(fp,s); } } else if(ch=='.') printf("%c\t%d\n",ch,53); else if(ch==':') { s=fgetc(fp); if(s=='=') printf("%c=\t%d\n",ch,45); else { printf("%c\t%d\n",ch,46); analyse(fp,s); } } else if(ch=='(') printf("%c\t%d\n",ch,47); else if(ch==')') printf("%c\t%d\n",ch,48); else if(ch==';') printf("%c\t%d\n",ch,54); else if(ch==',') printf("%c\t%d\n",ch,49); else if(ch=='{') printf("%c\t%d\n",ch,50); else if(ch=='}') printf("%c\t%d\n",ch,51); else if(ch=='"') { printf("%c\t%d\n",ch,52); } else if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z') { if((fp1=fopen("data.txt","w"))==NULL) { printf("Fileure to open data.txt!\n"); } do { fputc(ch,fp1); i++; ch=fgetc(fp); }while(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z'||ch>='0'&&ch<='9'); fclose(fp1); if((fp1=fopen("data.txt","r"))==NULL) { printf("Fileure to open data.txt!\n"); } fgets(alp,i+1,fp1); for(j=0;j<34;j++) { if(j==10) j=j+2; if(strcmp(alp,gjz[j])==0) { printf("%s\t%d\n",gjz[j],j); flag=1; break; } } if(flag==0) printf("%s\t%d\n",alp,t); fclose(fp1); analyse(fp,ch); } else if(ch>='0'&&ch<='9') { if((fp2=fopen("data2.txt","w"))==NULL) { printf("Fileure to open data2.txt!\n"); } do { fputc(ch,fp2); m++; ch=fgetc(fp); } while(ch>='0'&&ch<='9'); fclose(fp2); if((fp2=fopen("data2.txt","r"))==NULL) { printf("Fileure to open data2.txt!\n"); } fgets(num,m+1,fp2); printf("%s\t%d\n",num,num2); fclose(fp2); analyse(fp,ch); } else printf("\r"); ch=fgetc(fp); } }
<字母>:S-〉A|AA
A->{a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,w,y,
A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,S,Y,Z}
<数字>:S->A|AA|A0|A.
A->{1,2,3,4,5,6,7,8,9}
<整数常数> :S->A|AA|A0
A->{1,2,3,4,5,6,7,8,9}
<标识符>:S->A|A_|AB
A->{a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,w,y,
A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,S,Y,Z}
B->{0,1,2,3,4,5,6,7,8,9}
<关键字>:S->{
short","if","do","while","end","auto","double","int","struct","break","else","long","switch","case","enum","register","typedef","char","extern","union","const","float","unsigned","continue","signed","for","default","void","goto","sizeof","volatile","static"}
<运算符>:S->{+,-,*,/,>,<,==,<=,>=}
<界符>; S->{; , ( , ) , ,{,},'' }