0% found this document useful (0 votes)
16 views8 pages

A2 Solutions (v1)

Uploaded by

silveytrash
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)
16 views8 pages

A2 Solutions (v1)

Uploaded by

silveytrash
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/ 8

comp2022/2922 A2 (90 marks) – CFLs and non-regularity s2 2023

• This assignment is due in Week 8 on Sunday 24 September, 11:59pm.


• Submission is on GradeScope.
• All work must be done individually without consulting anyone else’s solutions in accor-
dance with the University’s “Academic Dishonesty and Plagiarism” policies.
• For clarifications and more details on all aspects of this assignment (e.g., level of justifica-
tion expected, late penalties, repeated submissions, what to do if you are stuck, etc.) you
are expected to regularly monitor the Ed Forum post “Assignment FAQ”.

Problem 1. (10 marks) Fix Σ = { a, b}. For each of the following grammars,
provide five strings that are in the language and five strings that are not (you
score max(0, 5 − num errors) points per part).

1.
S → aSbb | ϵ

2.
S → aA
A → aA | bB
B → bB | ϵ

Solution 1. For instance:

1. ϵ, abb, aabbbb, aaabbbbbb, aaaabbbb are in the language.


a, aa, aaa, aaaa, aaaaa are not in the language.
2. ab, abb, abbb, abbbb, abbbbb are in the language.
a, aa, aaa, aaaa, aaaaa are not in the language.

Problem 2. (10 marks) Consider the following context-free grammar G over


Σ = { a, b}.
S → AA
A → AAA | bA | Ab | a

1. Show the grammar is ambiguous by providing two leftmost derivations of


the same string.
2. Provide a grammar in Chomsky Normal Form equivalent to G.

Solution 2.

1
comp2022/2922 A2 (90 marks) – CFLs and non-regularity s2 2023

1. For instance, here are two leftmost derivations of aba:


S ⇒ AA ⇒ AbA ⇒ abA ⇒ aba
S ⇒ AA ⇒ aA ⇒ abA ⇒ aba
2. (a) START step: nothing to do
(b) TERM step:
S → AA
A → AAA | Nb A | ANb | Na
Na →a
Nb →b

(c) BIN step:


S → AA
A → AB | Nb A | ANb | Na
B → AA
Na →a
Nb →b

(d) EPSILON step: nothing to do


(e) UNIT step:
S → AA | ANa | Na A | Na Na
A → AB | Nb A | Nb Na | ANb | Na Nb
B → AA | ANa | Na A | Na Na
Na →a
Nb →b

Problem 3. (20 marks) Fix Σ = { a, b}. Design context-free grammars for the
following languages:

1. The set of strings of length 6 that have the same number of a and b.
2. The set of strings of the form uvw with u, v, w ∈ { an bn |n ∈ Z + }.
3. The set of palindromes whose length is a multiple of 20. Use at most 100
rules.
4. The set of strings not in the set { an b2n+3 |n ∈ Z + }.

Solution 3. Here are possible answers.

1. The idea is to explicitly generate all the relevant strings. This is possible
because there is a small number of them.

2
comp2022/2922 A2 (90 marks) – CFLs and non-regularity s2 2023

S → aaabbb | aababb | aabbab | aabbba |


abaabb | ababab | ababba | abbaab | abbaba | abbbaa |
baaabb | baabab | baabba | babaab | bababa | babbaa |
bbaaab | bbaaba | bbabaa | bbbaaa

2. The idea is that T generates the language { an bn |n ∈ Z + }, and so S → TTT


generates the required language.

S → TTT
T → aTb
T → ab

3. The idea is that Ti generates all palindromes whose length is equal to 2i


modulo 20.

S → T0
T0 → aT9 a | bT9 b | ϵ
T9 → aT8 a | bT8 b
T8 → aT7 a | bT7 b
..
.
T1 → aT0 a | bT0 b

4.

S → XbaX | U | V | bbb
X → Xa | Xb | ϵ
U → ϵ | b | bb | aUbb | aU
V → bbbb | aVbb | Vb
Here, XbaX generates any word with ba as a substring, which is in the
language since it is not of the form an bm . U generates all words of the form
an bm where m < 2n + 3. V generates all words of the form an bm where
m > 2n + 3.

Problem 4. (20 marks) Fix Σ = { a, b}. A string is extreme if its amount of a is


more than twice its amount of b plus one, or the other way around. For example,
aaaab and babbabbbbba are both extreme, while bbbbbaa and ababab are not.

1. Prove that the set E of extreme strings is not regular (10 marks).

3
comp2022/2922 A2 (90 marks) – CFLs and non-regularity s2 2023

2. Prove that the set P of strings that have an extreme string as a prefix is not
regular. Recall that a string x is a prefix of a string y if y = xw for some
string w. Thus aaaabbbb is in P (since it has aaaab as a prefix), while ababab
is not in P (since no prefix of it is extreme). (10 marks)

Solution 4.

