(123doc) - Bai-Tap-Ngon-Ngu-Hinh-Thuc
(123doc) - Bai-Tap-Ngon-Ngu-Hinh-Thuc
3. Which of the strings 0001, 01001, 0000110 are accepted by the following
dfa:
11. Find an nfa with three states that accepts the language {ab,abc}*
15. Find a regular expression for the set {anbm: (n+m) is even}
16. Give a regular expression for the language on ∑={a,b,c} containing no
run of a’s of length greater than two.
17. Give a regular expression for the language on ∑={a,b} containing all
strings not ending in 01.
19. Find regular expressions for the language accepted by the following
automaton:
20. Construct a dfa that accepts that language generated by the following
grammar
S abA
A baB
B aA | Bilbo
23. Construct a dfa that accepts that language generated by the following
grammar
S abA
A baB
B aA | bb
24. Construct left-linear grammar for the language in Problem 2
32. Show a derivation tree, together with the corresponding leftmost and
rightmost derivations of the string aabbbb with the grammar
S AB | λ
A aB
B Sb
33. Find a context-free grammar for the set of all regular expressions on the
alphabet {a,b}*. Give a derivation tree for (a+b)*+a+b
S AB | aaB
A a | aA
Bb
S aSbS|bSaS|λ
S abAB
A bAB | λ
B BAa | A | λ
S ab | aS | SS
45. Transform the following grammars to Chomsky normal form:
S aSb | ab
S aSaA | A
A abA |b
S abAB
A bAB | λ
B BAa | A | λ
47. Prove that the following dpa does not accept any string not in {wwR}
Q = {q0,q1,q2}, ∑ = {a,b}, Γ = {a,b,z}, F = {q2}
δ(q0,a,a) = {(q0,aa)}
δ(q0,b,a) = {(q0,ba)}
δ(q0,a,b) = {(q0,ab)}
δ(q0,b,b) = {(q0,bb)}
δ(q0,a,z) = {(q0,az)}
δ(q0,b,z) = {(q0,bz)}
δ(q0,λ,a) = {(q1,a)}
δ(q0,λ,b) = {(q1,b)}
δ(q1,a,a) = {(q1,λ)}
δ(q1,b,b) = {(q1,λ)}
δ(q1,λ,z) = {(q2,λ)}
49. Prove that the following pda accepts the language L = {an+1b2n}
δ(q0,λ,z) = {(q1,Sz)}
δ(q1,a,S) = {(q1,SA),(q1,λ)}
δ(q1,b,A) = {(q1,B)}
δ(q1,b,B) = {(q1,λ)}
δ(q1,λ,z) = {(q2,λ)}
0001
01001
δ*(q0,1010) = {q0,q2}
δ*(q1,00) = {}
62. Find an nfa with three states that accepts the language {ab,abc}*
b.
Solutions:
a.
b.
Optimize the DFA if you want (read Section 1.3 for DFA optimization):
65. Find a regular expression for the set {anbm: (n+m) is even}
(aa)*(bb)* + (aa)*a(bb)*b
(a(b+c)+(b+c)+aa(b+c))(aa+a+λ)
Method 2:
The required regular expression can then be derived from the DFA, which
should be the same as that given from Method 1
67. Give a regular expression for the language on ∑={a,b} containing all
strings not ending in ab.
(a+b)*(aa+ab+ba) + b + λ
1*01*01*0
70. Construct a dfa that accepts that language generated by the following
grammar
S abA
A baB
B aA | bb
nor(L1,L2) = L1 ∪ L2
a. L = {anbm: n ≤ m +3}
S ABC
B aBb | λ
A aaa | aa | a | λ
C Cb | λ
b. L = {anbm: n ≠ m -1}
S S1 | S2
S1 BA
B aB | a
A aAb | b
S2 AC
C bC | b
c. L = {anbm: n ≠ 2m}
S S1 | S2 | aSb
S1 BA
B aB | a
A aaAb | λ
S2 AC
C bC | b
d. L = {anbm: 2n ≤ m ≤ 3m}
S aSB | λ
B bb | bbb
e. L = {anbmck: n = m or m ≤ k}
S S1 | S2
S1 AB
A aAb | λ
B cB | λ
S2 CD
C aC | λ
D bDc | B
f. L = {anbmck: k = |n-m|}
S S1 | S2
S1 aS1c | S3
S3 aS3b | λ
S2 S3S4
S4 bS4c | λ
g. L = {anwwRbn: w ∈{a,b}*}
S aSb | A
A aAa | aAb | λ
h. L = {anbn}2
S LL
L aLb | λ
79. Show a derivation tree, together with the corresponding leftmost and rightmost
derivations of the string aabbbb with the grammar
S AB | λ
A aB
B Sb
S => AB => aBB => aSbB => aABbB => aaBBbB => aaSbBbB => aabBbS => aabSbbB
=> aabbbB => aabbbSb => aabbbb
80. Find a context-free grammar for the set of all regular expressions on the alphabet
{a,b}*. Give a derivation tree for (a+b)*+a+b
EE+T|T
T T.F | F
F (E) | E*| a | b
The tree is left for students to construct
S aSbS|bSaS|λ
Draw the corresponding trees for the above derivations to observe the ambiguities.
S aSbT | T
T ST | λ
Show a derivation for the string w = aabbab using both orignal and rewriten grammars
S => SS => aSbS =>aaSbbS => aabbS => aabbaSb => aabbab
S => aSbT => aaSbTbT => aaTbTbT => aabTbT => aabbT => aabbST => aabbaSbTT =>
aabbabTT => aabbabTT => aabbabT => aabbab
86. Eliminate useless and unit productions for the following grammar:
S a | aA | B | C
A aB | λ
B Aa
C cCD
D ddd
Directly replace A by λ
S aB | aaB
B bb | λ
a)
S aSb | ab
b)
S aSaA | A
A abA |b
c)
S abAB
A bAB | λ
B BAa | A | λ
Solution:
a)
S AS1 | AB
S1 SB
Aa
B b
b)
Remove unit production
S aSaA | abA | b
A abA | b
c)
Remove λ-production
S abAB | abB | abA | ab
A bAB | bA | bB | b
B BAa | A | Ba | Aa | a
Remove unit-production
S abAB | abB | abA | ab
A bAB | bA | bB | b
B BAa | Ba | Aa | bAB | bA | bB | b | a
Solutions:
a)
- Conversion to Chomsky form:
S AS1 | AB
S1 SB
A a
Bb
- Labeling
A1 A2A3 | A2A4
A3 A1A4
A2 a
A4 b
- Sorting
A1 A2A3 | A2A4
A3 aA3A4 | aA4A4
A2 a
A4 b
- Transforming
A1 aA3 | aA4
A3 aA3A4 | aA4A4
A2 a
A4 b
b)
- Conversion to Chomsky form:
S AB | AS | SS
Aa
Bb
- Labeling
A1 A2A3 | A2A1| A1A1
A2 a
A3 b
- Sorting
A1 A2A3 | A2A1| A2A3T | A2A1T
T A1T | A1
A2 a
A3 b
- Transforming
A1 aA3 | aA1| aA3T | aA1T
T aA3T | aA1T| aA3TT | aA1TT | aA3 | aA1| aA3T | aA1T
A2 a
A3 b
(One can realize that A2 a now becomes useless and removable from the grammar)
90. Prove that the following ndpa does not accept any string not in {wwR}
Q = {q0,q1,q2}, ∑ = {a,b}, Γ = {a,b,z}, F = {q2}
δ(q0,a,a) = {(q0,aa)}
δ(q0,b,a) = {(q0,ba)}
δ(q0,a,b) = {(q0,ab)}
δ(q0,b,b) = {(q0,bb)}
δ(q0,a,z) = {(q0,az)}
δ(q0,b,z) = {(q0,bz)}
δ(q0,λ,a) = {(q1,a)}
δ(q0,λ,b) = {(q1,b)}
δ(q1,a,a) = {(q1,λ)}
δ(q1,b,b) = {(q1,λ)}
δ(q1,λ,z) = {(q2,λ)}
One can observe that the following move sequences cannot happen on the ndpa:
(q1,x,y) |* (q0,x’,y’) ∀x,x’∈ ∑*, y,y’∈ ∑*; and
(q2,x,y) |* (q1,x’,y’) ∀x,x’∈ ∑*, y,y’∈ ∑*.
Thus any string w accepted by the ndpa must be of the sequence form as follows:
(q0,w,z) |* (q1,x’,y’) |*(q1,λ, z) | (q2,λ, λ).
It can be observed easily that in order to make above sequence possible, w must be of the
form {wwR}
a) L = {anb2n}
δ(q0,λ,z) = {(qf,z)}
δ(q0,a,z) = {(q0,aaz)}
δ(q0,a,a) = {(q0,aaa)}
δ(q0,b,a) = {(q1,λ)}
δ(q1,b,a) = {(q1,λ)}
δ(q1,λ,z) = {(qf,z)}
b) L = {wcwR}
δ(q0,a,z) = {(q0,az)}
δ(q0,b,z) = {(q0,bz)}
δ(q0,c,z) = {(q0,cz)}
δ(q0,c,z) = {(q1,z)}
δ(q0,a,a) = {(q0,aa)}
δ(q0,a,b) = {(q0,ab)}
δ(q0,a,c) = {(q0,ac)}
δ(q0,b,a) = {(q0,ba)}
δ(q0,b,b) = {(q0,bb)}
δ(q0,b,c) = {(q0,bc)}
δ(q0,c,a) = {(q0,ca)}
δ(q0,c,b) = {(q0,cb)}
δ(q0,c,c) = {(q0,cc)}
δ(q0,c,a) = {(q1,a)}
δ(q0,c,b) = {(q1,b)}
δ(q0,c,c) = {(q1,c)}
δ(q1,a,a) = {(q1,λ)}
δ(q1,b,b) = {(q1,λ)}
δ(q1,c,c) = {(q1,λ)}
δ(q1,λ,z) = {(qf,z)}
c) L = {a3bncn}
δ(q0,λ,z) = {(q0,3z)}
δ(q0,a,3) = {(q0,2)}
δ(q0,a,2) = {(q0,1)}
δ(q0,a,1) = {(q0,0)}
δ(q0,b,0) = {(q0,b)}
δ(q0,b,b) = {(q0,bb)}
δ(q0,c,b) = {(q1,λ)}
δ(q1,c,b) = {(q1,λ)}
δ(q1,λ,z) = {(qf,z)}
d) L = {anbm, n ≤ m ≤ 3n}
δ(q0,λ,z) = {(qf,z)}
δ(q0,a,z) = {(q0,az)}
δ(q0,a,z) = {(q0,aaz)}
δ(q0,a,z) = {(q0,aaaz)}
δ(q0,a,a) = {(q0,aa)}
δ(q0,a,a) = {(q0,aaa)}
δ(q0,a,a) = {(q0,aaaa)}
δ(q0,b,a) = {(q1,λ)}
δ(q1,b,a) = {(q1,λ)}
δ(q1,λ,z) = {(qf,z)}
92. Prove that the following pda accepts the language L = {an+1b2n}
δ(q0,λ,z) = {(q1,Sz)}
δ(q1,a,S) = {(q1,SA),(q1,λ)}
δ(q1,b,A) = {(q1,B)}
δ(q1,b,B) = {(q1,λ)}
δ(q1,λ,z) = {(q2,λ)}
S aSA | a
A bB
Bb
or
S aSA | a
A bb
or
S aSbb | a