The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+18 votes
1.8k 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 (69k points)
edited by | 1.8k views

2 Answers

+31 votes
Best answer

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

answered by Veteran (31k points)
edited by

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 ?

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

so according to this answer should be 9+5+2 ???
+11 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 (346k points)
sir why not option (c)...
@SONAM

check the semantic action location
@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.
In L attributed SDT we can specify the actions anywhere in the production.

Is it L attributed SDT or S attributed SDT sir?

@AnilGoudar It is L attributed SDT

Can we think of it as print statement embedded in the production?


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

33,705 questions
40,252 answers
114,342 comments
38,861 users