




已阅读5页,还剩40页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Central South University编译原理实验报告学院: 信息科学与工程学院 班级: 学号: 姓名: 时间: 实验一:词法分析一、实验目的给出PL/0语言文法规范,编写PL/0语言的词法分析程序。二、实验准备微机CPU主频1.3G以上,128M内存,安装好C语言,PASCAL语言,或C+。三、实验时间24学时四、实验内容给出PL/0语言文法,输出单词(关键字、专用符号以及其它标记)。1、格式输入:源程序文件。 输出:关键字、专用符号以及其它标记。2、实现原理程序中先判断这个句语句中每个单元为关键字、常数、运算符、界符,对与不同的单词符号给出不同编码形式的编码,用以区分之。3、实验方法读懂PL/0语言源代码,自己重点独立实现对常量的判别。4、实现过程本次实验所用的语言为标准C,以下同。本功能实现的主函数为getToken函数。通过从文件中读取字符到缓冲区中并由C语言字符的状态转换图流程判断返回一个字符(Token)。分析出来的Token主要分为关键字,专用符号,标记符号。本实验实现的C语言的基本词法如下:关键字: els if int return void while专用符号: + - * / = = != = ; , ( ) /* */其它标记: id num ID = letter letter*NUM = digit digit*letter = a|b|.|z|A|B|.|Z|ditit= 0|1|.|9通过在C语言中定义一个枚举类型来识别这些符号:PL/0语言的EBNF表示:=;:=|;:=+|-:=*|/:=|#|=:=a|b|X|Y|Z:=0|1|2|8|95、 设计过程1 关键字:void,main,if,then,break,int,Char,float,include,for,while,printfscanf 并为小写。 2+”;”-”;”*”;”/”;”:=“;”:”;”“;”“;”=“;”“;”=“;”(“;”)”;”;”;”#”为运算符。3 其他标记 如字符串,表示以字母开头的标识符。 4 空格符跳过。 5 各符号对应种别码 关键字分别对应1-13运算符分别对应401-418,501-513。字符串对应100常量对应200结束符#六、实验结果首先设置终止符,以#开头,#后加任意字符,设置为终止符。输入一段字符串,程序能判断其中是否有关键字、专用字符、其他标记、数字等,并显示出来是哪些关键字、专用字符、数字、其它标记。不同的flag值,表示不同的信息。比如,flag=1表示含有关键字void,flag=2表示含有关键字main,flag=502表示含有(,flag=200表示含有数字等。七、源程序#include#include#include #includeint i,j,k,flag,number,status;/*status which is use to judge the string is keywords or not!*/char ch;char words10 = ;char program500;int Scan(char program) char *keywords13 = void,main,if,then,break,int, char, float, include,for,while,printf, scanf;number = 0; status = 0;j = 0;ch = programi+; /* To handle the lettle space ands tab*/*handle letters*/if (ch = a) & (ch = a) & (ch = z )wordsj+=ch;ch=programi+; i-; wordsj+ = 0; for (k = 0; k = 0) & (ch = 0 ) & (ch : if (ch = ) wordsj+ = ch; wordsj = 0; ch= programi+; if (ch = =) wordsj+ = ch; wordsj = 0; flag = 403; else i-; flag = 404; break;case: if (ch = ) wordsj+ = ch; wordsj = 0; ch= programi+; if (ch = =) wordsj+ = ch; wordsj= 0; flag = 405; else i-; flag = 406; break;case!: if (ch = !) wordsj+ = ch; wordsj= 0; ch = programi+; if (ch = =) wordsj+ = ch; wordsj= 0; flag= 407; else i-; flag = 408; break;case+: if (ch = +) wordsj+ = ch; wordsj = 0; ch= programi+; if (ch = =) wordsj+ = ch; wordsj = 0; flag= 409; else if (ch = +) wordsj+ = ch; wordsj = 0; flag= 410; else i-;flag= 411; break;case-: if (ch = -) wordsj+ = ch; wordsj = 0; ch= programi+; if (ch = =) wordsj+ = ch;wordsj = 0;flag = 412; else if( ch = -) wordsj+ = ch;wordsj = 0;flag = 413; else i-;flag = 414; break;case*: if (ch = *) wordsj+ = ch; wordsj = 0; ch= programi+; if (ch = =) wordsj+ = ch; wordsj = 0; flag = 415; else i-; flag = 416; break; case/: if (ch = /) wordsj+ = ch; wordsj = 0; ch= programi+; if (ch = =) wordsj+ = ch; wordsj = 0; flag= 417; else i-; flag = 418; break;case;: wordsj = ch; wordsj+1 = 0; flag = 501; break; case(: wordsj = ch; wordsj+1 = 0; flag = 502; break; case): wordsj = ch; wordsj+1 = 0; flag = 503; break;case: wordsj = ch; wordsj+1 = 0; flag = 504; break; case: wordsj = ch; wordsj+1 = 0; flag = 505; break; case: wordsj = ch; wordsj+1 = 0; flag= 506; break; case: wordsj = ch; wordsj+1 = 0; flag = 507; break; case: wordsj = ch; wordsj+1 = 0; flag = 508; break;case: wordsj = ch; wordsj+1 = 0; flag = 509; break;case%: if (ch = %) wordsj+ = ch; wordsj = 0; ch = programi+; if (ch = =) wordsj+ = ch; wordsj = 0; flag= 510; else i-; flag = 511; break;case,: wordsj = ch; wordsj+1 = 0; flag = 512; break;case#: wordsj = ch; wordsj+1 = 0; flag = 513; break;case: wordsj = #; flag = 0; break;default: flag = -1; break;return flag;main()i=0;printf(please input a program end with );do ch = getchar(); programi+ = ch;while(ch != ); i = 0;do flag = Scan(program); if (flag = 200) printf(%2d,%4d),flag,number); else if (flag = -1) printf(%d,error),flag); else printf(%2d,%4s),flag,words); while (flag != 0); system(pause);实验二:语法分析一、实验目的给出PL/0语言文法规范,要求编写PL/0语言语言的语法分析程序。二、实验准备安装好C语言或C+。三、实验时间 24学时四、实验内容已给PL/0语言文法,利用递归子程序法,编制语法分析程序,要求将错误信息输出到语法错误文件中,输出语法树。 1、实验方法读懂PL/0语言源代码,给出相关源代码的注释。2、递归子程序法: 对于每个非终结符,编写一个子程序,由该子程序负责识别该语法单位是否正确。表达式的文法表达式=+|-项(+|-)项项=因子(*|/)因子因子=标识符|无符号整数|(表达式)表达式的递归子程序实现procedure expr;begin if sym in plus, minus then begin getsym; term; end else term; while sym in plus, minus do begin getsym; term; endend; 项=因子(*|/)因子 项的递归子程序实现procedure term;begin factor; while sym in times, slash do begin getsym; factor; endend;因子=标识符|无符号整数|(表达式)因子的递归子程序实现procedure factor;begin if sym ident then if sym number then if sym = ( then begin getsym; expr; if sym = ) then getsym else error end else error else getsym else getsym end; 五、程序核心代码和注释:public void analyzer()/*/循环读取grammar.txt/* /*此处代码略*/*/循环读取 lengh.txt/* /*此处代码略*/*/ 读入文件,进行语法分析/*string strReadFile;strReadFile=input.txt;myTextRead.myStreamReader=new StreamReader(strReadFile);string strBufferText;int wid =0;Console.WriteLine(分析读入程序(记号ID):n);dostrBufferText =myTextRead.myStreamReader.ReadLine();if(strBufferText=null)break;foreach (String subString in strBufferText.Split()if(subString!=)int ll;if(subString!=null)ll= subString.Length; /每一个长度elsebreak;int a=ll+1;char b = new chara;StringReader sr = new StringReader(subString);sr.Read(b, 0, ll); /把substring 读到char数组里int sort=(int)b0;/ wordi 和 wordNumi对应/先识别出一整个串,再根据开头识别是数字还是字母Wordwid=subString;if(subString.Equals(void)wordNumwid=0;else if(subString.Equals(main)wordNumwid=1;else if(subString.Equals()wordNumwid=2;else if(subString.Equals()wordNumwid=3;else if(subString.Equals(int)wordNumwid=4;else if(subString.Equals(=)wordNumwid=6;else if(subString.Equals()wordNumwid=22;else if(subString.Equals(;)wordNumwid=23;else /识别变量和数字if(sort47&sort58)wordNumwid=7;else wordNumwid=5;Console.Write(subString+(+wordNumwid+)+ );wid+;Console.WriteLine(n);while (strBufferText!=null);wordNumwid=24;myTextRead.myStreamReader.Close();/*/读入LR分析表/* /*此处代码略*/int state = new int100;string symbol =new string100;state0=0;symbol0=#;int p1=0;int p2=0;Console.WriteLine(n按文法规则归约顺序如下:n);/*/ 归约算法如下所显示/*while(true)int j,k;j=statep2;k=wordNump1;t=LRj,k; /当出现t为0的时候if(t=0)/错误类型string error;if(k=0)error=void;elseif(k=1)error=main;elseif(k=2)error=();elseif(k=3)error=;elseif(k=4)error=int;elseif(k=6)error=;elseif(k=22)error=;elseif(k=23)error=;elseerror=其他错误符号;Console.WriteLine(n检测结果:);Console.WriteLine(代码中存在语法错误);Console.WriteLine(错误状况:错误状态编号为 +j+ 读头下符号为 +error);break;elseif(t=-100) /-100为达到接受状态Console.WriteLine(n);Console.WriteLine(n检测结果:);Console.WriteLine(代码通过语法检测);break;if(t0)p2=p2+1;statep2=t;symbolp2=Convert.ToString(wordNump1);p1=p1+1;myTextRead.myStreamReader.Close();Console.Read();实验三:语义分析一、实验目的给出PL/0文法规范,要求编写PL/0语言的语义分析程序。二、实验准备微机CPU主频1.3G以上,128M内存,安装好C语言,PASCAL语言,或C+。三、实验时间 24学时四、实验内容已给PL/0语言文法,条件语句的翻译分析程序设计(输出四元式)实验方法:(1) 写出符合LL(1)法的文法及属性文法。 (2) 完成题目要求的中间代码四元式的描述。 (3) 写出LL(1)法的思想,完成语义分析程序设计。 (4) 编制好分析程序后,设计若干用例,上机测试并通过所设计的分析程序。 5、 实验结果六、源代码程序:#include stdio.h#include stdlib.h#define MaxRuleNum 8#define MaxVnNum 5#define MaxVtNum 5#define MaxStackDepth 20#define MaxPLength 20#define MaxStLength 50/*-main struct define-*/*声明:非终结符序号 = 100 + Vn的下标 终结符序号 = Vn的下标*/*+文法结构+*/struct pRNode /*产生式右部结构*/ int rCursor; /*右部序号*/ struct pRNode *next;struct pNode /*产生式结点结构*/ int lCursor; /*左部符号序号*/ int rLength; /*右部长度*/ /*注当rLength = 1 时,rCursor = -1为空产生式*/ struct pRNode *rHead; /*右部结点头指针*/;char VnMaxVnNum + 1; /*非终结符集*/int vnNum;char VtMaxVtNum + 1; /*终结符集*/int vtNum;struct pNode PMaxRuleNum; /*产生式*/int PNum; /*产生式实际个数*/char bufferMaxPLength + 1;char ch; /*符号或string ch;*/char stMaxStLength; /*要分析的符号串*/*+first and follow collect struct+*/struct collectNode /*集合元素结点结构*/ int nVt; /*在终结符集中的下标*/ struct collectNode *next;struct collectNode* firstMaxVnNum + 1; /*first集*/struct collectNode* followMaxVnNum + 1; /*follow集*/*+analysis table struct+*/int analyseTableMaxVnNum + 1MaxVtNum + 1 + 1;/*预测分析表存放为产生式的编号,+1用于存放结束符,多+1用于存放#(-1)*/*+analysis stack struct+*/int analyseStackMaxStackDepth + 1; /*分析栈*/int topAnalyse; /*分析栈顶*/*int reverseStackMaxStackDepth + 1; /*颠倒顺序栈*/*int topReverse; /*倒叙栈顶*/*-function declare-*/void Init();/*初始化*/int IndexCh(char ch);/*返回Vn在Vn表中的位置+100、Vt在Vt表中的位置,-1表示未找到*/void InputVt(); /*输入终结符*/void InputVn();/*输入非终结符*/void ShowChArray(char* collect, int num);/*输出Vn或Vt的内容*/void InputP();/*产生式输入*/bool CheckP(char * st);/*判断产生式正确性*/void First(int U);/*计算first集,U-xx.*/void AddFirst(int U, int nCh); /*加入first集*/bool HaveEmpty(int nVn); /*判断first集中是否有空(-1)*/void Follow(int V);/*计算follow集*/void AddFollow(int V, int nCh, int kind);/*加入follow集, kind = 0表加入follow集,kind = 1加入first集*/void ShowCollect(struct collectNode *collect);/*输出first或follow集*/void FirstFollow();/*计算first和follow*/void CreateAT();/*构造预测分析表*/void ShowAT();/*输出分析表*/void Identify(char *st);/*主控程序,为操作方便*/*分析过程显示操作为本行变换所用,与教程的显示方式不同*/void InitStack();/*初始化栈及符号串*/void ShowStack();/*显示符号栈中内容*/void Pop();/*栈顶出栈*/void Push(int r);/*使用产生式入栈操作*/LL1.CPP :/*-*/#include LL1.h/*-main function-*/void main(void)char todo,ch;Init();InputVn();InputVt();InputP();getchar();FirstFollow();printf(所得first集为:);ShowCollect(first);printf(所得follow集为:);ShowCollect(follow);CreateAT();ShowAT();todo = y;while(y = todo)printf(n是否继续进行句型分析?(y / n):);todo = getchar();while(y != todo & n != todo)printf(n(y / n)? );todo = getchar();if(y = todo)int i;InitStack();printf(请输入符号串(以#结束) : );ch = getchar();i = 0;while(# != ch & i MaxStLength)if( != ch & n != ch) sti+ = ch; ch = getchar(); if(# = ch & i MaxStLength) sti = ch; Identify(st); else printf(输入出错!n); getchar();/*-function definition-*/void Init() int i,j; vnNum = 0; vtNum = 0; PNum = 0; for(i = 0; i = MaxVnNum; i+) Vni = 0; for(i = 0; i = MaxVtNum; i+) Vti = 0; for(i = 0; i MaxRuleNum; i+) Pi.lCursor = NULL; Pi.rHead = NULL; Pi.rLength = 0; PNum = 0; for(i = 0; i = MaxPLength; i+) bufferi = 0; for(i = 0; i MaxVnNum; i+) firsti = NULL; followi = NULL; for(i = 0; i = MaxVnNum; i+) for(j = 0; j = MaxVnNum + 1; j+) analyseTableij = -1; /*返回Vn在Vn表中的位置+100、Vt在Vt表中的位置,-1表示未找到*/int IndexCh(char ch) int n; n = 0; /*is Vn?*/ while(ch != Vnn & 0 != Vnn) n+; if(0 != Vnn) return 100 + n; n = 0; /*is Vt?*/ while(ch != Vtn & 0 != Vtn) n+; if(0 != Vtn) return n; return -1;/*输出Vn或Vt的内容*/void ShowChArray(char* collect) int k = 0; while(0 != collectk) printf( %c , collectk+); printf(n);/*输入非终结符*/void InputVn() int inErr = 1; int n,k; char ch; while(inErr) printf(n请输入所有的非终结符,注意:); printf(请将开始符放在第一位,并以#号结束:n); ch = ; n = 0; /*初始化数组*/ while(n MaxVnNum) Vnn+ = 0; n = 0; while(# != ch) & (n MaxVnNum) if( != ch & n != ch & -1 = IndexCh(ch) Vnn+ = ch; vnNum+; ch = getchar(); Vnn = #; /*以“#”标志结束用于判断长度是否合法*/ k = n; /*k用于记录n以便改Vnn=0*/ if(# != ch) if( # != (ch = getchar() while(# != (ch = getchar() ; printf(n符号数目超过限制!n); inErr = 1; continue; /*正确性确认,正确则,执行下下面,否则重新输入*
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025海南儋州市总工会招聘工会社会工作者15人(第1号)笔试参考题库附答案解析
- 金融系毕业论文怎么写
- 乡村智慧农业技术操作手册
- 大四金融系毕业论文范文
- 毕业论文英语
- 2025浙江省创新投资集团有限公司社会招聘笔试模拟试题及答案解析
- 2025年淮南寿县公开招聘高中教师29人考试参考题库附答案解析
- 土地资源合理利用合作发展方案协议
- 2025年公共设施外墙油漆施工质量保证合同
- 2025年温州鹿城区区属国有企业面向社会和面向退役士兵公开招聘(选聘)工作人员34人考试参考题库附答案解析
- 产科医疗安全与质量控制制度
- 石油化工设备维护与检修手册
- 拆迁工程成本控制方案
- 原发性肝癌的CT表现和诊疗
- DB31-T 1502-2024 工贸行业有限空间作业安全管理规范
- 合肥新华书店招聘笔试题库2024
- 新解读《JTG 5120-2021公路桥涵养护规范》
- 2.3.4运动图像与追及问题课件高一上学期物理人教版
- 项目化学习在跨学科教学中的实施
- 国家保安员资格考试试卷(十套)
- 注射泵操作使用课件
评论
0/150
提交评论