Uploaded by Ghonim.ce

compiler-final20-solved

advertisement
Final Exam, 4th of July 2020
Time allowed 3 hours
Compilers
7 Questions in 3 pages [90 degrees]
Benha University,
Faculty of Engineering at Shoubra,
4th Year Computer Systems Eng,
Electrical Engineering Department.
1. [15 degrees] Choose the most right answer (Use a table for answers):
1) Usually interpreted code executes faster than compiled code.
a) True
b) False
2) In ………. code fragments are attached to productions of a grammar.
a) syntax-directed definition
b) syntax-directed translation scheme
3) Usually, there is a symbol table for each …….
a) variable
b) scope
c) program
d) function
4) A …….. is a character sequence that conforms to a specific pattern.
a) token
b) pattern
c) lexeme
5) In regular expressions: rr* = r*r
a) True
d) production
b) False
6) In ….. , each state and each symbol there is an edge for this symbol leaving this state.
a) NFA
b) DFA
7) A grammar that produces more than one parse tree for a given sentence is ……….
a) syntax-free
b) ambiguous
c) top-down
d) bottom-up
8) Top-down parsing can handle left-recursive grammars.
a) True
b) False
9) When a shift reduce parser cannot decide which production of several possible productions to use, this is
called ……….
a) shift/reduce conflict
b) reduce/reduce conflict
10)In ……… code all assignments are to variables with distinct names.
a) three-address
b) quadruples
c) SSA
d) machine
11) A field within a record cannot have the same name as a field outside the record.
a) True
b) False
12) Local variables of a procedure are usually stored in ……….
a) registers
b) stack
c) heap
d) cache memory
13) Cyclic data structures (objects that point to each other in a cycle but no other object points to any of them)
that are actually garbage but cannot be identified as garbage is a problem in ………. Garbage collection.
a) reference-count
b) mark-&-sweep
c) mark-&-compact
d) copying
14) Selecting which variables will reside in registers at each point of the program is called ……..
a) register allocation
b) register assignment
15) Graph coloring is an algorithm used for ……… problem.
a) register allocation
b) register assignment
Solution:
1
3
b
b
4
5
6
7
8
9
10
11
13
13
14
15
c
a
b
b
b
b
c
b
b
a
a
b
1 out of 7
Final Exam, 4th of July 2020
Time allowed 3 hours
Compilers
7 Questions in 3 pages [90 degrees]
Benha University,
Faculty of Engineering at Shoubra,
4th Year Computer Systems Eng,
Electrical Engineering Department.
2. [17 degrees] For the following regular expression (assume that digit is a terminal):
n → (+|-) (digit digit*)
1) [2 degrees] Describe the language that this regular expression represents.
Signed Integers.
2) [5 degrees] Convert it into NFA.
3) [8 degrees] Then, convert into DFA. (Draw transition table and the resulting DFA) .
NFA states DFA state + - digit
{0, 1, 3}
A
B C
Φ
{2, 5}
B
Φ Φ
D
{4, 5}
C
Φ Φ
D
{6, 7, 9}
D
Φ Φ
E
{7, 8, 9}
E
Φ Φ
E
4) [2 degrees] What is the problem with the resulting DFA.
DFA has states that have no edges for some symbols (ex: A state does not have an edge for digit).
2 out of 7
Final Exam, 4th of July 2020
Time allowed 3 hours
Compilers
7 Questions in 3 pages [90 degrees]
Benha University,
Faculty of Engineering at Shoubra,
4th Year Computer Systems Eng,
Electrical Engineering Department.
3. [9 degrees] Consider the following grammar:
B→+BB|*BB|a
and the string +*aaa
1) [3 degrees] Give a leftmost derivation for the string.
B→+BB→+*BBB→+*aBB→+*aaB→+*aaa
2) [3 degrees] Give a rightmost derivation for the string.
B→+BB→+Ba→+*BBa→+*Baa→+*aaa
3) [3 degrees] Give a parse tree for the string.
4. [4 degrees] Left-factor the following grammar:
B→0B1|01
B→0A
A→B1|1
5. [15 degrees] Consider the following translation scheme:
PRODUCTION
S → id = E ;
| L=E;
E → E1 + E2
Code Fragments
{ gen( top.get(id.lexeme) ‘=’ E.addr); }
{ gen( L.addr.base ‘[‘ L.addr ‘]’ ‘=’ E.addr); }
{ E.addr = new Temp();
gen( E.addr ‘=’ E1.addr ‘+’ E2.addr); }
| id
{ E.addr = top.get(id.lexeme); }
| L
{ E.addr = new Temp();
gen( E.addr ‘=’ L.addr.base ‘[‘ L.addr ‘]’); }
L → id [ E ]
| L1 [ E ]
{ L.array = top.get(id.lexeme);
L.type = L.array.type.elem;
L.addr = new Temp();
gen( L.addr ‘=’ E.addr ‘*‘ L.type.width); }
{ L.array = L1.array;
L.type = L1.type.elem;
t = new Temp();
L.addr = new Temp();
gen(t ‘=’ E.addr ‘*’ L.type.width);
gen(L.addr ‘=’ L1.addr ‘+’ t); }
For the following assignment:
x = a[i] + b[j]
3 out of 7
Final Exam, 4th of July 2020
Time allowed 3 hours
Compilers
7 Questions in 3 pages [90 degrees]
Benha University,
Faculty of Engineering at Shoubra,
4th Year Computer Systems Eng,
Electrical Engineering Department.
1) [8 degrees] Generate annotated parse tree.
2) [7 degrees] Generate three address code.
t1 = I * 4
t2 = a [t1]
t3 = j * 4
t4 = b[t3]
t5 = t 2 + t 4
x = t5
4 out of 7
Final Exam, 4th of July 2020
Time allowed 3 hours
Compilers
7 Questions in 3 pages [90 degrees]
Benha University,
Faculty of Engineering at Shoubra,
4th Year Computer Systems Eng,
Electrical Engineering Department.
6. [15 degrees] Given the following figure:
State what happens to the reference counts of the objects in the figure if:
1) [5 degrees] The pointer from b to c is deleted.
2) [5 degrees] The pointer from a to b is deleted.
3) [5 degrees] The node e is deleted.
Assume changes are independent.
initial pointer b → c deleted pointer a → b deleted node e deleted
a
b 1
0 (removed)
c 1
0 (removed)
0 (removed)
d 1
0 (removed)
0 (removed)
e 2
1
f 1
0 (removed)
0 (removed)
g 2
1
1
h 2
1
1
m 2
1
n 1
5 out of 7
Final Exam, 4th of July 2020
Time allowed 3 hours
Compilers
7 Questions in 3 pages [90 degrees]
Benha University,
Faculty of Engineering at Shoubra,
4th Year Computer Systems Eng,
Electrical Engineering Department.
7. [15 degrees] For the following code:
for (i=0; i<n; i++)
for (j=0; j<n; j++)
c[i][j] = 0.0;
for (i=0; i<n; i++)
for(j=0; j<n; j++)
for (k=0; k<n; k++)
c[i][j] = c[i][j] + a[i][k] *b[k][j];
Assume array elements require 8 bytes each, and the array is in row-major order .
1) [5 degrees] Translate into three-address code.
2) [5 degrees] Construct the flow graph for the resulting three-address code.
3) [5 degrees] Identify the loops in the resulting flow graph.
6 out of 7
Final Exam, 4th of July 2020
Time allowed 3 hours
Compilers
7 Questions in 3 pages [90 degrees]
Benha University,
Faculty of Engineering at Shoubra,
4th Year Computer Systems Eng,
Electrical Engineering Department.
B1
1) i = 0
B2
2) if i > n goto (13)
B3
3) j = 0
B4
4) if j > n goto (11)
B5
5) t1 = n * i
6) t2 = t1 + j
7) t3 = t2 * 8
8) c[t3] = 0.0
9) j = j + 1
10) goto (4)
B6
11) i = i + 1
12) goto (2)
B7
13) i = 0
B8
14) if i > n goto (40)
B9
15) j = 0
B10
16) if j >= n goto (38)
B11
17) k = 0
B12
18) if k >= n goto (36)
B13
19) t4 = n * i
20) t5 = t4 + j
21) t6 = t5 * 8
22) t7 = c[t6]
23) t8 = n * I
24) t9 = t8 + k
25) t10 = t9 * 8
26) t11 = a[t10]
27) t12 = n * k
28) t13 = t12 + j
29) t14 = t13 * 8
30) t15 = b[t14]
31) t16 = t11 * t15
32) t17 = t7 + t16
33) c[t6] = t17
34) k = k + 1
35) goto (18)
B14
36) j = j + 1
37) goto (16)
B15
38) i = i + 1
39) goto (14)
Loops:
{4, 5}
{2, 3, 4, 5, 6}
{12, 13}
{10, 11, 12, 13, 14}
{8, 9, 10, 11, 12, 13,
14, 15}
40) …
Best Wishes,,,
7 out of 7
Download