You are on page 1of 5

UNIVERSITY COLLEGE LONDON

EXAMINATION FOR INTERNAL STUDENTS

MODULE CODE COMP2010

ASSESSMENT COMP2010C
PATTERN

MODULE NAME Compilers

DATE 15-May-15

TIME 14:30

TIME ALLOWED 2 Hours 30 Minutes

2014/15-COMP2010C-001-EXAM-95
©2014 University College London TURN OVER
Section A (Answer BOTH Questions)
1. Finite State Automata and Lexical Analysis
(a) S++ uses':' as the type specification operator and also to map keys to values in dictionaries, ': :'
as concatenation, and ': =' as assignment. All share the prefix': '.
i. How does a lexer differentiate the ':', ': : " and': =' tokens from each other? [4]
ii. How does a lexer differentiate the two uses of ' : '? [2]
(b) Your parser supported multiline comments, but not nested multiline comments.
1. Describe a partial solution using JFlexj sketch your solution in JFlex' syntax to demonstrate
your familiarity with JFlex. [4]
]1. Why is this solution partial? [2]
111. Describe how you would change your parser to solve it generally. [4]
(c) For ~ = {a,b,c}, produce an NFA that accepts the following regular expression: [8J

(b I E)(a(~ - {a})*a I cc)*b

(d) Consider the following NFA over the alphabet ~ = {a, b}. Convert this NFA to a DFA that
recognizes the same language.
b,C:

[10]
Total for Question 1: [34]

COMP2010 -2- Turn Over


2. Intermediate Code, Runtime, and Code Optimisation
(a) Briefly explain what the following code optimisation techniques do, and for each technique write
down at least one potential risk that compilers should look out for.
• Constant Folding
• Common Subexpression Elimination
[8]
(b) Write the Intermediate Representation translation scheme for the ternary conditional assignment
Exprl = Expr2 ? Expr3 : Expr4. The ternary conditional expression A ? B : C first evaluates
the Boolean expression A - the expression evaluates to B if it is true, C if false. For example,
after the declaration int a = (l == 0) ? 3 : 5;, variable a is assigned with 5. Use T[] to mark
further recursive translation.
[6]
(c) Write down the data structure in which local variables of functions are stored during runtime, and
justify the choice of the data structure. Also, describe which address the Frame Pointer (FP) register
points to, and why. [10]
(d) Translate the following fragment of code to Three Address Code.
while (a < b) {
i f (b == 10){
c[b*2] = a*5 - b % 2;
a = a + 3;
}
else{

c [b+ 1] 7 - a;

a = c [b] + 2;

[10]
Total for Question 2: [34]

COMP2010 -3- Turn Over


Section B (Answer ONE Question)

3. Syntax Analysis
(a) Explain the role nondeterminism plays in the parsing of an arbitrary context-free grammar.
[5]
(b) LR(l) parsing contends with three types of conflicts, shift/shift, shift/reduce and reduce/reduce.
i. True/False. [3]
ii. Justify. [5]
(c) Most parser generators use LALR(l), not LR(l).
1. In terms of item sets, define LALR(l) parsing and explain how it differs from LR(l). [5]
ii. What is the principal disadvantage of LALR(l) relative to LR(l)? [2]
iii. What is the principal advantage of LALR(l) over LR(l) parsing? [2]
(d) LALR compresses two closed LR(l) states (item sets) that contain co=on cores. Create LR( 1)
states whose LALR compression introduces a reduce/reduce conflict.
[5]
(e) Prove that LALR state compression cannot introduce a shift/reduce conflict.
[5]
Total for Question 3: [32]

COMP2010 -4- Turn Over


4. AST and Semantic Analysis
(a) Write down two major semantic analyses that are context sensitive. Explain why they cannot be
performed during parsing. [10]
(b) Assume that we want to implement a language that accepts function declarations such as below
(fdef is a keyword):
fdef add (a: int, b: int) : int {
c : int;

c = a + bi

return c;

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

i f (a) return true;

else return b;

Draw a UML class diagram for the classes that are required to build ASTs that correspond to
declarations like this. Include the association notation (i.e. multiplicity). Assume a reasonable
Context Free Grammar for this language to decide types of AST nodes. Use class B to represent
the body of the functions. [10]
(c) Consider the following code fragment written in a fictional language.
int x = 10 ;

int A(){

int Y = x + 5',

return
Yi
}

int B(){

int x = 5',

return A()i

int main () {

print BO;

When 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. [6]
(d) Explain what forward reference is, and its impact on semantic analysis. [6]
Total for Question 4: [32]

COMP2010 -5- END OF EXAM

You might also like