GATE / CS ARCHIVE

GATE CSE 1995 / QUESTION 9(a)

Build an arithmetic syntax tree

Compiler Design · Intermediate Code · Full GATE CSE 1995 paper

GATE 1995, Q9(a) Descriptive
Report issue

Intermediate Code

Question 9(a), GATE 1995: Translate a * −(b+c) into an abstract syntax tree.
Tap to enlarge
Answer & solution

Answer: mul(a, neg(add(b,c))) (no official key was published)

The parentheses force addition of b and c first. The minus sign immediately before the parentheses is unary negation, since it has only the parenthesized expression as operand. Multiplication then combines a with that negated result.

The tree is:
        *
       / \
      a  unary−
            |
            +
           / \
          b   c

Equivalently, its exact parenthesized representation is mul(a,neg(add(b,c))). Leaves are identifiers and internal nodes are operations. The abstract syntax tree need not contain separate parenthesis nodes because the tree structure already records grouping. It is important not to turn the unary minus into a binary subtraction with a missing left child. A code generator could evaluate add(b,c), negate its result, and finally multiply by a; the tree expresses that dependency regardless of the eventual instruction order.

Original paper, page 6