0% found this document useful (0 votes)
189 views23 pages

(123doc) - Bai-Tap-Ngon-Ngu-Hinh-Thuc

The document contains questions about formal languages and automata theory including regular expressions, finite automata, context-free grammars, and pushdown automata. It covers topics like determining language membership, constructing automata and grammars, operations on languages, and properties of language classes. Many problems ask to find regular expressions, convert between models of computation, or prove languages are/aren't regular or context-free.

Uploaded by

Hương Hương
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)
189 views23 pages

(123doc) - Bai-Tap-Ngon-Ngu-Hinh-Thuc

The document contains questions about formal languages and automata theory including regular expressions, finite automata, context-free grammars, and pushdown automata. It covers topics like determining language membership, constructing automata and grammars, operations on languages, and properties of language classes. Many problems ask to find regular expressions, convert between models of computation, or prove languages are/aren't regular or context-free.

Uploaded by

Hương Hương
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/ 23

1. Let L = {ab,aa,baa}.

Which of the following strings are in L*:


abaabaaabaa, aaaabaaaa,
baaaaabaaaab
baaaaabaa?

2. For which language it is true that L = L*?


a. L = {anbn+1: n≥0}
b. L = {w: na(w)=nb(w)}

3. Which of the strings 0001, 01001, 0000110 are accepted by the following
dfa:

4. Give dfa’s for the sets consisting of


a. all strings with exactly one a.
b. all strings with no more than three a’s

5. Give a dfa for the language L = {ab5wb4: w ∈ {a,b}*}

6. Find dfa’s for the following languages on ∑ = {a,b}


a. L = {w: |w| mod 3 = 0}
b. L = {w: |w| mod 5 ≠ 0}
c. L = {w: na(w) mod 3 > 1}

7. Consider the set of strings on {0,1} in which every 00 is followed


immediately by 1. For example 101, 0010, 0010011001 are in the language,
but 0001 and 00100 are not. Construct an accepting dfa

8. Show that the language L = {an: n ≥ 0, n ≠ 4} is regular.

9. Find δ*(q0,a) and δ*(q1,λ) for the following nfa


10. For the following nfa, find δ*(q0,1010) and δ*(q1,00)

11. Find an nfa with three states that accepts the language {ab,abc}*

12. Convert the following nfa into dfa

13. Convert the following nfa into dfa

14. Find all strings in L((a+b)*b(a+ab)*) of length less than four.

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.

18. Give a dfa that accepts L((a+b)*b(a+ab)*)

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

21. Construct left-linear grammar for the language in Problem 8

22. Construct right-linear and left-linear grammars for the language L =


{anbm,n≥2,m≥3}

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

25. Construct right-linear and left-linear grammars for the language L =


{anbm,n≥2,m≥3}
26. Find intersection of the two following languages: L1 = {anb2mc2k} and L2
= {a2nb4mck}

27. The nor of two languages is defined as follows:


nor(L1,L2) = {w: w ∉L1 and w ∉L2 }
Show that nor is closed for regular lanaguages

28. Let L1 = L(a*baa*) and L2 = (aba*). Find L1/L2

29. Suppose L1∪L2 is regular and L1 is finite. Can we conclude that L2 is


regular?

30. Prove that the following languages are not regular


a. L = {anbm, n ≤ m}
b. L = {w: na(w) = nb(w)}

31. Find context-free grammars for the following languages


a. L = {anbm: n ≤ m +3}
b. L = {anbm: n ≠ m -1}
c. L = {anbm: n ≠ 2m}
d. L = {anbm: 2n ≤ m ≤ 3m}
e. L = {anbmck: n = m or m ≤ k}
f. L = {anbmck: k = |n-m|}
g. L = {anwwRbn: w ∈{a,b}*}
h. L = {anbn}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

34. Find an s-grammar for L(aaa*b+b)

35. Show that every s-grammar is unambigous

36. Show that the following grammars is ambigous

S  AB | aaB
A  a | aA
Bb

S  aSbS|bSaS|λ

37. Is it possible for a regular grammar to be ambigous?

38. Find an s-grammar for L(aaa*b+b)

39. Show that every s-grammar is unambigous

40. Consider the following grammar:


S  aSb | SS | λ

Eliminate left-recursive for the grammar.


Show a derivation for the string w = aabbab using both orignal and rewriten
grammars
41. Eliminate useless and unit productions for the following grammar:
S  a | aA | B | C
A  aB | λ
B  Aa
C  cCD
D  ddd

42. Eliminate all λ-production from


S  AaB | aaB
Aλ
B  bbA | λ
43. Transform the following grammars to Chomsky normal form:
S  aSb | ab
S  aSaA | A
A  abA |b

S  abAB
A  bAB | λ
B  BAa | A | λ

44. Convert the following grammars into Greibach normal form:


S  aSb | ab

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 | λ

46. Convert the following grammars into Greibach normal form:


S  aSb | ab
S  ab | aS | SS

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,λ)}

