Anda di halaman 1dari 7

BITS, Pilani- K K Birla Goa Campus Date: 03/05/2016

Course: CS F363 Semester-II, 2015-16, Comprehensive Marks: 100 Time: 14:00-17:00


Compiler construction Weightage: 50% Mode: Open Book Duration: 180 min

Note: No marks will be given if the justification for your answer is not provided.

1. Consider the type hierarchy and the assignment statement x=y+z;


double float long int char

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

Solution: Order A, B, C, D in this manner.


i=1, 2 nothing happens
1. Replace C → A to C → B | a | CBD [i=3, j=1]
2. Replace C → B to C → C | b [i=3, j=2]
3. Here you will have two immediate left recursions. Now, the production C → C is useless and
does not change the grammar. It can be dropped. Rest is easy.
You can order D, C, B, A as well:
i=1,2 nothing happens
0. Replace B → C to B → c | A [i=3, j=2]
1. Replace A → CBD to A → cBD | ABD [i=4, j=2]
2. Replace A → B to A → c | b | A [i=4, j=3]
3. You will have two immediate left recursions. Now, the production A → A is useless and does
not change the grammar. It can be dropped. After eliminating left recursion you can easily
get the new grammar.

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

Solution: First(f)={f} First(g)={g} First(S)={f, g, } First(F)={f, } First(G)={g, } 2 marks


++++
Follow(S)={$} Follow(F)={g, $} Follow(G)={g, $} 2 marks

The grammar is not LL(1) due to multiple entries in M[G,g]. 6 mark for table.

5. Consider the following sketch of C code:


Here Ei represents some boolean expression and S1, S2, and
S8| while (E1) {
S3 represent some statement that does not involve control-flow.
| S7| if (E2)
Statements S4 through S8 represent the higher-level control-
| | S4| while (E3)
flow constructs. For example, S4 represents the while-statement
| | | S1;
while(E3)S1.
| | else {
This translation needs to be implemented using backpatching,
| | S6| S5| if (E4)
where the attribute S.next will maintain a list of places where
| | | | S2;
you must put the label to which control flows when S is finished.
| | | S3
To accomplish this translation, you have to define suitable at-
| | } }
tributes for the boolean expressions E1..E4.

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).

2 marks per rule.

6. Consider the arithmatic expression (a+b)*(c+d)/-(e+f)


(a) Compute the Ershov numbers for this expression. 6
(b) Generate the code using two registers by following the instruction below. 14
The function gen bb code(Node v, int b, int r) gen- gen_bb_code(*,1,2)
erates code considering an upper limit of registers r re- |
cursively. This function calls an overloaded function |gen_bb_code(+,1,2)
gen bb code(Node v, int b) that generates code with- | |
out any restriction on the number of registers, using op- | |gen_bb_code(-,1)--> DIV R1,R1,R0
| | ADD R0,R1,R1
timal number of registers. Show the recursive call trace
| | UMINUS R1
of gen bb code(Node v, int b, int r) and the gener-
| |--> LD R1, e
ated code-fragment in each recursive call as shown here. It | | ADD R0, R1, R1
is sufficient to show the code fragment generated by the | |
overloaded function gen bb code(Node v, int b), with- | |gen_bb_code(/,1,2)
out showing its call-trace. |

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

The expression tree with * in


the root with a right child ”/”
* 3 um 2
is not correct, since * and /
are left associative and have
same precedence. The call-
sequence and code-generation
+ 2 + 2 + 2
is as follows:

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
|

7. For the grammar: S 0 → S S → SS+ | a


(a) Construct the set of LR(1) items and the DFA capable of recognizing it. 9
(b) Construct the LR(1) parsing table to determine if this grammar is LR(1). 8

Solution: (a) There are eight sets of items:


I0: [S 0 → •S, $], [S → •SS+, $a], [S → •a, $a]
I1: [S 0 → S•, $], [S → S • S+, $a], [S → •SS+, +a], [S → •a, +a]
I2: [S → SS • +, $a], [S → S • S+, +a], [S → •SS+, +a], [S → •a, +a]
I3: [S → SS • +, +a], [S → S • S+, +a], [S → •SS+, +a], [S → •a, +a]
I4: [S → SS + •, $a]
I5: [S → SS + •, +a]
I6: [S → a•, $a]

P.T.O.. . . 4
CS F363

I7: [S → a•, +a]

4 marks for 8 sets.


The figure below depicts the DFA that recognizes the set of valid LR(1) items for this grammar.
There are 10 edges, 5 marks for 10 edges.

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.

8. Consider the following NFA.

 
 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.

9. Consider the simplified dangling else grammar S 0 → S S → i S e S | i S | a


State i e a $ S Here a is a simplified terminal that denotes everything else. The
0 s2 s3 1 shift/reduce conflict is resolved and the SLR parsing table shown
1 acc in the left is created. Fill the empty slots with error recovery
2 s2 s3 4 routines e1, e2 etc using phrase level recovery. For each error re-
3 r3 r3 covery routine write the diagnostic string like “missing ...”. Note
4 s5 r2 that trivial answers like each empty cell filled with a new error
5 s2 s3 6 handling routine or all cells with one error handling routine will
6 r1 r1 result in zero marks. 9

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

Anda mungkin juga menyukai