南航金城学院编译原理课程设计杨阳教的.doc_第1页
南航金城学院编译原理课程设计杨阳教的.doc_第2页
南航金城学院编译原理课程设计杨阳教的.doc_第3页
南航金城学院编译原理课程设计杨阳教的.doc_第4页
南航金城学院编译原理课程设计杨阳教的.doc_第5页
已阅读5页,还剩11页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

产生式主程序:Program- main () DefList; StmtList定义变量:DefList- Def; DefListDefList- DefDef- Type IDListType- intType- floatIDList- id, IDListIDList- id定义语句:StmtList- Stmt; StmtListStmtList- Stmt;赋值语句:Stmt- id:=E条件分支语句Stmt- if (BE) StmtList else StmtList算数表达式:(1) 自上而下分析法 (2) 自下而上分析法E- TEE- TE- +TEE- E+TE- T- FT- FTT- T*FT- *FTF- (E)T- F- idF- (E)F- numF- idF- floatnumF- intnumF- floatnum布尔表达式:BE- id id idBE- id = idBE- id id intnumBE- id = intnum课程设计要实现的内容:(1)设计词法分析器设计各单词的状态转换图,并为不同的单词设计种别码。功能包括:a. 能够拼出语言中的各个关键字、运算符和界符;b. 能够识别出标识符和不同类型的常量;c. 能对输入的一段文本程序输出其词法成分的各种别编码和属性值(对关键字,输出其种别编码,属性值为空;对常量,输出其种别编码和常量的值;对变量,输出其种别编码和变量名)。(2)语法分析要求用递归下降分析法、LL(1)分析法或SLR(1)分析法,对词法分析的结果进行语法分析。(实现对标识符表的插入和查找,见(3)若语法正确,则输出一棵语法树。若语法错误,则报错。(3)设计标识符表,并实现一定的语义分析(可选)标识符表应至少包括名字栏和数据类型栏。在语法分析中:对在声明语句中出现的关于标识符的声明,如果该名字在符号表中不存在,则将其加入到符号表中,并将数据类型填入。若名字已经存在被声明过,则报错。对一般语句中出现的标识符,可以查找标识符表,若标识符不在表中,则报错。验收要求1、 有标识符表的构造说明文档(可选)2、 有种别编码表的说明文档3、 有词法分析的DFA图4、 若使用了递归下降分析法需要有根据产生式构造的first/follow集、若使用了LL(1)分析法需要有预测分析表,若使用了SLR(1)分析法需要有SLR(1)分析表5、 有可执行的源程序,能输出词法分析的结果、语法分析的语法树(和标识符表)源程序:#include #includeusing namespace std;#include#include #includeint i,j,k,flag,number,status,m=0,n=0,d,dian;/d为在计算小数时除以0.1的次数/*status which is use to judge the string is keywords or not!*/char ch;float number1;char words10 = ;char program500;int flags500; /存储输入句子string cnt500;/标识符int temp=0; /数组下标int is_right=1; /判断输出信息char num500;/-词法分析-int Scan(char program) char *keywords9 = int,char,float,if,else,do,while,printf,main; /关键字number=0;status=0;j=0;ch=programi+; /遍历while(ch= |ch=n) /跳过空字符(无效输入) ch=programi+; if (ch = a) & (ch = a) & (ch = z ) wordsj+=ch; ch=programi+; i-; wordsj+ = 0;for (k = 0; k = 0) & (ch = 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;elsei-;flag = 406;break;case!:if (ch = !)wordsj+ = ch;wordsj = 0;ch = programi+;if (ch = =)wordsj+ = ch;wordsj = 0;flag = 407;elsei-;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;elsei-;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;elsei-;flag = 414;break;case*:if (ch = *)wordsj+ = ch;wordsj = 0;ch = programi+;if (ch = =)wordsj+ = ch;wordsj = 0;flag = 415;elsei-;flag = 416;break;case/:if (ch = /)wordsj+ = ch;wordsj = 0;ch = programi+;if (ch = =)wordsj+ = ch;wordsj = 0;flag = 417;elsei-;flag = 418;break;case:wordsj = ch;wordsj+1 = 0;flag = 419;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;elsei-;flag = 511;break;case,:wordsj = ch;wordsj+1 = 0;flag = 512;break;case#:wordsj = ch;wordsj+1 = 0;flag = 513;break;case:wordsj = ch;wordsj+1 = 0;flag = 514;break;case :/空格 wordsj =_; wordsj+1 = 0; flag = 515; break;case$:wordsj = #;wordsj+1 = 0;flag = 0;break;default:flag = -1;break;return flag;/-语法分析(递归下降)-void E();void E1();void T();void T1();void F();void E()coutTE endl;T();E1();void E1()if(flagstemp=411) cout+Eendl; temp+; T(); E1();void T()coutFTendl; F();T1();void T1()if(flagstemp=416)cout*Fendl;temp+;F();T1();void F() if(flagstemp=100|flagstemp=200|flagstemp=201) coutcnttempendl; temp+; else if(flagstemp=502) cout(endl; temp+; E(); if(flagstemp=503) cout)endl; temp+; if(flagstemp=0|flagstemp=501) is_right=1; temp+; else is_right=0; else is_right =0;void main() int i=0,num=0; cout请输入测试程序或者表达式,以$结束endl; do ch =getchar(); programi+ = ch; while(ch != $); i = 0; cout词法分析:endl; do flag = Scan(program);/词法分析 if (flag = 200) cout(flag,number)endl;flagsnum=flag;stringstream stream;streamcntnum;stream.clear(); num+; else if(flag=201) for(i=d;i0;i-) number1=number1*0.1; number1=number1+number; cout(flag,number1)endl;flagsnum=flag;stringstream stream;streamcntnum;stream.clear();num+; e

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论