in Compiler Design edited by
7,029 views
38 votes
38 votes

Consider two binary operators $\text{‘} \uparrow \text{’}$  and $\text{‘} \downarrow \text{’}$ with the precedence of operator $\downarrow$ being lower than that of the operator $\uparrow$. Operator $\uparrow$ is right associative while operator $\downarrow$ is left associative. Which one of the following represents the parse tree for expression $(7 \downarrow 3 \uparrow 4 \uparrow 3 \downarrow 2)$



  1.  


  2.  


in Compiler Design edited by
7.0k views

4 Answers

51 votes
51 votes
Best answer

Answer is B.

To make the parse tree start compiling the identifiers into blocks based on associativity and precedence.

Grouping: $(7 \downarrow (3 \uparrow(4 \uparrow 3)))  \downarrow2 $

Tree can be made by opening inner braces and move towards braces.

edited by

1 comment

Is 3↑4↑3↑7↓2↓ also possible (assume there exist such a tree) ???????
1
1
12 votes
12 votes
Higher precedence operator comes at lowest level in the tree.And and if there is left recursion on operator then it is left assosiative ,same if right recursive then it is right associative. Scince tree is given it can be easily seen.As B) is correct.
1 vote
1 vote

Higher precedence operators comes at the bottom

↑ has higher precedence so  $3↑4↑3$ will be evaluated first.As ↑ is left associative so $4↑3$ will be evaluated so $4↑3$ should be at last level then 

$3↑(4↑3)$

↓ i this is left associative so left operand i.e & will be evaluated 

$(7↓(3↑(4↑3))$

and then 2 

$((7↓(3↑(4↑3))↓2)$

which gives option (B)

 

by
0 votes
0 votes
answer - B

3 Comments

How B Explain In your words
0
0
Since the precedence of ↑ is higher, the sub-expression ([3 ↑ 4 ↑ 3) will be evaluated first. In this sub-expression, 4 ↑ 3 would be evaluated first because ↑ is right to left associative. So the expression is evaluated as ((7 ↓ (3 ↑ (4 ↑ 3))) ↓ 2). Also, note that among the two ↓ operators, first one is evaluated before the second one because the associativity of ↓ is left to right.
1
1
Right on!
0
0
Answer:

Related questions