This presentation is the property of its rightful owner.
Sponsored Links
1 / 119

第 8 章 语法制导翻译和中间代码生成 PowerPoint PPT Presentation


  • 232 Views
  • Uploaded on
  • Presentation posted in: General

第 8 章 语法制导翻译和中间代码生成. 教学目的: [1] 掌握语法制导翻译基本原理。 [2] 了解自上而下分析制导翻译基本思想和实现方法。 [3] 掌握几种常用的中间代码:四元式、三元式、逆波兰表示。 [4] 掌握简单表达式的中间代码生成。. 教学重点: 语法制导翻译基本思想、中间代码的形式、布尔表达式的翻译与控制结构的翻译;布尔表达式与控制结构的语义过程。. 学时分配: 8 学时. 本章内容. 8.1 属性文法. 8.2 语法制导翻译概论. 8.3 中间代码的形式. 8.4 简单赋值语句的翻译. 8.5 布尔表达式的翻译.

Download Presentation

第 8 章 语法制导翻译和中间代码生成

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


8

8

  • [1]

    [2]

    [3]

    [4]

  • 8


8

8.1

8.2

8.3

8.4

8.5

8.6

8.7

8.8


8

8.1

(Semantics

:


8

:

1

2Cbreakwhileforswitch

3

4Ada

(5)


8

()


8

,BackusNaus

(BNF)

(BNF)

(attribute grammar)


8

8.1

Attribute


8

Attribute Grammar

A(G,V,F)

  • G

  • V,

    ,

  • F(),


8

  • ,

    1 . . . . . .n


8

number number digit | digitdigit 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

val

1 digit0

digit.val = 0

2 number digit

number.val = digit.val

3number number digit , number1 number2 digit

number1.val:=number2.val*10+digit.val


8

D T L

T int | real

L L1,id

L id

[] type

1T intT.type = int

2D T L L.type = T.type

3L id addtype(id.entry,L.type)

4L L1,id

L1.type = L.type addtype(id.entry,L.type)

addtype


8

1inherited attribute

2synthesized attribute


8

(1)

(2)


8

ETFval

:

LE print(E.val)

EE1 +T E.val:=E1.val+T.val

ET E.val:=T.val

TT1 * F T.val:=T1.valF.val

TF T.val:=F.val

F(E) F.val:=E.val

Fdigit F.val=digit.lexval

L

digit


8

L.in

DTL Lin:=T type

T int T type :=int

T real T type :=real

L L1,id L1in :=L in

addtype(id entry,L in)

L id addtype(id entry,L in)


8

8.2


8

1

2

3

4


8

1);

2)

;

3);


8

  • b

    b:= f(c1,c2, ck)

  • bccb


8

forn do

fora do

a

forn do

forn

b:=f(c1,c2,ck) do

for i :=1 to k do

cib


8

A.a

X.x

Y.y

X.i

A.a:=f( X.x, Y.y )

AXY,X.xY.yA.a A.a,X.x,Y.yA.aX.xX.xA.a.A.aY.yY.yA.a.

AXY

X .i:=g(A.a,Y.y)

A.aX.iY.yX.i

,X.iA.aY.y.


8

EE1+E2 E.val:=E1.val+E2.val

E

E.val

E1

+

E2

E1.val

E2.val


8


8

(Topological Sort)

(DirectedAcyclicGraphsDAG)


8

  • m1m2mk

  • mimjmimj,mimj


8

real id1,id2,id3

DTL Lin:=T type

T int T type :=integer

T real T type :=real

L L1,id L1in :=L in

addtype(id entry,L in)

L id addtype(id entry,L in)


8

D

T

4

type

in 5

L

6

real

,

in 7

id3

3 entry

L

8

in 9

L

10

,

id2

2 entry

id1

1 entry


8

1 2 345678910

a4:=real;

a5:=a4;

addtype(id3entry,a5);

a7:=a5;

addtype(id2entry,a7);

