2.7k 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$
edited | 2.7k views

$9\ 5+2+$

edited
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 ???
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.

+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 ?
0
How to detect L attributed or S attributed in such grammar

1
2