• 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

6.2 节的练习

6.2.1

1. 抽象语法树
2. 四元式序列
3. 三元式序列
4. 间接三元式序列

解答

1. 抽象语法树

1. 四元式序列

op arg1 arg2 result
0 + b c t1
1 minus t1 t2
2 + a t2 t3
2. 三元式序列

op arg1 arg2
0 + b c
1 minus (0)
2 + a (1)
3. 间接三元式序列

op arg1 arg2
0 + b c
1 minus (0)
2 + a (1)
instruction
0 (0)
1 (1)
2 (2)

6.2.2

1. a = b[i] + c[j]
2. a[i] = b*c - b*d
3. x = f(y+1) + 2
4. x = *p + &y

解答

1. a = b[i] + c[j]

• 四元式

``````  0) =[]   b    i    t1
1) =[]   c    j    t2
2) +     t1   t2   t3
3) =     t3        a
``````
• 三元式

``````  0) =[]   b    i
1) =[]   c    j
2) +     (0)  (1)
3) =     a    (2)
``````
• 间接三元式

``````  0) =[]   b    i
1) =[]   c    j
2) +     (0)  (1)
3) =     a    (2)

0）
1）
2）
3）
``````
2. a[i] = b*c - b*d

• 四元式

``````  0) *    b    c    t1
1) *    b    d    t2
2) -    t1   t2   t3
3) []=  a    i    t4
4) =    t3        t4
``````
• 三元式

``````  0) *    b    c
1) *    b    d
2) -    (0)  (1)
3) []=  a    i
4) =    (3)  (2)
``````
• 间接三元式

``````  0) *    b    c
1) *    b    d
2) -    (0)  (1)
3) []=  a    i
4) =    (3)  (2)

0)
1)
2)
3)
4)
``````
3. x = f(y+1) + 2

• 四元式

``````  0) +        y    1    t1
1) param    t1
2) call     f    1    t2
3) +        t2    2   t3
4) =        t3        x
``````
• 三元式

``````  0) +        y     1
1) param    (0)
2) call     f     1
3) +        (2)   2
4) =        x     (3)
``````
• 间接三元式

``````  0) +        y     1
1) param    (0)
2) call     f     1
3) +        (2)   2
4) =        x     (3)

0)
1)
2)
3)
4)
``````