a9:=a7;

addtype(id1entry,a9);


8


8

  • 1)

    (2)

  • L-S-


8


8


8

S-

  • S

  • S-LRS-LR


8

:

LEn print(E.val)

EE1 +T E.val:=E1.val+T.val

ET E.val:=T.val

TT1 * F T.val:=T1.valF.val

TF T.val:=F.val

F(E) F.val:=E.val

Fdigit F.val=digit.lexval

3*5+4n


8

state

val

...

...

X

X.x

Y

Y.y

top

Z

Z.z

...

...


8

ET1 + T2 if T1.type = int and T2.type= int then E.type :=int else error

E T1 o T2 if T1.type=bool and T2.type= bool then E.type :=bool else error

T n T.type := int

T b T.type := bool

LR

LL1


8

LRS-S-,


8

L-

L-


8

  • L-AX1X2Xn,Xj(1<=j<= n)

    (1)XjX1X2Xj-l

    (2) A

  • S-L-


8

L-

ALM

A QR

L.i:=l(A.i)

M.i:=m(L.s)

A.s:=f(M.s)

R.i:=r(A.i)

Q.i:=q(R.s)

A.s:=f(Q.s)

L-

Q

R


8

  • Translation schemes


8

1

2

3


8

ETR

Raddop T {print(addop.lexeme)}R1|

Tnum{print(num.val)}

9-5+2,

9 5 - 2 +

9-5+2


8

E

T

R

-

R

9

R

Pt(-)

Pt(9)

T

Pt(+)

+

T

5

Pt(5)

2

Pt(2)

9-5+2


8

L-

1.

TT1*F Tval:=T1 val*F val

TT1*F { Tval:=T1 val*F val}


8

  • 2.


8

SA1A2 {A1in:=1; A2 in:=2}

A a { print(A in) }

aaA.inA1in A2 inSA1A2 A1A2A.inprint(A in)


8

AaB {print 0 }

Ac {print 1 }

BAb { print 2 }

aacbb?


8

aacbb

12020


8

+,

EE+T | T

Tnum.num | num


8

a)type


8

S->(L)|a

L->L,S|S

()(a,(a,a))2


8

S'S'->S

num


8

S.valS(101.101S.val=5.625)

SL.L | L

LLB | B

B0 | 1


8

val length


8

8.3

( Intermediate code)

(Intermediate representation)

(Intermediate language)


8

1)()

2)DAG

3)


8

1EEE

2EE1 op E2opE E1 E2op,E1E2E1E2

3E(E1)E1E


8

1)

2)


8

1a := b * - c + b * - 34

a b c - * b 34 - * + :=

2a*- (b+c)

a b c + uminus *

3

A + B * ( C - D ) + E / ( C - D ) ^N

A B C D - * + E C D -N ^ / +


8

4x+yza>O (8+z)>3

5a b c (b x=O c)

xy+zaO>8z+3>

ab cbxO=c

6 -a-(b*c/(c-d)+(-b)*a)

a-bc*cd-/b-a*+-


8

:

X:=y op z

xyzop

3

(1)

2

(3)


8

1

OPOperandl,Operand2OPOperandl,Operand2()


8

a:=b*-c+b*-c

op

arg1

arg2

(0)

(1)

(2)

(3)

(4)

(5)

uminus

*

uminus

*

+

assign

c

b

c

b

1

a

(0)

(2)

(3)

(4)


8

a:=b*-c+b*-c

statement

op

arg1

arg2

(0)

(1)

(2)

(3)

(4)

(5)

(14)

(15)

(16)

(17)

(18)

(19)

(14)

(15)

(16)

(17)

(18)

(19)

uminus

*

uminus

*

+

assign

c

b

c

b

15

a

(14)

(16)

(17)

(18)


8

2

4OPOperandl,Operand2Result

OPOperandl,Operand2,Result


8

a:=b*-c+b*-c

op

arg1

arg2

result

(0)