1. Define strings xi = ai for all positive integers i. We show that these strings
are pairwise distinguishable. Take xi , x j to be an arbitrary pair of these
strings, and without loss of generality assume i < j. Then take z = b2j+1 .
Notice that x j z is not extreme, because its amount of b is exactly equal to
twice its amount of a plus one, not greater, and clearly its amount of a is
not greater than twice its amount of b plus one. On the other hand, xi z is
extreme, because we have 2j + 1 > 2i + 1 as a consequence of j > i. Hence,
any two strings we defined are pairwise distinguishable, and thus we have
an infinite set of pairwise distinguishable strings.
2. Define strings xi = ( ab)i for all positive integers i. As before, take xi , x j
with i < j and take z = ai+2 . We have that xi z is in P, because its prefix xi z
has 2i + 2 ’a’ and i ’b’, so it is clearly extreme. On the other hand, consider
x j z = ( ab) j ai+2 . Prefixes of x j z are either prefixes of x j , or are x j followed
by some prefix of z. We consider these cases separately.
Case 1: a prefix of x j . Then it is of the form ( ab)k or ( ab)k a for some k. In
the first case, the amount of a and b are equal, so it is clearly not extreme. In
the second case, it has k + 1 ’a’ and k ’b’. Since it has more ’a’, it could only
be extreme from having more ’a’. But if so, we would have k + 1 > 2k + 1;
k < 0; a clear contradiction. So in this case the prefix is not extreme.
Case 2: x j followed by some prefix of z. Then it is of the form ( ab) j ak
for some k <= i + 2. This string contains j + k ’a’ and j ’b’, so it could
only be extreme from having more ’a’. This would imply j + k > 2j + 1;
j + i + 2 > 2j + 1; i + 1 > j; However, we have that i < j, and since i, j are
integers we have either i + 1 < j or i + 1 = j. So this is a contradiction, and
the prefix is not extreme.
Therefore, no prefix of x j z is extreme, and so x j z is not in P. Hence xi and
x j are distinguishable and the result follows.

Problem 5. (10 marks) Fix Σ = { a, b}. Write a program that takes in a context-
free grammar G in CNF and outputs a context-free grammar for the language of
strings that are in L( G ) and whose number of as is a multiple of 5.
For full marks, your solution should be able to take an arbitrary CNF grammar
with 10 variables and output a grammar with at most 100 variables.

Solution 5. Here is the idea.

4
comp2022/2922 A2 (90 marks) – CFLs and non-regularity s2 2023

For each variable V in G, we introduce five variables V0 , V1 , V2 , V3 , V4 in the new


grammar. The new starting variable is S0 . For each rule V → α in G, we add
every rule Vi → α′ into the new grammar such that

1. i is 0, 1, 2, 3 or 4;
2. α′ is the same as α, but for each occurrence of a variable in α, say W. re-
placed by one of W0 , W1 , W2 , W3 , W4 ;
3. i is equal, modulo 5, to the number of as in α′ , plus the sum of the subscripts
of each variable in α′ .

This construction works, since the third bullet point guarantees that the variable
Vi in the new grammar generates those words that V generated in the original
grammar whose number of as is equivalent to i modulo 5.
If G has n variables, the new grammar has 5n variables.
This idea also works for grammars that are not in CNF. For instance: S →
aSa|bSb|ϵ becomes
S0 → aS3 a|bS0 b|ϵ
S1 → aS4 a|bS1 b
S2 → aS0 a|bS2 b
S3 → aS1 a|bS3 b
S4 → aS2 a|bS4 b

Problem 6. (20 marks) You’re honing your skills for the world championship
of popular card game Magic: the Gathering. Your goal is to draw through all
the cards in your deck, and to this end, you’ve included lots of cards that help
you draw more cards. However, if you get unlucky, with some deck orderings
you’ve found that this is impossible. You’re interested in determining which
deck orderings can be won, and which can’t.
At the start of the game, you draw seven cards from your deck into your hand.
The cards in your deck are of the following types:

• Ancestral Recall. When played, you draw three cards.


• Wheel of Fortune. When played, you discard your hand, and then draw seven
cards.
• Cruel Bargain. When played for the first time in a game, you draw four
cards. It cannot be played more than once in a game.
• Other, useless cards.

Aside: Unlike in the actual game, you can play any number of cards from your
hand in any order, without needing to pay mana.

5
comp2022/2922 A2 (90 marks) – CFLs and non-regularity s2 2023

Your goal is to draw all the cards in your deck, at which point you win. Note that
”overdraw” (drawing more cards than exist in your deck) meets this condition,
and so is also a win. We encode a possible ordering of your deck as a string
over the alphabet Σ = { a, w, c, x } where a, w, c correspond to the cards starting
with those letters, and x corresponds to a useless card. We are interested in the
language of strings for which it is possible for you to win.
For example, the following strings are in the language:
aax11 , wx13 , cawx18 , awx5 wx13
while the following strings are not in the language:
aax12 , wx14 , cawx19 , awx5 wx14
Design a context-free grammar for the language described.

