The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+15 votes
1.3k views

Consider the grammar with the following translation rules and E as the start symbol

$\begin{align*}E \rightarrow &  E_ 1\# \: T \\ &\vert T \end{align*}$
$\begin{align*}T \rightarrow & T_1 \& \: F \\ &\vert F \end{align*}$
$F \rightarrow \text{num}$
$\left\{E.value = E_1.value * T.value\right\}$
$\{E.value = T.value\}$
$\{T.value = T_1.value + F.value\}$
$\{T.value = F.value\}$
$\{F.value=num.value\}$

Compute E.value for the root of the parse tree for the expression:$2$ # $3$ & $5$ # $6$ & $4$

  1. $200$
  2. $180$
  3. $160$
  4. $40$
asked in Compiler Design by Veteran (59.4k points)
edited by | 1.3k views
+5
GATE 2004_45
 original  question was :

Consider the grammar with the following translation rules and E as the start symbol

E→ E1 # T       {E.value=E1.value ∗ T.value}
        | T             {E.value=T.value}
T→T1 & F        {T.value=T1.value + F.value}
       | F               {T.value=F.value}
F→num            {F.value=num.value}
0
please draw parse tree?

3 Answers

+32 votes
Best answer

we can do in simple manner ...

here # is multiplication and & is addition by semantics rules given in the question ...

and by observation of productions ..

  1. here &(+) is higher precedence than #(*). bcoz & is far from starting symbol ...
  2. and both &,# are left associative so we can solve the express in this way ...

$((2*(3+5))*(6+4)) =160$ so ans should be (C).

answered by Boss (14.8k points)
edited by
0

Can you please explain how and both &,# are left associative ???

+7
if there is left recursion then operator will be left associative .. or if right recursion then operator will be right associate ..

eg : E---> E+T(here E derive E+T so it is left recursive

      E---->T+E (here E derive T+E so it is right recursive
+3
Nice explanation.
Formal procedure of determining associativity and precedence in operator precedence grammar is using "Leading" and "Trailing". After computing leading and trailing of Nonterminals, we make operator precedence table that tells everything u want to know about associativity and precedence of operators in grammar.
+1
I am not able to prepare parse tree here. Can anyone help?
0

parse tree

–2 votes
C   will   be ans.
answered by Junior (679 points)
–4 votes
answer - B
answered by Loyal (9.2k points)
Answer:

Related questions



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

35,499 questions
42,768 answers
121,499 comments
42,151 users