词法分析器的构造-南邮2016-编译原理实验一报告(共13页).doc
精选优质文档-倾情为你奉上实 验 报 告(2015 / 2016 学年 第 二 学期)课程名称编译原理实验名称词法分析器的构造实验时间2016年4月29日指导单位计算机软件教学中心指导教师学生姓名wujun班级学号学院(系)计算机学院、软件学院专 业计算机科学与技术专心-专注-专业实 验 报 告实验名称词法分析器的构造指导教师实验类型设计实验学时4实验时间2016.4.29一、 实验目的和要求1、实验目的:设计、编制、调试一个词法分析程序,对单词进行识别和编码,加深对词法分析原理的理解。2、实验要求:1)、允许用户自己输入源程序并保存为文件;2)、系统能够输出经过预处理后的源程序(去掉注释、换行、空格等);3)、能够将该源程序中所有的单词根据其所属类型(整数、保留字、运算符、标识符等。定义的类C语言中的标识符只能以字母或下划线开头)进行归类显示,例如:识别保留字:if、int、for、while、do、return、break、continue等,其他的都识别为标识符;常数为无符号整形数;运算符包括:+、-、*、/、=、>、<、>=、<=、!=等;分隔符包括:,、;、(、)等;4)、实现文件的读取操作,而不是将文本以字符串形式预存于程序中。文本内容为待分析的类C语言程序。二、实验环境(实验设备) 硬件:计算机 软件:Visual C +6.0二、 实验原理及内容 1、实验内容:设计并实现一个词法分析器,实现对指定位置的类C语言源程序文本文件的读取,并能够对该源程序中的所有单词进行分类,指出其所属类型,实现简单的词法分析操作。例如下面为一段C语言源程序: main() int a,b; a = 10; b = a + 20; 要求输出如下: (2,main) (5,() (5,)(5,) (5,)(5,)(5, )(1,int)(2,a)(5,,)(2,b)(5,;)(2,a)(4,=)(3,10)(5,;)(2,b)(4,=)(2,a)(4,+)(3,20)(5,;)(5,) 2、实验原理状态转换图 3、 实验代码:实验代码:#include <string.h> #include <iostream.h> #include <fstream> #include<stdlib.h> #include<stdio.h>#include<ctype.h>struct Char /创建一个结构用于存贮关键字char a15;typedef struct Char CH;/定义关键字CH keyWord67="auto","break","case","cout","cin","char","const","continue","default", "do","double","else","enum","endl","extern","float","for","goto","if","main", "include","int","long","register","return","short","signed","sizeof","static", "string","struct","switch","typedef","union","unsigned","void","stdio","while", "cin","cout","catch","calss","ctype","stdlib","fstream","export","iostream", "false","friend","inline","mutable","namespace","new","operator","private", "protected","public","static_cast","template","this","throw","true","try", "typename","using","virtual","asm"/检测是否为数字,是返回true,否则返回falsebool IsDigit(char A) if(A>='0' && A<='9')return true;elsereturn false; /检测是否为字符,是则返回true,否则返回falsebool IsLetter(char ch) if(ch>='A'&&ch<='Z') | (ch>='a'&&ch<='z')return true;return false;/检测是否为分隔符,是则返回true,否则返回falsebool IsSeparator(char ch) if(ch ='('|ch=')'|ch =''|ch =''|ch=''|ch=','|ch='.'|ch=':'|ch='''|ch='"'|ch=''|ch=''|ch='#'|ch='_')return true;return false;/检测是否为关键字,是则返回true,否则返回falsebool IsKeyWord(char *a) int i = 0;for(i; i < 67; i+)if (strcmp(a,keyWordi.a) = 0)return true;return false;/预处理去掉注释、换行、空格等void Scanner(char s,char a)int i = 0,j = 0,k = strlen(s);for (i; i < k; i +)if (si = '/' && si+1 ='/')doi+;while(si != 'n');i-;else if (si = '/' && si+1 = '*')doi+;while(si != '/' | si-1 != '*');continue;else if (si = 't' | si = 'n' | si = '?')aj+ = ' 'else aj+ = si;cout<<"n系统经过预处理后的输出(去掉注释和换行):"<<endl;cout<<a<<"nn"cout<<"系统经过预处理后的输出(去掉注释、换行、空格等):"<<endl;for (i = 0;i < strlen(a); i+)if(ai = ' ')continue;elsecout<<ai;cout<<endl<<endl<<endl;void Handle(char s) /词法分析char ch;for (int j = 0; j < strlen(s); j+)char word20 = '0'int i = 0;ch = sj;if (ch = ' ')continue;else if (IsLetter(ch)dowordi+ = ch;ch = s+j;while(IsLetter(ch)| IsDigit(ch)|ch='_');j-;wordi = '0'if(IsKeyWord(word)cout<<"(1,'"<<word<<"')"<<endl;elsecout<<"(2,'"<<word<<"')"<<endl;else if(IsDigit(ch)dowordi+ = ch;ch = s+j;while(IsDigit(ch);j-;wordi = '0'cout<<"(3,'"<<word<<"')"<<endl;else if(IsSeparator(ch)word0 = ch;cout<<"(5,'"<<word<<"')"<<endl;elseword0 = ch;if(word0='+'|word0='-'|word0='>'|word0='<'|word0='&'|word0='|')if(sj+1=word0 | sj+1='=')word1 = s+j;cout<<"(4,'"<<word<<"')"<<endl;elsecout<<"(4,'"<<word<<"')"<<endl;else if(word0='='|word0='*'|word0='/'|word0='!'|word0='%'|word0='')if (sj+1 = '=')word1 = s+j;cout<<"(4,'"<<word<<"')"<<endl;else cout<<"(4,'"<<word<<"')"<<endl;else if (word0 = '')if (sj+1 = 'n' |sj+1 = 't' |sj+1=''|sj+1='0')word1 = s+j;cout<<"(4,'"<<word<<"')"<<endl;else cout<<"(4,'"<<word<<"')"<<endl;elsecout<<"(无法识别字符,'"<<word<<"')"<<endl;cout<<"n"int main()int i;char b = 32,ch;FILE *fp;doi = 0; char s10000;char a10000 = '0'fp=fopen("code.txt","rb+");if(fp=NULL)cout<<"Can not create file"<<endl;exit(0);fseek(fp,0,2);cout<<"Please input you code(end with two'?'):"<<endl;doch = getchar();si+ = ch;if(ch = 'n' | ch= 't' )for (int j =0; j < 4;j+)fputc(b,fp);fputc(ch,fp);while(si-1 != '?' | si-2 != '?');si = '0'Scanner(s,a);cout<<"词法分析如下:"<<endl;Handle(a);fclose(fp);while(true);return 0;4、 实验测试和截图(1) 、测试一截图输入代码: 输出结果: (2)测试二截图输入代码: 输出结果: 四、实验小结(包括问题和解决方法、心得体会、意见与建议等) 在本实验中,我进一步学习了如何运用输入输出流,对文件进行读写操作。了解了什么是词法分析,熟悉了词法分析器的构造,更加深入了对词法分析原理的理解。程序将字符分为五类:保留字、标识符、整数、运算符、分隔符,对使用者输入的程序代码进行识别、分析和编码,简单分析了词法。实验过程中开始时由于对换行符(n)、制表符(t)、和注释等处理不好导致实验失败,后来预处理时讲它们全部换成空格符和把注释去掉才成功。五、指导教师评语成 绩批阅人日 期