• 4 marks will be awarded for cases with strings containing only a, x.


• 4 marks will be awarded for cases with strings containing only a, c, x.
• 4 marks will be awarded for cases with strings containing only w, x.
• 4 marks will be awarded for cases with strings containing only a, w, x.
• The final 4 marks will be awarded for the general case, i.e., for strings over
the alphabet a, w, c, x.

Solution 6.

6
comp2022/2922 A2 (90 marks) – CFLs and non-regularity s2 2023

Consider the following CFG with starting variable S′ .


S →
Dϵ | Dw S
Dϵ →
Tϵ Tϵ Tϵ Tϵ Tϵ Tϵ Tϵ
Tϵ →
aTϵ Tϵ Tϵ | a | w | c | x | ϵ
Dw →
Tw TTTTTT | TTw TTTTT | TTTw TTTT | TTTTw TTT |
TTTTTw TT | TTTTTTw T | TTTTTTTw
T → aTTT | a | w | c | x
Tw → aTw TT | aTTw T | aTTTw | w
S′ → Dϵ′ | Dw

S | Dw S ′
Dϵ′ → Tϵ′ Tϵ Tϵ Tϵ Tϵ Tϵ Tϵ | Tϵ Tϵ′ Tϵ Tϵ Tϵ Tϵ Tϵ | Tϵ Tϵ Tϵ′ Tϵ Tϵ Tϵ Tϵ | Tϵ Tϵ Tϵ Tϵ′ Tϵ Tϵ Tϵ
| Tϵ Tϵ Tϵ Tϵ Tϵ′ Tϵ Tϵ | Tϵ Tϵ Tϵ Tϵ Tϵ Tϵ′ Tϵ | Tϵ Tϵ Tϵ Tϵ Tϵ Tϵ Tϵ′
Tϵ′ → cTϵ Tϵ Tϵ Tϵ | aTϵ′ Tϵ Tϵ | aTϵ Tϵ′ Tϵ | aTϵ Tϵ Tϵ′ | a | w | c | x | ϵ

Dw → Tw′ TTTTTT | T ′ Tw TTTTT | T ′ TTw TTTT | T ′ TTTw TTT |
T ′ TTTTw TT | T ′ TTTTTw T | T ′ TTTTTTw |
Tw T ′ TTTTT | TTw′ TTTTT | TT ′ Tw TTTT | TT ′ TTw TTT |
TT ′ TTTw TT | TT ′ TTTTw T | TT ′ TTTTTw |
Tw TT ′ TTTT | TTw T ′ TTTT | TTTw′ TTTT | TTT ′ Tw TTT |
TTT ′ TTw TT | TTT ′ TTTw T | TTT ′ TTTTw |
Tw TTT ′ TTT | TTw TT ′ TTT | TTTw T ′ TTT | TTTTw′ TTT |
TTTT ′ Tw TT | TTTT ′ TTw T | TTTT ′ TTTw |
Tw TTTT ′ TT | TTw TTT ′ TT | TTTw TT ′ TT | TTTTw T ′ TT |
TTTTTw′ TT | TTTTT ′ Tw T | TTTTT ′ TTw |
Tw TTTTT ′ T | TTw TTTT ′ T | TTTw TTT ′ T | TTTTw TT ′ T |
TTTTTw T ′ T | TTTTTTw′ T | TTTTTT ′ Tw |
Tw TTTTTT ′ | TTw TTTTT ′ | TTTw TTTT ′ | TTTTw TTT ′ |
TTTTTw TT ′ | TTTTTTw T ′ | TTTTTTTw′ |
T′ → aT ′ TT | aTT ′ T | aTTT ′ | a | w | c | x
Tw′ → cTw TTT | cTTw TT | cTTTw T | cTTTTw |
aTw TT | aTTw T | aTTTw | w
S generates those decks drawable according to the rules, but without using any
c moves.
Dϵ generates those decks drawable only using a moves possibly drawing past
the end of the deck, if you start with (up to) the first 7 cards in hand, without
using any c moves.
Dw is like D, but without drawing past the end of the deck, and the deck must
contain at least one w.
T generates those sequences drawable using only a moves if you start with just
the first card in hand, without drawing past the end of the deck, without using

7
comp2022/2922 A2 (90 marks) – CFLs and non-regularity s2 2023

any c moves.
Tϵ is like T, but possibly drawing past the end of the deck.
Tw is like T, but the sequence must contain at least one w.
S′ , Dϵ′ , Dw
′ , T ′ , T ′ and T ′ are like the above, but using up to one c move.
ϵ w
Note that each variable of the form Vw produces either at least one w or at
least one other variable of the form Ww , to ensure at least one w is produced.
Similarly, each variable of the form V ′ produces either at most c or at most one
other variable of the form W ′ , to ensure at most one c move is used.

You might also like