You are on page 1of 6

Candidate No. . .

UNIVERSITY COLLEGE LONDON Seat No .

EXAMINATION FOR INTERNAL STUDENTS

MODULE CODE COMP2010

ASSESSMENT COMP2010C
PATTERN

MODULE NAME Compilers

DATE 11 May 2017

TIME 10 ; 00 AfV1

TIME ALLOWED 2 hours 30 mins

This paper is suitable for candidates who attended classes for this
module in the following academic year(s):

2014/2015 to 2016/2017

Under no circumstances are the

attached papers to be removed from

the examination by the candidate.

2016/17-COMP2010C-1-EXAM-1
© 2016 University College London TURN OVER
.. ,

COMP207P/COMP2010, Compilers, 2016-17

Exam Rubric
• This is a 2.5 hour exam.

• Answer both questions from Section A (Ql and Q2) and one question from Section B (Q3 or Q4).

• Marks for each question are indicated in square brackets.

• Draw a line through any rough notes you make.

• Calculators are not permitted.

Suggestions
• When in doubt, state any assumptions you make in solving a problem.

• Read the questions carefully. Be sure to answer all parts of each question.

• Clearly identify your answers!

• Be concise and precise.

• Problems are not necessarily in the order of difficulty.

• Grab the low-hanging fruit: make sure you solve all the problems whose answers are easy or obvious
to you before tackling the harder problems.

Good Luck!

COMP207P/COMP2010 - 1 - Turn Over


Section ·A (Answer BOTH Questions)
1. Finite State Automata and Lexical Analysis
(a) In English (not a regular expression), characterise the strings the mystery DFA over {a, b} in Figure 1
accepts. [4]

b b b

a b
a

Figure 1: Ivlystery DFA.

(b) The regex (a(bb)*(aa)*)* accepts all strings over {a, b} that have an odd number of as and an even
number of bs. True/False [2]. Justify [3]. [5]
(c) JFlex accepts only regular languages. True/False [21. Justify [3]. [5]
(d) For L: = {a,b,c}, produce an NFA that accepts the following regular expression: [10]

(b* I c)(a(L: - {a})*c I cc)*

(e) Convert the following NFA over the alphabet L: = {a, b} into a DFA that recognizes the same
language. [10]
E

Total for Question 1: [34]

COMP207P/COMP2010 -2- Continued


2. Intermediate Code, Runtime, and Code Optimisation
(a) Indirect triples offer two benefits compared to quadruples and triples. Describe these benefits and
explain why they occur. [4]
(b) Briefly explain what the following code optimisation techniques do, and for each technique write
down at least one potential risk that compilers shoulcl look out for.
• Inlining
• Constant Folding
[8]
(c) Each live activation has an activation record on the control stack. Describe the content of an
activation record. [7]
(d) Write the Intermediate Representation translation scheme for the FOR statement, which is defined
as below. Assume that all three expressions (exp rl, exp r2, exp r3) that form the loop header
always appear in IR.

for_stmt .- FOR(exprl; expr2; expr3) block

[5]
(e) The following is a Three Address Code (TAC) Intermediate Representation for a register machine:
it contains a function called foo, which takes an integer parameter n. Identify and describe what
the code does in plain English, and write down the high-level language counterpart in Java-like
pseudocode. '
1 faa:

2 tl = n EQ I

3 CJUMP tl labell

4 t2 = n - I

5 PARAM t2

6 t3 = CALL faa

7 t4 = n MULT t3

8 return t4

9 labeU:

10 return I

[10]
Total for Question 2: [34]

COMP207P/COMP2010 -3- Turn Over


Section B (Answer ONE Question)

3. Syntax Analysis
(a) Remove 'left recursion from the following grammar. [8]

S -> Sa I Xx I y
X -> z I Sd
Y -> Yy I d I €

(b) Consider the following grammar.

S -> xA
A -> Bx I D
B -> xS I CxD
C -> yD I €
D -> zD I €

i. Compute the FIRST and FOLLOW sets for the above grammar. [5]
ii. Construct the LL(l) parsing table for this grammar. [5]
(c) Create the closed LR(l) item set for the following grammar's start symbol. [7]

E -> (E) IA IB
A A+AIA*Alt
B ~B IB v Bit

(d) Most parser generators use LALR(l), not LR(l).


i. In terms of item sets, define LALR(l) parsing and explain how it differs from LR(l). [3]
ii. What is the principal disadvantage of LALR(l) relative to LR(l)? [2]
iii. What is the principaladvantage of LALR(l) over LR(1) parsing? [2]
Total for Question 3: [32]

COMP207P/COMP2010 -4- Continued


4. Semantic Analysis
(a) Write down two major semantic analyses that are context sensitive. Explain why they cannot be
performed during parsing. [10]
(b) Consider the following code fragment written in a fictional language.
1 int X = 10;

3 int A() {

4 int y = X + 5;

5 return y;

6 }

8 int B(){

9 int X = 5;

10 returnA();

11 }

12

13 int main () {

14 print B();

15 }

vVhen executed, the function main is called; print B( ) will print the return value of function call
B() to the standard output. Write down the output when this language uses 1) static binding,
and 2) dynamic binding. If the output values are different, explain the reason why. [81
(c) Given the following scoping rules for a fictional language:
1. Use an identifier only if defined in the enclosing scope
2. Do not declare identifiers of the same kind with the same name more than once in the same
lexical scope
Define a Symbol Table data structure that a semantic analyser could use to check the above rules
in lava-like pseudocode. Illustrate how to use the Visitor pattern and the Symbol Table previously
defined to check the above rules.
[14]
Total for Question 4: [32]

COMP207P/COMP20 10 -5- END OF EXAM

You might also like