פרק 5 תרגום מונחה תחביר

  • View
    46

  • Download
    5

Embed Size (px)

DESCRIPTION

5 . ( analysis ). lexical analyser. syntax analyser. semantic analyser. intermediate code generator. code optimizer. ( synthesis ). - PowerPoint PPT Presentation

Transcript

  • semantic analyser

  • (Semantic Rules) (attributes) (semantic rules)

    (syntax directed definitions) (translation schemes)

  • , .

  • (Syntax Directed Definitions) b = f (c1, c2, . . . ck) b -ci (synthesized attribute) b -ci (inherited attribute) b -ci / /

  • " (side effects) : (dummy) " ( ) -S (S-attributed definition) ( )

  • /(Annotated/Decorated Parse Tree) E d1 + d2E.val := d1.val + d2.val7 + 5E.val = 12d1.val = 7d2.val = 5+

  • L E nprint ( E.val )E E1 + TE.val := E1.val + T.valE TE.val := T.valT T1 FT.val := T1.val F.valT FT.val := F.valF ( E )F.val := E.valF digitF.val := digit.lexval

  • 3 5 + 4 n

  • D T LT intT realL L1, idL idL.in := T.typeT.type := integerT.type := realL1.in := L.inaddtype ( id.entry, L.in )addtype ( id.entry, L.in )

  • DT.type = realrealL.in = realL.in = real,id3L.in = real,id2id1 realid1 , id2 , id3

  • (Dependency Graph) :: : ' ' n a n n.a n b = f(c1,c2, ck) n i [1..k] ci b

  • ' E E1 + E2 E.Val := E1.val + E2.val '

    579LLLDTrealid1id2id3,,68104typeinininentry21entry3entry

  • "

  • (parse-tree methods) , (rule-based methods) ( ) (oblivious methods) ( )

  • (Syntax Tree) (syntax tree)

    (X Y)

    S if B then S1 else S2 if-then-elseBS1S2

  • : 3 5 + 4 n

  • " (label) ( ) 3 :mknode(op,left,right) " " op left -rightmkleaf(id,entry) " " id entry mkleaf(num,val) " " num val

  • : a 4 + c p1 = mkleaf(id, entry_a);p2 = mkleaf(num, 4);p3 = mknode( , p1, p2);p4 = mkleaf(id, entry_c);p5 = mknode( + , p3, p4);idto entry for ato entry for c

  • -S, E E1 + TE.nptr := mknode( + , E1.nptr , T.nptr ) E E1 TE.nptr := mknode( , E1.nptr , T.nptr ) E TE.nptr := T.nptr T ( E )T.nptr := E.nptr T idT.nptr := mkleaf(id, id.entry) T numT.nptr := mkleaf(num, num.val)

  • : a 4 + c

  • -S -S - , , ,

  • LR(1) action -goto, , - :state ( ) val

  • v, u -w A XYZ A.t := f(X.v, Y.u, Z.w)

  • L E nprint ( stack [top].val ); E E1 + Tstack[ntop].val = stack[top2].val + stack[top].val; E T// stack[ntop].val = stack[top].val;F ( E )stack[ntop].val = stack[top1].val;F digit// stack[ntop].val = stack[top].val;T T1 Fstack[ntop].val = stack[top2].val stack[top].val;T F// stack[ntop].val = stack[top].val;

  • top r ntop = top (r 1) " ntop top = ntop ( r 1 )

  • : 3 5 + 4 nstateval 3 5 + 4 n 5 + 4 n 5 + 4 n 5 + 4 n5 + 4 n+ 4 n+ 4 n+ 4 n+ 4 n4 nnnnnLE nE E + TE + FE + 4E +ETT FT 5T TF3L E nE E + TT FF digitE TT T FF digitT F F digit1919 _1915 _ 415 _ 415 _ 415 _15153 _ 53 _ 53 _333

  • -S

    " -S

  • (Depth First)void dfvisit (node n){foreach child m of n, from left to right {evaluate inherited attributes of m;dfvisit (m);}evaluate synthesized attributes of n;}

  • -L(L-attributed Definitions) -L : A X1X2. . . Xn j [1..n] Xj : X1X2. . .Xj-1 ( Xj ) A: -S -L

  • (Translation Scheme) (translation scheme) ( { } )

  • / (infix) (postfix)E T R R addop T { print(addop.lexeme)} R1 | T num { print(num.val)} 9 5 + 2 9 5 2 +

  • -L :

  • 5