各种语言成分的语法及其翻译方案示例_第1页
各种语言成分的语法及其翻译方案示例_第2页
各种语言成分的语法及其翻译方案示例_第3页
各种语言成分的语法及其翻译方案示例_第4页
免费预览已结束,剩余1页可下载查看

下载本文档

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

文档简介

1、各种语言成分的语法及其翻译方案(示例 )1. 普通声明语句的翻译下面是声明语句的文法:P prog id (input , output ) D ; SD D ; D | List : T | proc id D ; SList List 1, id | idT integer | real | array C of T1 | T1 | record DC num C | 声明语句的翻译模式:P prog id (input , output ) offset := 0 D ; SDD ;DD id : T enter (id .name, T.type, offset); offset:=

2、offset + T.width T integer T.type := integer ; T.width := 4T real T.type :=real ; T.width :=8T array num of T1 T.type := array (num .val , T1.type);T.width := num .val×T1.width TT1 T.type := pointer (T1.type); T.width := 42. 嵌套过程中声明语句的翻译嵌套过程声明语句的产生式。P prog id (input , output ) D ; SD D ; D | id

3、 : T | proc id ; D ; S(7.1)嵌套过程声明语句的翻译模式:P prog id (input , output ) MD ;S addwidth (top(tblptr ),top( offset); pop(tblptr );pop(offset)M t := mktable(nil );push(t, tblptr ); push(0, offset)DD1;D2D proc id ; N D 1 ; S t:= top(tblptr );addwidth (t, top(offset);pop(tblptr ); pop(offset); enterproc (to

4、p(tblptr ), id .name,t)D id : T enter(top(tblptr ), id .name, T.type, top(offset); top(offset) := top(offset) + T.width N t:= mktable( top(tblptr ); push(t, tblptr ); push(0, offset)3. 记录的翻译下面是生成记录类型的产生式:T record D end生成记录类型的翻译模式:T record L D end T.type := record(top(tblptr );T.width := top(offset);

5、pop(tblptr ); pop(offset)L t:= mktable(nil ); push(t, tblptr ); push(0, offset)4. 赋值语句的翻译下面是典型的赋值语句文法:S Left := EE E1 + E2 | E1 * E2 | - E1 | (E1 ) | LeftLeft Elist | idElist Elist , E | id E(7.2)赋值语句的翻译模式: SLeft :=E if Left.offset=null then /* Left 是简单变量 id */ gencode(Left.addr ':=' E.addr

6、);elsegencode(Left.addr '' Left.offset ' ' ':=' E.addr ) /* Left 是数组元素 */ E E1+E2 E.addr :=newtemp;gencode(E.addr ':='E1.addr '+'E2.addr ) E(E1) E.addr := E1.addr E Left if Left.offset=null then/* Left 是简单 id */E.addr := Left .addrelse begin/* Left 是数组元素 */E.a

7、ddr :=newtemp;gencode(E.addr ':=' Left .addr ' ' Left .offset '')end Left Elist Left.addr :=newtemp;/* Left 是数组元素, 因此存放基址和位移 */gencode(Left .addr ':=' c(Elist .array );gencode(Left .offset ':=' Elist .addr '*' width (Elist .array ) Left id Left.addr :=

8、id .addr ; Left .offset:= null Elist Elist 1, E t:=newtemp;m:= Elist 1.ndim +1;gencode(t ':=' Elist 1.addr '*' limit (Elist 1.array , m); /* 计算 em-1 ×nm */gencode(t ':=' t '+' E.addr ); /* 计算 + im */ Elist .array := Elist 1.array ; Elist id E Elist .array :=id .a

9、ddr ; Elist .addr := E.addr; Elist .ndim :=15.各种控制结构的翻译5.1 布尔表达式的翻译布尔表达式的文法为: B B1 or M B2 B B1 and M B2 B not B1 B(B1) B E1 relop E2 B true B false M布尔表达式的翻译模式如下所示: B B1 or M B2 backpatch(B1.falselist , M.quad); B B1 and M B2 backpatch(B1.truelist , M.quad); B.truelist := B2.truelist ;B.falselist :

10、= merge(B1.falselist , B2.falselist ) B not B1 B.truelist := B1.falselist ; B.falselist := B1 .truelist B(B1) B.truelist := B1.truelist ; B.falselist := B1.falselist B E1 relop E2 B.truelist :=makelist (nextquad); B.falselist := makelist(nextquad+1);gencode('if' E1.addr relop .opE1.addr '

11、;goto ');gencode('goto ') B true B.truelist := makelist(nextquad); gencode('goto ') B false B.falselist := makelist (nextquad); gencode('goto ') M M.quad := nextquad5.2 常用控制流语句的翻译控制流语句if -then ,if -then -else 和 while -do 的文法为: S if B then S1 S if B then S1 else S2 Swhile

12、B do S1 Sbegin L end SA LL1;S LS(7.9)if -then , if -then -else 和 while -do 语句的翻译模式: S if B then M 1 S1 N else M2 S2 backpatch(B.truelist , M1.quad);backpatch(B.falselist , M 2.quad); N N.nextlist := makelist( nextquad); gencode('goto ') M M.quad := nextquad S if B then M S1 backpatch(B.truel

13、ist , M.quad); Swhile M 1 B do M2 S1 backpatch(S1.nextlist, M1.quad);backpatch(B.truelist ,M2.quad);S.nextlist := B.falselist ; gencode('goto'M1.quad) Sbegin L end S.nextlist:= L.nextlist SA S.nextlist := nil LL 1;MS backpatch(L 1.nextlist , M .quad); L.nextlist := S.nextlist LS L.nextlist :

14、= S.nextlist 5.3 for 循环语句的翻译for 循环语句的文法如下所示:S for id := E1 to E2 step E3 do S1for 循环语句的翻译模式如下所示:S for id := E1 to E2 step E3 do M S1 backpatch(S1.nextlist, M.again, ); gencode( goto-,-, ,M.again); S.nextlist := M.again ;M M.addr := entry(id ); gencode( :=E1.addr, , -, M.addr ); T1:= newtemp;gencode(

15、 :=E2.addr, , -, T1 ); T2:=newtemp; gencode( :=E3.addr, , -, T2 ); q:=nextquad; gencode( goto-,-, ,q+2); M.again :=q+1; gencode( +M.addr, , T2, M.addr ); M.nextlist:=nextquad; gencode( ifM.addr > T1 goto );5.4 repeat语句的翻译repeat 语句的文法如下所示:S repeat S1 until BRepeat 语句的翻译模式如下所示:S repeat M S1until N

16、B backpatch( B.falselist ,M.quad);S.nextlist :=B.truelist M M.quad := nextquadNbackpatch(S1.nextlist , nextquad)6. switch 语句的语法制导翻译switch语句的文法为:S switch (E ) ClistClist case V : S Clist |default: Sswitch语句的翻译模式如下所示:S switch (E) i:=0; Si.nextlist:=0; push Si.nextlist ; push E.addr; push i; q:=0; push

17、 qClist pop q;pop i;pop E.addr ;pop Si .nextlist;S.nextlist :=merge(Si.nextlist , q); push S.nextlist Clist case V :pop q; pop i; i:=i+1; pop E.addr;if nextquad 0 then backpatch(q, nextquad);q:=nextquad;gencode(ifE.addr Vi gotoL i);push E.addr ; push i;push q Spop q; pop i; pop E.addr; pop Si-1 .nex

18、tlist ;p:=nextquad;gencode( goto - ); gencode(L i:);Si.nextlist :=merge(Si.nextlist, p);Si.nextlist :=merge(Si.nextlist, Si-1.nextlist);push Si .nextlist; push E.addr ; push i; push q ClistClist default :pop q; pop i; i:= i+1; pop E.addr ;if nextquad 0 then backpatch( q, nextquad);q:= nextquad;genco

19、de( ifE.addr Vi gotoVi+1);push E.addr ; push i;push q Spop q; pop i; pop E.addr ; pop Si-1 .nextlist;p:= nextquad;gencode(goto -); gencode(L i:);Si.nextlist :=merge(Si.nextlist, p);Si.nextlist :=merge(Si.nextlist, Si-1 .nextlist );push Si.nextlist; push E.addr ; push i; push q7. 过程调用和返回语句的翻译过程调用和返回语

20、句的文法如下所示:S call id (Elist )Elist Elist , E | ES returnE过程调用语句的翻译模式如下所示: Scall id (Elist ) n :=0;repeatn:= n+1;从 queue 的队首取出一个实参地址p;gencode('param', -, -, p);until queue 为空 ;gencode('call', id .addr , n, -) Elist Elist , E 将 E.addr 添加到 queue 的队尾 Elist E 初始化 queue,然后将 E.addr 加入到 queue 的队尾。 过程返回语句的翻译模式为:S return E if需要返回结果then gencode(:=,E.addr, -, F);gencode(ret,-, -, -)其中, F 是存放结果的指定单元,四元式 恢复主调程序的寄存器内容; 释放过程运行时所占用的数据区; 按返回地址返回到主调程序。(ret,-, -, -)

温馨提示

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

评论

0/150

提交评论