第19讲 控制语句的翻译

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

(L1,Lnext) S
S ( -, L1)
E(L2, Lnext) E (L3, L4)
S( -, L1) E
EE
视频区域S( -, L1) E
EE
while a < b do if c < d then x := y + z else x := y - z
if a<b goto L2 goto Lnext if c<d goto L3 goto L4 L3: T1 :=y+z x:=T1 goto L1 L4: T2 :=y-z x:=T2
E.true: E.false: S.next
E.code
S1.code goto S.next
S2.code
……
To E.true To E.false
视频区域
if 语句的文法
相关产生式 S → if E then S1 S → if E then S1 else S2
改写后的产生式 S → if E then M S1 S → if E then M1 S1 N else M2 S2 M→ε N→ε
while-do语句的翻译模式
1. S → while M1 E do M2 S1 { backpatch(E.truelist, M2.quad);
backpatch(S1.nextlist, M1.quad); S.nextlist := E.falselist; emit(‘j,-,-,’ M1.quad) }
‘:’)
视频区域
|| E.code
||
gen(E.true gen(‘goto’
S‘:’.)b|e| gSin1.)code
||
编译原理
控制语句的属性计算示例
根据属性文法翻译控制语句
根据属性文法翻译如下语句 : while a<b do if c<d then x:=y+z else x:=y-z
2. M → ε { M.quad := nextquad }
视频区域
复合语句的文法
相关产生式 S → begin L end L→L; S | S
改写后的产生式 S → begin L end L → L1; M S | S
M→ε
视频区域
复合语句的翻译模式
1. S → begin L end { S.nextlist := L.nextlist }
EMS→→→SeSMbeEAεEema..1mnn1mc..+i{q{feetkia(tEiuxxMSpt(‘lj(‘stta2,aj.‘ll.-’jneiidq,tssercel)ue-ttim,h;s::x-al=={ot(ti,:dStEmlp=i(,E-s:’E1..=meft..oMa:p.nnr,=ppaglle1e0seamlk.eax‘xe’qlc,e)s(ct’atlueEllqie}ieiiksdsa:su.=etftetd‘1aa,(:nlr.)=indlpsrese’o}tlwe}(aExrlct)ti1eqs}e.}ptmu,‘la,aSp’dc;i1d+e.n12‘e.)+p;x’lwtaElicsh2et.)ip‘l,l}e’acae‘0<)’})b; do if c<d then x:=y+z;
……
To E.true To E.false
视频区域
if-then-else语句的属性文法
产生式
S→if E then S1 else S2
语义规则
E.true:=newlabel;
E.false:=newlabel;
SSS.12c..nnoeedxxett:::===ESS...cnnoeedxxett; ||
S( -, L1) E
视频区域S( -, L1) E
E(L2, Lnext) E (L3, L4)
EE
EE
while a < b do if c < d then x := y + z else x := y - z
产生式 S→id:=E
E→E1+E2
语义规则 S.code:=E.code || gen(id.place ‘:=’ E.place)
(L1,Lnext) S
S ( -, L1)
E(L2, Lnext) E (L3, L4)
S( -, L1) E
EE
视频区域S( -, L1) E
EE
while a < b do if c < d then x := y + z else x := y - z
L1: if a<b goto L2 goto Lnext
(L1,Lnext) S
L2: if c<d goto L3 goto L4
L3: T1 :=y+z x:=T1 goto L1
L4: T2 :=y-z x:=T2 goto L1
E(L2, Lnext)
S ( -, L1)
E (L3, L4)
S( -, L1) E
EE
while a < b do if c < d then x := y + z
编译原理
控制语句的翻译
编译程序总框
源程序
词法分析器
单词符号

语法分析器


语法单位

语义分析与中间代码生成器

