## 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?

 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
Compiler-Design       Associativity-and-Precedence       Gate 2018
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 __________.

 A 9 B 10 C 11 D 12
Compiler-Design       Associativity-and-Precedence       2016 set-01
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
 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 --> Id
```
Which one of the following is TRUE?
 A + is left associative, while ∗ is right associative B + is right associative, while ∗ is left associative C Both + and ∗ are right associative D Both + and ∗ are left associative
Compiler-Design       Associativity-and-Precedence       Gate 2014 Set -02
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.
There are 3 questions to complete.