词法分析:
     
    #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);
}
}