







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!








Summer 2023
Name
Student number Seat
Do not open this exam booklet before we ask you to. Do read this page carefully.
You can only write the exam at the seat allocated for you, and you must use the exam booklet that carries your name and student number.
This is a closed-book exam. Leave bags and jackets out of reach. Turn off all electronic devices and leave them with your bag. You may only take writing utensils, drinks, food, and your student or identity card to your seat. Please have your student or identity card clearly visible on your table.
Leaving your table or the room with your exam booklet is regarded as an attempt of deception. You may not leave the room during the first 30 minutes and the last 15 minutes of the exam. If you need to use the restrooms, please alert the supervisor. Only one person at a time may leave for the restrooms.
Before you start, please check that your exam booklet consists of 13 pages, sequentially numbered, on 13 pages sheets of paper, and contains questions 1 through 7 b). This is page 1.
Write your solutions on the (printed) right pages of the exam booklet, in the space provided below the respective questions. Solutions written in a language other than English, in red or similar colours, on the first or last page of the booklet, on the (blank) left pages, or on additional sheets not referenced from the booklet, will not be graded. Should you run out of space, ask the supervisor for an additional sheet of paper. You may use a pencil.
The duration of the exam is 120 minutes. The total number of points of the questions in this exam is 120. To pass the exam, 60 points will be sufficient.
Have fun!
1 2 3 4 5 6 7
Sum
For each of the following statements, decide whether it is true or false. If a statement is false, very briefly (i.e. in at most one sentence) correct it or explain why it is false.
a) Deciding whether a context-free grammar is ambiguous is an NP-hard problem.
b) 2-address code linear IRs are no longer in common use today.
c) When using a global display for addressability, storing the display in global memory works well for multi-threaded programs.
The following grammar, in ANTLR-like notation, is for expressions with binary addition (+) and subtraction (-) operators over single-letter identifiers of token type NAME. The start symbol is expr.
1 expr : '( ' expr ') ' 2 | expr op NAME ; 3 | NAME ; 4 op : '+ ' | ' - ';
a) What is the associativity of the two operators according to this grammar? Mark the correct answer. (1 point) □ + and - are right-associative. □ + and - are left-associative.
b) Is the grammar LL(1)? Briefly but precisely say why or why not. (3 points)
c) Draw the parse tree for the expression “(a + b) - c”. (6 points)
Now consider the following grammar in BNF, where uppercase letters are nonterminals and lowercase latin letters are terminals, and A is the start symbol:
A → B C | a B → C b | ϵ C → c | ϵ
d) Give the FIRST and FOLLOW sets for all nonterminals, and the FIRST+ sets for all rules. (12 points)
FIRST+(A → a) = {
FIRST+(B → C b) = {
FIRST+(B → ϵ) = {
FIRST+(C → c) = {
FIRST+(C → ϵ) = {
e) Is the grammar LL(1)? Briefly but precisely say why or why not. (3 points)
Consider the following Java-like code snippet, which finds the maximum element ≥ 0 contained in array a:
1 int max = 0; 2 int i = 0; 3 while ( i < a. length ) { 4 if ( a [ i ] > max ) 5 max = a [ i ]; 6 i = i + 1; 7 } 8 print ( " Max : ␣ " , max );
a) Draw the control flow graph of the code snippet. Use the line numbers as the nodes of your graph, ignoring line 7. (7 points)
b) List all basic blocks of the snippet’s control flow graph that consist of more than one node. (2 points)
We recall the code snippet from the previous page:
1 int max = 0; 2 int i = 0; 3 while ( i < a. length ) { 4 if ( a [ i ] > max ) 5 max = a [ i ]; 6 i = i + 1; 7 } 8 print ( " Max : ␣ " , max );
c) Draw the data dependence graph for the code snippet. Again use the relevant line numbers as nodes, and add a node for the array a. (7 points)
We continue with following Pascal program:
1 program Main ; 2 var x : integer = 3 , y : integer = 4 , z : integer = 5; 3 function A ( r : integer ): integer ; 4 var x : integer = 6; 5 begin A := r + x + y ; end ; 6 procedure B ( x : integer ); 7 begin { body B } 8 A ( x + 1); 9 end ; 10 begin { Main } 11 B (1); 12 end.
c) Draw a graph (as in the lectures) that visualises the structure and contents of the activation records (ARs), including the values of variables, at the following point in the program’s execution: In the implementation of line 5, A is about to perform the jump back to the return address.
Annotate the ARs’ contents (e.g. “return address”, “return value”, etc.). Draw pointers as arrows. Omit saved registers, and use static links to implement ad- dressability. Write “—” for uninitialised or unknown values. For return addresses, use the line number of the call in the caller’s code. Assume a memory-to-memory model and heap-based allocation of ARs. (11 points)
We recall the Pascal program from the previous page:
1 program Main ; 2 var x : integer = 3 , y : integer = 4 , z : integer = 5; 3 function A ( r : integer ): integer ; 4 var x : integer = 6; 5 begin A := r + x + y ; end ; 6 procedure B ( x : integer ); 7 begin { body B } 8 A ( x + 1); 9 end ; 10 begin { Main } 11 B (1); 12 end.
d) Write ILOC code implementing procedure B using a memory-to-memory model, heap-based allocation of ARs, and static links. Assume that register r_hp points to an area of memory to allocate A’s AR in (over increasing memory addresses), that the ILOC code implementing A has label code_A, and that the callees restore r_arp before returning. Do not use symbolic constants for memory offsets—use concrete values. Ignore register saving. Use only the ILOC instructions listed on the last page of this exam booklet. (11 points)
(This question may be slightly too complex/time-consuming to appear in an exam, but it is good practice for similar but simpler/shorter questions that could appear.)
Opcode Sources Targets Meaning
Memory operations loadI c 1 r 2 c 1 ⇒ r 2 load r 1 r 2 Memory(r 1 ) ⇒ r 2 loadAI r 1 , c 2 r 3 Memory(r 1 + c 2 ) ⇒ r 3 loadAO r 1 , r 2 r 3 Memory(r 1 + r 2 ) ⇒ r 3 store r 1 r 2 r 1 ⇒ Memory(r 2 ) storeAI r 1 r 2 , c 3 r 1 ⇒ Memory(r 2 + c 3 ) storeA0 r 1 r 2 , r 3 r 1 ⇒ Memory(r 2 + r 3 )
Arithmetic add r 1 , r 2 r 3 r 1 +r 2 ⇒ r 3 addI r 1 , c 2 r 3 r 1 +c 2 ⇒ r 3 sub r 1 , r 2 r 3 r 1 −r 2 ⇒ r 3 subI r 1 , c 2 r 3 r 1 −c 2 ⇒ r 3 mult r 1 , r 2 r 3 r 1 ∗ r 2 ⇒ r 3 multI r 1 , c 2 r 3 r 1 ∗ c 2 ⇒ r 3
Control flow cmp_LT r 1 , r 2 r 3 true ⇒ r 3 false ⇒ r 3
if r 1 < r 2 otherwise cmp_LE r 1 , r 2 r 3 true ⇒ r 3 false ⇒ r 3
if r 1 ≤ r 2 otherwise cmp_GT r 1 , r 2 r 3 true ⇒ r 3 false ⇒ r 3
if r 1 > r 2 otherwise cmp_GE r 1 , r 2 r 3 true ⇒ r 3 false ⇒ r 3
if r 1 ≥ r 2 otherwise cmp_EQ r 1 , r 2 r 3 true ⇒ r 3 false ⇒ r 3
if r 1 = r 2 otherwise cmp_NE r 1 , r 2 r 3 true ⇒ r 3 false ⇒ r 3
if r 1 ̸= r 2 otherwise cbr r 1 l 2 , l 3 l 2 → PC l 3 → PC
if r 1 = true otherwise Jumps
jumpI — l 1 l 1 → PC jump — r 1 r 1 → PC