声明语句的翻译教学课件.ppt

上传人:豆**** 文档编号:61825790 上传时间:2022-11-21 格式:PPT 页数:35 大小:777.50KB
返回 下载 相关 举报
声明语句的翻译教学课件.ppt_第1页
第1页 / 共35页
声明语句的翻译教学课件.ppt_第2页
第2页 / 共35页
点击查看更多>>
资源描述

《声明语句的翻译教学课件.ppt》由会员分享,可在线阅读,更多相关《声明语句的翻译教学课件.ppt(35页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。

1、声明语句的翻译教学课件 Still waters run deep.流静水深流静水深,人静心深人静心深 Where there is life,there is hope。有生命必有希望。有生命必有希望24.4 4.4 声明语句的翻译声明语句的翻译声明语句的翻译声明语句的翻译3.作用域信息的保存过程的作用域与程序块类似,在允许嵌套定义过程的程序设计语言中,相同的名字可以同时出现在不同的作用域中,因此有必要讨论如何设计符号表来存放它们。此处讨论的过程作用域,同样遵守静态作用域和最近嵌套原则。34.4 4.4 声明语句的翻译声明语句的翻译声明语句的翻译声明语句的翻译定义定义4.2(嵌套深度)设主程

2、序(最外层过程)的嵌套深度dmain=1,则若过程A直接嵌套定义过程B,则dB=dA+1;变量声明时所在过程的嵌套深度,被认为是该变量的嵌套深度。与程序块相比,有两点不同,使得过程声明的处理复杂:过程头是一个名字,可象引用变量一样被调用程序块的执行与静态一致,而过程不一致。1voidmain()2inta=0,b=0;/B03intb=1;/B14inta=2,c=4,d=5;/B27intb=3;/B31112voidswap(int&x,int&y)inttemp;temp=x;x=y;y=temp;4例4.14快排序的Pascal程序:programsort(input,output);

3、vara:array0.10ofinteger;x:integer;procedurereadarray;vari:integer;beginfori:=1to9doread(ai)endreadarray;procedureexchange(i,j:integer);beginx:=ai;ai:=aj;aj:=x;endexchange;procedurequicksort(m,n:integer);vari,v:integer;functionpartition(y,z:integer):integer;vari,j:integer;begin.a.;.v.;.exchange(i,j);

4、.endpartition;beginif(nm)thenbegini:=partition(m,n);quicksort(m,i-1);quicksort(i+1,n)end;endquicksort;begina0:=-9999;a10:=9999;readarray;quicksort(1,9)endsort.过程过程变量变量 深度深度sorta,x1readarrayi2exchange2quicksorti,v2partitioni,j354.4 4.4 声明语句的翻译声明语句的翻译声明语句的翻译声明语句的翻译嵌套关系的树形表示F过程定义为节点F节点a是节点b的父亲,当且仅当过程b直

5、接嵌套在过程a中过程过程变量变量 深度深度sorta,x1readarrayi2exchange2quicksorti,v2partitioni,j364.4 4.4 声明语句的翻译声明语句的翻译声明语句的翻译声明语句的翻译符号表中的作用域信息F过程定义的文法(忽略了参数)PD(1)DD;D(2)|id:T(3)|procid;D;S(4)过程是一个声明语句可以声明若干个过程(变量)变量声明,T是type过程定义,S是可执行语句T是类型(整型,实型等)细节忽略S是一个可执行语句(赋值、控制语句等)细节忽略74.4 4.4 声明语句的翻译声明语句的翻译声明语句的翻译声明语句的翻译嵌套过程中的名字

6、作用域信息,使用有嵌套结构的符号表保存。每个过程被认为是一个子符号表,或者是符号表中的一个节点。嵌套的节点之间用双向链连接,正向链指示过程的嵌套关系,逆向链实现按作用域对名字进行访问。快速排序沿着逆向链访问不到的名字,作用域不相交84.4 4.4 声明语句的翻译声明语句的翻译声明语句的翻译声明语句的翻译F在过程声明时要做的工作之一是在(语法)分析的过程中逐步生成符号表,并将正确的内容填写进符号表的相应栏目旧文法:PD(1)DD;D(2)|id:T(3)|procid;D;S(4)修改文法,在定义D之前生成符号表(LR分析)PMD(1)DD;D(2)|id:T(3)|procid;ND;S(4)

7、M(5)N(6)94.4 4.4 声明语句的翻译声明语句的翻译声明语句的翻译声明语句的翻译b)全程量、属性与语义函数1.全程量:有序对栈(tblptr,offset)(符号表节点的指针,符号表节点所需宽度)2.栈上的操作:push(t,o)、pop、top(stack)3.语义函数与过程:函数mktable(previous):建立一个新的符号表,返回指向符号表的指针。previous是逆向链,指向前一符号表(外层)。过程enter(table,name,type,offset):在table指向的节点中为名字name建立新的条目,包括名字的类型和存储位置等。过程addwidth(table,

8、width):计算table节点中所有条目的累加宽度,并记录在table的头部信息中。过程enterproc(table,name,newtable):为过程name在table指向的节点中建立一个新的条目。参数newtable是正向链,指向name过程自身的符号表节点。104.4 4.4 声明语句的翻译声明语句的翻译声明语句的翻译声明语句的翻译c)语义规则(1)PMDaddwidth(top(tblptr),top(offset);pop;(2)Mt:=mktable(null);push(t,0,);(3)DD;D(4)Did:Tenter(top(tblptr),id.name,T.ty

9、pe,top(offset);top(offset):=top(offset)+T.width;(5)Dprocid;ND1;St:=top(tblptr);addwidth(t,top(offset);pop;enterproc(top(tblptr),id.name,t);(6)Nt:=mktable(top(tblptr);push(t,0);114.4 4.4 声明语句的翻译声明语句的翻译声明语句的翻译声明语句的翻译d)语法制导翻译的过程procsort;a:array10ofint;x:int;procreadarry;i:int;read(a);readarrayPMD(1)DD;

10、D(2)|id:T(3)|procid;ND;S(4)M(5)N(6)t1t2t312(1)M1t1:=mktable(null);push(t1,0);nullt1 0t113(1)M1t1:=mktable(null);push(t1,0);(2)N1t2:=mktable(top(tblptr);push(t2,0);nullt1 0t2 0t1t1t214(1)M1t1:=mktable(null);push(t1,0);(2)N1t2:=mktable(top(tblptr);push(t2,0);(3)T1intT1.type=integer,T1.width=4nullt1 0t

11、2 0t1t1t215(1)M1t1:=mktable(null);push(t1,0);(2)N1t2:=mktable(top(tblptr);push(t2,0);(3)T1intT1.type=integer,T1.width=4(4)T2array10ofT1T2.type=array(10,int),T2.width=40nullt1 0t2 0t1t1t216(1)M1t1:=mktable(null);push(t1,0);(2)N1t2:=mktable(top(tblptr);push(t2,0);(3)T1intT1.type=integer,T1.width=4(4)T

12、2array10ofT1T2.type=array(10,int),T2.width=40(5)D1a:T2(a,arr,0)填进t2,top(offset):=40nullt1 0t2 0aarr,0t1t1t2t2 4017(1)M1t1:=mktable(null);push(t1,0);(2)N1t2:=mktable(top(tblptr);push(t2,0);(3)T1intT1.type=integer,T1.width=4(4)T2array10ofT1T2.type=array(10,int),T2.width=40(5)D1a:T2(a,arr,0)填进t2所指节点,to

13、p(offset):=40(6)T3intT2.type=integer,T2.width=4nullt1 0t2 40aarr,0t1t1t218(1)M1t1:=mktable(null);push(t1,0);(2)N1t2:=mktable(top(tblptr);push(t2,0);(3)T1intT1.type=integer,T1.width=4(4)T2array10ofT1T2.type=array(10,int),T2.width=40(5)D1a:T2(a,arr,0)填进t2所指节点,top(offset):=40(6)T3intT2.type=integer,T2.

14、width=4(7)D2x:T3(x,int,40)填进t2所指节点,top(offset):=44nullt1 0t2 40aarr,0 xInt,40t1t1t2t2 4419(1)M1t1:=mktable(null);push(t1,0);(2)N1t2:=mktable(top(tblptr);push(t2,0);(3)T1intT1.type=integer,T1.width=4(4)T2array10ofT1T2.type=array(10,int),T2.width=40(5)D1a:T2(a,arr,0)填进t2所指节点,top(offset):=40(6)T3intT2.

15、type=integer,T2.width=4(7)D2x:T3(x,int,40)填进t2所指节点,top(offset):=44(8)N2t3:=mktable(top(tblptr);push(t3,0);nullt1 0t2 44t1t2t3t3 0aarr,0 xInt,40t1t2(1)M1t1:=mktable(null);push(t1,0);(2)N1t2:=mktable(top(tblptr);push(t2,0);(3)T1intT1.type=integer,T1.width=4(4)T2array10ofT1T2.type=array(10,int),T2.widt

16、h=40(5)D1a:T2(a,arr,0)填进t2所指节点,top(offset):=40(6)T3intT2.type=integer,T2.width=4(7)D2x:T3(x,int,40)填进t2所指节点,top(offset):=44(8)N2t3:=mktable(top(tblptr);push(t3,0);(9)T4intT4.type=integer,T4.width=420nullt1 0t2 44t1t2t3t3 0aarr,0 xInt,40t1t221(1)M1t1:=mktable(null);push(t1,0);(2)N1t2:=mktable(top(tbl

17、ptr);push(t2,0);(3)T1intT1.type=integer,T1.width=4(4)T2array10ofT1T2.type=array(10,int),T2.width=40(5)D1a:T2(a,arr,0)填进t2所指节点,top(offset):=40(6)T3intT2.type=integer,T2.width=4(7)D2x:T3(x,int,40)填进t2所指节点,top(offset):=44(8)N2t3:=mktable(top(tblptr);push(t3,0);(9)T4intT4.type=integer,T4.width=4(10)D3i:

18、T4(i,int,0)填进t3所指节点,top(offset):=4nullt1 0t2 44aarr,0 xInt,40t1t1t2iint,0t2t3t3 4t3 022(1)M1t1:=mktable(null);push(t1,0);(2)N1t2:=mktable(top(tblptr);push(t2,0);(3)T1intT1.type=integer,T1.width=4(4)T2array10ofT1T2.type=array(10,int),T2.width=40(5)D1a:T2(a,arr,0)填进t2所指节点,top(offset):=40(6)T3intT2.typ

19、e=integer,T2.width=4(7)D2x:T3(x,int,40)填进t2所指节点,top(offset):=44(8)N2t3:=mktable(top(tblptr);push(t3,0);(9)T4intT4.type=integer,T4.width=4(10)D3i:T4(i,int,0)填进t3所指节点,top(offset):=4(11)D4procreadarrayN2D3;St:=top(tblptr);addwidth(t,top(offset);pop;enterproc(top(tblptr),readarray,t);nullt1 0t2 44aarr,0

20、 xInt,40t1t1t24iint,0t2t3t3 423(1)M1t1:=mktable(null);push(t1,0);(2)N1t2:=mktable(top(tblptr);push(t2,0);(3)T1intT1.type=integer,T1.width=4(4)T2array10ofT1T2.type=array(10,int),T2.width=40(5)D1a:T2(a,arr,0)填进t2所指节点,top(offset):=40(6)T3intT2.type=integer,T2.width=4(7)D2x:T3(x,int,40)填进t2所指节点,top(offs

21、et):=44(8)N2t3:=mktable(top(tblptr);push(t3,0);(9)T4intT4.type=integer,T4.width=4(10)D3i:T4(i,int,0)填进t3所指节点,top(offset):=4(11)D4procreadarrayN2D3;St:=top(tblptr);addwidth(t,top(offset);pop;enterproc(top(tblptr),readarray,t);nullt1 0t2 44aarr,0 xInt,40t1t1t24iint,0t2t324(1)M1t1:=mktable(null);push(t

22、1,0);(2)N1t2:=mktable(top(tblptr);push(t2,0);(3)T1intT1.type=integer,T1.width=4(4)T2array10ofT1T2.type=array(10,int),T2.width=40(5)D1a:T2(a,arr,0)填进t2所指节点,top(offset):=40(6)T3intT2.type=integer,T2.width=4(7)D2x:T3(x,int,40)填进t2所指节点,top(offset):=44(8)N2t3:=mktable(top(tblptr);push(t3,0);(9)T4intT4.ty

23、pe=integer,T4.width=4(10)D3i:T4(i,int,0)填进t3所指节点,top(offset):=4(11)D4procreadarrayN2D3;St:=top(tblptr);addwidth(t,top(offset);pop;enterproc(top(tblptr),readarray,t);nullt1 0t2 44aarr,0 xInt,40readarrayt3t1t1t24iint,0t2t325(1)M1t1:=mktable(null);push(t1,0);(2)N1t2:=mktable(top(tblptr);push(t2,0);(3)T

24、1intT1.type=integer,T1.width=4(4)T2array10ofT1T2.type=array(10,int),T2.width=40(5)D1a:T2(a,arr,0)填进t2所指节点,top(offset):=40(6)T3intT2.type=integer,T2.width=4(7)D2x:T3(x,int,40)填进t2所指节点,top(offset):=44(8)N2t3:=mktable(top(tblptr);push(t3,0);(9)T4intT4.type=integer,T4.width=4(10)D3i:T4(i,int,0)填进t3所指节点,

25、top(offset):=4(11)D4procreadarrayN2D3;St:=top(tblptr);addwidth(t,top(offset);pop;enterproc(top(tblptr),readarray,t);(14)D7procsortN1D6;St:=top(tblptr);addwidth(t,top(offset);pop;enterproc(top(tblptr),sort,t);nullt1 0t2 4444aarr,0 xInt,40readarrayt3t1t1t24iint,0t2t326(1)M1t1:=mktable(null);push(t1,0)

26、;(2)N1t2:=mktable(top(tblptr);push(t2,0);(3)T1intT1.type=integer,T1.width=4(4)T2array10ofT1T2.type=array(10,int),T2.width=40(5)D1a:T2(a,arr,0)填进t2所指节点,top(offset):=40(6)T3intT2.type=integer,T2.width=4(7)D2x:T3(x,int,40)填进t2所指节点,top(offset):=44(8)N2t3:=mktable(top(tblptr);push(t3,0);(9)T4intT4.type=i

27、nteger,T4.width=4(10)D3i:T4(i,int,0)填进t3所指节点,top(offset):=4(11)D4procreadarrayN2D3;St:=top(tblptr);addwidth(t,top(offset);pop;enterproc(top(tblptr),readarray,t);(14)D7procsortN1D6;St:=top(tblptr);addwidth(t,top(offset);pop;enterproc(top(tblptr),sort,t);nullt1 044aarr,0 xInt,40readarrayt3t1t1t24iint,

28、0t2t327(1)M1t1:=mktable(null);push(t1,0);(2)N1t2:=mktable(top(tblptr);push(t2,0);(3)T1intT1.type=integer,T1.width=4(4)T2array10ofT1T2.type=array(10,int),T2.width=40(5)D1a:T2(a,arr,0)填进t2所指节点,top(offset):=40(6)T3intT2.type=integer,T2.width=4(7)D2x:T3(x,int,40)填进t2所指节点,top(offset):=44(8)N2t3:=mktable(

29、top(tblptr);push(t3,0);(9)T4intT4.type=integer,T4.width=4(10)D3i:T4(i,int,0)填进t3所指节点,top(offset):=4(11)D4procreadarrayN2D3;St:=top(tblptr);addwidth(t,top(offset);pop;enterproc(top(tblptr),readarray,t);(14)D7procsortN1D6;St:=top(tblptr);addwidth(t,top(offset);pop;enterproc(top(tblptr),sort,t);sortt2n

30、ullt1 0t14iint,0t2t344aarr,0 xInt,40readarrayt3t1t228(1)M1t1:=mktable(null);push(t1,0);(2)N1t2:=mktable(top(tblptr);push(t2,0);(3)T1intT1.type=integer,T1.width=4(4)T2array10ofT1T2.type=array(10,int),T2.width=40(5)D1a:T2(a,arr,0)填进t2所指节点,top(offset):=40(6)T3intT2.type=integer,T2.width=4(7)D2x:T3(x,in

31、t,40)填进t2所指节点,top(offset):=44(8)N2t3:=mktable(top(tblptr);push(t3,0);(9)T4intT4.type=integer,T4.width=4(10)D3i:T4(i,int,0)填进t3所指节点,top(offset):=4(11)D4procreadarrayN2D3;St:=top(tblptr);addwidth(t,top(offset);pop;enterproc(top(tblptr),readarray,t);(14)D7procsortN1D6;St:=top(tblptr);addwidth(t,top(off

32、set);pop;enterproc(top(tblptr),sort,t);(15)PM1D7addwidth(top(tblptr),top(offset);pop;0sortt2nullt1 0t14iint,0t2t344aarr,0 xInt,40readarrayt3t1t229(1)M1t1:=mktable(null);push(t1,0);(2)N1t2:=mktable(top(tblptr);push(t2,0);(3)T1intT1.type=integer,T1.width=4(4)T2array10ofT1T2.type=array(10,int),T2.width

33、=40(5)D1a:T2(a,arr,0)填进t2所指节点,top(offset):=40(6)T3intT2.type=integer,T2.width=4(7)D2x:T3(x,int,40)填进t2所指节点,top(offset):=44(8)N2t3:=mktable(top(tblptr);push(t3,0);(9)T4intT4.type=integer,T4.width=4(10)D3i:T4(i,int,0)填进t3所指节点,top(offset):=4(11)D4procreadarrayN2D3;St:=top(tblptr);addwidth(t,top(offset)

34、;pop;enterproc(top(tblptr),readarray,t);(14)D7procsortN1D6;St:=top(tblptr);addwidth(t,top(offset);pop;enterproc(top(tblptr),sort,t);(15)PM1D7addwidth(top(tblptr),top(offset);pop;0sortt2nullt14iint,0t2t344aarr,0 xInt,40readarrayt3t1t2304.5 4.5 简单算术表达式与赋值句简单算术表达式与赋值句简单算术表达式与赋值句简单算术表达式与赋值句 讨论所基于的文法:Aid

35、:=EEE+E|E*E|-E|(E)|idF简单变量的语法制导翻译简单变量的语法制导翻译属性.place:存放E的变量名地址(符号表中地址或临时变量)过程emit():生成result:=arg1oparg2的三地址码。(1)Aid:=Eemit(entry(id.name):=E.place)(2)EE1+E2 E.place:=newtemp;emit(E.place:=E1.place+E2.place)(3)EE1*E2 E.place:=newtemp;emit(E.place:=E1.place*E2.place)(4)E-E1E.place:=newtemp;emit(E.pla

36、ce:=-E1.place)(5)E(E1)E.place:=E1.place(6)EidE.place:=entry(id.name)314.5 4.5 简单算术表达式与赋值句简单算术表达式与赋值句简单算术表达式与赋值句简单算术表达式与赋值句 F变量的变量的(内部内部)类型转换类型转换强制(coercion):按照一定的原则,将不同类型的变量在内部转换为相同的类型,然后进行同类型变量的计算。属性.mode:取值int或real表达式EE1opE2的类型判定树:运算的转换原则赋值的转换原则 324.5 4.5 简单算术表达式与赋值句简单算术表达式与赋值句简单算术表达式与赋值句简单算术表达式与赋

37、值句 三地址码:T:=itrE:将E从整型变为实型,结果存放T中T:=rtiE:将E从实型变为整型,结果存放T中语义规则(加入类型转换之后):Aid:=Etmode:=entry(id.name).mode;iftmode=E.modethenemit(entry(id.name):=E.place);elseT:=newtemp;iftmode=intthenemit(T:=rtiE.place);elseemit(T:=itrE.place);endif;emit(entry(id.name):=T);endif;334.5 4.5 简单算术表达式与赋值句简单算术表达式与赋值句简单算术表达

38、式与赋值句简单算术表达式与赋值句 EE1opE2 T:=newtemp;E.mode:=real;ifE1.mode=intthen ifE2.mode=intthenemit(T:=E1.placeOPiE2.place);E.mode:=int;elseU:=newtemp;emit(U:=itrE1.place);emit(T:=UOPrE2.place);endif;else ifE2.mode=intthenU:=newtemp;emit(U:=itrE2.place);emit(T:=E1.placeOPrU);elseemit(T:=E1.placeOPrE2.place);en

39、dif;endif;E.place:=T;其他语义规则看教材197344.5 4.5 简单算术表达式与赋值句简单算术表达式与赋值句简单算术表达式与赋值句简单算术表达式与赋值句 例4.17x:=-a*b+c的语法制导翻译,x、a、b是整型数,c是实型数。序号产生式 中间代码(1)E1a(2)E2-E1 t1:=-a(3)E3b(4)E4E2*E3t2:=t1*ib(5)E5c(6)E6E4+E5t4:=itrt2t3:=t4+rc(7)Ax:=E6t5:=rtit3x:=t5.int.int.int.int.int.int.real.real.int(itor.int(itor).real(rtoi).real(rtoi)FP241:4.5,4.735

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 小学资料

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

工信部备案号:黑ICP备15003705号-8 |  经营许可证:黑B2-20190332号 |   黑公网安备:91230400333293403D

© 2020-2023 www.deliwenku.com 得利文库. All Rights Reserved 黑龙江转换宝科技有限公司 

黑龙江省互联网违法和不良信息举报
举报电话:0468-3380021 邮箱:hgswwxb@163.com