Associativity-and-Precedence
Question 1 |
Consider the following parse tree for the expression a#b$c$d#e#f, involving two binary operators $ and #.

Which one of the following is correct for the given parse tree?
$ has higher precedence and is left associative; # is right associative
| |
# has higher precedence and is left associative; $ is right associative
| |
$ has higher precedence and is left associative; # is left associative | |
# has higher precedence and is right associative; $ is left associative
|
Question 1 Explanation:
Since $ will be evaluated before # so $ has higher precedence and the left $ i.e., in b$c$d the left “$” (i.e., b$c) will be evaluated first so it is left associative, whereas # is right associative (as in d#e#f) , the right one (i.e., e#f) will be evaluated first.
Question 2 |
The attributes of three arithmetic operators in some programming language are given below.
Operator Precedence Associativity Arity + High Left Binary − Medium Right Binary ∗ Low Left Binary
The value of the expression 2 – 5 + 1 – 7 * 3 in this language is __________.
9 | |
10 | |
11 | |
12 |
Question 2 Explanation:
+ has highest precedence, so it will be evaluated first.
2 − 5 + 1 − 7 * 3 = 2 − (5 + 1) − 7 * 3 = 2 − 6 − 7 * 3
Now, − has more precedence than *, so sub will be evaluated before * and – has right associative so (6 − 7) will be evaluated first.
2 − 6 − 7 * 3 = (2 − (6 − 7)) * 3 = (2 – (−1)) * 3 = 3 * 3 = 9
2 − 5 + 1 − 7 * 3 = 2 − (5 + 1) − 7 * 3 = 2 − 6 − 7 * 3
Now, − has more precedence than *, so sub will be evaluated before * and – has right associative so (6 − 7) will be evaluated first.
2 − 6 − 7 * 3 = (2 − (6 − 7)) * 3 = (2 – (−1)) * 3 = 3 * 3 = 9
Question 3 |
Consider the grammar defined by the following production rules, with two operators ∗ and +
S --> T * P T --> U | T * U P --> Q + P | Q Q --> Id U --> IdWhich one of the following is TRUE?
+ is left associative, while ∗ is right associative | |
+ is right associative, while ∗ is left associative | |
Both + and ∗ are right associative | |
Both + and ∗ are left associative |
Question 3 Explanation:
In production: T ⟶ T * U, since T is left recursive, hence * is left associative.
P ⟶ Q + P, here P is right recursive, so + is right associative.
P ⟶ Q + P, here P is right recursive, so + is right associative.
There are 3 questions to complete.