48. Construct npda’s that accept the following languages on ∑ = {a,b,c}


a) L = {anb2n}
b) L = {wcwR}
c) L = {a3bncn}
d) L = {anbm, n ≤ m ≤ 3n}
e) L = {w: na(w) = nb(w)}
f) L = {w: na(w) = 2nb(w)}

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,λ)}

50. Construct an npda corresponding to the following grammar


S  aABB | aAA
A  aBB | a
B  bBB | A

51. Show that L = {anb2n} is a deterministic context-free language

52. Let L = {ab,aa,baa}. Which of the following strings are in L*:


abaabaaabaa, aaaabaaaa, baaaaabaaaab, baaaaabaa?
abaabaaabaa
aaaabaaaa
baaaaabaa

53. For which language it is true that L = L*?


a. L = {anbn+1: n≥0}
b. L = {w: na(w)=nb(w)}
a. No (give a counter-example)
b. Yes (Proof is left for students)
54. Which of the strings 0001, 01001, 0000110 are accepted by the
following nfa:

0001
01001

55. Give dfa’s for the sets consisting of


a. all strings with exactly one a.
b. all strings with no more than three a’s

56. Give a nfa for the language L = {ab5wb4: w ∈ {a,b}*}

57. Find dfa’s for the following languages on ∑ = {a,b}


a. L = {w: |w| mod 3 = 0}
b. L = {w: |w| mod 5 ≠ 0}
c. L = {w: na(w) mod 3 > 1}
58. Consider the set of strings on {0,1} in which every 00 is followed
immediately by 1. For example 101, 0010, 0010011001 are in the language,
but 0001 and 00100 are not. Construct an accepting dfa

59. Show that the language L = {an: n ≥ 0, n ≠ 4} is regular.

60. Find δ*(q0,a) and δ*(q1,λ) for the following nfa


δ*(q0,a) = {q0,q1,q2}
δ*(q1,λ) = {q0,q1,q2}

61. For the following nfa, find δ*(q0,1010) and δ*(q1,00)

δ*(q0,1010) = {q0,q2}
δ*(q1,00) = {}

62. Find an nfa with three states that accepts the language {ab,abc}*

63. Convert the following nfa into dfa


a.

b.
Solutions:
a.

b.

Optimize the DFA if you want (read Section 1.3 for DFA optimization):

Thus, the language is, surprisingly, (0+1)*.

64. Find all strings in L((a+b)*b(a+ab)*) of length less than four.


|w| = 0: λ
|w| = 1: b
|w| = 2: ab,bb,ba
|w| = 3: aab,abb,bab,bbb,aba,bba

65. Find a regular expression for the set {anbm: (n+m) is even}
(aa)*(bb)* + (aa)*a(bb)*b

66. Give a regular expression for the language on ∑={a,b,c} containing no


run of a’s of length greater than two
Method 1:

A string of the given language can be considered as a sequence of patterns


each of which is either ax, x or aax (when a run of 2 a’s appears, it must be
followed immediately by b to make sure that the run stops with no more a)
where x is either b or c . The sequence can stop exceptionally by either aa or
a.
The corresponding regular expression:

(a(b+c)+(b+c)+aa(b+c))(aa+a+λ)

Method 2:

One can easily construct a DFA for the language as follows:

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 + λ

68. Give a dfa that accepts L((a+b)*b(a+ab)*)


69. Find regular expressions for the language accepted by the following
automaton:

1*01*01*0

70. Construct a dfa that accepts that language generated by the following
grammar
S  abA
A  baB
B  aA | bb

71. Construct left-linear grammar for the language in Problem 2


S  Bbb
B  Aba
A  Ba | ab

72. Construct right-linear and left-linear grammars for the language L =


{anbm,n≥2,m≥3}
S  aaA
A  aA | bbbB
B  bB | λ
S  Abbb
A  Ab | Baa
B  Ba | λ

73. Find intersection of the two following languages: L1 = {anb2mc2k} and L2


= {a2nb4mck}
{a2nb4mc2k}
74. The nor of two languages is defined as follows:
nor(L1,L2) = {w: w ∉L1 and w ∉L2 }
Show that nor is closed for regular languages

nor(L1,L2) = L1 ∪ L2

75. Let L1 = L(a*baa*) and L2 = (aba*). Find L1/L2


L(a*)

76. Suppose L1∪L2 is regular and L1 is finite. Can we conclude that L2 is


regular?
YES.
Proof:
L2 = (L2 – L1) ∪ (L2 ∩ L1) (1).
L2 – L1 = (L2 ∪ L1) ∩ L1 . Thus L2 – L1 is regular (2).
Since L1 is finite, so is L2 ∩ L1. Hence L2 ∩ L1 is also regular (3).
From (1),(2) and (3), L2 is regular.
Note: If L1 is regular but not finite, we could not conclude anything about L2
since the fact (3) above can not be claimed to complete the proof. One can
easily find a counter-example showing that whereas L1∪L2 and L1 are
regular, L2 can still be not regular.

