• Introduction
• 1. 1.Introduction
• 2. 2.A Simple Syntax-Directed Translator
• 3. 3.Lexical Analysis
• 4. 4.Syntax Analysis
• 5. 5.Syntax-Directed Translation
• 6. 6.Intermediate-Code Generation
• 7. 7.Run-Time Environments
• 8. 8.Code Generation
• 9. 12.InterProcedural Analysis
• Published with GitBook

# 5.2 节的练习

### 5.2.1

#### 解答

``````[ 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
[ 1, 2, 3, 5, 4, 6, 7, 8, 9 ],
[ 1, 2, 4, 3, 5, 6, 7, 8, 9 ],
[ 1, 3, 2, 4, 5, 6, 7, 8, 9 ],
[ 1, 3, 2, 5, 4, 6, 7, 8, 9 ],
[ 1, 3, 5, 2, 4, 6, 7, 8, 9 ],
[ 2, 1, 3, 4, 5, 6, 7, 8, 9 ],
[ 2, 1, 3, 5, 4, 6, 7, 8, 9 ],
[ 2, 1, 4, 3, 5, 6, 7, 8, 9 ],
[ 2, 4, 1, 3, 5, 6, 7, 8, 9 ]
``````

### 5.2.2

1. int a, b , c
2. float w, x, y, z

1. int a, b, c

### 5.2.3

1. A.s = B.i + C.s
2. A.s = B.i + C.s , D.i = A.i + B.s
3. A.s = B.s + D.s
4. ! A.s = D.i , B.i = A.s + C.s , C.i = B.s , D.i = B.i + C.i

1. 否， ？
2. 否， 是
3. 是， 是
4. 否， 否

### 5.2.4 ！

``````S -> L.L|L
L -> LB|B
B -> 0|1
``````

#### 解答

1) S -> L_1.L_2 L_1.isLeft = true
L_2.isLeft = false
S.val = L_1.val + L_2.val
2) S -> L L.isLeft = true
S.val = L.val
3) L -> L_1B L_1.isLeft = L.isLeft
L.len = L_1.len + 1
L.val = L.isLeft ? L_1.val * 2 + B.val : L_1.val + B.val * 2^(-L.len)
4) L -> B L.len = 1
L.val = L.isLeft ? B.val : B.val/2
5) B -> 0 B.val = 0
6) B -> 1 B.val = 1

• isLeft 为继承属性，表示节点是否在小数点的左边
• len 为综合属性，表示节点包含的二进制串的长度
• val 为综合属性

### 5.2.5 ！！

#### 解答

1) S -> L_1.L_2 S.val = L_1.val +　L_2.val/L_2.f
2) S -> L S.val = L.val
3) L -> L_1B L.val = L_1.val*2 + B.val
L.f = L_1.f * 2
4) L -> B L.val = B.val
L.f = 2
5) B -> 0 B.val = 0
6) B -> 1 B.val = 1