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

Consider the translation scheme shown below.

S $\rightarrow$ T R

R $\rightarrow$ + T {print(‘+’);} R$\mid \varepsilon$

T  $\rightarrow$ num {print(num.val);}

Here num is a token that represents an integer and num.val represents the corresponding integer value. For an input string ‘$9 + 5 + 2$’, this translation scheme will print

  1. $9 + 5 + 2$
  2. $9 \ 5 + 2 +$
  3. $9 \ 5 \ 2 + +$
  4. $+ + 9 \ 5 \ 2$
asked in Compiler Design by Veteran (59.4k points)
edited by | 2k views

2 Answers

+32 votes
Best answer

answer = option B
$9\ 5+2+$

answered by Boss (30.6k points)
edited by
0

Could someone explain this solution in words a little bit ?
Im having trouble in understainding how

9+5 prints 95+ ?

9+5+2

  • T+5+2  ( T-> num  output : 9 )
  •  

Im stuck at here , How to proceed ?

0
in L attribute we evaluate the node first time when we visit it.

so according to this answer should be 9+5+2 ???
+13 votes
Input Translation Output
9 + 5 + 2 S → T R  
9 + 5 + 2 T  → num {print(num.val);} 9
+ 5 + 2 R → + T {print(‘+’);} R  (the + is simply consumed as there is no print corresponding to it)
5 + 2 T  → num {print(num.val);} 5+
+ 2  R → + T {print(‘+’);} R  
2 T  → num {print(num.val);} 2+

 

So, output 95+2+. Option B.

answered by Veteran (342k points)
+2
sir why not option (c)...
+1
@SONAM

check the semantic action location
+1
@Pranabesh Ghosh 1

Even i also felt the same doubt. But it seems like the actual question in gate with respect to what is mentioned here is different. Notice the location of semantic action.
0
In L attributed SDT we can specify the actions anywhere in the production.

Is it L attributed SDT or S attributed SDT sir?
0

@AnilGoudar It is L attributed SDT

0
Can we think of it as print statement embedded in the production?
0
Please explain how it is L attributed SDT ?


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,507 questions
42,829 answers
121,693 comments
42,183 users