compiler 5

Upload: jayaswalaman

Post on 02-Jun-2018

225 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/10/2019 Compiler 5

    1/20

  • 8/10/2019 Compiler 5

    2/20

    Translation of languages guided by context-freegrammars.

    Attach attributes to the grammar symbols. Values of theattributes are computed by semantic rules associatedwith the grammar productions.

    Two notations:-- Syntax directed definitions: Each production has a set of

    semantic rules associated with it.--Translation Schemes: Embed the semantic actions within

    the productions. Conceptually we parse the input token stream, build theparse tree and then traverse the tree as needed toevaluate the semantic rules. The rules may generatecode, save information in the symbol table, issue errormessages, etc.

  • 8/10/2019 Compiler 5

    3/20

    In some cases we dont have to follow this outlineliterally. We may evaluate the rules during parsing.

    Synthesized attribute: The value of the attribute at aparent node can be found from the attributes of itschildren. It can be evaluated by traversing the treebottom-up.

    Inherited attribute: The value of the attribute at a nodecan be found from the attributes at its parent node and/orits sibling nodes. Can be evaluated by traversing the treetop-down.

    S-attributed definition: A syntax-directed definition whereall attributes are synthesized (the S stands forsynthesized).

  • 8/10/2019 Compiler 5

    4/20

    Example: Simple desk calculator

    Production L En E E1 + T E T T T1 * F T F F (E) F digit

    Semantic Rules print(E.val) E.val := E1.val + T.val E.val := T.val T.val := T1.val * F.val T.val := F.val F.val := E.val F.val := digit.lexval

  • 8/10/2019 Compiler 5

    5/20

    Example: Annotated parse tree for3*5+4n

    L

    E.Val = 19

    n

    +E.Val = 15

    T.Val = 15

    *T.Val = 3

    F.Val = 3

    digit.lexval = 3

    F.Val = 5

    digit.lexval = 5

    T.Val = 4

    F.Val = 4

    digit.lexval = 4

  • 8/10/2019 Compiler 5

    6/20

    Example

    Using inheritedattributes to parse adeclaration and add

    type information tothe symbol table.

    Production Semantic Rules

    D TL L.In := T.type

    T int T.Type :=integer

    T real T.Type := real

    L L1, id L1.in := L.in Addtype(id.entry, L.in)

    L id Addtype(id.entry, L.in)

  • 8/10/2019 Compiler 5

    7/20

    Annotated Parse Tree for real, id1,id2, id3

    D

    T.Type = real

    real

    L.in = real

    L.in = real

    L.In = real

    id1

    , id2

    , id3

  • 8/10/2019 Compiler 5

    8/20

    If all attributes are synthesized then a bottom up parser can evaluate the attributes while itparses.

    Add attribute fields to the nonterminals on thestack. Before each reproduction apply the semantic

    rule associated with the production.

    E.g. Assume the semantic rule associated withthe A XYZ production is : A.a := f (X.x, Y.y,Z.z).

  • 8/10/2019 Compiler 5

    9/20

    Before the reductionthe parser stacklooks like:

    After the reduction theparser stack lookslike:

    X X.X

    Y Y.yZ Z.Z

    A A.A

  • 8/10/2019 Compiler 5

    10/20

    Many top-down and bottom-up translation methods canbe obtained by applying a depth-first visit procedure tothe root of the parse tree:

    Procedure dfvisit(n : node);Beginfor each child m of n, from left to right do

    beginevaluate inherited attributes of m;dfvisit(m) end;

    evaluate synthesized attributes of nEnd.

  • 8/10/2019 Compiler 5

    11/20

    This method works as long as the inherited attributes ofeach child node just depend on attributes of the parentnode and the children to its left; there is no dependenceon attributes of children to the right.

    A syntax-directed definition is L-attributed if eachinherited attribute of Xj on the right side of a production A x1, X2,., Xn depends only on the attributes of X1,

    X2,, Xj -1 to the left of Xj in the production and theinherited attributes of A.

    The L stands for left. There are no restrictions on the synthesized attributes of

    Xj so every S-attributed definition is L-attributed.

  • 8/10/2019 Compiler 5

    12/20

    Top-Down Translation E.g.E E1 + T {E.val := E1.val + T.val}E E1 T {E.val := E1.val T.val}

    E T {E.val := T.val}T (E) {T.val := E.val}T num {T.val := num.val} To eliminate the left-recursion we add a

    nonterminal R to the grammar with an inheritedattribute, R.i and a synthesized attribute, R.s. The transformed translation scheme is:

  • 8/10/2019 Compiler 5

    13/20

    E T {R.i := T.val} R {E.val := R.s}R + T {R1.i := R.i + T.val} R1 {R.s := R1.s}R - T {R1.i := R.i T.val} R1 {R.s := R1.s}R {T.val := num.val}

    In general suppose the translation scheme has thefolowing left-recursive structure: A A1 Y {A.a := g (A1.a, Y.y)} A X {A.a := f (X.x)}

    f and g are arbitrary functions and A.a, A1.a, X.x and Y.yare synthesized attributes.

    Add a new nonterminal R, with a synthesized attributeR.s and an inherited attribute, R.i.

  • 8/10/2019 Compiler 5

    14/20

    To eliminate the left-recursion the translationscheme is transformed to:

    A X {R.i := f (X.x)} R {A.a := R.s}

    R Y {R1.i := g (R.i, Y.y)} R1 {R.s :=R1.s}R {R.s := R.i}

    E.g. A left recursive translation scheme forgenerating syntax trees:E E1 + T {E.nptr := mknode (+, E1.nptr,

    T.nptr)}

  • 8/10/2019 Compiler 5

    15/20

    E E1 - T {E.nptr := mknode ( -, E1.nptr,T.nptr)}

    E T {E.nptr := T.nptr}T (E) {T.npte := E.nptr}T id {T.nptr := mkleaf (id, id.entry)}

    T num {T.nptr := mkleaf (num,num.value)}

  • 8/10/2019 Compiler 5

    16/20

    Eliminating left recursion gives :E T {R.i := T.nptr} r {E.nptr := R.s}R + T {R1.i := mknode (+, R.i, T.nptr)}R1 {R.s := R1.s}R - T {R1.i := mknode ( -, R.i, T.nptr)}R1 {R.s := R1.s}R {R.s := R.i}T (E) {T.nptr := E.nptr}T id {T.nptr := mkleaf (id, id.entry)}T num {T.nptr := mkleaf (num, num.value)}

  • 8/10/2019 Compiler 5

    17/20

    The technique to write predictive parsers using recursiveprocedures can be modified to include the actions of atranslation scheme.

    Use recursive functions instead of recursive procedures

    to pass the values of attributes. For each nonterminal write a recursive function. The function accepts the values of the imherited

    attributes in its formal parameters and returns the valuesof the synthesized attributes.

    If there are multiple synthesized attributes their valuescan be returned in a record or a pointer to a record.

  • 8/10/2019 Compiler 5

    18/20

    Example Consider three

    nonterminals E, R and T.Write three recursivefunctions.

    E and T have noarguments. Function E handles the

    production:E T {R.i := T.nptr} R{E.nptr := R.s}function E : tree_node;begin e := R (T) end;

    Function Synthesized Attributes

    Inherited Attributes

    E E.Nptr (none)

    R R.S R>I

    T T.Nptr (none)

  • 8/10/2019 Compiler 5

    19/20

    Function R handles three productions:R + T {R1.i := mknode (+, R.i, T.nptr)}

    R1 {R.s := R1.s}R - T {R1.i := mknode ( -, R.i, T.nptr)}

    R1 {R.s := R1.s}

    R {R.s := R.i}

  • 8/10/2019 Compiler 5

    20/20

    function R (I : tree_node) : tree-node;var nptr, i1 : tree_node;begin

    if lookahead = + then begin match (+); nptr := T; i1 := mknode (+, I nptr); R := R (i1) end

    else if lookahead = - then begin match ( -); nptr := T; i1 := mknode ( -, I, nptr);

    R := R (i1) endelse R := i end;