编译原理 中间代码生成.ppt
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_05.gif)
《编译原理 中间代码生成.ppt》由会员分享,可在线阅读,更多相关《编译原理 中间代码生成.ppt(53页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、中间代码生成1编译器的前端与后端 2 3语法树,DAG 后缀表达式可以看做语法树的一种线性表示 a b c uminus*b c uminus*+assign 4语法树能通过语法制导定义(SDD)生成.E.g:56三地址代码三地址代码形如:x:=y op z ;其它形式的三地址语句 7翻译时生成临时变量:x+y*z t1:=y*zt2:=x+t1 DAG8表达式的三地址代码表达式的三地址代码9newtemp()生成临时变量:t1,t2,gen()生成中间代码 10生成四元式生成四元式11E.g.关于数组 xi:=y 或者 x:=yi三元式12间接三元式13过程声明14Integers have
2、 width=4 and reals have the width=815跟踪记录过程中变量的作用域P DD D;D|id:T|proc id;D;S16嵌套过程的符号表嵌套过程的符号表exchangereadarrayxaheaderNill sortquicksortto readarraypartitionvkheaderquicksortreadarraryiheaderexchangeheaderto exchangepartition17过程:mktable(previous)-create a new symbol table and return a pointer to th
3、e new table(placed in the header)enter(table,name,type,offset)create a new entry name addwidth(table,width)enterproc(table,name,newtable)create a new entry for the procedure name in the symbol table pointed by table 18For each procedure generated by this grammar,the translation scheme sets up a sepa
4、rate symbol table 1920记录类型记录类型T record D endT record L D endT.type:=record(top(tblptr);T.width:=top(offset);pop(tblptr);pop(offset)L t:=mktable(nil);push(t,tblprt);push(0,offset)21赋值语句赋值语句22lookup()procedure is modified:23By modifying the lookup procedure,we can apply the translation scheme if the m
5、ost closely nested scope rule applies to nonlocal names(like Pascal)Fig.8.1524e.g.pp 479,last paragraph25数组的地址计算Arrays are stored normally in block of contiguous locations A 1-D array Alowhigh,with elements of size w,the ith element Ai begin at location:base+(i-low)w=i w+(base low w)=i w+cA 2-D arra
6、y is normally stored row-major(row-by-row)or column major(column-by-column).26For row-major form,Ai1,i2 is stored at:Some languages give the possibility to specify the size of the array dynamically,when a procedure is called run-time(see sect.7.3,pp.408).The formulas above remain valid,but the upper
7、 and lower limits are not known at compile time.Generalizations for row-major form for Ai1,i2,.,ik (i1n2+i2)n3+i3)nk+ik)w+base(low1n2+low2)n3+low3)nk+lowk)w In order to generate code for array reference we must do a connection of the computation above with the grammar for array reference 27 The key
8、problem in generating code for array references:relate the computation below to a grammar for array referencesA pointer to the symbol-table for array name is passed as synthesized attribute array of Elist28类型转换参与运算的数据类型必须一致 2930布尔表达式布尔表达式 E E or E|E and E|not E|(E)|id relop id|true|falserelop ,3132布
9、尔表达式的SDT:3334控制流语句文法:353637Control-Flow Translation of Boolean Expressions(E.code)E is translate into TAS that evaluate the E as sequence of conditional and unconditional jumps to one of the two locations:E.true and E.false The basic idea for translation:Remember(pp.468):E.place keep the name that h
10、old the value of E E.code keep the sequence of TAS that evaluate E 38(syntax directed definition to produce TAS code for Booleans)39例子:40Revisited:E E1+E2E.type:=arith;if E1.type=arith and E2.type=arith thenbegin /*normal arithmetic add*/E.place:=newtemp;E.code:=E1.code|E2.code|gen(E.place:=E1.place
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 编译原理 中间代码生成 编译 原理 中间 代码 生成
![提示](https://www.taowenge.com/images/bang_tan.gif)
限制150内