GATE CSE
First time here? Checkout the FAQ!
x
+2 votes
250 views
$\begin{align*} &E\rightarrow E+E \qquad {\color{red}{\text{E.val} = \text{E}_1\text{.val} { \;\;\large + \;\; } \text{E}_2\text{.val}}} \\ &E\rightarrow E*E \; \qquad {\color{red}{\text{E.val} = \text{E}_1\text{.val} { \;\;\large - \;\; } \text{E}_2\text{.val}}} \\ &E\rightarrow E - E \qquad {\color{red}{\text{E.val} = \text{E}_1\text{.val} { \;\;\large * \;\; } \text{E}_2\text{.val}}} \\ &E\rightarrow \left ( E \right ) \;\;\; \; \qquad {\color{red}{\text{E.val} = \text{E}_1\text{.val}}} \\ &E\rightarrow \text{id} \;\;\; \; \;\;\; \qquad {\color{red}{\text{E.val} = \text{id}\text{.val}}} \\ \\ &\text{What is that value of the attribute comupted at the root when the expression} \\ &{\large\color{blue}{\left [ \left ( 3*3 \right )+\left ( 3*5 \right ) - 6 \right ]+7}} \quad \text{is evaluated using above SDT ?} \\ \end{align*}$
asked in Compiler Design by Boss (6.5k points) 8 115 247
retagged by | 250 views
answer is coming out to be -5

vaishali jhalani please check for any typo ...QS updated !

2 Answers

+3 votes
Best answer
$\begin{align*} &\text{Operator priority order } \\ &\rightarrow 1st \quad {\large \color{blue}{-}} \\ &\rightarrow 2nd \quad {\large \color{blue}{*}} \\ &\rightarrow 3rd \quad {\large \color{blue}{+}} \\ \\ \hline \\ &\text{While reducing to a production corresponding SDT is evaluated} \\ \\ &\Rightarrow \left ( {\color{red}{\left ( 3*3 \right )}}+{\color{red}{\left ( 3*5 \right )}} - 6 \right )+7 \\\\ &\Rightarrow \left ( 0 \;\;{\large \color{blue}{+}} \;\; {\color{red}{(- 2) \;\;{\large \color{red}{-}}\;\; 6}} \right ) + 7 \\\\ &\Rightarrow \left ( 0 \;\;{\large \color{blue}{+}} \;\; (-12) \right ) + 7 \\ \\ &\Rightarrow \left ( -12 \right ) + 7 \\ \\ &\Rightarrow -5 \\ \end{align*}$
answered by Veteran (56.9k points) 36 189 500
selected by
That is what..i was saying
I used word "priority" in the same sense as @arkriti is saying "precedence"
I have a doubt. Do the operators really have priority here?

The grammar seems to be E→E+E / E∗E / E−E / (E) / id

All the operators are at same level. So how is the priority decided? Shouldn't they all have equal priority?
The grammar seems ambiguous, so how is the priority/precedence decided here?

yes grammar is ambiguous so priority couldnt be decided(in general case)
but here they have explicitly mentioned that we have to evaluate the expression ..so we have 2 choices either give priority urself or use their gramamar and decide(this is what we have to do..see their levels )....if they would have mentioned everything in one line like  E→E+E / E∗E / E−E / (E) / id..then there was a confusion which was having higher priority...but again they have given grammar in levels..saying treat lowest with highest priority..

NOTE:for ambiguous grammars precednce is nt defined..we can play with it according our needs or according to their needs...this question is about satisfying their worries..!

Okay!! so its a trick. thanks @sudsho
+1 vote

see this one..little bit messy though..!

answered by Veteran (14.8k points) 15 151 316
@Akriti... - has more priority than + so it should come at lower level..
who said - has more priority than +.both have same priority but precedence is from left to right(As far as i know)
[(3*3) + (3*3) -6]---in this expression,both - and + have same priority
but + before 7 has the lowest priority among all,thats why it is at the highest
and [] has more precedence than any operator,so obviously it will be at lower level
Ya after that + ans - ,
In SDT that one is farthest from the start has high priority.
So - has more priority than +
did'nt get you..can u show ur SDT?whats ur answer??

This is the sdt tree..

"precedence is from left to right" We do not say that.

We say - Associativity is from left-to-right.
yes..sorry..associativity it is

Related questions

+1 vote
2 answers
2
asked in Compiler Design by vaishali jhalani Boss (6.5k points) 8 115 247 | 215 views
+2 votes
1 answer
3
asked in Compiler Design by Tushar Shinde Loyal (2.8k points) 4 63 108 | 191 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
Top Users Oct 2017
  1. Arjun

    23398 Points

  2. Bikram

    17078 Points

  3. Habibkhan

    8280 Points

  4. srestha

    6300 Points

  5. Debashish Deka

    5438 Points

  6. jothee

    4978 Points

  7. Sachin Mittal 1

    4772 Points

  8. joshi_nitish

    4348 Points

  9. sushmita

    3970 Points

  10. Rishi yadav

    3804 Points


Recent Badges

Commentator Shivam Chauhan
Notable Question set2018
Nice Comment srestha
Notable Question set2018
Regular Shankar Jha
Popular Question Shubhanshu
Good Comment mcjoshi
Notable Question antarachoudhury
Popular Question shweta12345
Good Comment Rohan Mundhey
27,325 questions
35,176 answers
84,121 comments
33,280 users