chapter 5. syntax-directed translation
DESCRIPTION
Chapter 5. Syntax-Directed Translation. Fig. 5.2. Syntax-directed definition of a simple desk calculator. Fig. 5.3. Annotated parse tree for 3*5+4 n. Fig. 5.4. Syntax-directed definition with inherited attribute L.in. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Chapter 5. Syntax-Directed Translation](https://reader033.vdocument.in/reader033/viewer/2022061321/5681489d550346895db5b269/html5/thumbnails/1.jpg)
Chapter 5.
Syntax-Directed
Translation
![Page 2: Chapter 5. Syntax-Directed Translation](https://reader033.vdocument.in/reader033/viewer/2022061321/5681489d550346895db5b269/html5/thumbnails/2.jpg)
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
![Page 3: Chapter 5. Syntax-Directed Translation](https://reader033.vdocument.in/reader033/viewer/2022061321/5681489d550346895db5b269/html5/thumbnails/3.jpg)
3
Fig. 5.3. Annotated parse tree for 3*5+4n.
L
nE.val = 19
T.val = 4
F.val = 4
digit.lexval = 4
T.val = 15
E.val = 15
T.val = 3
F.val = 3
digit.lexval = 3
* F.val = 5
+
digit.lexval = 5
![Page 4: Chapter 5. Syntax-Directed Translation](https://reader033.vdocument.in/reader033/viewer/2022061321/5681489d550346895db5b269/html5/thumbnails/4.jpg)
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 addtype ( id.entry , L.in )
![Page 5: Chapter 5. Syntax-Directed Translation](https://reader033.vdocument.in/reader033/viewer/2022061321/5681489d550346895db5b269/html5/thumbnails/5.jpg)
5
Fig. 5.5. Parse tree with inherited attribute in at each node labeled L.
D
L.in = realT.type = real
real
,
id1
id2
id3L.in = real
L.in = real
,
![Page 6: Chapter 5. Syntax-Directed Translation](https://reader033.vdocument.in/reader033/viewer/2022061321/5681489d550346895db5b269/html5/thumbnails/6.jpg)
6
Fig. 5.6. E.val is synthesized from E1.val and E2.val
val val
valE
+E1 E2
![Page 7: Chapter 5. Syntax-Directed Translation](https://reader033.vdocument.in/reader033/viewer/2022061321/5681489d550346895db5b269/html5/thumbnails/7.jpg)
7
Fig. 5.7. Dependency graph for parse tree of Fig. 5.5
D
LT
real
,
id1
id2
id3L
L
,
4type
in 5
3 entry
2 entry
1 entry
in 7
in 9
6
8
10
![Page 8: Chapter 5. Syntax-Directed Translation](https://reader033.vdocument.in/reader033/viewer/2022061321/5681489d550346895db5b269/html5/thumbnails/8.jpg)
8
Fig. 5.9. Syntax-directed definition for constructing a syntax tree for an expression
Production Semantic Rules
E E1 + T E.nptr := mknode(‘+’, E1.nptr, T.nptr)
E E1 - T E.nptr := mknode(‘-’, E1.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)
![Page 9: Chapter 5. Syntax-Directed Translation](https://reader033.vdocument.in/reader033/viewer/2022061321/5681489d550346895db5b269/html5/thumbnails/9.jpg)
9
Fig. 5.10. Construction of a syntax-tree for a-4+c
![Page 10: Chapter 5. Syntax-Directed Translation](https://reader033.vdocument.in/reader033/viewer/2022061321/5681489d550346895db5b269/html5/thumbnails/10.jpg)
10
Fig. 5.11. Dag for the expression a+a*(b-c)+(b-c)*d.
![Page 11: Chapter 5. Syntax-Directed Translation](https://reader033.vdocument.in/reader033/viewer/2022061321/5681489d550346895db5b269/html5/thumbnails/11.jpg)
11
Fig. 5.12. Instructions for constructing the dag of Fig. 5.11
(1) p1 := mkleaf ( id, a )
(2) p2 := mkleaf ( id, a )
(3) p3 := mkleaf ( id, b )
(4) p4 := mkleaf ( id, c )
(5) p5 := mknode ( ‘-’, p3, p4 )
(6) p6 := mknode ( ‘*’, p2, p5 )
(7) p7 := mknode ( ‘+’, p1, p6 )
(8) p8 := mkleaf ( id, b )
(9) p9 := mkleaf ( id, c )
(10) p10 := mknode ( ‘-’, p8, p9 )
(11) p11 := mkleaf ( id, d )
(12) p12 := mknode ( ‘*’, p10, p11 )
(13) p13 := mknode ( ‘+’, p7, p12 )
![Page 12: Chapter 5. Syntax-Directed Translation](https://reader033.vdocument.in/reader033/viewer/2022061321/5681489d550346895db5b269/html5/thumbnails/12.jpg)
12
Fig. 5.15. Parser stack with a field for synthesized attributes
![Page 13: Chapter 5. Syntax-Directed Translation](https://reader033.vdocument.in/reader033/viewer/2022061321/5681489d550346895db5b269/html5/thumbnails/13.jpg)
13
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
![Page 14: Chapter 5. Syntax-Directed Translation](https://reader033.vdocument.in/reader033/viewer/2022061321/5681489d550346895db5b269/html5/thumbnails/14.jpg)
14
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 pr
oduction 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
![Page 15: Chapter 5. Syntax-Directed Translation](https://reader033.vdocument.in/reader033/viewer/2022061321/5681489d550346895db5b269/html5/thumbnails/15.jpg)
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 { L1.in := L.in } L1 , id { addtype ( id.entry , L.in ) }L id { addtype ( id.entry , L.in ) }
![Page 16: Chapter 5. Syntax-Directed Translation](https://reader033.vdocument.in/reader033/viewer/2022061321/5681489d550346895db5b269/html5/thumbnails/16.jpg)
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.
D
LT
real
,
p
q
rL
L
,
typein
in
in
Fig.5.32. At each node for L, L.in = T.type.
![Page 17: Chapter 5. Syntax-Directed Translation](https://reader033.vdocument.in/reader033/viewer/2022061321/5681489d550346895db5b269/html5/thumbnails/17.jpg)
17
Fig. 5.33.Whenever a right side for L is reduced, T is just below the right side.
Input state Production 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
![Page 18: Chapter 5. Syntax-Directed Translation](https://reader033.vdocument.in/reader033/viewer/2022061321/5681489d550346895db5b269/html5/thumbnails/18.jpg)
18
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] )
![Page 19: Chapter 5. Syntax-Directed Translation](https://reader033.vdocument.in/reader033/viewer/2022061321/5681489d550346895db5b269/html5/thumbnails/19.jpg)
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.
Production Semantic Rules
S → aAC C.i := A.s
S → bABC C.i := A.s
C → c C.s := g ( C.i )
![Page 20: Chapter 5. Syntax-Directed Translation](https://reader033.vdocument.in/reader033/viewer/2022061321/5681489d550346895db5b269/html5/thumbnails/20.jpg)
20
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
S
Cb A Bs
i
S
Cb A Bs i
Mi s
є
(a) original production (b) modified dependencies
![Page 21: Chapter 5. Syntax-Directed Translation](https://reader033.vdocument.in/reader033/viewer/2022061321/5681489d550346895db5b269/html5/thumbnails/21.jpg)
21Fig. 8.7. Semantic rules generating code for a while statement
Production Semantic Rules
S -> while E do S1 S.begin := newlabel ;
S.after := newlabel ;
S.code := gen( S.begin ‘ :’) ||
E.code ||
gen( ‘ if ’ E.place ‘ = ‘ ‘ 0 ’ ‘ goto’ S.after ) ||
S1.code ||
gen(‘ goto’ S.begin ) ||
gen( S.after ‘ : ‘ )