Question

Consider the following parse tree for the expression a#b$c$d#e#f, involving two binary operators $ and #.

Parse tree for the expression a#b$c$d#e#f showing precedence and associativity of operators # and $.

Which one of the following is correct for the given parse tree?

Choose one option
Official answer and solution
Official answer
A
From
Official GATE 2018 answer key

Solution

Deduce operator precedence from tree depth and operator associativity from branch direction.

In a parse tree for an arithmetic expression:

  1. Precedence: Operators evaluated deeper in the tree have higher precedence because their results are computed first and passed upward. In the given tree, the subexpression containing $ operations ((b$c)$d(b \$ c) \$ d) is nested inside the # operations. Therefore, operator $ has higher precedence than #.
  1. Associativity:
  • For $: The subtree evaluating b$c$db \$ c \$ d is structured as ((b$c)$d)((b \$ c) \$ d), which grows to the left. Hence, $ is left associative.
  • For #: The expression evaluates a#(… )a \# (\dots), with # grouping to the right as a#(T#(e#f))a \# (T \# (e \# f)). Hence, # is right associative.

Thus, $ has higher precedence and is left associative; # is right associative, matching option A.

Final answer

A

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2018 Computer Science and Information Technology (CS)
Question
48 of 65, printed as CS Q.38
Section
Computer Science and Information Technology

Report an error in this question

GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.