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

上传人:枫** 文档编号:497552276 上传时间:2022-07-23 格式:DOC 页数:22 大小:86.50KB
返回 下载 相关 举报
编译原理C语言词法分析器_第1页
第1页 / 共22页
编译原理C语言词法分析器_第2页
第2页 / 共22页
编译原理C语言词法分析器_第3页
第3页 / 共22页
编译原理C语言词法分析器_第4页
第4页 / 共22页
编译原理C语言词法分析器_第5页
第5页 / 共22页
点击查看更多>>
资源描述

《编译原理C语言词法分析器》由会员分享,可在线阅读,更多相关《编译原理C语言词法分析器(22页珍藏版)》请在金锄头文库上搜索。

1、-编译原理 C语言词法分析器一、实验题目编制并调试C词法分析程序。a. t*t源代码:main() int sum=0 ,it=1;/* Variable declaration*/ if (sum=1) it+; else it=it+2; 设计其词法分析程序,能识别出所有的关键字、标识符、常数、运算符包括复合运算符,如+、界符;能过滤掉源程序中的注释、空格、制表符、换行符;并且能够对一些词法规则的错误进展必要的处理,如:标识符只能由字母、数字和下划线组成,且第一个字符必须为字母或下划线。实验要求:要给出所分析语言的词法说明,相应的状态转换图,单词的种别编码方案,词法分析程序的主要算法思想等

2、。 二、实验目的 1、理解词法分析在编译程序中的作用; 2、掌握词法分析程序的实现方法和技术; 3、加深对有穷自动机模型的理解。 三、主要函数void load ( ) void char_search(char *word)void main ( )void intb_search(char *word)void scan ( )void c_search(char *word)void inta_search(char *word)void cc_search(char *word)四、设计1.主函数 void main ( )绘制程序界面调用初始化函数:void load ( ) 实现文

3、件的建立调用主扫描函数:void scan ( ) 实现文件的扫描分析完成后指引用户查看相关文件,直到用户输入退出命令函数完毕2. 初始化函数 void load ( )通过文件指针建立相关文件函数完毕3. 保存字及标识符判断函数 void char_search(char *word)接收数据是否为保存字否:查标识符表是:写入输出文件没找到:写入表文件找到:写入输出文件写入输出文件4. 整数类型判断函数 void inta_search(char *word)接收数据找到:写入输出文件写入输出文件查表没找到:写入整数常量表文件5. 浮点类型判断函数 void intb_search(char

4、 *word)没找到:写入整数常量表文件找到:写入输出文件查表接收数据写入输出文件6. 字符串常量判断函数 void cc_search(char *word)7. 字符常量判断函数 void c_search(char *word)同4、5函数图8.主扫描函数 void scan ( )函数开场读入源文件中的一个单词判断宏定义忽略无效字符忽略调用保存字及标识符函数void char_search进展处理字符串字符常量调用字符常量判断函数void c_search进展处理调用字符串常量判断函数void cc_search进展处理字符串常量整数常量调用整数类型判断函数void inta_sear

5、ch进展处理调用浮点类型判断函数void intb_search进展处理浮点数常量注释写入注释文件查运算符、分隔符表并写入输出文件限制符五、关键代码#include #include #include char *key0= ,auto,break,case,char,const,continue,default,do,double,else,enum,e*tern,float,for,goto,if,int,long,register,return,short,signed,sizeof,static,struct,switch,typedef,_ple*,_Imaginary,union,

6、unsigned,void,volatile,while;/*保存字表*/char *key1= ,(,),;,;/*分隔符表*/char *key2= ,+,-,*,/,%,=,=,=,!=,!,&,|,|,&,=,:,-,+,-,.,+=,-=,*=,/=;/*运算符表*/int *035,*110,*231;int temp_key3=0,temp_c40=0,temp_c41=0,temp_c42=0,temp_c43=0;/* 初始化函数 */void load()int mm;for (mm=0;mm=34;mm+)*0mm=0;for (mm=0;mm=9;mm+)*1mm=0;

7、for (mm=0;mm=30;mm+)*2mm=0;FILE *floading;if (floading=fopen(key0.t*t,w)=NULL)printf(Error! Cant create file : key0.t*t);return;fclose (floading);/*建立保存字表文件:key0.t*t*/if (floading=fopen(key1.t*t,w)=NULL)printf(Error! Cant create file : key1.t*t);return;/*建立分隔符表文件:key1.t*t*/if (floading=fopen(key2.t*

8、t,w)=NULL)printf(Error! Cant create file : key2.t*t);return;fclose(floading);/*建立运算符表文件:key2.t*t*/if (floading=fopen(key3.t*t,w)=NULL)printf(Error! Cant create file : key3.t*t);return;fclose (floading);/*建立标识符表文件:key3.t*t*/if (floading=fopen(c40.t*t,w)=NULL)printf(Error! Cant create file : c40.t*t);

9、return;fclose (floading);/*建立整数类型常量表文件:c40.t*t*/if (floading=fopen(c41.t*t,w)=NULL)printf(Error! Cant create file : c41.t*t);return;fclose (floading);/*建立浮点类型常量表文件:c41.t*t*/if (floading=fopen(c42.t*t,w)=NULL)printf(Error! Cant create file : c42.t*t);return;fclose (floading);/*建立字符类型常量表文件:c42.t*t*/if

10、 (floading=fopen(c43.t*t,w)=NULL)printf(Error! Cant create file : c43.t*t);return;fclose (floading);/*建立字符串类型常量表文件:c43.t*t*/ if (floading=fopen(defination.t*t,w)=NULL)printf(Error! Cant create file : defination.t*t);return;fclose (floading);/*建立注释文件:defination.t*t*/if (floading=fopen(output.t*t,w)=N

11、ULL)printf(Error! Cant create file : output.t*t);return;fclose (floading);/*建立部码文件:output.t*t*/if (floading=fopen(temp_key1,w)=NULL)printf(Error! Cant create file : temp_key1);return;fclose (floading);/*建立保存字临时表文件:temp_key1*/if (floading=fopen(temp_key3,w)=NULL)printf(Error! Cant create file : temp_

12、key3);return;fclose (floading);/*建立标识符临时文件:temp_key3*/if (floading=fopen(temp_c40,w)=NULL)printf(Error! Cant create file : temp_c40);return;fclose (floading);/*建立整数类型常量临时文件:temp_c40*/if (floading=fopen(temp_c41,w)=NULL)printf(Error! Cant create file : temp_c41);return;fclose (floading);/*建立浮点类型常量临时文件:temp_c41*/if (floading=fopen(temp_c42,w)=NULL)printf(Error! Cant create file : temp_c42);return;fclose (floading);/*建立字符类型常量临时文件:temp_c42*/if (floading=fopen(temp_c43,w)=NULL)printf(Error! Cant create file : temp_c43);retu

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 资格认证/考试 > 自考

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号