文档介绍:该【词法分析器的构造-南邮2024-编译原理实验一报告 】是由【小果冻】上传分享,文档一共【13】页,该文档可以免费在线阅读,需要了解更多关于【词法分析器的构造-南邮2024-编译原理实验一报告 】的内容,可以使用淘豆网的站内搜索功能,选择自己适合的文档,以下文字是截取该文章内的部分文字,如需要获得完整电子版,请下载此文档到您的设备,方便您编辑和打印。实验报告〔2024/2024学年第二学期〕课程名称编译原理实验名称词法分析器的构造实验时间2024年4月29日指导单位计算机软件教学中心指导教师学生姓名wujun班级学号学院(系)计算机学院、软件学院专业计算机科学与技术-12--1-、实验目的:设计、编制、调试一个词法分析程序,对单词进行识别和编码,加深对词法分析原理的理解。2、实验要求:1)、允许用户自己输入源程序并保存为文件;2)、系统能够输出经过预处理后的源程序〔去掉注释、换行、空格等〕;3)、能够将该源程序中所有的单词根据其所属类型〔整数、保存字、运算符、标识符等。定义的类C语言中的标识符只能以字母或下划线开头〕进行归类显示,例如:识别保存字:if、int、for、while、do、return、break、continue等,其他的都识别为标识符;常数为无符号整形数;运算符包括:+、-、*、/、=、>、<、>=、<=、!=等;分隔符包括:,、;、{、}、(、)等;4)、实现文件的读取操作,而不是将文本以字符串形式预存于程序中。文本内容为待分析的类C语言程序。二、实验环境(实验设备)硬件:计算机软件:VisualC++-2-实验原理及内容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,’,’〕-3-〔2,’b’〕〔5,’;’〕〔2,’a’〕〔4,’=’〕〔3,’10’〕〔5,’;’〕〔2,’b’〕〔4,’=’〕〔2,’a’〕〔4,’+’〕〔3,’20’〕〔5,’;’〕〔5,’}’〕2、实验原理状态转换图-4-实验代码:实验代码:#include<>#include<>#include<fstream>#include<>#include<>#include<>structChar{//创立一个结构用于存贮关键字 chara[15];};typedefstructCharCH;//定义关键字CHkeyWord[67]={"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",-5- "false","friend","inline","mutable","namespace","new","operator","private", "protected","public","static_cast","template","this","throw","true","try", "typename","using","virtual","asm"};//检测是否为数字,是返回true,否那么返回falseboolIsDigit(charA){ if(A>='0'&&A<='9') returntrue; else returnfalse;}//检测是否为字符,是那么返回true,否那么返回falseboolIsLetter(charch){ if((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='z')) returntrue; returnfalse;}//检测是否为分隔符,是那么返回true,否那么返回falseboolIsSeparator(charch){ if(ch=='('||ch==')'||ch=='{'||ch=='}'||ch==';'||ch==','||ch=='.'|| ch==':'||ch=='\''||ch=='\"'||ch=='['||ch==']'||ch=='#'||ch=='_') returntrue; returnfalse;}//检测是否为关键字,是那么返回true,否那么返回falseboolIsKeyWord(char*a){ inti=0; for(i;i<67;i++) if(strcmp(a,keyWord[i].a)==0){ returntrue; } returnfalse;}//预处理去掉注释、换行、空格等voidScanner(chars[],chara[]){ inti=0,j=0,k=strlen(s); for(i;i<k;i++){ if(s[i]=='/'&&s[i+1]=='/'){ do{ i++;-6- }while(s[i]!='\n'); i--; } elseif(s[i]=='/'&&s[i+1]=='*'){ do{ i++; }while(s[i]!='/'||s[i-1]!='*'); continue; } elseif(s[i]=='\t'||s[i]=='\n'||s[i]=='?') a[j++]=''; else a[j++]=s[i]; } cout<<"\n系统经过预处理后的输出〔去掉注释和换行〕:"<<endl; cout<<a<<"\n\n"; cout<<"系统经过预处理后的输出〔去掉注释、换行、空格等〕:"<<endl; for(i=0;i<strlen(a);i++){ if(a[i]=='') continue; else cout<<a[i]; } cout<<endl<<endl<<endl;}voidHandle(chars[]){//词法分析 charch; for(intj=0;j<strlen(s);j++){ charword[20]={'\0'}; inti=0; ch=s[j]; if(ch=='') continue; elseif(IsLetter(ch)){ do{ word[i++]=ch; ch=s[++j]; }while(IsLetter(ch)||IsDigit(ch)||ch=='_'); j--; word[i]='\0'; if(IsKeyWord(word)) cout<<"(1,'"<<word<<"')"<<endl; else-7- cout<<"(2,'"<<word<<"')"<<endl; } elseif(IsDigit(ch)){ do{ word[i++]=ch; ch=s[++j]; }while(IsDigit(ch)); j--; word[i]='\0'; cout<<"(3,'"<<word<<"')"<<endl; } elseif(IsSeparator(ch)){ word[0]=ch; cout<<"(5,'"<<word<<"')"<<endl; } else{ word[0]=ch; if(word[0]=='+'||word[0]=='-'||word[0]=='>'||word[0]=='<'|| word[0]=='&'||word[0]=='|'){ if(s[j+1]==word[0]||s[j+1]=='='){ word[1]=s[++j]; cout<<"(4,'"<<word<<"')"<<endl; } else cout<<"(4,'"<<word<<"')"<<endl; } elseif(word[0]=='='||word[0]=='*'||word[0]=='/'|| word[0]=='!'||word[0]=='%'||word[0]=='^'){ if(s[j+1]=='='){ word[1]=s[++j]; cout<<"(4,'"<<word<<"')"<<endl; } else cout<<"(4,'"<<word<<"')"<<endl; } elseif(word[0]=='\\'){ if(s[j+1]=='n'||s[j+1]=='t'|| s[j+1]=='\\'||s[j+1]=='0'){ word[1]=s[++j]; cout<<"(4,'"<<word<<"')"<<endl; } else cout<<"(4,'"<<word<<"')"<<endl; }-8- else cout<<"(无法识别字符,'"<<word<<"')"<<endl; } } cout<<"\n";}intmain(){ inti; charb=32,ch; FILE*fp; do{ i=0; chars[10000]; chara[10000]={'\0'}; fp=fopen("","rb+"); if(fp==NULL){ cout<<"Cannotcreatefile"<<endl; exit(0); } fseek(fp,0,2); cout<<"Pleaseinputyoucode(endwithtwo'?'):"<<endl; do{ ch=getchar(); s[i++]=ch; if(ch=='\n'||ch=='\t') for(intj=0;j<4;j++) fputc(b,fp); fputc(ch,fp); }while(s[i-1]!='?'||s[i-2]!='?'); s[i]='\0'; Scanner(s,a); cout<<"词法分析如下:"<<endl; Handle(a); fclose(fp); }while(true); return0;}实验测试和截图-10-、测试一截图输入代码:输出结果: