0% found this document useful (0 votes)
63 views6 pages

University College London: Module Code Assessment Patiern Module Name

Computer science, maths and statistics.

Uploaded by

Dragan Petkanov
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views6 pages

University College London: Module Code Assessment Patiern Module Name

Computer science, maths and statistics.

Uploaded by

Dragan Petkanov
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

UNIVERSITY COLLEGE LONDON

EXAMINATION FOR INTERNAL STUDENTS

MODULE CODE COMP2010

ASSESSMENT COMP2010C
PATIERN

MODULE NAME Compilers

DATE 19 May 2016

TIME 2:30 pm

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/15

2015/16-COMP2010C-001-EXAM-4
© 2015 University College London TURN OVER
COMP207P: Compilers, 2016

Exam Rubric
• Answer both questions from Section A (Q1 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 proble~.

Good Luck!

COMP207P -1- Thrn Over


Section A (Answer BOTH Questions)
1. Finite State Automata and Lexical Analysis
(a) Consider a programming language whose boolean constants are T and F. Identifiers include upper
case letters. What does your parser do if you give it a program containing an identifier that starts
with T or F, like T : = 5;? If it handles this case, explain how. If it does not, explain how you could
change it to handle this case. [5]
(b) You must write the lexer for Qter, a programming langauge that supports keywords in multiple
natural languages. For example, the English word "if" is "si" in French, "ob" in German, and
"gdyby" in Polish. Qter allows a programmer to use any of these when writing an if-then-else
construct. For example, a Polish programmer using this programming language could start an "if"
construct with "gdyby".
1. How would you implement this feature in JFlex? Give an example of the rule for the i f
construct. [5J
ii. No widely used programming language provides this feature. Why not? [4]
(c) For ~ = {a, b, cl, produce an NFA that accepts the following regular expression: [lOJ

b(a(~ - {a})*a I cc)'"

(d) Convert the following NFA over the alphabet ~ = {a, b} into a DFA that recognizes the same
language. [lOJ

Total for Question 1: [34J

COMP207P -2- Turn Over


2. Intermediate Code, Rtilltime, and Code Optimisation
(a) Provide two benefits of the Intermediate Representation for stack machines, compared to that for
register machines, with a brief description of each benefit. [4]
(b) Translate the arithmetic expression d = a [i] - b '" c into the following intermediate representa­
tions.
• Abstract Syntax Tree
• Quadruples
• Triples
[9]
(c) Indirect triples offer two benefits compared to quadruples and triples. Describe these benefits and
explain why they occur. [4]
(d) Each live activation has an activation record on the control stack. Describe the content of an
activation record. [7]
(e) Translate the following fragment of code into TAC IR for register machine. Optirnise it as much as
possible during the translation, and provide a list of all optimisations that have been applied.
i = 0

a = 10

while (i < 10) {

if(a > b){


a += b + 2 '" i

else{

a "'= i + b
}

i++

[10]
Total for Question 2: [34]

COMP201P -3- Turn Over


Section B (Answer ONE Question)

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

S -+ Xx I y I Sz
X -+ Xz I Sxd I yd It
(b) Consider the following grammar.

S -+ yzA
A -+ xB I D
B -+ xS I CxD
C -+ yzD I C
D -+ zD I t

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

E -+ (E) I A I B
A-+A+Alt

B-+B*Blt

(d) LALR compresses two closed LR(l) states (item sets) that contain common cores. Create LR(l)
states whose LALR compression introduces a reduce/reduce conflict.
[5]
Total for Question 3: [32J

COMP207P -4- Turn Over


4. Semantic Analysis
(a) Write down two major challenges of the single table approach for symbol table implementation. [6]
(b) Write a context-free grammar G to define a list of function declarations. A single function is de­
clared as follows:

fdef functionName ( formal parameter list ) : type body

where fdef is a keyword; functionName is the name of the function; the formal parameters form a
comma separated list, with each parameter being defined as paramName:type, where paramName
is the name of the parameter and type can be int, bool or float. The body is enclosed in { } and
consists of local variable declarations followed by statements. Use non-terminal B as a placeholder
to refer to a function body (no need to define it further).
An example list of function declarations follows:
fdef add (a: int, b: int) : int {
c : int;

c = a + b;

return c;

}
fdef or (a: bool, b: bool) bool {

if (a) return true;

else return b;

[12]
(c) Consider the following fragment of pseudo code:

public class Compare {


public bool compare (int a, int b) {return a<=b;}
public bool compare (double d, double f) {return d<=f;}
}

Compare c = new Compare();

Cl] c.compare Cl, 2 * 3);

[2] c. compare Cl, 2 + 2.3);

List the type rules that a plausible type checker would check when dealing with statements [lJ and
[2J. For each statement, state also whether the type checker would fire a type checking error and
why. [14]
Total for Question 4: [32]

COMP207P -5- END OF EXAM

You might also like