Professional Documents
Culture Documents
Assignment 3
Assignment 3
Assignment 3
Grades: 24+16+10+10+40=100
m
e r as
Exercise 1: 24=(6*4)
Give a context free grammar G=(V, ∑, R, S) for each of the following languages (you are
co
eH w
required to define the four components of your grammar):
a. All binary strings of the form: 10n10n1
o.
rs e
b. All strings of the form: 0n1m2k with n=m+k where n>=0
ou urc
c. All binary strings of the form: 0n1m0m1n where n>=0 and m>=0
d. All strings of the form: 0i1j2k with i=j or i=k where i>=0
e. All strings over the alphabet ∑={a, b} of the form: ambn with m <= n <= 2m
o
f. All strings over the alphabet ∑={a, b} such that the middle symbol is a
aC s
v i y re
Solutions:
ed d
ar stu
sh is
Th
https://www.coursehero.com/file/14281570/Solutions-assignment-3/
(e)
(f)
Exercise 2: 16=(2+2+2+2+8)
Consider the following G=(V, ∑, R, S) where ∑={a} :
S BB
B SS | a
a. What is the smallest string in L(G)
m
b. Give the parse tree of this smallest string
e r as
c. Is G ambiguous?
co
eH w
d. Describe in English the language generated by G.
e. Is L(G) regular? In the affirmative, give the RE and the regular grammar that generate
o.
L(G). rs e
ou urc
Solutions:
o
aC s
v i y re
ed d
ar stu
sh is
Th
Exercise 3: 10
Consider the alphabet ∑= {0, 1} ; give a context free grammar that generates the language of
binary strings with an unequal number of 0's and 1's.
Solution:
G=(V, ∑, R, S) where V-∑={S, M, L, E} and R given by:
https://www.coursehero.com/file/14281570/Solutions-assignment-3/
Exercise 4: 10
Give the context free grammar G=(V, ∑, R, S) such that L(G) is the set of all strings
over ∑,= {a, b} with twice as many a's as b's.
Solution:
m
G=(V, ∑, R, S) where V- ∑= {S} and R is given by:
e r as
S aSaSbS | aSbSaS | bSaSaS | ϵ
co
eH w
o.
rs e
ou urc
Exercise 5: 40=(4*(5+5))
o
Construct a PDA for each of the languages defined in question 1. You are requires to
provide the algorithm in terms of "Read, Push, Pop, Accept" and the six components
aC s
Solutions:
a.
ed d
M=(K, ∑, Г, Δ, s, F)
ar stu
Δ: (q, 1, e)(p, 0)
(p, 0, e)(p, 0)
(p, 1, e)(t, e)
sh is
(t, 0, 0)(t, e)
(t, 1, e)(f, e)
Th
K={q, p, t, f}
F={f}
Г={0}
b.
M=(K, ∑, Г, Δ, s, F)
Δ: (q, 0, e)(q, 0)
(q, 1, 0)(p, e)
(q, 2, 0)(t, e)
(p, 1, 0)(p, e)
(p, 2, 0)(t, e)
(t, 2, 0)(t, e)
K={q, p, t} F={q, p, t} and Г={0}
https://www.coursehero.com/file/14281570/Solutions-assignment-3/
C.
M=(K, ∑, Г, Δ, s, F)
Δ: (q, 0, e)(q, 0)
(q, 1, e)(p, 1)
(q, 1, 0)(f, e)
(p, 1, e)(p, 1)
(p, 0, 1)(t, e)
(t, 0, 1)(t, e)
(t, 1, 0)(f, e)
(f, 1, 0)(f, e)
K={q, p, t, f}
F={q, p, t; f}
Г={0, 1}
m
e r as
d.
co
M=(K, ∑, Г, Δ, s, F)
eH w
Δ: (q, 0, e)(q, 0)
o.
rs e
(q, 1, 0)(p, e) (q, 1, e)(t, e)
ou urc
(q, 2, e)(l, e) (t, 1, e)(t, e)
(p, 1, 0)(p, e) (t, 2, 0)(f, e)
(p, 2, e)(k, e) (f, 2, 0)(f, e)
o
(k, 2, e)(k, e)
aC s
(l, 2, e)(l, e)
v i y re
K={q, p, t, f, l}
F={q, p, t, f, l}
Г={0, 1}
ed d
ar stu
e.
M=(K, ∑, Г, Δ, s, F)
Δ: (q, e, e)(p, e) this is to accept the empty string
sh is
(q, a, e)(q, a)
(q, a, e)(p, aa)
(q, b, a)(p, e)
(p, b, a)(p, e)
K={q, p}
F={p}
Г={a}
https://www.coursehero.com/file/14281570/Solutions-assignment-3/
f.
M=(K, ∑, Г, Δ, s, F)
Δ: (q, a, e)(q, x) for each a or b read, we push x into the stack
(q, b, e)(q, x)
(q, a, e)(p, e) here we consider non deterministically that this a is the middle
K={q, p}
F={p}
Г={a, b}
m
e r as
co
eH w
o.
rs e
ou urc
o
aC s
v i y re
ed d
ar stu
sh is
Th
https://www.coursehero.com/file/14281570/Solutions-assignment-3/