77. Prove that the following languages are not regular


a. L = {anbm, n ≤ m}
b. L = {w: na(w) ≠ nb(w)}

Read Linz’s book.

78. Find context-free grammars for the following languages

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
EE+T|T
T  T.F | F
F  (E) | E*| a | b
The tree is left for students to construct

81. Show that the following grammars is ambigous


S  AB | aaB
A  a | aA
Bb

S => aaB => aab


S => AB => aAB => aaB => aab

S  aSbS|bSaS|λ

S => aSbS => abSaSbS =>* abab


S => aSbS => aSbaSbS =>* abab

Draw the corresponding trees for the above derivations to observe the ambiguities.

82. Is it possible for a regular grammar to be ambigous?


S  aAB | aaB
Aa
Bb
83. Find an s-grammar for L(aaa*b+b)
S aS1 | b
S1  aA
A  aA | b

84. Show that every s-grammar is unambigous


Consider an s-grammar Gs. If Gs is ambigous, there is at least a string s = a1a2…an ∈
L(Gs) of which two distinct derivation tree exits, namely T1 and T2. Let D1 and D2 be the
two leftmost derivations of T1 and T2 respectively. Since T1 and T2 are distinct, D1 and D2
must be different. However, since Gs is an s-grammar, w should have only one unique
leftmost derivation as S=> a1x1 => a1a2x2 =>…=> a1a2…an, hence the conflict.

85. Consider the following grammar:


S  aSb | SS | λ

Eliminate left-recursive for the grammar.

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

Eliminate unit production


S  a | aA | Aa | cCD
A  aB | λ
B  Aa
C  cCD
D  ddd

Eliminate useless production


S  a | aA | Aa
A  aB | λ
B  Aa
87. Eliminate all λ-production from
S  AaB | aaB
Aλ
B  bbA | λ

Directly replace A by λ
S  aB | aaB
B  bb | λ

Remove the remained λ-production


S  aB | aaB | a | aa
B  bb

88. Transform the following grammars to Chomsky normal form:

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
Aa
B b

b)
Remove unit production
S  aSaA | abA | b
A  abA | b

Convert to Chomsky form:


S  A2S1 | A2S2 | b
A2  a
S1  SS3
S3 A2A2
S2  BA2
A  A2S4 | b
S4  BA2

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

Convert to Chomsky form:


S  A’S1 | A’S3 | A’S4 | A’B’
A’ a
B’  b
S1  BS2
S2  AB
S3  B’B
S4  B’A
A  B’S2 | B’A | B’B | b
B  BS5 | BA’ | AA’ | B’S2 | B’A | B’B | b | a
S5  AA’

89. Convert the following grammars into Greibach normal form:


a) S  aSb | ab
b) S  ab | aS | SS

Solutions:
a)
- Conversion to Chomsky form:
S AS1 | AB
S1 SB
A a
Bb

- 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
Aa
Bb

- 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}

91. Construct npda’s that accept the following languages on ∑ = {a,b,c}

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)}

e) L = {w: na(w) = nb(w)}


δ(q0,λ,z) = {(qf,z)}
δ(q0,a,z) = {(q0,az)}
δ(q0,a,a) = {(q0,aa)}
δ(q0,b,a) = {(q0,λ)}
δ(q0,b,z) = {(q0,bz)}
δ(q0,b,b) = {(q0,bb)}
δ(q0,a,b) = {(q0,λ)}

f) L = {w: na(w) = 2nb(w)}


δ(q0,λ,z) = {(qf,z)}
δ(q0,a,z) = {(q0,az)}
δ(q0,a,a) = {(q0,aa)}
δ(q0,b,a) = {(q1,λ)}
δ(q1,λ,a) = {(q0,λ)}
δ(q1,λ,z) = {(q0,b)}
δ(q0,b,z) = {(q0,bbz)}
δ(q0,b,b) = {(q0,bbb)}
δ(q0,a,b) = {(q0,λ)}

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,λ)}

The above pda corresponds to the following grammar

S  aSA | a
A  bB
Bb
or
S  aSA | a
A  bb
or
S  aSbb | a

93. Construct an npda corresponding to the following grammar


S  aABB | aAA
A  aBB | a
B  bBB | A
Remove unit production
S  aABB | aAA
A  aBB | a
B  bBB | aBB | a
δ(q0,λ,z) = {(q1,Sz)}
δ(q1,a,S) = {(q1,ABB),(q1,AA)}
δ(q1,A,a) = {(q1,BB),(q1,λ)}
δ(q1,B,b) = {(q1,BB)}
δ(q1,B,a) = {(q1,BB),(q1,λ)}
δ(q1,λ,z) = {(qf,λ)}

94. Show that L = {anb2n} is a deterministic context-free language


δ(q0,a,z) = {(q1,aaz)}
δ(q1,a,a) = {(q1,aaa)}
δ(q1,b,a) = {(q2,λ)}
δ(q2,b,a) = {(q2,λ)} δ(q2,λ,z) = {(q0,λ)}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy