HW3 Solutions 2017 Spring
HW3 Solutions 2017 Spring
HW3 Solutions 2017 Spring
Homework 3
Instructor: Prof. Wen-Guey Tzeng Scribe: Amir Rezapour
1-1
2
For example, for w = aaababbb we can use the grammar for derivation as follows:
S2 ⇒ aS2 b ⇒ aaS2 S2 bb ⇒ aaaS2 bS2 bb ⇒ aaabaS2 bbb ⇒ aaababbb. Since we only
allow string start with a’s, any prefix v of a string w has na (v) ≥ nb (v). For example,
v = a, aa, aaa, aaab, aaaba, aaabab, aaababb and aaababbb are all possible prefix of
w with na (v) ≥ nb (v).
4. Find context-free grammars for the language L = {an bm ck : k = |n − m|}. (with
n ≥ 0, m ≥ 0, k ≥ 0)
Answer.
∵ k = |n − m| ⇒ k = n − m for n ≥ m or k = m − n for m ≥ n, i.e., n = k + m or
m = k + n.
∴ Parse L as L = L1 ∪ L2 , where L1 = {an bm ck : n = k + m} and L2 = {an bm ck :
m = k + n}.
A CFG for L1 is G1 = ({S1 , T1 , T2 }, {a, b, c}, S1 , P1 ) with production P1 as:
S1 → T1 |T2
T1 → aT1 b|λ
T2 → aT2 c|T1 |λ
A CFG for L2 is G2 = ({S2 , T3 , T4 }, {a, b, c}, S2 , P2 ) with production P2 as:
S2 → T3 |T4
T3 → aT3 b|λ
T4 → bT4 c|λ
Thus, a CFG for L is G = ({S, S1 , S2 , T1 , T2 , T3 , T4 }, {a, b, c}, S, P ) with production
P as:
S → S1 |S2
S1 → T1 |T2
T1 → aT1 b|λ
T2 → aT2 c|T1 |λ
S2 → T3 |T4
T3 → aT3 b|λ
T4 → bT4 c|λ
2
5. Show that the complement of the language L = {wwR : w ∈ {a, b}∗ } is context-free.
Answer.
A CFG for L is G = ({S}, {a, b}, S, P ) with production P as:
S → a|b|ab|ba|aSa|aSb|bSa|bSb
2
1-2
6. Consider the grammar with productions
S → aaB,
A → bBb|λ,
B → Aa.
Show that the string aabbabba is not in the language generated by this grammar.
Answer.
S → AB|aaaB,
A → a|Aa,
B → b.
Answer.
This grammar produces the strings ab, aab, aaab, . . ., i.e., it generates G = ({S, A}, {a, b}, S, P )
with productions.
S → Ab,
A → a|Aa.
8. Give the derivation tree for (((a + b) ∗ c)) + a + b, using the grammar G = (V, T, E, P )
with V = {E, T, F, I}, T = {a, b, c, +, ∗, (, )},
E → T,
T → F,
F → I,
E → E + T,
T → T ∗ F,
F → (E),
I → a|b|c.
Answer.
2
1-3
9. Is the string aabbababb in the language generated by the grammar S → aaS|b?
Answer.
No. S → aaS|b cannot generate aabbababb.
• 1st round: (1) S ⇒ aaS; (2) S ⇒ b;
• 2nd round: from (1), S ⇒ aaS ⇒ aaaaS;
Thus, we conclude that the string aabbababb cannot be generated by the given gram-
mar. 2
1-4