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

第七章 语义分析和中间代码产生 PowerPoint PPT Presentation


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

第七章 语义分析和中间代码产生. 7.1 中间代码. 7.1.1 逆波兰表示法. 逆波兰表示法是波兰逻辑家 Lukasiewicz 发明的一种表示表达式的方法。该方法是将运算量写在前面,算符写在后面,用这种方法表示的表达式称为后缀式,如 a+b 可写成 ab+。 一般而言,若 θ 是 K 目算符,它对后缀式 e 1 ,e 2 ,...,e k 作用的结果将被表示为 e 1 e 2 ... e k θ。.

Download Presentation

第七章 语义分析和中间代码产生

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


5109270


5109270

71


7 1 1

7.1.1

Lukasiewicza+bab+Ke1 e2 ...eke1 e2... ek


5109270

  • KKK


5109270

  • if e then x else y [email protected]@


5109270

  • EE1+ E2

    { E.CODE := E1.CODE|| E2.CODE }

    E(E1)

    { E.CODE := E1.CODE}

    Ei {E.CODE:=i}


7 1 2

7.1.2

  • :(OP ,ARG1,ARG2)OPARG1ARG2A+B*C

    (1)(*BC)

    (2)(+A1)

  • 2A+B*C21


5109270

  • LOOKUPNAMENAMELOOKUPLOOKUPnull

  • FILLSYMNAMENAMEFILLSYM


5109270

  • TRIPOPARG1ARG2OPARG1ARG2

  • ERTRY(i)i


5109270

1EE1op E2

