0% found this document useful (0 votes)
49 views4 pages

Homework 6

Uploaded by

shixinren813
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)
49 views4 pages

Homework 6

Uploaded by

shixinren813
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/ 4

Formal Languages and Automata Ren Shixin

2024 Autumn 2023012444

Homework 6

Problem 1. Design context-free grammars for the following languages:


(a) (Exercise 5.1.1 (b)) The set {ai bj ck | i ̸= j or j ̸= k}, that is, the set of
strings of a’s followed by b’s followed by c’s, such that there are either
a different number of a’s and b’s or a different number of b’s and c’s, or
both.
(b) (Exercise 5.1.1 (c)) The set of all strings of a’s and b’s that are not of the
form ww, that is, not equal to any string repeated.
(c) {an bm | n, m ≥ 0 ∧ 3n ≥ m ≥ 2n}.
(d) {am bn cp dq | n, m, p, q ≥ 0, m + n = p + q}.
(e) {an bi cj dm | n, m, i, j ≥ 0, n + m = i + j}.
Solution. (a) We can construct this context-free grammars for it. Then G =
(V, P, T, S), V = {S, X, Y, A, B, C}, where S is the start variable; set of terminal
is T = {a, b, c}, and rules

S → AX | Y C
A → aA | ϵ
B → bB | ϵ
C → cC | ϵ
X → bXc | bB | cC
Y → aY b | aA | bB

(b) We can construct this context-free grammars for it. Then G = (V, P, T, S),
V = {S, A, B}, where S is the start variable; set of terminal is T = {a, b}, and
rules

S → AB | BA | A | B
A → aAa | aAb | bAa | bAb | a
B → aBa | aBb | bBa | bBb | b

(c) We can construct this context-free grammars for it. Then G = (V, P, T, S),
V = {S, A}, where S is the start variable; set of terminal is T = {a, b}, and
rules

S→A
A → aAbbb | aAbb | ϵ

(d) We can construct this context-free grammars for it. Then G = (V, P, T, S),
V = {S, A, B, C, D}, where S is the start variable; set of terminal is T =

1
Homework 6 Ren Shixin

{a, b, c, d}, and rules


S→A
A → aAd | aBc | bCd | bDc | ϵ
B → aBc | bDc | ϵ
C → bCd | bDc | ϵ
D → bDc | ϵ

(e) We can construct this context-free grammars for it. Then G = (V, P, T, S),
V = {S, A, B, C, D, E, F }, where S is the start variable; set of terminal is
T = {a, b, c, d}, and rules
S → AB | CD | ϵ
A → aAb | ϵ
B → bBd | Eϵ
C → aCc | F ϵ
D → cDd | ϵ
E → cEd | ϵ
F → aF b | ϵ

Problem 2. (Exercise 5.1.8) Consider the CFG G defined by productions:


S → aSbS | bSaS | ϵ
Prove that L(G) is the set of all strings with an equal number of a’s and b’s.
Solution. we consider the set of all strings with an equal number of a’s and b’s
as S.
Part 1, we prove that L(G) ⊆ S. We show inductively if S →n x, then x
has a balanced number of a’s and b’s. Our base case is the length 1 derivation
is S → ϵ, and it’s clearly.
Next we assume if S →k x for any k ≤ n then x has a balanced number of
a’s and b’s. Now suppose S →n+1 x. Then consider two cases.
′ ′′ ′ ′′
1. S → aS bS . In this case we know S → y, S → z, where y, z is balanced.
Thus x is balanced as well.
′ ′′ ′ ′′
2. S → bS aS . In this case we know S → y, S → z, where y, z is balanced.
Thus x is balanced as well.
Second, We prove S ⊆ G.
We show inductively if x has a balanced number of a’s and b’s, then x ∈ L(G).
Our base case is the n = 0, x = ϵ, n = 1, x = {ab, ba}, and it’s clearly.
Next we assume if that for any k ≤ n, |x| ≤ 2n is correct. Now suppose
x = |2n + 2|, n ≥ 2.
We can assume x begins with a(the cases for b is the same), then find the
first postion j, s.t. x[1 : j] has equal number of a’s and b’s. so xj = b.
1. j = 2n + 2, then x = an+1 bn+1 ∈ S
2. j ̸= 2n + 2, xj = b, so x = ax[2 : j − 1]bx[j + 1 : 2n + 2] ∈ aSbS ⊆ S

2
Homework 6 Ren Shixin

Problem 3. (Exercise 5.4.7) The following grammar generates prefix expressions


with operands x and y and binary operators +, − and ∗:
E → +EE | ∗EE | −EE | x | y.

(a) Find leftmost and rightmost derivations, and a derivation tree for the
string +∗−xyxy.
(b) Prove that this grammar is unambiguous.
Solution. (a)

E ==⇒ +EE ==⇒ +∗EEE ==⇒ +∗−EEEE ==⇒ + ∗ −xEEE ==⇒ +∗−xyEE
lm lm lm lm lm

==⇒ +∗−xyxE ==⇒ +∗−xyxy


lm lm
E

+ E E

* E E y

- E E x

x y
(b)
We put the operator on their father node, just like below, we call them
operator tree. It clearly that them form parse trees double mapping.
+

* E

- E y

E E x

x y

3
Homework 6 Ren Shixin

We prove that by contradiction. If this grammar is ambiguous, then it


corresponds two parse trees at least. The same time, it corresponds two operator
tree.
We consider 2 simple Lemma:
1. Every prefix expression’s operator tree, the number of (’+’, ’*’, ’-’) is
1 less than that of variables. 2. From back to front on prefix expression, its
variables nodes more than operator nodes.
If a prefix expression E has length of l corresponds two tree T1 , T2 , T1 has two
subsree TE[1:k] , TE[k+1,l] , T2 has two subtree TE[1:m] , TE[m+1,l] , m > k, consider
E[k + 1 : m], it should have the same amount of variables node and operator
nodes.
But this interval is suffix of E[1 : m], so according Lemma 2, we know the
number of variablesnode is more than that of operator nodes. So we get the
contradiction.
And We prove the grammar is unambiguous.

Problem 4. Give both an ambiguous and unambiguous CFG for {am bn | m ≥


2n ≥ 0}.
Solution. the ambiguous:
We can construct this context-free grammars for it. Then G = (V, P, T, S),
V = {S}, where S is the start variable; set of terminal is T = {a, b}, and rules

S → ϵ | aS | aaSb

the unambiguous:
We can construct this context-free grammars for it. Then G = (V, P, T, S),
V = {S, B}, where S is the start variable; set of terminal is T = {a, b}, and
rules

S → ϵ | aB | aaSb
B → ϵ | aB

You might also like