Machine-translated by Google. Questions are authored in English.
Top

Discussion

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

Parse tree: the root # has left child a and right child #; that # has left child $ and right child # with children e and f; the $ has left child another $ with children b and c, and right child d

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

  • A.$ has higher precedence and is left associative; # is right associative
  • B.# has higher precedence and is left associative; $ is right associative
  • C.$ has higher precedence and is left associative; # is left associative
  • D.# has higher precedence and is right associative; $ is left associative

Answer: A

The tree shows that $ has higher precedence and is left associative; # is right associative.

Precedence: the $ operations sit below the # operations, so b$c$d is grouped first and becomes an operand of #. The operator applied first, lower in the tree, binds tighter, so $ has higher precedence.

Associativity of $: in b$c$d the lower $ joins b and c, and the upper $ joins that result with d, giving (b$c)$d, which is left associative.

Associativity of #: the tree reads a # ((b$c$d) # (e#f)), so each # takes everything to its right as its right operand. That is right associative. The options giving # the higher precedence, or calling # left associative, contradict the tree.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.