天津理工大学编译原理实验3语义分析与中间代码生成_第1页
天津理工大学编译原理实验3语义分析与中间代码生成_第2页
天津理工大学编译原理实验3语义分析与中间代码生成_第3页
天津理工大学编译原理实验3语义分析与中间代码生成_第4页
天津理工大学编译原理实验3语义分析与中间代码生成_第5页
已阅读5页,还剩11页未读 继续免费阅读

下载本文档

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

文档简介

1、实验报告学院(系)名称:计算机与通信工程学院姓名学号专业班级实验项目实验三:语义分析与中间代码生成课程名称编译原理课程代码实验时间实验地点计算机软件实验室7-220计算机软件实验室7-215批改意见成绩教师签字: 实验内容:可选择LL1分析法、算符优先分析法、LR分析法之一,实现如下表达式文法的语法制导翻译过程。文法GE如下所示:EE+T | E-T | TTT*F | T/F | FFPF | PP(E) | i要求构造出符合语义分析要求的属性文法描述,并在完成实验二(语法分析)的基础上,进行语义分析程序设计,最终输出与测试用例等价的四元式中间代码序列。实验目的:1掌握语法制导翻译的基本功能

2、。2巩固对语义分析的基本功能和原理的认识。3能够基于语法制导翻译的知识进行语义分析。4掌握类高级语言中基本语句所对应的语义动作。5理解并处理语义分析中的异常和错误。实验要求:1在实验二的基础上,实现语法制导翻译功能,输出翻译后所得四元式序列;2要求详细描述所选分析方法进行制导翻译的设计过程;3完成对所设计分析器的功能测试,并给出测试数据和实验结果;4为增加程序可读性,请在程序中进行适当注释说明;5整理上机步骤,总结经验和体会;6认真完成并按时提交实验报告。【实验过程记录(源程序、测试用例、测试结果及心得体会等)】#include<iostream>#include<cstri

3、ng>#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, 也可以是

4、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"

5、,/* 1 */ "s7", "s8","err","err","err","err","acc","err","err","err","err","err","err",/* 2 */ "r3", "r3", "s9","s10","err&quo

6、t;, "r3", "r3","err","err","err","err","err","err",/* 3 */ "r6", "r6", "r6", "r6","err", "r6", "r6","err","err","err",&q

7、uot;err","err","err",/* 4 */ "r8", "r8", "r8", "r8","s11", "r8", "r8","err","err","err","err","err","err",/* 5 */ "err","err"

8、,"err","err","err","err","err", "s5", "s6", "c", "2", "3", "4",/* 6 */ "r10","r10","r10","r10","r10","r10","r10","e

9、rr","err","err","err","err","err",/* 7 */ "err","err","err","err","err","err","err", "s5", "s6","err", "d", "3", "4"

10、;,/* 8 */ "err","err","err","err","err","err","err", "s5", "s6","err", "e", "3", "4",/* 9 */ "err","err","err","err","err&quo

11、t;,"err","err", "s5", "s6","err","err", "f", "4",/* 10*/ "err","err","err","err","err","err","err", "s5", "s6","err",&qu

12、ot;err", "g", "4",/* 11*/ "err","err","err","err","err","err","err", "s5", "s6","err","err", "h", "4",/* 12*/ "s7", "s8",&quo

13、t;err","err","err","s18","err","err","err","err","err","err","err",/* 13*/ "r1", "r1", "s9","s10","err", "r1", "r1","err

14、","err","err","err","err","err",/* 14*/ "r2", "r2", "s9","s10","err", "r2", "r2","err","err","err","err","err","err"

15、;,/* 15*/ "r4", "r4", "r4", "r4","err", "r4", "r4","err","err","err","err","err","err",/* 16*/ "r5", "r5", "r5", "r5","err&qu

16、ot;, "r5", "r5","err","err","err","err","err","err",/* 17*/ "r7", "r7", "r7", "r7","err", "r7", "r7","err","err","err",&

17、quot;err","err","err",/* 18*/ "r9", "r9", "r9", "r9", "r9", "r9", "r9","err","err","err","err","err","err"int getLength(char strsize)int i=0;whil

18、e(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 p

19、opstack(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

20、,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(*poi

21、nter).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 '(':retu

22、rn 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':

23、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;cas

24、e '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;

25、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(st

26、r="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"c

27、ase 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

28、"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;vo

29、id 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<

30、;<"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<<"Er

31、ror! 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_s

32、tack,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&q

33、uot;) 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,poi

34、nter,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;strin

35、g 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<<&qu

36、ot;,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=&

37、#39;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;str

38、ing 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<<&

39、quot;,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

40、='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_

41、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(*point

42、er).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='*'

43、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=g

44、etcol('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).end

45、char=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='/

46、9;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

47、=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).e

48、ndchar=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规约且"

49、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

50、 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<<",

51、"<<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);

52、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(se

53、cond);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&q

温馨提示

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

评论

0/150

提交评论