Compilers Chapter5 Syntax-Directed Translation

  • Published on
    21-Jan-2016

  • View
    70

  • Download
    0

DESCRIPTION

Compilers Chapter5 Syntax-Directed Translation. 4 . . syntax-directed definition & translation scheme L Top-down Bottom-up . Syntax-Directed Translation. - PowerPoint PPT Presentation

Transcript

  • CompilersChapter5 Syntax-Directed Translation

    4

  • syntax-directed definition & translation scheme

    LTop-downBottom-up

  • Syntax-Directed Translation

    syntax-directed definition translation scheme2

    parsing L

  • Syntax-directed Definition& Translation Schemesyntax-directed definition

    translation scheme { SEMANTIC ACTION }T T1 F { T.val := T1.val F.val}

    PRODUCTIONSEMANTIC RULET T1 FT.val := T1.val F.val

  • Synthesized Attribute& Inherited Attribute

    ACBDE

  • Dependency GraphCircularity

    Strong Noncircular Syntax-Directed Definition

    i s t

    i s t i s tEE1E2

  • LAttributed DefinitionsL()AX1XnXX1X-1Atranslation scheme1X2X3A

  • Top-down

    translation scheme predictive translator 1.A 2.A 3.production )X )B c:=B(b1,b2,,bk)c: b: )

  • Bottom-uptranslation schemeint p, q, r; D T { L.in := T.type } L ;T int { T.type := integer }T real { T.type := real }L { L1.in := L.in } L1, id { addtype(id.entry, L.in) }L id { addtype(id.entry, L.in) }

  • Bottom-upAXYZ XYZ A top

    INPUTstatePRODUCTION USEDreal pqr-pqrrealpqrTT realqrT pqrT LL idqrT L rT L qrT LL L idrT L T L rT LL L idDD T L

    statevalXX.xYY.yZZ.z

  • Bottom-up

    top, ntop

    Lid id.entry T.typeaddtype(val[top],val[top-1])addtype(id.entry,T.type)T.typeL.in

    PRODUCTIONCODE FRAGMENTD T L T intval[ntop] := integerT realval[ntop] := realL L idaddtype(val[top]val[top-3])L idaddtype(val[top]val[top-1])

  • Bottom-uptranslation scheme production

    R + T {print('+')} RR + T M RM e {print('+')}

  • Bottom-up

    1/2C.i val[top-1]

    PRODUCTIONSEMANTIC RULESS aACC.i := A.sS bABCC.i := A.sC cC.s := g(C.i)

    PRODUCTIONSEMANTIC RULESS aACC.i := A.sS bABMCC.i := A.s; C.i := M.sC cC.s := g(C.i)M M.s := M.i

  • 1 2iii

    A.i

    A.iB1.i

    A.iB1.iB1.s

    A.iB1.iB1.sB2.i

    A.iB1.iB1.sB2.iB2.s

    A.iA.s

    A.i

    B1.sB2.s

    A.s

    B1.s

  • LifetimeLifetime Lifetime

Recommended

View more >