欢迎来到淘文阁 - 分享文档赚钱的网站! | 帮助中心 好文档才是您的得力助手!
淘文阁 - 分享文档赚钱的网站
全部分类
  • 研究报告>
  • 管理文献>
  • 标准材料>
  • 技术资料>
  • 教育专区>
  • 应用文书>
  • 生活休闲>
  • 考试试题>
  • pptx模板>
  • 工商注册>
  • 期刊短文>
  • 图片设计>
  • ImageVerifierCode 换一换

    天津理工大学编译原理实验3:语义分析与中间代码生成.doc

    • 资源ID:24056445       资源大小:497KB        全文页数:86页
    • 资源格式: DOC        下载积分:15金币
    快捷下载 游客一键下载
    会员登录下载
    微信登录下载
    三方登录下载: 微信开放平台登录   QQ登录  
    二维码
    微信扫一扫登录
    下载资源需要15金币
    邮箱/手机:
    温馨提示:
    快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
    如填写123,账号就是123,密码也是123。
    支付方式: 支付宝    微信支付   
    验证码:   换一换

     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    天津理工大学编译原理实验3:语义分析与中间代码生成.doc

    Four short words sum up what has lifted most successful individuals above the crowd: a little bit more.-author-date天津理工大学编译原理实验3:语义分析与中间代码生成天津理工大学实验报告实验报告学院(系)名称:计算机与通信工程学院姓名学号专业班级实验项目实验三:语义分析与中间代码生成课程名称编译原理课程代码实验时间实验地点计算机软件实验室7-220计算机软件实验室7-215批改意见成绩教师签字: 实验内容:可选择LL1分析法、算符优先分析法、LR分析法之一,实现如下表达式文法的语法制导翻译过程。文法GE如下所示:EE+T | E-T | TTT*F | T/F | FFPF | PP(E) | i要求构造出符合语义分析要求的属性文法描述,并在完成实验二(语法分析)的基础上,进行语义分析程序设计,最终输出与测试用例等价的四元式中间代码序列。实验目的:1掌握语法制导翻译的基本功能。2巩固对语义分析的基本功能和原理的认识。3能够基于语法制导翻译的知识进行语义分析。4掌握类高级语言中基本语句所对应的语义动作。5理解并处理语义分析中的异常和错误。实验要求:1在实验二的基础上,实现语法制导翻译功能,输出翻译后所得四元式序列;2要求详细描述所选分析方法进行制导翻译的设计过程;3完成对所设计分析器的功能测试,并给出测试数据和实验结果;4为增加程序可读性,请在程序中进行适当注释说明;5整理上机步骤,总结经验和体会;6认真完成并按时提交实验报告。【实验过程记录(源程序、测试用例、测试结果及心得体会等)】#include<iostream>#include<cstring>#define size 1024using namespace std;int step=0;typedef struct variable_Tchar operate;/操作符 string var1;/变量 1 string var2;/变量 2 int num;/第几个变量 variable_T;variable_T tsize;/记录四元式变量的变量 int tsize=-1;/表示是第tsize+1个变量typedef struct char_stackchar content;/当前字符 string endchar;/这个符号代表的中间变量 可以是 i, 也可以是 t1, t2, 等等 int num;/和该字符相关的中间变量的序号 char_stack;string table1913=/ + - * / ) # ( i E T F P /* 0 */ "err","err","err","err","err","err","err", "s5", "s6", "1", "2", "3", "4",/* 1 */ "s7", "s8","err","err","err","err","acc","err","err","err","err","err","err",/* 2 */ "r3", "r3", "s9","s10","err", "r3", "r3","err","err","err","err","err","err",/* 3 */ "r6", "r6", "r6", "r6","err", "r6", "r6","err","err","err","err","err","err",/* 4 */ "r8", "r8", "r8", "r8","s11", "r8", "r8","err","err","err","err","err","err",/* 5 */ "err","err","err","err","err","err","err", "s5", "s6", "c", "2", "3", "4",/* 6 */ "r10","r10","r10","r10","r10","r10","r10","err","err","err","err","err","err",/* 7 */ "err","err","err","err","err","err","err", "s5", "s6","err", "d", "3", "4",/* 8 */ "err","err","err","err","err","err","err", "s5", "s6","err", "e", "3", "4",/* 9 */ "err","err","err","err","err","err","err", "s5", "s6","err","err", "f", "4",/* 10*/ "err","err","err","err","err","err","err", "s5", "s6","err","err", "g", "4",/* 11*/ "err","err","err","err","err","err","err", "s5", "s6","err","err", "h", "4",/* 12*/ "s7", "s8","err","err","err","s18","err","err","err","err","err","err","err",/* 13*/ "r1", "r1", "s9","s10","err", "r1", "r1","err","err","err","err","err","err",/* 14*/ "r2", "r2", "s9","s10","err", "r2", "r2","err","err","err","err","err","err",/* 15*/ "r4", "r4", "r4", "r4","err", "r4", "r4","err","err","err","err","err","err",/* 16*/ "r5", "r5", "r5", "r5","err", "r5", "r5","err","err","err","err","err","err",/* 17*/ "r7", "r7", "r7", "r7","err", "r7", "r7","err","err","err","err","err","err",/* 18*/ "r9", "r9", "r9", "r9", "r9", "r9", "r9","err","err","err","err","err","err"int getLength(char strsize)int i=0;while(stri!='0')i+;return i; int getLengthc(char_stack strsize)int i=0;while(stri.content!='0')i+;return i; int getstringLength(string str)int i=0;while(stri!='0')i+;return i; char gettop(char stacksize,int top)if(stacktop!='0')return stacktop;elsereturn '#'void popstack(char *stack,int *pointer,int times)int p;for(int i=1;i<=times;i+)p=*pointer;stackp='0'(*pointer)-;void popstackc(char_stack *stack,int *pointer,int times)int p;for(int i=1;i<=times;i+)p=*pointer;stackp.content='0'(*pointer)-;void pushstack(char_stack *stack,int *pointer,char *stack_state,int *pointer_state,char str,char sx,int x)int i=0;if(x=0)cout<<"ttt状态 "<<sx<<" 进状态栈 "else if(x=1)cout<<" 状态 "<<sx<<" 进状态栈 "if(str!='#')cout<<str<<" 进字符栈"(*pointer)+;stack(*pointer).content=str;(*pointer_state)+;stack_state(*pointer_state)=sx;int getcol(char top)switch(top)case '+':return 0;case '-':return 1;case '*':return 2;case '/':return 3;case '':return 4;case ')':return 5;case '#':return 6;case '(':return 7;case 'i':return 8;case 'E':return 9;case 'T':return 10;case 'F':return 11;case 'P':return 12;default:cout<<"Error! This character string is not this grammers sentence."<<endl;return -1;int getraw(char raw)switch(raw)case '0':return 0;case '1':return 1;case '2':return 2;case '3':return 3;case '4':return 4;case '5':return 5;case '6':return 6;case '7':return 7;case '8':return 8;case '9':return 9;case 'a':return 10;case 'b':return 11;case 'c':return 12;case 'd':return 13;case 'e':return 14;case 'f':return 15;case 'g':return 16;case 'h':return 17;case 'i':return 18;default:cout<<"Error! This character string is not this grammers sentence."<<endl;return -1;char getraw_content(string str)if(str="1")return '1'else if(str="2")return '2'else if(str="3")return '3'else if(str="4")return '4'else if(str="c")return 'c'else if(str="d")return 'd'else if(str="e")return 'e'else if(str="f")return 'f'else if(str="g")return 'g'else if(str="h")return 'h'else if(str="i")return 'i'string get_tx(int num)switch(num)case 1:return "t1"case 2:return "t2"case 3:return "t3"case 4:return "t4"case 5:return "t5"case 6:return "t6"case 7:return "t7"case 8:return "t8"case 9:return "t9"case 10:return "t10"case 11:return "t11"case 12:return "t12"case 13:return "t13"case 14:return "t14"case 15:return "t15"case 16:return "t16"/.本程序暂时用到这么多,等有时间编写合适的可以将数字转换为字符串的函数时,即可更改本函数 void show(char strsize,int index)int length=getLength(str);if(index!=-1)cout<<"t"for(int i=index+1;i<length;i+)cout<<stri;void showc(char_stack strsize,int index)int length=getLengthc(str);if(index!=-1)cout<<"t"for(int i=index+1;i<length;i+)cout<<stri.content;void switch_method(char_stack *stack,int *pointer,char *state_stack,int *pointer_state,string production,char *str,int *index)step+;cout<<"n"<<step<<"t"/显示步骤 show(state_stack,-1);/显示状态栈 cout<<"t"showc(stack,-1);/显示符号站 cout<<"t"<<str(*index)<<"t"/显示当前字符 show(str,(*index);/显示输入串 char c=str(*index);if(production="err")cout<<"Error! This character string is not this grammers sentence."<<endl;return ;else if(production="s5")char sx='5'(*index)+;pushstack(stack,pointer,state_stack,pointer_state,c,sx,0);else if(production="s6") char sx='6'(*index)+;pushstack(stack,pointer,state_stack,pointer_state,c,sx,0);else if(production="s7") char sx='7'(*index)+;pushstack(stack,pointer,state_stack,pointer_state,c,sx,0);else if(production="s8") char sx='8'(*index)+;pushstack(stack,pointer,state_stack,pointer_state,c,sx,0);else if(production="s9") char sx='9'(*index)+;pushstack(stack,pointer,state_stack,pointer_state,c,sx,0);else if(production="s10") char sx='a'(*index)+;pushstack(stack,pointer,state_stack,pointer_state,c,sx,0);else if(production="s11") char sx='b'(*index)+;pushstack(stack,pointer,state_stack,pointer_state,c,sx,0);else if(production="s18") char sx='i'(*index)+;pushstack(stack,pointer,state_stack,pointer_state,c,sx,0);else if(production="r1") int po=(*pointer);/用P规约该表达式,有效变量在E的endchar中,需要找到E的位置,即下面的操作 string st=stackpo.endchar;/对应 F po-=2;string se=stackpo.endchar;/在规约之前记录下要规约的字符所代表的变量。对应 T tsize+;/新增临时变量 ttsize.num=tsize+1;/下面四个表达式是按照上面的规约式进行的赋值 ttsize.operate='+'ttsize.var1=se;ttsize.var2=st;cout<<"t("<<ttsize.operate<<","<<ttsize.var1<<","<<ttsize.var2<<",t"<<ttsize.num<<")"int p=(*pointer_state);p-=3;char second=state_stackp;int i=getraw(second);int j=getcol('E');char c_out=getraw_content(tableij);cout<<"tr1:用E->E+T规约且" popstack(state_stack,pointer_state,3);popstackc(stack,pointer,3);char c='E'/str(*index);pushstack(stack,pointer,state_stack,pointer_state,c,c_out,1);string s=get_tx(ttsize.num);stack(*pointer).endchar=s;/把保存E+T规约的结果的变量保存至当前字符的终结符 else if(production="r2")int po=(*pointer);/用P规约该表达式,有效变量在E的endchar中,需要找到E的位置,即下面的操作 string st=stackpo.endchar;/对应 T po-=2;string se=stackpo.endchar;/在规约之前记录下要规约的字符所代表的变量。对应 E tsize+;/新增临时变量 ttsize.num=tsize+1;/下面四个表达式是按照上面的规约式进行的赋值 ttsize.operate='-'ttsize.var1=se;ttsize.var2=st;cout<<"t("<<ttsize.operate<<","<<ttsize.var1<<","<<ttsize.var2<<",t"<<ttsize.num<<")"int p=(*pointer_state);p-=3;char second=state_stackp;int i=getraw(second);int j=getcol('E');char c_out=getraw_content(tableij);cout<<"tr2:用E->E-T规约且" popstack(state_stack,pointer_state,3);popstackc(stack,pointer,3);char c='E'/str(*index);pushstack(stack,pointer,state_stack,pointer_state,c,c_out,1);string s=get_tx(ttsize.num);stack(*pointer).endchar=s;/把保存E-T规约的结果的变量保存至当前字符的终结符 else if(production="r3")string s=stack(*pointer).endchar;/在规约之前记录下要规约的字符所代表的变量 int p=(*pointer_state);p-;char second=state_stackp;int i=getraw(second);int j=getcol('E');char c_out=getraw_content(tableij);cout<<"tttr3:用E->T规约且" popstack(state_stack,pointer_state,1);popstackc(stack,pointer,1);char c='E'/str(*index);pushstack(stack,pointer,state_stack,pointer_state,c,c_out,1);stack(*pointer).endchar=s;/将记录下的变量赋值给规约后的字符else if(production="r4") int po=(*pointer);/用P规约该表达式,有效变量在E的endchar中,需要找到E的位置,即下面的操作 string sf=stackpo.endchar;/对应 F po-=2;string st=stackpo.endchar;/在规约之前记录下要规约的字符所代表的变量。对应 T tsize+;/新增临时变量 ttsize.num=tsize+1;/下面四个表达式是按照上面的规约式进行的赋值 ttsize.operate='*'ttsize.var1=st;ttsize.var2=sf;cout<<"t("<<ttsize.operate<<","<<ttsize.var1<<","<<ttsize.var2<<",t"<<ttsize.num<<")"int p=(*pointer_state);p-=3;char second=state_stackp;int i=getraw(second);int j=getcol('T');char c_out=getraw_content(tableij);cout<<"tr4:用T->T*F规约且" popstack(state_stack,pointer_state,3);popstackc(stack,pointer,3);char c='T'/str(*index);pushstack(stack,pointer,state_stack,pointer_state,c,c_out,1);string s=get_tx(ttsize.num);stack(*pointer).endchar=s;/把保存T*F规约的结果的变量保存至当前字符的终结符 else if(production="r5")int po=(*pointer);/用P规约该表达式,有效变量在E的endchar中,需要找到E的位置,即下面的操作 string sf=stackpo.endchar;/对应 F po-=2;string st=stackpo.endchar;/在规约之前记录下要规约的字符所代表的变量。对应 T tsize+;/新增临时变量 ttsize.num=tsize+1;/下面四个表达式是按照上面的规约式进行的赋值 ttsize.operate='/'ttsize.var1=st;ttsize.var2=sf;cout<<"t("<<ttsize.operate<<","<<ttsize.var1<<","<<ttsize.var2<<",t"<<ttsize.num<<")"int p=(*pointer_state);p-=3;char second=state_stackp;int i=getraw(second);int j=getcol('T');char c_out=getraw_content(tableij);cout<<"tr5:用T->T/F规约且" popstack(state_stack,pointer_state,3);popstackc(stack,pointer,3);char c='T'/str(*index);pushstack(stack,pointer,state_stack,pointer_state,c,c_out,1);string s=get_tx(ttsize.num);stack(*pointer).endchar=s;/把保存T/F规约的结果的变量保存至当前字符的终结符 else if(production="r6")string s=stack(*pointer).endchar;/在规约之前记录下要规约的字符所代表的变量 int p=(*pointer_state);p-;char second=state_stackp;int i=getraw(second);int j=getcol('T');char c_out=getraw_content(tableij);cout<<"tttr6:用T->F规约且" popstack(state_stack,pointer_state,1);popstackc(stack,pointer,1);char c='T'/str(*index);pushstack(stack,pointer,state_stack,pointer_state,c,c_out,1);stack(*pointer).endchar=s;/将记录下的变量赋值给规约后的字符else if(production="r7")int po=(*pointer);/用P规约该表达式,有效变量在E的endchar中,需要找到E的位置,即下面的操作 string sf=stackpo.endchar;/对应 F po-=2;string sp=stackpo.endchar;/在规约之前记录下要规约的字符所代表的变量。对应 P tsize+;/新增临时变量 ttsize.num=tsize+1;/下面四个表达式是按照上面的规约式进行的赋值 ttsize.operate=''ttsize.var1=sp;ttsize.var2=sf;cout<<"t("<<ttsize.operate<<","<<ttsize.var1<<","<<ttsize.var2<<",t"<<ttsize.num<<")"int p=(*pointer_state);p-=3;char second=state_stackp;int i=getraw(second);int j=getcol('F');char c_out=getraw_content(tableij);cout<<"tr7:用F->PF规约且" popstack(state_stack,pointer_state,3);popstackc(stack,pointer,3);char c='F'/str(*index);pushstack(stack,pointer,state_stack,pointer_state,c,c_out,1);string s=get_tx(ttsize.num);stack(*pointer).endchar=s;/把保存PF规约的结果的变量保存至当前字符的终结符 else if(production="r8") int p=(*pointer_state);p-;char second=state_stackp;int i=getraw(second);int j=getcol('F');char c_out=getraw_content(tableij);cout<<"tttr8:用F->P规约且" string s=stack(*pointer).endchar;/在规约之前记录下要规约的字符所代表的变量 popstack(state_stack,pointer_state,1);popstackc(stack,pointer,1);char c='F'/str(*index);pushstack(stack,pointer,state_stack,pointer_state,c,c_out,1);stack(*pointer).endchar=s;/将记录下的变量赋值给规约后的字符 else if(production="r9") int p=(*pointer_state);p-=3;char second=state_stackp;int i=getraw(second);int j=getcol('P');char c_out=getraw_content(tableij);cout<<"tttr9:用P->(E)规约且" int po=(*pointer);/用P规约该表达式,有效变量在E的endchar中,需要找到E的位置,即下面的操作 po-;string s=stackpo.endchar;/在规约之前记录下要规约的字符所代表的变量popstack(state_stack,pointer_state,3);popstackc(stack,pointer,3);char c='P'/str(*index);pushstack(stack,pointer,s

    注意事项

    本文(天津理工大学编译原理实验3:语义分析与中间代码生成.doc)为本站会员(豆****)主动上传,淘文阁 - 分享文档赚钱的网站仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知淘文阁 - 分享文档赚钱的网站(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    关于淘文阁 - 版权申诉 - 用户使用规则 - 积分规则 - 联系我们

    本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

    工信部备案号:黑ICP备15003705号 © 2020-2023 www.taowenge.com 淘文阁 

    收起
    展开