Answers: The University of Nottingham
Answers: The University of Nottingham
ANSWERS
Time allowed TWO hours
Candidates may complete the front cover of their answer book and sign
their desk card but must NOT write anything else until the start of the
examination period is announced.
Dictionaries are not allowed with one exception. Those whose first
language is not English may use a standard translation dictionary to
translate between that language and English provided that neither language
is the subject of this examination. Subject-specific translation directories
are not permitted.
Note: ANSWERS
Question 1 (Compulsory)
The following questions are multiple choice. There is at least one correct
choice, but there may be several. To get all the marks you have to list all
the correct answers and none of the wrong ones.
Answer: Note that the answer that should be provided is just a list of
the correct alternative(s). The additional explanations below are just for your
information.
(i) The empty word ǫ is the only word in the empty language ∅.
(ii) ǫ ∈ Σ∗ , where Σ = {0, 1}
(iii) ǫ ∈
/ ∅∗
(iv) The empty word ǫ belongs to all non-empty languages.
(v) ǫ ∈ {ai bj | i, j ∈ N, i + j ≤ 42}
(5)
Answer: Correct: ii, v
Incorrect:
i The empty language ∅, i.e. the empty set, does not contain any
words, not even the empty one.
iii By definition, ǫ ∈ L∗ for any language L, including the empty
language ∅.
iv {a} is an example of a non-empty language that does not contain
the empty word ǫ.
G52MAL-E1
3 G52MAL-E1
a, b, c a, b, c a, b, c a, b, c a, b, c
a b b a
0 1 2 3 4
a∗ (ab)∗ (abc)∗
(i) (a + ab + abc)∗
(ii) a∗ (a + b)∗ (a + b + c)∗
(iii) a∗ (ǫ + ab)∗ (∅ + abc)∗
(iv) a∗ + (ab)∗ + (abc)∗
(v) a∗ (a∗ b∗ )∗ (a∗ b∗ c∗ )∗
(5)
Answer: Correct: iii
Incorrect: i, ii, iv, v
S → XX | Y
X → aXc | aY c
Y → Yb|ǫ
(i) ǫ ∈ L(G)
(ii) aabbbccac ∈ L(G)
(iii) aabbbccbb ∈ L(G)
(iv) L(G) = L1 L1 ∪ L2 where L1 = {ai bj ci | i, j ∈ N, i > 0} and
L2 = {bi | i ∈ N}
(v) The following CFG G′ is equivalent to G above, i.e. L(G′ ) =
L(G):
S → XX
X → aXc | Y
Y → Yb|ǫ
(5)
Answer: Correct: i, ii, iv
Incorrect:
iii Since the word contains a’s and c’s, the derivation must begin
S ⇒ XX. The only possibility to derive the word from XX is
to split it into two parts after the last c, and derive the first part
from the first X and the last part from the second X. But while
aabbbcc can be derived from the first X, bb cannot be derived from
the second.
v Not equivalent because ǫ can now be derived from X, meaning
that a word like ac ∈ L(G′ ). However, ac ∈
/ L(G).
G52MAL-E1
5 G52MAL-E1
Question 2
(a) Given the following NFA N over the alphabet Σ = {0, 1, 2}, construct
a DFA D(N ) that accepts the same language as N by applying the
subset construction:
q1
1 1, 2
1 0, 2
q0 q3 q4
0, 2 0
q2
To save work, consider only the reachable part of D(N ). Clearly show
your calculations in a state-transition table. Then draw the transition
diagram for the resulting DFA D(N ). Do not forget to indicate the
initial state and the final states both in the transition table and the
final transition diagram. (15)
Answer: Starting from S = {q0 }, the set of start states of N and thus
the start state of D(N ), we compute δ̂N (S, x) for each x ∈ Σ. When-
ever we encounter a state P ⊆ Q of D(N ) that has not been considered
before, we add P to the table and proceed to tabulate δ̂N (P, x) for each
x ∈ Σ. We repeat the process until no new states are encountered.
Finally, we identify the initial state (→ to the left of the state) and
all accepting states (∗ to the left of the state). Note that a DFA state
is accepting if it contains at least one accepting NFA state (as this
means it is possible to reach at least one accepting state on a given
word, which means that word is considered to be in the language of the
NFA).
δD(N ) 0 1 2
→ {q0 } {q2 } {q1 , q3 } ∅
{q2 } {q0 } ∅ {q0 }
{q1 , q3 } ∅ ∪ {q4 } = {q4 } {q0 } ∪ ∅ = {q0 } {q0 } ∪ {q4 } = {q0 , q4 }
∅ ∅ ∅ ∅
∗ {q4 } ∅ ∅ ∅
∗ {q0 , q4 } {q2 } ∪ ∅ = {q2 } {q1 , q3 } ∪ ∅ = {q1 , q3 } ∅∪∅=∅
0, 1, 2
2
1
{q2 } ∅
0 2
0
0, 2
{q0 } {q0 , q4 } 0, 1, 2
1 2
1 1 0
{q1 , q3 } {q4 }
(b) Consider the language L over the alphabet Σ = {3, 5} of all words for
which the arithmetic sum of the constituent symbols is divisible by 5.
For example, ǫ ∈ L (there are no symbols in the empty string, the sum
is thus 0 which is divisible by 5), 555 ∈ L (5 + 5 + 5 = 15 which is
divisible by 5), and 335333 ∈ L (3 + 3 + 5 + 3 + 3 + 3 = 20 which is
divisible by 5), but 333 ∈
/ L (3 + 3 + 3 = 9 which is not divisible by 5
(the reminder is 4)).
Is L a regular language or not? If it is, construct a DFA A such that
L(A) = L. Your answer should consist of the transition diagram for
A, with initial and final state(s) clearly identified, along with a brief
justification (in plain English) that makes the idea behind the con-
struction clear and thus explains why the given automaton accepts
the language in question.
If L is not a regular language, prove this by using the pumping lemma
for regular languages. (10)
Answer: The language L is regular. It is just a variation of the type of
language exemplified by “all strings with an odd number of a particular
symbol”, and can thus be recognised by a variation of the DFAs for
recognising that type of language.
We need one state for each possible reminder when dividing by 5, i.e.
5 states. Let us label each state with the reminder in question. State 0
is thus both the initial and the only final state. We then just note that
if the reminder when dividing the sum n of the symbols seen so far by
5 is r, and the next symbol is i, then the reminder of n + i divided by
5 is just the reminder of r + i divided by 5.
G52MAL-E1
7 G52MAL-E1
5 3 5
4 1
3
3
3 3
3 2
5 5
0
3
3
5 5
2 3
4 1
3
5 5
Question 3
(a) Give regular expressions defining the following languages over the al-
phabet Σ = {a, b, c}:
(a(ǫ + b))∗ (c + d + ∅)
G52MAL-E1
9 G52MAL-E1
2
a
0 1
b
3 4
Join the above two NFAs to obtain an NFA for a(ǫ + b), keeping in
mind that the left DFA does not accept ǫ:
a
2
a
0 1
b
a 3 4
0 b
a b
3 4
c
6 7
d
8 9
10
It’s clear that state 10 will become a dead end, so it can be removed.
Now, joining A with B (less state 10), while keeping in mind that A
can accept ǫ which means states 6 and 8 will remain initial states,
gives:
a
a a c
2 6 7
a
0 b b
a b d
3 4 8 9
It’s now clear that states 2, 4, and 5 are all dead ends, so we can
simplify and obtain the final NFA for (a(ǫ + b))∗ (c + d + ∅):
a
a c
6 7
a b
0
b
a b d
3 8 9
For example,
(a(ǫ + b))∗ + ∅
G52MAL-E1
11 G52MAL-E1
(a
a+
E → E + E1 | E1
E1 → E1 E2 | E2
E2 → E2 ∗ | E3
E3 → ( E ) | EP
EP → a | b | c | ǫ | ∅
Question 4
S → SpA | A
A → BmA | B
B → a | b | c | lSr
B m A
a B m A
l S r B
S p A a
S p A B
A B c
B b
G52MAL-E1
13 G52MAL-E1
(c) Is the following CFG ambiguous? If yes, show this. If no, explain why.
A → AaA | AbA | B
B → c
A ⇒ AaA
lm
⇒ BaA
lm
⇒ caA
lm
⇒ caAaA
lm
⇒ caBaA
lm
⇒ cacaA
lm
⇒ cacaB
lm
⇒ cacac
lm
and
A ⇒ AaA
lm
⇒ AaAaA
lm
⇒ BaAaA
lm
⇒ caAaA
lm
⇒ caBaA
lm
⇒ cacaA
lm
⇒ cacaB
lm
⇒ cacac
lm
S → aS | bX
X → XXc | Xd | Y
Y → Ye|f |g
A → Aα1 | . . . | Aαm
A → β1 | . . . | βn
Then replace those productions with new productions for A and pro-
ductions for A′ , where A′ is a new name, as follows:
A → β1 A′ | . . . | βn A′
A′ → α1 A′ | . . . | αm A′ | ǫ
S → aS | bX
X → Y X′
X ′ → XcX ′ | dX ′ | ǫ
Y → f Y ′ | gY ′
Y ′ → eY ′ | ǫ
G52MAL-E1
15 G52MAL-E1
Question 5
Consider the following Pushdown Automaton (PDA) P :
P = (Q = {q0 , q1 , q2 }, Σ = {a, b, c}, Γ = {a, #}, δ, q0 , Z0 = #, F = {q2 })
where the transition function δ is given by
δ(q0 , a, #) = {(q0 , a#)}
δ(q0 , c, #) = {(q0 , #)}
δ(q0 , a, a) = {(q0 , aa)}
δ(q0 , b, a) = {(q1 , ǫ)}
δ(q0 , c, a) = {(q0 , a)}
δ(q1 , c, #) = {(q1 , #)}
δ(q1 , b, a) = {(q1 , ǫ)}
δ(q1 , c, a) = {(q1 , a)}
δ(q1 , ǫ, #) = {(q2 , #)}
δ(q, w, z) = ∅ everywhere else
Acceptance is by final state.
(ii) The string abcabc is not accepted. For the first two moves, there
is no choice:
⊢ (q1 , abc, #)
⊢ (q2 , abc, #)
G52MAL-E1
17 G52MAL-E1
(iii) Set up the subset constraint system that defines the follow sets for
all nonterminals, i.e. follow(S), follow(A), follow(B), and follow(C).
Simplify where possible using the law
and the fact that constraints like A ⊆ A are trivially satisfied and
can be omitted. (7)
Answer: Note: detailed account below for clarity. It is sufficient
to just state the constraints according to the definitions and then
simplify.
Constraints for follow(S):
{$} ⊆ follow(S)
S → ABC
A → aA
first(BC) ⊆ follow(A)
follow(S) ⊆ follow(A)
first(ǫ) ⊆ follow(A)
follow(A) ⊆ follow(A)
S → ABC
S → BC
(note: nullable(C)):
first(C) ⊆ follow(B)
follow(S) ⊆ follow(B)
first(C) ⊆ follow(B)
follow(S) ⊆ follow(B)
S → ABC
S → BC
G52MAL-E1
19 G52MAL-E1
(note: nullable(ǫ)):
first(ǫ) ⊆ follow(C)
follow(S) ⊆ follow(C)
Using
first(ǫ) = ∅
first(C) = {c, d}
first(BC) = first(B) ∪ first(C) ∪ ∅
= {b} ∪ {c, d} = {b, c, d}
{b, c, d} ⊆ follow(A)
follow(S) ⊆ follow(A)
∅ ⊆ follow(A)
{c, d} ⊆ follow(B)
follow(S) ⊆ follow(B)
∅ ⊆ follow(C)
follow(S) ⊆ follow(C)
This is equivalent to
{$} ⊆ follow(S)
{b, c, d} ∪ follow(S) ∪ ∅ ⊆ follow(A)
{c, d} ∪ follow(S) ⊆ follow(B)
∅ ∪ follow(S) ⊆ follow(C)
{$} ⊆ follow(S)
{b, c, d} ∪ follow(S) ⊆ follow(A)
{c, d} ∪ follow(S) ⊆ follow(B)
∅ ∪ follow(S) ⊆ follow(C)
(iv) Solve the subset constraint system for the follow sets from the
previous question by finding the smallest sets satisfying the con-
straints. (2)
Answer: The smallest set satisfying the constraint for follow(S)
is obviously just {$}. Substituting this into the remaining con-
straints makes the smallest sets satisfying those obvious too. Thus:
follow(S) = {$}
follow(A) = {b, c, d} ∪ {$} = {b, c, d, $}
follow(B) = {c, d} ∪ {$} = {c, d, $}
follow(C) = {$}
G52MAL-E1 End