• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

学习编译原理时做的词法分析器

发布: 2007-7-01 20:40 | 作者: admin | 来源: | 查看: 55次 | 进入软件测试论坛讨论

领测软件测试网

主要的代码如下(这里给出的是命令行代码,稍加修改就可以做成上面的界面了),具体请参考源文件:

MILY: Fixedsys" borderColor=#008080 cellSpacing=0 cellPadding=0 width="100%" align=center bgColor=#f6f6f6 border=0>

 
#include
#include
#include
#include

#define  MAX 18   //分析表的最大容量
#define  MAXBUF 255
char  ch =@# @#;   // 存放读入当前的输入字符
int lineno;

struct reserve   //关键字
{
 char lexptr[MAXBUF];
 int token;
};

struct reserve symtable[MAX];
char * str[]={"program","input","output","begin","end","var","integer","real","for","to","if","then","else","do","while","write","array","proceure" };


void init()     //对符号表进行初始化
{
 for( int j=0; j<18; j++)
 {
  strcpy(symtable[j].lexptr,str[j]);
  symtable[j].token=j+3;
 }
}
 

int search(char *temp)
{
 for(int i=0; i {
  if(!strcmp(symtable[i].lexptr ,temp))
  {
   return  symtable[i].token;
  }
 }
 return 0;
}    

 

void analyse(FILE *fpin,FILE *fpout)    //分析程序
{
 char arr[MAXBUF];
 int i=0;
 int j=0;
 
 while((ch=fgetc(fpin))!=EOF)    //读入字符判断,空格、字母、数字、界符
 {
  if(ch==@# @#||ch==@#\t@#)
  {
   
  }
  else if(ch==@#\n@#)    //如果是换行符,则行号加1
  { 
   lineno++;
  }
  else if(isdigit(ch))   //如果是数字
  {
   while(isdigit(ch))   //判断和读取数字
   {
    arr[j]=ch;
    j++;
    ch=fgetc(fpin);
   }
   arr[j]=@#\0@#;
   j=0;
   fseek(fpin,-1L,SEEK_CUR);
   fprintf(fpout,"%s\t%d\n",arr,2) ;
  }
  else if (isalpha(ch))  //如果是字母
  {
   while(isalpha(ch)||isdigit(ch))
   {
    arr[j]=ch;
    j++;
    ch=fgetc(fpin);
   }
   fseek(fpin,-1L,SEEK_CUR);
   arr[j]=@#\0@#;
   j=0;
   if (search(arr)) //如果是关键字
   {
    fprintf(fpout,"%s\t%d\n",arr,search(arr));
   }
   else 
    fprintf(fpout,"%s\t%d\n",arr,1); //普通标志符
  }
  else if(ch==@#:@#) 
  {
   ch=fgetc(fpin);
   if(ch==@#=@#)
   {
    fprintf(fpout,"%s\t%d\n",":=",29);    //如果是 :=
   }
   else
   {
    fprintf(fpout,"%s\t%d\n",":",30);   //如果是 :
    fseek(fpin,-1L,SEEK_CUR);
   }
  }
  else if (ch==@#>@#)
  {
   ch=fgetc(fpin);
   if(ch==@#=@#)    //如果是 >=
   {
    fprintf(fpout,"%s\t%d\n",">=",32);
   }
   else
   {
    fprintf(fpout,"%s\t%d\n",">",31);  //如果是 >
    fseek(fpin,-1L,SEEK_CUR);
   }
  }
  else if(ch==@#<@#)
  {
   ch=fgetc(fpin);
   if(ch==@#>@#)
   {
    fprintf(fpout,"%s\t%d\n","<>",35);  // 如果是 <>
   }
   else if(ch==@#=@#)
   {
    fprintf(fpout,"%s\t%d\n","<=",34);   //如果是 <=
   }
   else
   {
    fprintf(fpout,"%s\t%d\n","<",33);   //如果是 <
    fseek(fpin,-1L,SEEK_CUR);
   }
  }
  else if(ch==@#/@#)
  {
   ch=fgetc(fpin);
   if(ch==@#*@#)
   {
    ch=fgetc(fpin);
s:
    while(ch!=@#*@#)
    {
     ch=fgetc(fpin);
    }
    while(ch==@#*@#)
    {
     ch=fgetc(fpin);
     while(ch!=@#/@#)
     {
      goto s;   //如果是注释 /*  */
     }
    }
   }
   else if(ch==@#/@#)
   {
    ch=fgetc(fpin);
    while(ch!=@#\n@#)
    {
     ch=fgetc(fpin);   //如果是注释 //
    }
   }
   else
   { 
    fprintf(fpout,"%s\t%d\n","/",24);
    fseek(fpin,-1L,SEEK_CUR);
   }
  }
  else if(ch==@#+@#)
  {
   fprintf(fpout,"%s\t%d\n","+",21);
  }
  else if(ch==@#-@#)
  {
   fprintf(fpout,"%s\t%d\n","-",22);
  }
  else if(ch==@#*@#)
  {
   fprintf(fpout,"%s\t%d\n","*",23);
  }
  else if(ch==@#(@#)
  {
   fprintf(fpout,"%s\t%d\n","(",25);
  }
  else if(ch==@#)@#)
  {
   fprintf(fpout,"%s\t%d\n",")",26);
  }
  else if(ch==@#[@#)
  {
   fprintf(fpout,"%s\t%d\n","[",27);
  }
  else if(ch==@#]@#)
  {
   fprintf(fpout,"%s\t%d\n","]",28);
  }
  else if(ch==@#.@#)
  {
   fprintf(fpout,"%s\t%d\n",".",39);
  }
  else if(ch==@#;@#)
  {
   fprintf(fpout,"%s\t%d\n",";",36);
  }
  else if(ch==@#=@#)
  {
   fprintf(fpout,"%s\t%d\n","=",38);
  }
  else if(ch==@#,@#)
  {
   fprintf(fpout,"%s\t%d\n",",",40);
  }
  else fprintf(fpout,"无法识别的字符 %c\n",ch)  ;
   }
}


void main()
{
 printf("***************************词法分析器*************************\n\n\n");
 printf("直接输入源代码请按1,从文件中读入源代码请按2 \n") ;
 
 
 char filenamein[10];
 char filenameout[10];
   
 printf("请输入源文件名:\n");
 scanf("%s",filenamein);
 printf("请输入保存词法分析结果的文件名:\n");
 scanf("%s",filenameout);
 FILE* fpin=fopen(filenamein,"r");
 FILE* fpout=fopen(filenameout,"w");
 init();
 analyse(fpin,fpout);
 fclose(fpin);
 fclose(fpout);
 
 
 _getch();
}


 



延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网