中间代码(四元式)
优化段
中间代码(四元式)
目标代码生成器
目标代码
出 视频区域 错 处 理
常用的控制语句
S → if E then S1 S → if E then S1 else S2 S → while E do S1 其中E为布尔表达式
视频区域
编译原理
if语句的属性文法
if-then语句的语义
S → if E then S1
E.true: E.false:
E.code S1.code
……
To E.true To E.false
视频区域
if-then语句的属性文法
产生式
S→if E then S1
语义规则
E.true:=newlabel; E.flase:=S.next; SS.1c.noedxet::==ES..cnoedxet || gen(E.true ‘:’) |视| S频1区.c域ode
E(L2, Lnext) E (L3, L4)
EE
EE
while a < b do if c < d then x := y + z else x := y - z
if a<b goto L2 goto Lnext if c<d goto L3 goto L4
(L1,Lnext) S
S ( -, L1)
E.place:=newtemp; E.code:=E1.code || E2.code || gen(E.place ‘:=’ E1.place ‘+’ E2.place)
if a<b goto L2 goto Lnext if c<d goto L3 goto L4 T1 :=y+z x:=T1 goto L1 T2 :=y-z x:=T2
视频区域
S表示语句,L表示语句表,A为赋值语句,E表 示布尔表达式
if 语句的文法
相关产生式 S → if E then S1 S → if E then S1 else S2
Leabharlann Baidu
视频区域
if 语句的文法
相关产生式 S → if E then S1 S → if E then S1 else S2
L3: T1 :=y+z x:=T1 goto L1
L4: T2 :=y-z x:=T2 goto L1
编译原理
一遍扫描翻译控制语句
控制语句的文法
(1) S → if E then S (2) S → if E then S else S (3) S → while E do S (4) S → begin L end (5) S → A (6) L → L;S (7) L → S
M→ε { M.quad:=nextquad }
100 ( j<, a, b, 1002 ) 101 ( j, -, -, 1007 ) 102 ( j<, c, d, 1004 ) 103 ( j, -, -, 1000 ) 104 (+, y, z, T ) 105 (:=, T, -, x ) 106 ( j, -, -, 100 ) 107
视频区域
if 语句的翻译模式
1. S→if E then M S1 { backpatch(E.truelist, M.quad);
S.nextlist:=merge(E.falselist, S1.nextlist) }
if-then的语义
2. S→if E then M1 S1 N else M2 S2 { backpatch(E.truelist, M1.quad);
视频区域S( -, L1) E
EE else x := y - z
根据属性文法翻译控制语句
根据属性文法翻译如下语句 : while a<b do if c<d then x:=y+z else x:=y-z
L1: if a<b goto L2 goto Lnext
L2: if c<d got视o频L区3域 goto L4
goto S.begin
E.false:
……
To E.true 视频区域 To E.false
while-do语句的属性文法
产生式
S→while E do S1
语义规则
S.begin:=newlabel;
E.true:=newlabel;
E.false:=S.next;
SS.1c.noedxet::==gSe.bne(Sg.inb;egin
视频区域
(L1,Lnext) S
S ( -, L1) S
视频区域S
E
E
E(L2, Lnext) E
EE
EE
while a < b do if c < d then x := y + z else x := y - z
(L1,Lnext) S
S ( -, L1)
S( -, L1) E
视频区域S( -, L1) E
视频区域
编译原理
一遍扫描翻译控制语句示例
示例:翻译语句
将下面的语句翻译为四元式 while a<b do if c<d then x:=y+z;
视频区域
SSE{→→{bbEaiiwfddac.th1:ckrE=ikuprlEepeatlaltohMicstipechft1:nh{(pi=EEd(≠mEp2M.ndt:.ra=itoulrkluSteoMehelo1liei2slksinstSut,(tep,1nMm(Mei.dxqi2t.tu.(nqpaaud‘ma:)=d;e’));E; .place)
2. L → L1; M S { backpatch(L1.nextlist, M.quad); L.nextlist := S.nextlist }
3. M → ε { M.quad := nextquad }
视频区域
其它几个语句的翻译
1. S → A 2. L → S
{ S.nextlist := makelist( ) } { L.nextlist := S.nextlist }
backpatch(E.falselist, M2.quad); S.nextlist:=merge(S1.nextlist, N.nextlist, S2.nextlist) }
3. M→ε { M.quad:=nextquad }
视频区域
4. N→ε { N.nextlist:=makelist(nextquad); emit(‘j,-,-,-’) }
if-then-else的语义
while语句的文法
相关产生式
S → while E do S1 改写后的产生式
S → while M1 E do M2 S1
M→ε
S.begin: E.code
E.true:
S1.code
goto S.begin
E.false:
……
视频区域
To E.true To E.false
视频区域
gen(E.true gen(‘goto’
S‘:’.)n|e| xSt1).|c|ode
||
gen(E.false ‘:’) || S2.code
编译原理
while语句的属性文法
while-do语句的语义
S → while E do S1
S.begin: E.code
E.true: S1.code
E.true: E.false:
E.code S1.code
……
To E.true To E.false
if-then-else语句的语义
S → if E then S1 else S2
E.true: E.false: S.next
E.code
S1.code goto S.next
S2.code
相关文档
最新文档