资源描述:
《编译原理 中间代码生成.ppt》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、中间代码生成1编译器的前端与后端23语法树,DAG后缀表达式可以看做语法树的一种线性表示abcuminus*bcuminus*+assign4语法树能通过语法制导定义(SDD)生成.E.g:56三地址代码形如:x:=yopz;其它形式的三地址语句7翻译时生成临时变量:x+y*zt1:=y*zt2:=x+t1DAG8表达式的三地址代码9newtemp()生成临时变量:t1,t2,…gen()生成中间代码10生成四元式11E.g.关于数组x[i]:=y或者x:=y[i]三元式12间接三元式13过程声明14Integershavewidth=4andrealshave
2、thewidth=815跟踪记录过程中变量的作用域PDDD;D
3、id:T
4、procid;D;S16嵌套过程的符号表exchangereadarrayxaheaderNillsortquicksorttoreadarraypartitionvkheaderquicksortreadarraryiheaderexchangeheadertoexchangepartition17过程:mktable(previous)-createanewsymboltableandreturnapointertothenewtable(placedintheheader)ent
5、er(table,name,type,offset)–createanewentrynameaddwidth(table,width)enterproc(table,name,newtable)–createanewentryfortheprocedurenameinthesymboltablepointedbytable18Foreachproceduregeneratedbythisgrammar,thetranslationschemesetsupaseparatesymboltable1920记录类型TrecordDendTrecordLDend{T
6、.type:=record(top(tblptr));T.width:=top(offset);pop(tblptr);pop(offset)}L{t:=mktable(nil);push(t,tblprt);push(0,offset)}21赋值语句22lookup()procedureismodified:23Bymodifyingthelookupprocedure,wecanapplythetranslationschemeifthemostcloselynestedscoperuleappliestononlocalnames(likePascal
7、)–Fig.8.1524e.g.pp479,lastparagraph25数组的地址计算ArraysarestorednormallyinblockofcontiguouslocationsA1-DarrayA[low…high],withelementsofsizew,theithelementA[i]beginatlocation:base+(i-low)w=iw+(base–loww)=iw+cA2-Darrayisnormallystoredrow-major(row-by-row)orcolumnmajor(column-by-column).
8、26Forrow-majorform,A[i1,i2]isstoredat:Somelanguagesgivethepossibilitytospecifythesizeofthearraydynamically,whenaprocedureiscalledrun-time(seesect.7.3,pp.408).Theformulasaboveremainvalid,buttheupperandlowerlimitsarenotknownatcompiletime.Generalizationsforrow-majorformforA[i1,i2,..,ik]
9、((…((i1n2+i2)n3+i3)…)nk+ik)w+base–((…((low1n2+low2)n3+low3)…)nk+lowk)wInordertogeneratecodeforarrayreferencewemustdoaconnectionofthecomputationabovewiththegrammarforarrayreference27Thekeyproblemingeneratingcodeforarrayreferences:relatethecomputationbelowtoagrammarforarrayreferences
10、Apointertoth