The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
+2 votes
Consider the following parse tree:

                          /    |    \     
                 string    +     string
                /    |    \              |
       string     -    string      2
           |                 |
           9                5

Which of the following statement is true?

(A) Both + and - are having equal precendence

(B) + is having higher precedence

(C) - is having higher precedence

(D) None of these
asked in Compiler Design by Active (1.9k points) | 251 views

2 Answers

+8 votes
Best answer
See the task is easier here than if the grammar were given and then asked that which operator has more precedence.But here we can answer about the precedence by simply examining the dervation tree.The behaviour is similar to recursion tree if you recall

According to the tree , we know that calling is done in top down manner but execution is done in bottom up manner.So the '-' being lower in the tree will be executed first then the '+' which is at higher level.

So '-' has higher precedence than '+'.

Hence C) is correct option.
answered by Veteran (96.6k points)
selected by
dats what m confused....but the grammar is ambiguous na...u r seeing according to this instance of parse tree only?
what if S->S-S/S+S was given?

both - and + are on same level...but again since it is ambiguous how can we decide about precedence?
Ya it is an ambiguous grammar but it is not of concern since in this case - and + will be in same level of the derivation tree although the derivation tree is not unique.
0 votes
Option C will be right option for it.

The operator having low level have higher precedence compare to operator having low level.

That's option C will be right option for.
answered by Boss (6.2k points)

Related questions

0 votes
2 answers
asked in Compiler Design by thor Boss (9.2k points) | 341 views
+2 votes
1 answer
asked in Compiler Design by Rakesh K Active (1.9k points) | 82 views

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

29,115 questions
36,924 answers
34,782 users