{ E.VAL:=TRIP(op,E1.VAL,E2.VAL }

2E-E1

{ E.VAL = TRIP(@,E1.VAL,-) }

3EE1

{E.VAL= E1.VAL}


5109270

4Ei {E.VAL=ENTRY(i)}

X=A+B*C

Y=DA+B


5109270

  • <1>+AB

    <2>*C

    <3>:=X

    <4> (+A B)

    <5> (D)

    <6> (:=Y)


5109270

  • 41456


5109270

<1>+AB

<2>*C

<3>:=,X,

<4> (,D,)

<5> (:=,Y,)


7 1 3

7.1.3

  • OPARG1ARG2RESULTOPARG1ARG2RESULT


5109270

1

2


5109270

7.2

NEWTEMPTi

ENTRYi


Gen op arg1 arg2 result op arg1 arg2 result e place e e

GENOPARG1ARG2RESULTOPARG1ARG2RESULT

EPLACE EE


A i e e e e e e e e i

Ai:=E

EE+E|E*E|-E|(E)|i


5109270

  • :(1) Ai:=E

    {GEN(:=E.PLACE ENTRYi)}(2) EE1+ E2

    {E.PLACE:=NEWTEMP;GEN(+E1.PLACEE2.PLACEE.PLACE)} (3) EE1* E2 {E.PLACE:=NEWTEMP;GEN (*E1.PLACEE2.PLACEE.PLACE)}


5109270

(4) EE1

{ E.PLACE:= E1.PLACE}

(5) E-E1

{ E.PLACE:=NEWTEMP;GEN(@E1.PLACEE.PLACE)}

(6) Ei {E.PLACE:=ENTRY(i)}


5109270

7.3


5109270

  • AB if A then true else BAB if A then B else falseA if A then false else trueABCDif A then true else if B thenif C then D else trueelse false


5109270

  • EEEEEE


5109270

  • EE1E2 E1EE1EE1EE2E1E2jnzA P A0PjropA1A2PA1ropA2PjPP


5109270

7.1if ABC then S1 ELSE S21jnzA52j33j<BC54jP+1.....S1p. (jq)P+1....S2q.


5109270

NXQ

GENoparg1arg2resultGENNXQ1


5109270

MERGEP1P2P1P2P2

BACKPATCHPTTPRESULT


5109270

  • EEE|EE|E|E|i|i rop i

    EEE2

    {E.FC:= E2 .FC; E.TC:=MERGE(E.TC,E2.TC)

    EEE2{E.TC:= E2 .TC; E.FC:=MERGE(E.FC,E2.FC)


5109270

EE1

{E.TC:= E1 .FC; E.FC:= E1.TC}

EE1

{E.TC:= E1 .TC; E.FC:= E1.FC}

Ei

{E.TC:=NXQ; E.FC:=NXQ+1; GEN(jnz,ENTRY(i), ,0);GEN(j,,,0);}


5109270

Ei1 rop i2

{E.TC:=NXQ; E.FC:=NXQ+1; GEN(jrop,ENTRY(i1), ENTRY(i2 ),0);GEN(j,,,0);}

EE1

{BACKPATCH(E1.FC,NXQ); E.TC:= E1.TC}


5109270

EE1

{BACKPATCH(E1.TC,NXQ); E.TC:= E1.TC}


5109270

7.4

7.4.1 GOTO

  • LS LS

    GOTO L


5109270

  • GOTOGOTO

  • GOTOGOTO

  • : GOTOGOTO


Name type def addr l i i i def addr nxq

NAME TYPE......DEF ADDR

L->i:

:

iiDEFADDRNXQ


5109270

iDEF,.

iDEF,DEF,ADDRNXQ,BACKPATCH(q,NXQ).qADDR.


5109270

  • GOTO S-> GOTO L;

  • :

    { lookup:=LOOKUP(L);

    if lookup=null then

    begin

    ENTRY(L);

    ENTER.TYPE:=L;

    ENTER.DEF:=0;

    ENTER.ADDR:=NXQ;

    GEN(j,,,0);

    END


5109270

ELSE if lookup.def=1

then

GEN(j,,,lookup.ADDR) else

begin

n:=NXQ;

GEN(j,,,lookup.ADDR)

lookup.ADDR:=n;

end

  • ,?


7 4 2

7.4.2

  • if,while.


5109270

  • :

    G[S]:

    1.S-> if E then S|if E then S else S

    |while E do S|begin L end|A

    L->L;S|S

    :S--,L--,

    A--,E--


5109270

:

S->C S1

{S.CHAIN:=MERGE(C.CHAIN, S1.CHAIN)}

S-> TP S2

{S.CHAIN:=MERGE(TP .CHAIN, S2 .CHAIN)}

S-> WD S3

{BACKPATCH(S3 .CHAIN, WD .QUAD)

GEN(J,,, WD .QUAD );S.CHAIN:= WD .CHAIN }


5109270

S->begin L end

{S.CHAIN:=L.CHAIN}

S->A

{S.CHAIN:=0}

L-> LS S1

{L.CHAIN:= S1.CHAIN}

L->S

{L.CHAIN:=S.CHAIN}


5109270

C-> if E then {BACKPATCH(E.TC,NXQ);C.CHAIN:=E.FC}

TP->C S1 ELSE{q:=NXQ;GEN(J,,,0);BACKPATCH(C.CHAIN,NXQ);TP .CHAIN=MERGE(S1.CHAIN,q) }

W->while{W.QUAD:=NXQ}

WD->W E do

{BACKPATCH(E.TC,NXQ);WD .CHAIN:=E.FC;

WD .QUAD:=W.QUAD}

LS ->L;{BACKPATCH(L.CHAIN,NXQ)}


5109270

  • 7.2:

    while (A<B) do

    if (C<D) then:=Y+Z


7 4 3

7.4.3

  • S->for i:= E1 step E2 until E3 do S1


5109270

,:

i:= E1;

gotoOVER;

AGAIN:i:=i+ E2 ;

OVER :if i<= E3 then

begin S1 ; goto AGIAN end;

,:


5109270

F1 ->for i:=E1

{GEN(:=,E1 .PLACE, ,ENTRY(i));

F1 .PLACE:=ENTRY(i);

F1 .CHAIN:=NXQ;

GEN(j,,,0);

F1 .QUAD:=NXQ;}


5109270

F2 ->F1 step E2

{ F2 .QUAD:= F1 .QUAD; F2 .PLACE:= F1 .PLACE;

GEN(+,F1 .PLACE, E2 .PLACE ,F1 .PLACE);

BACKPATCH(F1 .CHAIN,NXQ);}


5109270

F3 -> F2 until E3

{ F3.QUAD:= F2 .QUAD;

q:=NXQ;

GEN(j<=, F2 .PLACE, E3 .PLACE ,q+2);

F3.CHAIN:=NXQ

GEN(j,,,0)}


5109270

S-> F3 do S1

{GEN(j, , , F3.QUAD);BACKPATCH(S1 .CHAIN, F3.QUAD);S.CHAIN:= F3.CHAIN}


5109270

7.5

  • array A [l1u1l2u2...lnun]

  • AA[i1i2..., in]


5109270

  • di = ui- li+1,i=1,2,...,nD=CONSPART+VARPARTCONSPART=a-CaA[l1l2...ln]

  • C=(...(( l1d2 +l2)d3+l3)d4+...+ln-1)dn +ln

  • VARPART==(...(( i1d2 +i2)d3+i3)d4+...+in-1)dn +in


5109270

  • CONSPART

  • VARPARTVARPART

    VARPARTTCONSPARTT1T1[T]


5109270

  • ->=[], T1[T], ,X /*X:= T1[T]*/

  • ->([]=,X, ,T1[T]) /*T1[T]:=X */


5109270

  • A->V:=E

    V->i[elist]|i

    elist->elist,E|E

    E->E+E|(E)|V

    ,:

    V->elist]|ielist->elist,E|i[E


5109270

  • :

    elist.ARRAY:.

    eLIST.PLACE:VARPART.

    elist.DIM:.

    LIMIT(ARRAY,K):ARRAYK.ARRAY.


5109270

V.PLACE, V.OFFSET:

Vi,V.PLACEi, V.OFFSETNULL; V,V.PLACECONSPART,V.OFFSETVARPART.


5109270

A->V:=E

{if (V.OFFSET=NULL)

then GEN(:=,E.PLACE, ,V.PLACE)

ELSE GEN([]=,E.PLACE,V.PLACE[V.OFFSET])}

E->E1+E2 {T:=NEWTEMP;GEN(+,E1.PLACE,E2.PLACE,T);E.PLACE:=T}


5109270

E->( E1 ) {E.PLACE:= E1.PLACE}

E->V

{if (V.OFFSET=NULL) then E.PLACE:=V.PLACE

else begin T:=NEWTEMP;GEN(=[],V.PLACE[V.OFFSET], ,T)E.PLACE:=T;end}


5109270

V->elist]

{ T:=NEWTEMP; GEN(-,elist.ARRAY,C,T);

V.PLACE:=T;

V.OFFSET:=elist.PLACE

} /*elist.ARRAYC*/

V->i {V.OFFSET:=NULL; V.PLACE:=ENTRY(i)}


5109270

elist->elist1,E

{T:=NEWTEMP; K:=elist.DIM+1; dk:=LIMIT(elist1.ARRAY,K); GEN(*,elist1.PLACE,dk,T) GEN(+,E.PLACE,T,T); elist.ARRAY:=elist1.ARRAY; elist.PLACE:=T; elist.DIM:=K }

elist->i[E

{elist.PLACE:=E.PLACE; elist.DIM:=1;

elist.ARRAY:=ENTRY(i);}


5109270

7.6

  • ,;

    TT


5109270


5109270

CALL S(A+BZ)

A+BT /*T=A+B */

par T/* */

par Z/* */

call S /**/


5109270

S->call (arglist)

{for arglist.QUEUEp DO

GEN(par, , ,p);

GEN(call, , ,ENTRY(i))}


5109270

arglist->arglist1 ,E

{E.PLACEarglist1.QUEUE;

arglist.QUEUE:=arglist1.QUEUE}

arglist->E

{arglist.QUEUE,E.PLACE}


  • Login