Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 5. Syntax-Directed Translation

Similar presentations


Presentation on theme: "Chapter 5. Syntax-Directed Translation"— Presentation transcript:

1 Chapter 5. Syntax-Directed Translation

2 Fig. 5.2. Syntax-directed definition of a simple desk calculator
Production Semantic Rules L  E n print ( E.val ) E  E1 + T E.val := E1.val + T.val E  T E.val := T.val T  T1 * F T.val := T1.val + F.val T  F T.val := F.val F  ( E ) F.val := E.val F  digit F.val := digit.lexval Fig Syntax-directed definition of a simple desk calculator

3 Fig. 5.3. Annotated parse tree for 3*5+4n.

4 Fig. 5.4. Syntax-directed definition with inherited attribute L.in.
Production Semantic Rules D  T L 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 Fig Syntax-directed definition with inherited attribute L.in.

5 Fig. 5.5. Parse tree with inherited attribute in at each node labeled L.

6 Fig. 5.6. E.val is synthesized from E1.val and E2.val

7 Fig. 5.7. Dependency graph for parse tree of Fig. 5.5

8 Fig. 5.16. Implementation of a desk calculator with an LR parser.
Production Code Fragment L  E n print ( val [top] ) E  E1 + T val [ntop] := val [top – 2]+val [top] E  T T  T1 * F val [ntop] := val [top – 2]×val [top] T  F F  ( E ) val [ntop] := val [top – 1] F  digit Fig Implementation of a desk calculator with an LR parser.

9 L-Attributed Definitions
A syntax-directed definition is L-attribute if each inherited attribute of Xj, 1≤ j≤n, on the right side of A → X1X2 · · · Xn , depends only on 1. the attributes of the symbols X1 , X2 , · · · , Xj-1 to the left of Xj in the production and 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

10 Example 5.17. The type of an identifier can be passed by copy rules
using inherited attributes as shown in Fig (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  { L1.in := L.in } L1 , id { addtype ( id.entry , L.in ) } L  id { addtype ( id.entry , L.in ) }

11 Fig.5.32. At each node for L, L.in = T.type.
If we ignore the actions in the above translation scheme, the sequence of moves made by the parser on the input of Fig is as in Fig For clarity, we show the corresponding grammar symbol instead of a stack state and the actual identifier instead of the token id.

12 Input state Production Used real p,q,r − real T T → real ,q,r T p T L
L → id q,r T L , ,r T L , q L → L , id r T L , r D D → T L Fig Whenever a right side for L is reduced, T is just below the right side.

13 Fig. 5.34. The value of T.type is used in place of L.in
Production Code Fragment D  T L ; T  int val [ntop] := integer T  real val [ntop] := real L  L1 , id addtype (val [top] , val [top−3] ) L  id addtype (val [top] , val [top−1] ) Fig The value of T.type is used in place of L.in

14 S → aAC C.i := A.s S → bABC C → c C.s := g ( C.i )
Example 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. Production Semantic Rules S → aAC C.i := A.s S → bABC C → c C.s := g ( C.i )

15 Fig. 5.35. Copying an attribute value through a marker M.
Production Semantic 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.s := M.i є (a) original production (b) modified dependencies Fig Copying an attribute value through a marker M.


Download ppt "Chapter 5. Syntax-Directed Translation"

Similar presentations


Ads by Google