下载此文档

编译原理C语言词法分析器.doc


文档分类:IT计算机 | 页数:约38页 举报非法文档有奖
1/38
下载提示
  • 1.该资料是网友上传的,本站提供全文预览,预览什么样,下载就什么样。
  • 2.下载该文档所得收入归上传者、原创者。
  • 3.下载的文档,不会出现我们的网址水印。
1/38 下载此文档
文档列表 文档介绍
该【编译原理C语言词法分析器 】是由【花双韵芝】上传分享,文档一共【38】页,该文档可以免费在线阅读,需要了解更多关于【编译原理C语言词法分析器 】的内容,可以使用淘豆网的站内搜索功能,选择自己适合的文档,以下文字是截取该文章内的部分文字,如需要获得完整电子版,请下载此文档到您的设备,方便您编辑和打印。 可编辑可修改编译原理 C 语言词法分析器一、实验题目编制并调试 C词法分析程序。 源代码:?main(){intsum=0,it=1;/*Variabledeclaration*/if(sum==1)it++;elseit=it+2;}?设计其词法分析程序 ,能识别出所有的关键字、标识符、常数、运算符(包括复合运算符,如 ++)、界符;能过滤掉源程序中的注释、空格、制表符、换行符;并且能够对一些词法规则的错误进行必要的处理,如:标识符只能由字母、数字和下划线组成,且第一个字符必须为字母或下划线。实验要求 :要给出所分析语言的词法说明,相应的状态转换图,单词的种别编码方案,词法分析程序的主要算法思想等。二、实验目的、理解词法分析在编译程序中的作用;、掌握词法分析程序的实现方法和技术;、加深对有穷自动机模型的理解。三、主要函数voidload() voidchar_search(char*word)voidmain() voidintb_search(char*word) 可编辑可修改voidscan() voidc_search(char*word)voidinta_search(char*word) _search(char*word)四、 voidmain()绘制程序界面调用初始化函数: voidload() 实现文件的建调用主扫描函数: voidscan() 实现文件的扫分析完成后指引用户查看相关文件,直到用户输入退出命令函数结束2. 初始化函数 voidload()通过文件指针建立相关文件函数结束3. 保留字及标识符判断函数 voidchar_search(char*word)接收数据是否为保留字是:写入输出文件 否:查标识符表找到:写入输出文件 没找到: 可编辑可修改写入输出文件4. 整数类型判断函数 voidinta_search(char*word)接收数据查表找到:写入输出文件没找到:写入整数常量表文件写入输出文件5. 浮点类型判断函数 voidintb_search(char*word)接收数据查表找到:写入输出文件 没找到:写入整数常量表文件写入输出文件_search(char*word)字符常量判断函数voidc_search(char*word)同4、 voidscan()函数开始33读入源文件中的一个单词判断宏定义无效字符字符串字符常量字符串常量整数常量浮点数常量注释限制符 可编辑可修改忽略忽略调用保留字及标识符函数 voidchar_search 进行处理调用字符常量判断函数 voidc_search 进行处理调用字符串常量判断函数 _search 进行处理调用整数类型判断函数 voidinta_search 进行处理调用浮点类型判断函数 voidintb_search 进行处理写入注释文件查运算符、分隔符表并写入输出文件五、关键代码#include<>#include<> 可编辑可修改#include<>char *key0[]={"","auto","break","case","char","const","continue","default","do","double","else","enum","extern","float","for","goto","if","int","long","register","return","short","signed","sizeof","static","struct","switch","typedef","_Complex","_Imaginary","union","unsigned","void","volatile","while"};/*保留字表*/char*key1[]={"","(",")","[","]","{","}",",",";","'"};/*分隔符表*/char *key2[]={"","+","-","*","/","%","<",">","==",">=","<=","!=","!","&&","||","<<",">>","~","|","^","&","=",":","->","++","--",".","+=","-=","*=","/="};/*运算符表*/intxx0[35],xx1[10],xx2[31];inttemp_key3=0,temp_c40=0,temp_c41=0,temp_c42=0,temp_c43=0;/******* 初始化函数 *******/voidload(){intmm;for(mm=0;mm<=34;mm++){xx0[mm]=0;}for(mm=0;mm<=9;mm++){xx1[mm]=0;} 可编辑可修改for(mm=0;mm<=30;mm++){xx2[mm]=0;}FILE*floading;if((floading=fopen("","w"))==NULL){printf("Error!Can'tcreatefile:");return;}fclose(floading);/*建立保留字表文件: */if((floading=fopen("","w"))==NULL){printf("Error!Can'tcreatefile:");return;}/*建立分隔符表文件: */if((floading=fopen("","w"))==NULL){printf("Error!Can'tcreatefile:");return;}fclose(floading);/*建立运算符表文件: */if((floading=fopen("","w"))==NULL){printf("Error!Can'tcreatefile:"); 可编辑可修改return;}fclose(floading);/*建立标识符表文件: */if((floading=fopen("","w"))==NULL){printf("Error!Can'tcreatefile:");return;}fclose(floading);/*建立整数类型常量表文件: */if((floading=fopen("","w"))==NULL){printf("Error!Can'tcreatefile:");return;}fclose(floading);/*建立浮点类型常量表文件: */if((floading=fopen("","w"))==NULL){printf("Error!Can'tcreatefile:");return;}fclose(floading);/*建立字符类型常量表文件: */if((floading=fopen("","w"))==NULL){printf("Error!Can'tcreatefile:"); 可编辑可修改return;}fclose(floading);/*建立字符串类型常量表文件: */if((floading=fopen("","w"))==NULL){printf("Error!Can'tcreatefile:");return;}fclose(floading);/*建立注释文件: */if((floading=fopen("","w"))==NULL){printf("Error!Can'tcreatefile:");return;}fclose(floading);/*建立内部码文件: */if((floading=fopen("temp_key1","w"))==NULL){printf("Error!Can'tcreatefile:temp_key1");return;}fclose(floading);/*建立保留字临时表文件: temp_key1*/if((floading=fopen("temp_key3","w"))==NULL){printf("Error!Can'tcreatefile:temp_key3"); 可编辑可修改return;}fclose(floading);/*建立标识符临时文件: temp_key3*/if((floading=fopen("temp_c40","w"))==NULL){printf("Error!Can'tcreatefile:temp_c40");return;}fclose(floading);/*建立整数类型常量临时文件: temp_c40*/if((floading=fopen("temp_c41","w"))==NULL){printf("Error!Can'tcreatefile:temp_c41");return;}fclose(floading);/*建立浮点类型常量临时文件: temp_c41*/if((floading=fopen("temp_c42","w"))==NULL){printf("Error!Can'tcreatefile:temp_c42");return;}fclose(floading);/*建立字符类型常量临时文件: temp_c42*/if((floading=fopen("temp_c43","w"))==NULL){printf("Error!Can'tcreatefile:temp_c43"); 可编辑可修改return;}fclose(floading);/*建立字符串类型常量临时文件: temp_c43*/}/******* 保留字及标识符判断函数 *******/voidchar_search(char*word){intm,line=0,csi=0;intvalue=0;intvalue2=0;charc,cs[100];FILE*foutput,*finput;for(m=1;m<=34;m++){if(strcmp(word,key0[m])==0){value=1;break;}}if(value==1){if(xx0[m]==0){foutput=fopen("","a");fprintf(foutput,"0\t%d\t\t%s\n",m,word);1010

编译原理C语言词法分析器 来自淘豆网www.taodocs.com转载请标明出处.

相关文档 更多>>
非法内容举报中心
文档信息
  • 页数38
  • 收藏数0 收藏
  • 顶次数0
  • 上传人花双韵芝
  • 文件大小1.22 MB
  • 时间2024-03-25
最近更新