Note: No marks will be given if the justification for your answer is not provided.
short byte
Assume that Assume x is of type float, y is of type byte, and z is of type char. Coercion statements
are of the form a=(type) b.
(a) What is the process that generates t1= (float) y; called? Explain what is wrong with the
following sequence of 3 address statement: t1 = (float) y; t2 = (float) z; t3 = t1+t2; 4
(b) Write the correct sequence of 3 address statements. 4
Solution: (a) The process that generates t1= (float) y; is called type widening. (1 mark)
The translation of y+z must be carried out without knowing that the result will be assigned to a
value x of type float. The mistake in this example is that translation assumes that the value of x
is going to be float.
(b) Correct code
t1 = (int) y
t2 = (int) z
t3 = T1+T2
x = (float) t3
2. Consider the following boolean expression, which is translated to intermediate code in the normal
left-to-right manner, and a subexpression is not evaluated further if expressions to its left have already
determined its truth or falsehood.
-------------B6-------------- The elementary boolean conditions are represented by B1, B2,
---------B5-------- and B3. Subexpression B4 is the OR of B2 and B3; and so on.
--------B4-------- You are supposed to implement this translation using backpatch-
--B1-- --B2-- --B3-- ing, where for each boolean expression B, thre are two lists of
a == b && !(c == d || e == f) places in the code for B, which is denoted by B.true and B.false.
There are rules to derive the lists for B4, B5, B6 from the the lists for their subexpressions. Write
each of them down and explain. 10
Solution:
Hint: Note that when an AND operator is processed, the true list for the left operand is backpatched
to point to the beginning of the code of the right operand. However, false jumps from the code
for either operand are merged into the false list for the entire subexpression. An OR is treated
P.T.O.. . . 1
CS F363
similarly, except it is the false list of the left operand that is backpatched. Finally, the NOT
operator is implemented simply by swapping the true and false lists.
3. Given that the grammar below, remove the left-recursion from the grammar. You MUST show ALL
steps; for any non-terminal S, introduce a new non-terminal with name S 0 and write the final gram-
mar: (0) A → B | a | CBD (1) B → C | b (2) C → A | c (3) D → d 6
4. Given that the grammar below (S being the start symbol), show ALL steps to verify whether this
grammar is LL(1): (0) S → F GG (1) F → f F | (2) G → gF | 10
The grammar is not LL(1) due to multiple entries in M[G,g]. 6 mark for table.
P.T.O.. . . 2
CS F363
Derive the rules to compute S.next for S4 through S8 from components of the statement S. 10
Solution: You need to compute S4, S5, S6, S7 and S8. The hint is given below. 1. S4: A while-
statement can only be left when the boolean expression is false; otherwise, control stays within the
loop.
2. S5: You leave an if-statement if either the condition is false, or the body of the statement has
been executed.
3. S6: A block of statements is left only when the last statement is left.
4. S7: You leave an if-then-else statement by completing either the then-part or the else-part.
5. S8: Same argument as (1).
Solution: The Expression tree and Ershov numbers are: (No mark for leaf level nodes. 1 mark
for intermediate nodes. There are 6 intermediate nodes.)
/ 3
a 1 b 1 c 1 d 1 e 1 f 1
P.T.O.. . . 3
CS F363
gen_bb_code(/,1,2)
|
gen_bb_code(*,1,2)
|
gen_bb_code(+,1,2)
|
gen_bb_code(+,1)--> // Here you have show code generation (3 marks)
|
|
|--> // Here you have show code generation // 1 mark
gen_bb_code(+,1,2)
|
gen_bb_code(+,1)-->// Here you have show code generation (3 marks)
|
|
|--> // Here you have show code generation // 1 mark
|
|
|--> // Here you have show code generation // 1 mark
|
gen_bb_code(UM, 1, 2)
|
gen_bb_code(UM, 1)
|
gen_bb_code(+, 1)-->// Here you have show code generation (3 marks)
|
|
|--> // Here you have show code generation for UMINUS // 1 mark
|--> // Here you have show code generation // 1 mark
|
P.T.O.. . . 4
CS F363
I0 a I6
I1 S I2 + I4
S S
a
a
I3
a
I7 +
I5
(b) Based on the DFA above you can derive the LR(1) parsing table. It won’t have any shift/reduce
conflict. Clearly, this grammar is suitable for the LR(1) parsing method.
8 marks for 8 rows.
4 a 6 b 8
0 1 b 2 3 5 b 7 a 9 10 11 b 12 13
(a) Does this NFA accept the string bbabbbba? Explain your answer using the NFA. 2
(b) What is the regular expression that this NFA accept? 3
(c) Convert this NFA to a DFA using a suitable algorithm by showing every step in detail. 5
Solution: (a) The word w = ”bbabbbba” belongs to the language generated by this RE because
there is no path from the start state 0 to the accepting state 13 that spells out the word. It stops
at state 12.
(b) It is able to recognize the sentences generated by the regular expression b ∗ (ab|ba)b∗
P.T.O.. . . 5
CS F363
(c) Using the -closure, we have the following mapping of set of states of the NFA to sets of states
of the DFA:
I0 = -closure (0) =0,1,3,4,5
I1 = DFAedge(I0,a) = e-closure (0, 1, 3, 4, 5, a) = 6
I2 = DFAedge(I0,b) = e-closure(0,1,3,4,5,b)=1,2,3,4,5,7
I3 = DFAedge(I1,a) = e-closure (6, a) = Ierr
I4 = DFAedge(I1,b) = e-closure (6, b) = 8, 10, 11, 13
I5 = DFAedge(I2,a) = e-closure(1,2,3,4,5,7,a)=6,9,10,11,13
........
I13 = DFAedge(I10,a) = e-closure (8, 10, 11, 12, 13, a) = Ierr
I14 = DFAedge(I10,b) = e-closure (8, 10, 11, 12, 13, b) = I8
Effectively there are only 8 states and this is clearly not the minimal DFA that can recognize this
regular expression.
Solution: There is no unique answer, but the following points must be considered while designing
the error handler.
e1() in 0,2,5:
From the table we can see that the action part for states 0, 2 and 5 are the same. In these three
states, it expects either an if stmt or a non-if statement (a), nothing else. Getting else or getting
end of input is not acceptable. So the error routine e1 should indicate that either an if stmt or a
non-if statement is expected.
e1(): missing if or any-other-statement.
e2() in 3, 6
State 3 and 6 are end of a ”any-other” statement or an if statement respectively. At this point
if or any-other keyword is NOT expected. This can be indicated by the error routine e2. e2():
missing else or end of input.
e3(): State 4 means if-then has been consumed and it is the middle of an if-then-else. So it should
either get an else followed by a statement or it should be an end of the if-then (when there is no
else) statement. At this point it is not expected to have another ”if” or ”any-other” statement.
Though this looks similar to e2, it is better to have another error handler e3. e3(): within if-then
statement. Expecting matching else
P.T.O.. . . 6
CS F363
State 1 means it is going to be the end of input. No other keyword is expected. One should have
another error handling routine e4().
e4(): Only end of input expected
Furthermore, it is also a good idea to have same error handler for a given column as much as
possible.
Now the table can be easily constructed.
END 7