编译原理词法分析器实验报告 一、实验目的: 本实验的目的是通过实现词法分析器,更加深入地掌握词法分析的原理和技术。在编译原理课程中,词法分析是编译过程的第一阶段,它将源代码分解成一个个Token,以便后续的语法分析和语义分析阶段。本实验要求使用C++实现词法分析器,对输入的文本文件进行词法分析,并输出结果。 二、实验过程: 实验过程可以分为三个部分:主程序分析、词法分析过程和实验过程中的问题。 1. 主程序分析: 首先,需要读入一个文本文件,然后判断该文本文件是否成功打开和读入。如果成功打开,则进行词法分析。 2. 词法分析过程: 词法分析过程的核心是判断单词类别并产生类号。根据输入单词的第一个有效字符(有时还需读第二个字符),可以判断单词类别,如标识符、关键字、操作符、比较符等。对于标识符和常数,需要分别与标识符表和常数表中已登记的元素相比较,比较之后输出结果。 三、实验代码: 实验代码主要包括以下几个部分: 1. 包含头文件:#include <stdio.h>、#include <stdlib.h>、#include <string.h>、#include <ctype.h>、#include <malloc.h>、#include <conio.h> 2. 定义关键字数组:char *keyword[34]={"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", "printf","union","unsigned","void","volatile","while","main"} 3. 定义操作符数组:char *operatornum[6]={"+","-","*","/","++","--"} 4. 定义比较符数组:char *comparison[8]={"<","<=","=",">",">=","<>","==","!="} 5. 定义间隔符数组:char *interpunction[8]={",",";",":=",".","(",")","{","}"} 6. 定义特殊标识符数组:char *biaoshifu[6]={"%","$","^","&","_","#"} 7. 定义注释符数组:char *zhushifu[3]={"//","/*","*/"} 8. 定义逻辑运算符数组:char *luoji[3]={"&&","||","!"} 9. 实现search函数:bool search(char searchstr[],int wordtype),该函数用于判断输入单词是否在数组中。 四、实验结果: 通过实验,我们可以了解词法分析的原理和技术,掌握如何使用C++实现词法分析器,并输出结果。 五、实验中遇到的问题: 在实验过程中,我们遇到了很多问题,如不知道如何下手、不明确输入输出等。通过看PPT和书本内容,我们逐渐把握了诀窍,并最终实现了词法分析器。 本实验报告记录了编译原理实验的一次尝试,我们通过实现词法分析器,掌握了词法分析的原理和技术,并输出结果。
![](https://csdnimg.cn/release/download_crawler_static/3402964/bg1.jpg)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![thumb](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![avatar](https://profile-avatar.csdnimg.cn/63273b9951cb46ba80d558065353235a_lovehjornot.jpg!1)
- 粉丝: 3
- 资源: 7
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)
评论0