CS 132 Compiler Construction, Fall 2014 Instructor: Jens Palsberg Multiple Choice Exam, Nov 20, 2014
CS 132 Compiler Construction, Fall 2014 Instructor: Jens Palsberg Multiple Choice Exam, Nov 20, 2014
This exam consists of 22 questions. Each question has four options, exactly one of which is correct,
while the other three options are incorrect. For each question, you can check multiple options.
I will grade each question in the following way. If you check none of the options, you get 0 points.
If you check all four options, you get 0 points.
Check one option. If you check one option, and that option is correct, you get 2 points. If you
check one option, and that option is wrong, you get 0.667 points (yes, negative!).
Check two options. If you check two options, and one of those options is correct, you get 1
point. If you check two options, and both of them are wrong, you get 1 point (yes, negative!).
Check three options. If you check three options, and one of those options is correct, you get
0.415 points. If you check three options, and all three of them are wrong, you get 1.245 points (yes,
negative!).
The maximum point total is 22 2 = 44 points. I will calculate a percentage based on the points
in the following way:
max(0, point total)
100
44
Notice that if your point total is negative, you will get 0 percent
Example
Consider the grammar
A ::= x B y | C z
B ::= x A |
C ::= y B z
where {A, B, C} is the set of nonterminal symbols, A is the start symbol, {x, y, z} is the set of
terminal symbols, and denotes the empty string.
Question 1
Which nonterminals are nullable?
a
A
b
B
c
C
A and B
d
Question 2
What is First(A)?
a
{y}
b
{x, y}
c
{y, z}
{x, y, z}
d
Question 3
What is First(B)?
{x}
a
b
{y}
c
{z}
d
{x, y}
Question 4
What is First(C)?
a
{x}
b
{y}
c
{z}
d
{x, y}
Question 5
What is Follow(A)?
a
{x}
b
{y}
c
{z}
d
{y, z}
Question 6
What is Follow(B)?
{x}
a
b
{y}
c
{z}
d
{y, z}
Question 7
What is Follow(C)?
a
{x}
b
{y}
c
{z}
d
{y, z}
Question 8
Is the grammar LL(1)?
a
Yes
b
No
c
The question cannot be answered with the information provided
d
The LL(1)-checker would go into an infinite loop
Example
Consider the grammar
A ::= x B C
B ::= y | x A |
C ::= z A
where {A, B, C} is the set of nonterminal symbols, A is the start symbol, {x, y, z} is the set of
terminal symbols, and denotes the empty string. The grammar is LL(1). The predictive parsing
table is a two-dimensional table called table.
Question 9
What does table(A, x) contain?
error
a
b
c
xBC
d
xA
Question 10
What does table(A, y) contain?
a
error
b
c
xBC
d
xA
Question 11
What does table(A, z) contain?
error
a
b
c
xBC
d
xA
Question 12
What does table(B, x) contain?
a
error
b
c
y
d
xA
Question 13
What does table(B, y) contain?
a
error
b
c
y
d
xA
Question 14
What does table(B, z) contain?
a
error
b
c
y
d
zA
Question 15
What does table(C, x) contain?
a
error
b
c
xA
xBC
d
Question 16
What does table(C, y) contain?
a
error
b
c
y
d
zA
Question 17
What does table(C, z) contain?
error
a
b
c
y
d
zA
Example
Consider the grammar
A ::= x B | C x
B ::= z A |
C ::= y A | z C
where {A, B, C} is the set of nonterminal symbols, A is the start symbol, {x, y, z} is the set of
terminal symbols, and denotes the empty string. The grammar is LL(1). Assume that a recursivedescent parser for the above grammer declares a variable next of type token, and that the program
has three procedures A(), B(), C(), and the following main part:
void main() {
next = getnexttoken();
A();
}
The procedure getnexttoken() gets the next token from an input file. Assume also we have the
following helper procedure, written in pseudo-code:
void eat(token t) {
if (t == next) {
next = getnexttoken();
}
else {
error();
}
Question 18
The procedure A() looks like:
if (next == x) {
eat(x);
B()
}
else {
????
}
What
a
b
c
d
is ???? ?
/* do nothing */
error();
C(); eat(x);
if (next == y) { C(); eat(x); } else { error(); }
Question 19
The procedure B() looks like:
if (next == z) {
eat(z);
A()
}
else {
????
}
What
a
b
c
d
is ???? ?
/* do nothing */
error();
if (next == x) { error(); } else { /* do nothing */ }
if (next == x) { /* do nothing */ } else { error(); }
Question 20
The procedure C() looks like:
if (next == y) {
eat(y);
A()
}
else {
????
}
What
a
b
c
d
is ???? ?
/* do nothing */
error();
if (next == z) { eat(z); C() } else { eat(x); B() }
if (next == z) { eat(z); C() } else { error(); }
Example
Consider the grammar:
A ::= A x B | A y | z
B ::= z B |
where {A, B} is the set of nonterminal symbols, A is the start symbol, and {x, y, z} is the set of
terminal symbols, and denotes the empty string.
Question 21
Which grammar generates the same language as the above grammar?
a
A
B
C
D
::=
::=
::=
::=
zD
zB |
xB | y
CD |
b
A ::= A C | z
B ::= z B |
C ::= x C | y
c
A
B
C
D
d
::=
::=
::=
::=
zD
zB |
xB | z
CD |
Example
Consider the grammar:
A ::= y B | x y B
B ::= x y | x A
where {A, B} is the set of nonterminal symbols, A is the start symbol, {x, y} is the set of terminal
symbols, and denotes the empty string.
Question 22
Which grammar generates the same language as the above grammar?
a
A
B
C
D
::=
::=
::=
::=
yB | xyB
xC
yD | xyB
| B
b
A ::= y B | x y B
B ::= x y | x y B | x x A
c
A
B
C
D
d
::=
::=
::=
::=
yxyC | xyC
xC
yD | xyD
| B