CS F363 - Compre - Q
CS F363 - Compre - Q
1. Each record in a subtitles file shown below has three lines: the first is the sequence number, the second
shows the start and end time, in hours, minutes, seconds (separated by colons). The start and end
times are separated by -->. The third line is the dialogue text that must appear for the time duration
between the start and end times on the screen. Complete the flex program fragment that reads this
subtitle file and adds a delay of 7 seconds to each dialog, such that each dialogue would appear and
stay 7 seconds later than it would using the old subtitles file (but the durations of the staying do not
change). 10
Original file Complete Pattern1, 2, 3 and TODO code
%{
61 #include <time . h>
void a d d d e l a y ( char ∗ , char ∗ , i n t ) ;
00:04:52 --> 00:04:54 %}
When I was just a child in school %%
P a t t e r n 1 { /∗ TODO: add p r o p e r p r i n t s t m t ∗/ }
62 P a t t e r n 2 {char s [ 1 0 0 ] ; a d d d e l a y ( y y t e x t , s , 7 ) ; p r i n t f ( ”%s ” , s ) ; }
00:59:55 --> 00:59:58 ”−−>” { p r i n t f ( ” %s ” , y y t e x t ) ; }
I asked my teacher What should I try P a t t e r n 3 { /∗ TODO: P r i n t d i a l o g t e x t ∗/ }
%%
void a d d d e l a y ( char ∗ buf , char ∗mod , i n t d e l a y )
Output by the program should be: {
struct tm r e s u l t ; i n t SS , MM, HH;
SEQ: 61 s t r p t i m e ( buf , ”%T” , &r e s u l t ) ; // s t r i n g t o time
conversion
00:04:59 --> 00:05:01 SS= r e s u l t . t m s e c + d e l a y ;
When I was just a child in school // TODO: Code t o c o m p l e t e t h e d e l a y c a l c u l a t i o n
r e s u l t . t m s e c= SS ; r e s u l t . tm min= MM; r e s u l t . tm hour= HH;
SEQ: 62 s t r f t i m e (mod , s i z e o f (mod) , ”%T” , &r e s u l t ) ; // time t o
01:00:02 --> 01:00:05 s t r i n g conversion
}
I asked my teacher What should I try
2. Consider a grammar that generates a binary number with a decimal point. 1.S → L • L 2.S →
L 3.L → L B 4.L → B 5.B → 0 6.B → 1.
(a) Calculate the LR(0) set of items 3
(b) Create the parsing table so that there is no conflict. Then show the step by step parsing of the
string 110•01 13
(c) Create a S-attributed SDD and semantic rules to compute the decimal value of this binary number.
For instance, 110•011 should be 6.375. Rule: You must use attribute val for all NTs to store
the intermediate (as well as final) computations. Hint: You may require another attribute of a
non-terminal to compute the fractional part. 10
(d) Draw an annotated parse tree showing the detailed computation of attribute values for the string
10•01. Show the computation of the decimal value of this string at the root node of the tree. 8
3. Consider the following two rules, each representing a flow-of-control construct, as in the programming
language C. You must use the usual S and L attributes of S and C, such as S.next, S.code, C.code,
C.t, C.f . Use the function new Label() to generate a new label. Assume that the last intermediate
code in C.code jumps either to C.t or to C.f , depending on whether C is true or false. Use goto L to
jump to a particular label L. Answer the following.
(a) For S → if ( C ) S1 else S2 , the partially complete L-attributed SDD is {S1 .next = S.next,
L =new Label(), C.f =L }. Write the value of S.code to complete the SDD. After completing
the SDD, convert this SDD to an SDT. 4
(b) For S → do S1 while ( C ), write the L-attributed SDD and the corresponding SDT. 6
P.T.O.. . . 1
CS F363
4. For A → BCD, A, B, C, and D have two attributes: s: synthesized, and i: inherited. For each of
the sets of rules below, tell whether (i) the rules are consistent with an S-attributed definition (ii) the
rules are consistent with an L-attributed definition.
1. A.s = B.i + C.s , D.i = A.i + B.s
2. A.s = B.s + D.s
3. A.s = D.i , B.i = A.s + C.s , C.i = B.s , D.i = B.i + C.i 6
END 2