编译原理课程设计福建农林大学java.docx
- 文档编号:28660588
- 上传时间:2023-07-19
- 格式:DOCX
- 页数:23
- 大小:135.37KB
编译原理课程设计福建农林大学java.docx
《编译原理课程设计福建农林大学java.docx》由会员分享,可在线阅读,更多相关《编译原理课程设计福建农林大学java.docx(23页珍藏版)》请在冰豆网上搜索。
编译原理课程设计福建农林大学java
福建农林大学计算机及信息学院
计算机类
课程设计报告
课程名称:
编译原理
课程设计题目:
语法分析器
姓名:
陈锦灿
系:
计算机
专业:
计算机科学及技术
年级:
2012级
学号:
3126010067
指导教师:
林清波
2015~2016学年第一学期
福建农林大学计算机及信息学院计算机类
课程设计结果评定
评语:
成绩:
指导教师签字:
任务下达日期:
评定日期:
1正则表达式
1.1正则表达式
(a|b)*(aa|bb)(a|b)*
1.2确定化(化简)后的状态转换图
1.3分析程序代码
importjava.util.Scanner;
publicclassTest4{
publicstaticvoidmain(String[]args){
int[][]s={{1,2},{3,2},{1,4},{3,5},{6,4},{6,4},{3,5}};
System.out.println("文法规则:
\n(a|b)*(aa|bb)*(a|b)*");
System.out.println("请输入您所要验证的句子:
\n");
Scannersc=newScanner(System.in);
Stringstr1=sc.next();
intlen=str1.length();
charch;
inti,j=0,t,flag,index;
i=0;index=0;
t=1;//判断输入是否正确
flag=0;//判断句子是否正确
while(index ch=str1.charAt(index); if(ch=='a') j=0; if(ch=='b') j=1; if(ch! ='a'&&ch! ='b'){ t=0;break; } index++; i=s[i][j]; } flag=i; if(flag>=3&&t==1){//flag>2时可终结 System.out.println("\n您所要验证的句子正确! \n\n"); }else{ System.out.println("\n您所要验证的句子错误! \n\n"); } } }1.4程序运行截图 1.5小结: 通过该课程设计,掌握了什么是编译程序,编译程序工作的基本过程及其各阶段的基本任务,熟悉了编译程序总流程框图,了解了编译程序的生成过程、构造工具及其相关的技术对课本上的知识有了更深的理解。 2LL (1)分析 2.1LL (1)文法 E→TE' E'→+TE'|ε T→FT' T'→*FT'|ε F→(E)|i 2.2LL (1)预测分析表 i + * ( ) # E E→TE' E→TE' E' E'→+TE' E'→ε E'→ε T T→FT' T→FT' T' T'→ε T'→*FT' T'→ε T'→ε F F→i F→(E) 2.3分析程序代码 publicclassTest{ publicstaticvoidmain(Stringa[]) { intid=1; intindex1=1;//记录栈最后一个非终结符的位置 StringtopStack,topIn,operation; StringBufferinString=null; ArrayList System.out.println("请输入一个语句: "); Scannerin=newScanner(System.in); Stringins=in.nextLine().trim(); if(ins.indexOf("#")<0)//假如最后完了输入#号也没事 ins+="#"; else ins=ins.substring(0,ins.indexOf("#")+1);//截取#号在内的前部 inString=newStringBuffer(ins); intkong=inString.indexOf(""); while(kong>=0)//去掉输入串表达式中的空格 { inString.delete(kong,kong+1); kong=inString.indexOf(""); } StringBufferstack=newStringBuffer("#E");//分析栈,初始放入E Stringll[][]={{"","i","+","*","(",")","#"}, {"E","E’T/P","","","E’T/P","",""}, {"E’","","E’T/N","","","ε/P","ε/P"}, {"T","T’F/P","","","T’F/P","",""}, {"T’","","ε/P","T’F/N","","ε/P","ε/P"}, {"F","ε/N","","",")E/N","",""}, {")","","","","","ε/N",""}, {"#","","","","","","acc"} };//ll (1)分析矩阵 System.out.println("LL (1)分析过程如下: "); System.out.println("\n序号\t分析栈"+getBlank(20)+"输入数据"+getBlank(20)+"动作"); StringBufferliutemp=null; while(stack.length()>0) { intx=0,y=0;//记录在分析表中的的横纵坐标 if(stack.toString().endsWith("\'")||stack.toString().endsWith("’"))//证明是带了一撇的非终结符 index1=stack.length()-2;// else index1=stack.length()-1; topStack=String.valueOf(stack.substring(index1,stack.length()));//栈顶元素 if(inString.length()>0) topIn=String.valueOf(inString.charAt(0));//剩余输入串的第一个元素 else topIn=""; for(inti=1;i if(topStack.equals(ll[i][0])) { x=i; break; } for(inti=1;i if(topIn.equals(ll[0][i])) { y=i; break; } operation=ll[x][y];//动作命令 if(operation.length()>=3) { Stringfirst=operation.substring(0,operation.length()-2);//替换部分 Stringlast=operation.substring(operation.length()-2,operation.length());//是否换行部分 if(first.equals("ε"))//如果是空字符,有不要加入栈 first=""; if(operation.equals("acc")) { if(stack.length()==1&&inString.length()==1) { System.out.println(id+"\t"+stack+getBlank(21-stack.length()-inString.length())+inString+getBlank(11-operation.length())+operation); stack.delete(0,1); inString.delete(0,1); System.out.println("匹配成功! "); } else { System.out.println(id+"\t"+stack+getBlank(21-stack.length()-inString.length())+inString+getBlank(6)+"error"); System.out.println("不能完整匹配! "); } } elseif(last.equals("/P")) { System.out.println(id+"\t"+stack+getBlank(21-stack.length()-inString.length())+inString+getBlank(11-operation.length())+operation); stack.replace(index1,index1+topStack.length(),first);//把栈顶元素替换为分析表中值 if(first.equals("")) list.add(ll[x][0]+"->ε"); else{ liutemp=newStringBuffer(first); list.add(ll[x][0]+"->"+reverse(liutemp)); } id++; }elseif(last.equals("/N")) { System.out.println(id+"\t"+stack+getBlank(21-stack.length()-inString.length())+inString+getBlank(11-operation.length())+operation); stack.replace(index1,index1+topStack.length(),first); inString.delete(0,1);//相当于读下一个元素 liutemp=newStringBuffer(first); list.add(ll[x][0]+"->"+topIn+reverse(liutemp)); if(stack.toString().endsWith("\'")||stack.toString().endsWith("’")) index1=stack.length()-2;//重新设置index1值 else index1=stack.length()-1; id++; }else{ System.out.println("分析表构造出错! "); System.exit(0); } }elseif(y==0){ System.out.println(id+"\t"+stack+getBlank(21-stack.length()-inString.length())+inString+getBlank(6)+"error"); System.out.println("输入的符号不符合规定文法! "); System.exit(0); }elseif(x! =0&&y! =0&&operation.length()==0) { System.out.println(id+"\t"+stack+getBlank(21-stack.length()-inString.length())+inString+inString+getBlank(6)+"error"); System.out.println("输入符号串不完整! "); System.exit(0); } } System.out.println("\n该语句自顶向下构建语法树过程: "); for(inti=0;i System.out.println(list.get(i)); } publicstaticStringBufferreverse(StringBufferbuffer){//字母、运算符倒置 StringBufferbuf=newStringBuffer(); intix=-1; intlength=0; if(buffer.indexOf("'")<0&&buffer.indexOf("’")<0) buf.append(buffer.reverse()); else{ while(buffer.length()>0){ length=buffer.length(); if(buffer.charAt(length-1)=='\''||buffer.charAt(length-1)=='’') { buf.append(buffer.substring(length-2,length)); buffer.delete(length-2,length); }else{ buf.append(buffer.charAt(length-1)); buffer.delete(length-1,length); } } } returnbuf; } publicstaticStringgetBlank(intn){//得到n个连续空格 Stringblank=""; for(inti=0;i blank+=""; returnblank; } }2.4程序运行截图 2.5小结 通过该课程设计,全面系统的理解了编译原理程序构造的一般原理和基本实现方法。 把死板的课本知识变得生动有趣,激发了学习的积极性。 3算符优先分析 3.1算符优先文法 E->E+T|T T->T*F|F F->P%F|P P->(E)|i 3.2算符优先关系表 + * % i ( ) # + > < < < < > > * > > < < < > > % > > < < < > > i > > > > > ( < < < < < = ) > > > > > # < < < < < = 3.3分析程序代码 publicclassTest2{ staticHashMap staticStack publicstaticvoidtable(){ m.put("++",">");m.put("+*","<");m.put("+%","<"); m.put("+i","<");m.put("+(","<");m.put("+)",">"); m.put("+#",">");m.put("*+",">");m.put("**",">"); m.put("*%","<");m.put("*i","<");m.put("*(","<"); m.put("*)",">");m.put("*#",">");m.put("%+",">"); m.put("%*",">");m.put("%%","<");m.put("%i","<"); m.put("%(","<");m.put("%)",">");m.put("%#",">"); m.put("i+",">");m.put("i*",">");m.put("i%",">"); m.put("ii","");m.put("i(","");m.put("i)",">"); m.put("i#",">");m.put("(+","<");m.put("(*","<"); m.put("(%","<");m.put("(i","<");m.put("((","<"); m.put("()","=");m.put("(#","");m.put(")+",">"); m.put(")*",">");m.put(")%",">");m.put(")i",""); m.put(")(","");m.put("))",">");m.put(")#",">"); m.put("#+","<");m.put("#*","<");m.put("#%","<"); m.put("#i","<");m.put("#(","<");m.put("#)",""); m.put("##","="); } publicstaticvoidmain(Stringargs[]){ intk=1,i=0,xiabiao=0,f; sk=newStack table(); System.out.println("请输入符号串: "); Scannersc=newScanner(System.in); Strings=sc.next()+"#"; charc[]=s.toCharArray(); sk.push('#'); System.out.println("分析过程..."); while(i f=k-1; while(f>=0&&(Character)sk.get(f)=='A'){ f--; } if((m.get((Character)sk.get(f)+""+c[i])=="<") |(m.get((Character)sk.get(f)+""+c[i])=="=")){ k++; sk.push(c[i]); i++; }else{ xiabiao=f-1; while((xiabiao>=0) &&((m.get((Character)sk.get(xiabiao)+""+c[i])==">")|(Character)sk .get(xiabiao)=='A')){ xiabiao--; } for(intt=f;(t>=xiabiao&&t>0);t--){ sk.pop(); } sk.push('A'); sk.push(c[i]); k=sk.size(); i++; } for(inta=0;a if(sk.get(a)! ='A') System.out.print(sk.get(a)); } System.out.print("\n"); } if((Character)sk.peek()=='#'){ System.out.println(s.substring(0,s.length()-1)+"success! ! "); }else{ System.out.println(s+"error! "); }} }3.4程序运行截图 3.5小结 在这次课程设计中,我就是按照实验指导的思想来完成。 加深了理解文件系统的内部功能及内部实现,培养实践动手能力和程序开发能力的目的。 4LR分析 4.1LR文法 (0)E'->E (1)E->E+T (2)E->T (3)T->T*F (4)T->F (5)F->(E) (6)F->i 4.2LR分析表 ACTION GOTO i + * ( ) # E T F 0 S5 S4 1 2 2 1 S6 acc 2 r2 S7 r2 r2 3 r4 r4 r4 r4 7 4 S5 S4 8 2 3 5 r6 r6 r6 r6 6 S5 S4 9 3 7 S5 S4 10 8 S6 S11 9 r1 S7 r1 r1 10 r3 r3 r3 r3 11 r5 r5 r5 r5 4.3分析程序代码(代码原创) publicclassTest3{ String[][]sentence=newString[][]{//文法,是内定了 {"E","E+T"},//E->E+T {"E","T"},//E->T {"T","T*F"},//T->T*F {"T","F"},//T->F {"F","(E)"},//F->(E) {"F","i"}//F->i }; String[][]table=newString[][]{//为了简单,LR分析表就手工输入了 {"","i","+","*","(",")","#","E","T","F"}, {"S0","S5","","","S4","","","1","2","3"}, {"S1","","S6","","","","acc","","",""}, {"S2","","r2","S7","","r2","r2","","",""}, {"S3","","r4","r4","","r4","r4","","",""}, {"S4","S5","","","S4","","","8","2","3"}, {"S5",
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 编译 原理 课程设计 福建 农林 大学 java