(1)

(2)

(3)

(4)

(5)

uminus

*

uminus

*

+

assign

c

b

c

b

t2

t5

t1

t3

t4

t1

t2

t3

t4

t5

a


8

A=-B*(C+D)

OP ARGl ARG2 RESULT

(1) @ B T1 Tl

(2) + C D T2 T2

(3) * T1 T2 T3 Tl

(4) = T3 A


8


8

  • DAG


S if b then s1 else s2

if_then_else

B

S1

S2

S if B then S1 else S2


8

+

*

-

T

T

T

T

T

1 2 3

e1e2TlT2el+e2e1*e2-e1


3 5 4

+

4

*

5

3

354


8

DAG

Directed AcycliDAG

DAG

DAG

DAG


8

a+a*(b-c)+(b-c)*d DAG


8

a=b*-c+b*-c DAG


8

A+B*(C-D)+E(C-D)^N


8

(1)(-C,D)

(2)(*B(1))

(3)(+A(2))

(4)(-C,D)

(5)(^(4)N)

(6)(/E(5))

(7)(+(3)(6))

(1)(-C,D,T1)

(2)(*B,T1,T2)

(3)(+,A,T2,T3)

(4)(-C,D,T4)

(5)(^T4,N,T5)

(6)(/ET5T6)

(7)(+T3T6T7)


8

A+B*(C-D)+E(C-D)^N

(1) (1) (-,C,D)

(2) (2) (*B(1))

(3) (3) (+A(2))

(4) (4) (^(1)N)

(5) (5) (/ E(4))

(6) (6) (+(3)(5))


8

*

-

+

-

^

/

+

A B C D E C D N

A+B*(C-D)+E(C-D)^N


8

8.4

  • id.nameid

  • lookup(id.name)nil

  • emit


8

t=arg1 op arg2

id.name, E.place

lookup(id.name) ;

emit(t:=arg1 op arg2); newtemp;

(1) Sid:=E { P:=lookup (id.name) ; if Pnil then emit( P:=E.place) }

(2)EE1+E2 {E.place:= newtemp; emit(E.place:= E1.place+E2.place) }

(3)E- E1 { E.place:=newtemp; emit(E.place:=uminus E1.place) }

(4)E( E1) { E.place:= E1.place}

(5) Eid { P:=lookup(id.name); if Pnil then E.place:=P else error }


8

8.5

if-thenif-then-else while-do

and ornot )E1 relop E2, E1E2 relop < ,<=, = >, > , =, EE or E | E and E | not E | (E) | id relop id |id

reloprelop.opreloporand or and, not


8

:

,:

A or B if A then true else B

A and B if A then B else false

not A if A then false else true


8

a or b and not c

T1not c

T2=b and T1

T3=a or T2


8

a<b:

if a<b then 1 else 0 ,

100

100 if a<b goto 103

  • T:=0

  • goto 104

  • T:= 1

    104


8

