Compiler Design I

advertisement
Compiler Design I
Departement Informatik
ETH Zürich
Sample Exam
Name, First name:
Stud.number:
This is a sample exam from a prior year. There may be questions in this exam that cover material that was not
presented this year and that therefore will not be part of this year’s exam. There is also no guarantee that this
year’s exam will include only the topics presented here. But the overall style of the exam will be preserved for
this year’s exam.
GENERAL GUIDELINES AND INFORMATION
1. Start this exam only after the proctor has announced that the examination can begin.
2. You have 1 hr (60 min), and there are 60 points. Use the number of points as guidance on how much
time to spend on a question.
3. You should write your answers directly on the test. Use the space provided. If you need more space your
answer is probably too long.
4. Be sure to provide your name. Do this first so that you don’t forget! Print your name! Provide your
student ID (first 8 digits).
5. Clarity of presentation is essential and does influence the grade. Please write or print legibly. State all
assumptions that you make above those stated as part of a question.
6. With your signature below you certify that you solved these problems on your own, that you turn in your
solution, and that there were no environmental or other factors that disturbed you during this exam or
that diminished your performance.
Signature:
Problem
1
2
3
4
5
6
Total
Points
4
5
16
15
5
15
60
Score
1
Question 1 [ 4 points]
Consider the following grammar G1 :
S -> B a | b C
B -> d | e B f
C -> g C | g
The START symbol is S.
Which of these strings are in L(G1 ), the language generated by G1 ?
• da
• bddf
• eedffa
• faae
Question 2 [ 5 points]
Consider the following grammar G2 :
S -> a S | S B | d
B -> B b | c
Is this grammar ambiguous? Please justify your answer.
Question 3 [ 16 points]
1. Which kinds of errors in a JavaLi program can be discovered by the semantic analysis phase? (All
variations of a given kind of error must be recognizable.)
(a) Undefined variable
(b) Access to a non-existing element of an array
(c) Division by 0
(d) Overflow (value cannot be represented with 32 bits)
(e) Incorrect type (e.g., comparison of a float with an int value)
(f) Non-terminating loop
(g) Duplicate definition of a variable
2
(h) Allocation of more local variables than can be stored in the available space.
2. In the list above, are there types of errors that can be recognized by the semantic analyzer in specific
cases but that defy detection in the general case? If yes, please explain for at most two such types of
errors under what circumstances this kind of error can be detected by the semantic analysis phase.
Question 4 [ 15 points]
Consider the following JavaLi program segment from the body of a method. All variables have the type int,
a, b, etc. are user-defined variables, ti are temporaries generated by the compiler.
t1 = a + b ;
t2 = c + d ;
t3 = t1 + t2 ;
t4 = e + f ;
t5 = g + h ;
t6 = t4 + t5 ;
t7 = p + q ;
t8 = r + s ;
t9 = t7 + t8 ;
t10 = t6 + t9 ;
t11 = t3 + t10;
1. Sketch the IR graph that would be generated by your compiler for this code sequence. (We’re interested
in the overall structure, you can omit details.)
2. Pick a target machine of your choice (either the RISC machine introduced in class, or the target machine
of your compiler project). How many registers do you need at least for this machine if you try to
produce good code. Sketch the object code that would be generated. (Please don’t forget to tell us what
target machine you’ve chosen).
Additional space is on the next page.
3
Question 5 [ 5 points]
A compiler backend can allocate registers and then select code or can reverse the order of these two steps.
List briefly the advantages and disadvantages of these two options.
Please provide some guidance (a rule of thumb) on how to choose between these two options. Your rule of
thumb can depend on language properties and/or properties of the target machine.)
4
Question 6 [ 15 points]
Consider this JavaLi program segment
int
int
int
int
int
tmp_2ab;
tmp_aa;
tmp_bb;
x;
y;
tmp_2ab = 2 * a * b;
tmp_aa = a * a;
tmp_bb = b * b;
x = tmp_aa + tmp_2ab + tmp_bb;
y = tmp_aa - tmp_2ab + tmp_bb;
a and b are alive upon entry into this block and dead after exit. x and y are alive after exit from this block.
1. Draw the register interference graph for this code segment.
2. Color this graph. How many colors are needed?
Download