您好,欢迎访问三七文档
目录实验一词法分析器的设计与实现.........................................................................................11)实验目的.....................................................................................................................12)实验内容.....................................................................................................................13)实验要求.....................................................................................................................14)实验原理.....................................................................................................................15)实验步骤.....................................................................................................................16)状态转化图及词法分析程序.....................................................................................27)测试.............................................................................................................................7实验二语法分析器的设计与实现.........................................................................................91)实验目的.....................................................................................................................92)实验内容.....................................................................................................................93)实验要求.....................................................................................................................94)实验原理.....................................................................................................................95)实验步骤.....................................................................................................................96)语法分析程序...........................................................................................................107)测试...........................................................................................................................16编译原理实验报告专业:计算机科学与技术学生姓名:学号:1307064248完成时间:2020年6月26日1实验一词法分析器的设计与实现1)实验目的①掌握正规式、状态转换图、C语言单词符号的划分及词法分析器的实现②掌握词法分析程序的作用和接口。2)实验内容设计及实现C语言程序的词法分析器。3)实验要求①对任给的一个C语言源程序,能够虑掉空格、回车换行符、tab键及注释。②识别各类单词符号,如关键字、标识符、运算符、常数、界符,结果以二元式形式输出。并构造符号表。③输出有词法错误的单词及所在行号。4)实验原理根据扫描到的单词符号的第一个字符的种类,分别转到相应的程序进行处理。这些程序的功能就是识别以相应字符开头的各类单词符号。5)实验步骤①根据C语言各类单词的正规式,构造能识别各类单词的状态转换图。②根据状态转换图,构造识别各类单词的词法分析器。26)状态转化图及词法分析程序#includectype.h#includestring.h#includestdio.hFILE*fp;intid;voidmain(){charcbuffer;charalphaprocess(charbuffer);chardigitprocess(charbuffer);charotherprocess(charbuffer);if((fp=fopen(example.c,r))==NULL)/*以只读方式打开文件example.c,NULL在stdio.h文件中已被定义为0*/printf(error);else{cbuffer=fgetc(fp);/*文件不为空则从文件中取字符*/while(cbuffer!=EOF)/*EOF文件结束标志*/{if(cbuffer==''||cbuffer=='\n')/*掠过空格和回车符*/{cbuffer=fgetc(fp);3id=4;}elseif(isalpha(cbuffer))cbuffer=alphaprocess(cbuffer);/*检查cbuffer是否为字母,是则调用alphaprocess()函数*/elseif(isdigit(cbuffer))cbuffer=digitprocess(cbuffer);/*检查cbuffer是否为数字0~9,是则调用digitprocess()函数*/elsecbuffer=otherprocess(cbuffer);/*非上述两者则调用otherprocess()函数*/}}}charalphaprocess(charbuffer){intsearch(charsearchchar[],intwordtype);/*函数声明*/intatype;inti=-1;charalphatp[20];/*字符数组存储从文件中读取的字符*/while((isalpha(buffer))||(isdigit(buffer))||buffer=='_')/*标识符的组成成分*/{alphatp[++i]=buffer;/*将当前读取的字符存如数组*/buffer=fgetc(fp);/*读取下一个字符*/}alphatp[i+1]='\0';/*字符串以'\0'作为结束标志*/atype=search(alphatp,1);/*调用函数,判断当前字符串是否为关键字*/if(atype!=0)/*是关键字则输出该关键字,编号为1,并输出该关键字在关键字表中的位子*/{printf((%s,(1,%d))\n,alphatp,atype);id=1;/*关键字的ID为1*/}else{printf((%s,2)\n,alphatp);/*为标识符时,编号为2*/id=2;/*标识符的ID为2*/}return(buffer);}/*判断字符串是否为关键字*/4intsearch(charsearchchar[],intwordtype){char*key[32]={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,volatile,while,switch,typedef,union,unsigned,void};/*设置数组指针存储c语言中的32个关键字*/inti;intp;switch(wordtype){case1:for(i=0;i=31;i++){if(strcmp(key[i],searchchar)==0)/*比较字符串,为关键字则定位该关键字的序号*/{p=i+1;break;}elsep=0;}return(p);}}chardigitprocess(charbuffer){inti=-1;chardigittp[20];while((isdigit(buffer))||buffer=='.'||buffer=='e'||buffer=='E')//考虑数字为小数和指数时的情况{digittp[++i]=buffer;buffer=fgetc(fp);/*同上*/}digittp[i+1]='\0';printf((%s,3)\n,digittp);/*输出该数字,编号为3*/id=3;/*设置ID为3*/return(buffer);}charotherprocess(charbuffer){intn=0;charch[20];ch[0]=buffer;ch[1]='\0';5if(ch[0]=='%'||ch[0]=='\\'){buffer=fgetc(fp);ch[1]=buffer;ch[2]='\0';printf((%s,5)\n,ch);id=4;buffer=fgetc(fp);return(buffer);}if(ch[0]=='&'){buffer=fgetc(fp);if(buffer!='&'){printf((%s,5)\n,ch);id=4;return(buffer);}if(buffer=='&'){ch[1]=buffer;ch[2]='\0';printf((%s,4)\n,ch);id=3;buffer=fgetc(fp);return(buffer);}}if(ch[0]==','||ch[0]==';'||ch[0]=='{'||ch[0]=='}'||ch[0]=='('||ch[0]==')'){printf((%s,5)\n,ch);buffer=fgetc(fp);id=4;return(buffer);}if(ch[0]=='*'||ch[0]=='/'){printf((%s,4)\n,ch);buffer=fgetc(fp);id=4;return(buffer);}6if(ch[0]=='='||ch[0]=='!'||ch[0]==''||ch[0]==''){buffer=fgetc(fp);if(buffer=='=')/*防止'==','!=','=','='符号的分离*/{ch[1]=buffer;ch[2]='\0';printf((%s,4)\n,ch);}else{printf((%s,4)\n,ch);id=4;retur
本文标题:编译原理实验报告
链接地址:https://www.777doc.com/doc-6150824 .html