合肥工业大学编译原理实验.docx
- 文档编号:28190934
- 上传时间:2023-07-09
- 格式:DOCX
- 页数:15
- 大小:337.81KB
合肥工业大学编译原理实验.docx
《合肥工业大学编译原理实验.docx》由会员分享,可在线阅读,更多相关《合肥工业大学编译原理实验.docx(15页珍藏版)》请在冰豆网上搜索。
合肥工业大学编译原理实验
宣城校区
实验报告
课程名称编译原理
专业班级计算机0001班
学生姓名及学号赵保飞68
指导教师李芒宏
实验地点计算机中心楼第四机房
2017~2018学年第一学期
《编译原理》课程实验报告
实验名称
词法分析设计
姓名
赵保飞
系院专业
计算机科学与技术
班级
计算机01班
学号
68
实验日期
指导教师
李芒宏
成绩
一、实验目的和要求
通过本实验的编程实践,使学生了解词法分析的任务,掌握词法分析程序设
计的原理和构造方法,使学生对编译的基本概念、原理和方法有完整的和清楚的
理解,并能正确地、熟练地运用。
二、实验原理
(1)实验数据结构说明
K[]String数组-关键字表;s[]char数组—分界符;m[]char数组—算术运算符;r[]String数组—关系运算符;ArrayList型String数组ci—常数;ArrayList型String数组id—标识符
(2)实验算法描述
(3)算法流程图
三、源程序代码和测试结果
packagelexicalAnalysis;
import.*;
import.*;
publicclasslexicalAnalysis{
staticStringk[]={"for","main","if","while","void","public","static","printf","scanf","asm","do","return","typedef","auto","double","break","short","using","default","long"};ength()==1){quals(b))){
display(4,r[i],'');ength()==2){quals(a))){
display(4,r[i],'');
;
import.*;
importWinGridextendsJFrame{
GridLayoutgrid;
JPanelchessboard;
JTextFieldtext;
JTextAreatextShow;
JButtonbutton;
ReaderListenlistener;
WinGrid(){
init();
setVisible(true);
setDefaultCloseOperation;
}
voidinit(){
setLayout(newFlowLayout());
text=newJTextField(10);
setBounds(466,166,500,400);
button=newJButton("读取");
textShow=newJTextArea(9,30);
listener=newReaderListen();
(text);
(textShow);
(listener);
(listener);
add(text);
add(button);
add(newJScrollPane(textShow));
}
}
classReaderListenimplementsActionListener{
JTextFieldtext;
JTextAreatextShow;
LLone=newLL();
Strings;
Stringtemp1[][]=newString[18][6];
publicvoidsetJTextField(JTextFieldtext){
=text;
}
publicvoidsetJTextArea(JTextAreatextShow){
=textShow;
}
publicStringS(){
Stringt=();
returnt;
}
@Override
publicvoidactionPerformed(ActionEvente){
try{
Strings=()+"#";
(s+"\n");
("步骤"+"\t"+"分析栈"+"\t"+"剩余输入串"+"\t"+"所用产生式"+"\t"+"动作"+"\n");
(s,temp1);
for(inti=0;i<18;i++){
for(intk=0;k<5;k++){
(temp1[i][k]+"\t");
}
("\n");
}
}catch(Exceptione2){
();
}
}
}
publicclassexp3{
publicstaticvoidmain(String[]args){
;
import.*;
publicclassLL{
charVn[]={'E','T','G','F','S'};harAt(0);
intk;
if(vtNum(Gr[i].charAt(j))!
=-1){harAt(j)!
='ε'){harAt(j)不为ε时将s加到M[vn(u)][vt(Gr[i].charAt(j))]中
M[vnNum(u)][vtNum(Gr[i].charAt(j))]=s;
}
else{harAt(j)为ε,将属于u的follow集的元素b的位置用s加到M[vn(u)][vt(b)]中
if(FOLLOW[vnNum(u)][k]){
M[vnNum(u)][k]=s;
}
}
}
}
else{harAt(j)为非终结符
for(k=0;k<;k++){harAt(j)的first集时,将s加到M[vn(u)][vt(a)]中
if(FIRST[vnNum(Gr[i].charAt(j))][k]){
M[vnNum(u)][k]=s;
}
}
if(FIRST[vnNum(Gr[i].charAt(j))][vtNum('ε')]){harAt(j)的first集时,将属于u的follow集的b将s加到M[vn(u)][vt(b)]中
if(j==m-1){
for(k=0;k<;k++){
if(FOLLOW[vnNum(u)][k])M[vnNum(u)][k]=s;
}
}
else{
j=j+1;
MM(j,i,s,m);
}
}
}
}
voidbuildM(){ength()){
m=or(j,Gr[i]);
if(m==-1){
m=Gr[i].length();
}
s=Gr[i].substring(j,m);oString());
}
tempp[k][1]=temp0_string;
tempp[k][2]=(i);
tempp[k][3]=rule;
tempp[k][4]=();
v=();
action="pop";
if(vnNum(v)!
=-1){
left='E';
strcpy(s_have[1].sright,"E+T");left='E';
strcpy(s_have[2].sright,"T");left='T';
strcpy(s_have[3].sright,"T*F");left='T';
strcpy(s_have[4].sright,"F");left='F';
strcpy(s_have[5].sright,"(E)");left='F';
strcpy(s_have[6].sright,"i");ove[0]=5;
action[0].move[3]=4;
action[1].move[1]=6;
action[2].change[1]=2;
action[2].move[2]=7;
action[2].change[4]=2;
action[2].change[5]=2;
action[3].change[1]=4;
action[3].change[2]=4;
action[3].change[4]=4;
action[3].change[5]=4;
action[4].move[1]=5;
action[4].move[3]=4;
action[5].change[1]=6;
action[5].change[2]=6;
action[5].change[4]=6;
action[5].change[5]=6;
action[6].move[0]=5;
action[6].move[3]=4;
action[7].move[0]=5;
action[7].move[3]=4;
action[8].change[1]=6;
action[8].change[4]=11;
action[9].change[1]=1;
action[9].move[2]=7;
action[9].change[4]=1;
action[9].change[5]=1;
action[10].change[1]=3;
action[10].change[2]=3;
action[10].change[4]=3;
action[10].change[5]=3;
action[11].change[1]=5;
action[11].change[2]=5;
action[11].change[4]=5;
action[11].change[5]=5;
/*存储GOTO表*/
go[0].head[0]='E';ead[1]='T';
go[0].head[2]='F';
go[0].gt[0]=1;t[1]=2;
go[0].gt[2]=3;
go[4].head[0]='E';
go[4].head[1]='T';
go[4].head[2]='F';
go[4].gt[0]=8;
go[4].gt[1]=2;
go[4].gt[2]=3;
go[6].head[1]='T';
go[6].head[2]='F';
go[6].gt[1]=9;
go[6].gt[2]=3;
go[7].head[2]='F';
go[7].gt[2]=10;
ove[col]!
=0)
{
cout<<"ACTION["< <<",PUSH"< status[++sta_Index]=action[sta].move[col];hange[col]! =0) { cout<<"r"< "< < < inti=0; for(i=0;i { status[++sta_Index]=go[sta].gt[i]; cout< return; } } } /*查表*/ voidstart_find(void) { ints=status[sta_Index]; charexp_first_char=inputstring[instr_top]; charsym=symbol[sym_Index]; while(IsAccept! =1) { s=status[sta_Index]; exp_first_char=inputstring[instr_top]; sym=symbol[sym_Index]; /*输出步骤*/ cout< /*输出当前状态栈*/ inti=0; for(i=0;i<=sta_Index;i++) { cout< } cout<<"\t\t"; /*输出符号栈*/ cout< /*输出剩余的输入串*/ i=0; for(i=0;i cout<<""; for(i=instr_top;i<=instr_index;i++) { cout< } cout<<"\t\t"; switch(exp_first_char) { case'i': find_table(s,exp_first_char,0); break; case'+': find_table(s,exp_first_char,1); break; case'*': find_table(s,exp_first_char,2); break; case'(': find_table(s,exp_first_char,3); break; case')': find_table(s,exp_first_char,4); break; case'#': find_table(s,exp_first_char,5); break; } } } intmain() { //cout<<"输入要判断的分析串: "; stringtemp; cin>>temp; for(inti=0;i<();i++) inputstring[i]=temp[i]; inputstring[()]='#'; instr_index=()+1; Initlize(); cout<<"步骤\t\t状态栈\t符号栈\t输入串\t动作\n"; start_find(); return0; } 四、实验评价、收获与体会 这一周真是惊险万分,有周二四个小时OS实验,周三网络考试,周三就要验收实验,还有一堆事好像老师都喜欢集体行动把事情堆到一起解决。 好在挺过来。 这个实验总的来说太难,尝试了java写图形界面和自动化实现分析表等,但是时间紧迫只好以后有时间慢慢搞。 现在的实验只是一份低配版的实验,LR分析表都不是自动生成的,是存储在数组中直接使用的,所以老师很失望给了及格分,我也很无奈。 没有大神的超速只能怪自己学艺不精,效率太低。 希望以后可以快点。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 合肥 工业大学 编译 原理 实验