KOI: Some solutions to exercise 9

Question 1: Syntax Trees

A syntax tree

Question 2: Stack Machine Code

    rvalue a
    rvalue b
    <
    gofalse AFTERIF
WHILESTART:
    rvalue c
    rvalue d
    push 2
    *
    >
    gofalse AFTERWHILE
    lvalue b
    rvalue a
    push 1
    +
    =
    lvalue a
    rvalue c
    rvalue b
    rvalue c
    +
    rvalue d
    +
    *
    =
    lvalue a
    rvalue a
    push 1
    +
    =
    lvalue d
    rvalue c
    rvalue b
    rvalue c
    +
    rvalue d
    +
    *
    =
    goto WHILESTART
AFTERWHILE:
    lvalue b
    rvalue a
    push 1
    +
    =
AFTERIF:
    lvalue c
    rvalue a
    push 1
    +
    =

Question 3: Three-Address Code

a)
(1) if (a >= b) goto 14
(2) temp1 = d * 2
(3) if (c <= temp1) goto 13
(4) b = a + 1
(5) temp2 = b + c
(6) temp3 = temp2 + d
(7) a = c * temp3
(8) a = a + 1
(9) temp4 = b + c
(10) temp5 = temp4 + d
(11) d = c * temp5
(12) goto 2
(13) b = a + 1
(14) c = a + 1
b)

The flow graph

Question 4: Optimization

...


Thomas Padron-McCarthy (thomas.padron-mccarthy@oru.se) December 1, 2021