GATE | GATE CS 1999 | Question 57

[5 Marks question]

Let synthesized attribute val  give the value of the binary number generated by S in the following grammar. For example, on output 101.101, S.val=5.625

S → LL | L

L → LB | B

B → 0 | 1



Write S-attributed values corresponding to each of the productions to find S.val.


Answer:

Explanation: For the above grammar, the S-attributed values corresponding to each of the productions to find S.val is given below.

Here, val represents value and nb represents number of bit.

S → L.L   { S.val=L1.val  + L2.val / 2^L2.nb  }
S  → L     {  S.val = L.val } 
L → LB     {  L.val =  2 *  L.val  + B.val   ,  L.nb = L.nb +  B.nb } 
L →  B     {  L.val = B.val   ,  L.nb = B.nb } 
B → 0     {  B.val = 0  ,  B.nb = 1 } 
B  →  1   {  B.val = 1  ,  B.nb = 1 }  


Quiz of this Question

My Personal Notes arrow_drop_up

Improved By : ApurvaRaj

Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.