Download presentation
Presentation is loading. Please wait.
Published byAngela Cooper Modified over 9 years ago
1
Chapter 5. Syntax-Directed Translation
2
2 Fig. 5.2. Syntax-directed definition of a simple desk calculator ProductionSemantic Rules L E n print ( E.val ) E E 1 + T E.val := E 1.val + T.val E TE T E.val := T.val T T 1 * F T.val := T 1.val + F.val T FT F T.val := F.val F ( E ) F.val := E.val F digit F.val := digit.lexval
3
3 Fig. 5.3. Annotated parse tree for 3*5+4 n.
4
4 Fig. 5.4. Syntax-directed definition with inherited attribute L.in. ProductionSemantic Rules D T L L.in := T.type T int T.type := integer T real T.type := real L L 1, id L 1.in := L.in addtype ( id.entry, L.in ) L id addtype ( id.entry, L.in )
5
5 Fig. 5.5. Parse tree with inherited attribute in at each node labeled L.
6
6 Fig. 5.6. E.val is synthesized from E 1.val and E 2.val
7
7 Fig. 5.7. Dependency graph for parse tree of Fig. 5.5
8
8 Fig. 5.9. Syntax-directed definition for constructing a syntax tree for an expression ProductionSemantic Rules E E 1 + T E.nptr := mknode(‘+’, E 1.nptr, T.nptr) E E 1 - T E.nptr := mknode(‘-’, E 1.nptr, T.nptr) E T E.nptr := T.nptr T ( E ) T id T.nptr := E.nptr T.nptr := mkleaf (id, id.entry) T num T.nptr := mkleaf (num, num.val)
9
9 Fig. 5.10. Construction of a syntax-tree for a-4+c
10
10 Fig. 5.11. Dag for the expression a+a*(b-c)+(b-c)*d.
11
11 Fig. 5.12. Instructions for constructing the dag of Fig. 5.11 (1) p 1 := mkleaf ( id, a ) (2) p 2 := mkleaf ( id, a ) (3) p 3 := mkleaf ( id, b ) (4) p 4 := mkleaf ( id, c ) (5) p 5 := mknode ( ‘-’, p 3, p 4 ) (6) p 6 := mknode ( ‘*’, p 2, p 5 ) (7) p 7 := mknode ( ‘+’, p 1, p 6 ) (8) p 8 := mkleaf ( id, b ) (9) p 9 := mkleaf ( id, c ) (10) p 10 := mknode ( ‘-’, p 8, p 9 ) (11) p 11 := mkleaf ( id, d ) (12) p 12 := mknode ( ‘*’, p 10, p 11 ) (13) p 13 := mknode ( ‘+’, p 7, p 12 )
12
12 Fig. 5.15. Parser stack with a field for synthesized attributes
13
13 Fig. 5.16. Implementation of a desk calculator with an LR parser. ProductionCode Fragment L E n print ( val [top] ) E E 1 + T val [ntop] := val [top – 2]+val [top] E TE T T T 1 * F val [ntop] := val [top – 2]×val [top] T FT F F ( E ) val [ntop] := val [top – 1] F digit
14
14 L-Attributed Definitions A syntax-directed definition is L-attribute if each inherited attribute of X j, 1≤ j≤n, on the right side of A → X 1 X 2 · · · X n, depends only on 1. the attributes of the symbols X 1, X 2, · · ·, X j-1 to the left of X j in the production and 2.the inherited attributes of A. Note that every S-attributes definition is L-attributed, because the restrictions (1) and (2) apply only to inherited attributes
15
15 Example 5.17. The type of an identifier can be passed by copy rules using inherited attributes as shown in Fig. 5.32 (adapted from Fig. 5.7). We shall first examine the moves made by a bottom-up parser on the input real p, q, r then we show how the value of attirbute T.type can be accessed when the productions for L are applied. The translation scheme we wish to implement is D T L{ L.in := T.type } T int{ T.type := integer } T real{ T.type := real } L { L 1.in := L.in } L 1, id{ addtype ( id.entry, L.in ) } L id{ addtype ( id.entry, L.in ) }
16
16 If we ignore the actions in the above translation scheme, the sequence of moves made by the parser on the input of Fig. 5.32. is as in Fig. 5.33. For clarity, we show the corresponding grammar symbol instead of a stack state and the actual identifier instead of the token id. Fig.5.32. At each node for L, L.in = T.type.
17
17 Fig. 5.33.Whenever a right side for L is reduced, T is just below the right side. InputstateProduction Used real p,q,r − p,q,r real p,q,r T T → real,q,r T p,q,r T L L → id q,r T L,,r T L, q,r T L L → L, id r T L, T L, r T L L → L, id D D → T L
18
18 Fig. 5.34. The value of T.type is used in place of L.in ProductionCode Fragment D T L ; T int val [ntop] := integer T real val [ntop] := real L L 1, id addtype (val [top], val [top−3] ) L id addtype (val [top], val [top−1] )
19
19 Example 5.18. As an instance where we cannot predict the position, consider the following translation scheme: (5.6) C inherits the synthesized attribute A.s by a copy rule. Note that there may or may not be a B between A and C in the stack. When reduction by C → c is performed, the value of C.i is either in val [top−1]or in val [top−2], but it is not clear which case applies. ProductionSemantic Rules S → aAC C.i := A.s S → bABC C.i := A.s C → c C.s := g ( C.i )
20
20 Fig. 5.35. Copying an attribute value through a marker M. ProductionSemantic Rules S → aAC C.i := A.s S → bABMC M.i := A.s ; C.i := M.s C → c C.s := g ( C.i ) M → єM → є M.s := M.i є (a) original production(b) modified dependencies
21
21 Fig. 8.7. Semantic rules generating code for a while statement ProductionSemantic Rules S -> while E do S 1 S.begin := newlabel ; S.after := newlabel ; S.code := gen( S.begin ‘ :’) || E.code || gen( ‘ if ’ E.place ‘ = ‘ ‘ 0 ’ ‘ goto’ S.after ) || S 1.code || gen(‘ goto’ S.begin ) || gen( S.after ‘ : ‘ )
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.