E->E1 or E2 {Eplacenewtemp

emit(E.placeE1place or E2.place}

E-> E1 and E2 {Eplacenewtemp

emit(E.placeE1place and E2.place}

E->not E1{E.palce:=newtemp;

emit(Eplace=not E1place)}

E->(E1) {E.placeE1place}


8

E->id1 relop id2

{Eplacenewtemp

emit(if id1 .place relop id2place

goto nextstat+ 3) ;

emit(E.place=0)

emit(goto nextstat+2)

emit(Eplace1)

}

E->true { E.place:=newtemp;emit(E.place:=1);}

E->false { E.place:=newtemp;emit(E.place:=0);}


8

a<b or c<d and e<f

100


8

if-thenif-else-endwhile-doES1S2E

E

S1

S2


8

EE

if E goto E.true

goto E.false


8

T

T

T

F

F

F

a<b or c<d and e<f

e>f

a<b

c<d


8

if ab goto E.true

goto L1

L1if cd goto L2

goto E.false

L2if ef goto Ltrun

goto E.false


8

E.true E.false


8

T

T

F

F

F

a and b or cd

T

a b cd


8

8.6

if - thenif then-else, while-do

Sif E then S1

| if E then S1 else S2

| while E do S1

E.


8

T

E

F

S1

.

S2

1

if E then S1 else s2


8

IF a>0 THEN x:=x+1 ELSE x:=4*( x- 1)

(1) if a>0 goto

  • goto

  • x:=x+1

    (4)goto

    (5)t1:=x-1

    (7) t2:=t1*4

  • x:=t2

(3)//

(5)//

(9)//


8

T

E

F

S

2

while E do S


8

while a<b do

if c<d then x:=y+z

else x:=y-z

100: if a<b goto -

101 goto -

102 if c<d goto -

103 goto

104: T1:=y+z;

105 x:=T1

106 goto 100

107: T2:=y-z

108 x:=T2

109 goto 100

200:


8

3

for i:=E1 STEP E2 until E3 do S1

E2

i:=E1;

again: if i<= E3 then

begin

S1

i:=i+ E2

goto again

end


8

8.7 goto

LS (

goto L

goto

1goto L L

LS goto p(p)

2goto L Lgoto Lgoto _LLL


8

1goto LLLLnextstatL(goto 0)0

2L()(q)nextstat(goto q)


8

goto

S-->label S

label->i:

Label->i:

1i(Lnextstat.

2)L

3L(q)nextstatbackpatch(qnextstat)


8

goto L1;

S1;

goto L1;

L1:S2;

goto L1;

L2:S3;

goto L2;

;


8

8.8

AwA[i]

base + (i low)*w1

low basebaseAA[low]

(1) i*w + (base low*w)

  • i*w ,VARPART ;

  • base low*w)CONSPART


8

AA[i1,i2]

base + (( i1 l1)*d2 + i2 l2) *w

l1l2i1i2diuiidi=ui li +1.

i1,i2

( base (l1 *d2) + l2) *w+ ( (i1*d2) + i2) *w

CONSPART VARPART

:( base (l1 *d2) + l2) *w CONSPART

i1, i2 VARPART


8


8

,A

array A[l1:u1,l2:u2,..ln: un]

A[i1,i2,in]D

D=CONSPART+VARPART

CONSPART=a-c

C=(l1*d2+l2)*d3+l3)*d4+..+ln-1)*dn+ln

VARPART=

(i1*d2+i2)*d3+i3)*d4+..+in-1)*dn+in


8

A[i1,i2,in]

VARPARTT

CONSPART

CONSPART[T]

CONSPART


8

VARPART

T1CONSPARTT2T2[T1


8

[]A10*20d1=10,d2=20w=4X:=A[y,z]

1

(2

T1:=y*20

T1:=T1 + z

T2:= A-84 A-(l1*d2 +12 )*w

T3:= 4* T1

T4:= T2 [T3]

x:= T4

A


8

A10*20 d1=10, d2=20

(1) X:= A[I, J ]

(2) A[ I+2 , J+1 ] := M+N

1

* I, 20 T1 )

(+ , J , T1 , T1 )

( - , A , 21 , T2 )

( =[ ] , T2[T1] , , T3)

( := , T3 , , X )


8

2

( + , I , 2 , T1 )

( + , J , 1 , T2 )

( * , T1, 20 , T3 )

( + , T2 , T3 , T3 )

( - , A, 21 , T4 )

( + , M, N , T5 )

( [ ] =, T5, , T4[T3] )


8

1

1 -a-(b*c/(c-d) + (-b)*a)

(2) -A+B*C (D/E)/F

2

A+B*(C-D)-E/FG

3

while A<C and B<D do

if A=1 then C:=C+1

else A:=A+2


8

P202 123 4


8

4

i:=1;sum:=0;

while i<=10 do

begin

sum:=sum+a[i]*b[i];

i:=i+1;

end;

4


  • Login