Sei sulla pagina 1di 15

Sheet 1-8

Dr. Mostafa Aref


Format By : Mostafa Sayed

09

Introduction Assignment
1. For = {a, b}
a) Write 10 strings of the following languages
i)
All strings with no more than one a
b, bb, bbb, ab, abb, bab, bbba, babbb, bbabb, bbabbb
ii)

All strings with at least three as

aaa, ababab, abaa, ababababa, bababab, aaab, baaa, babababbbb, aaabbb, bbbaaa
b)

Write the grammar for the above two languages


i)
All strings with no more than one a
S
B
ii)
S
B

BaB | B
bB |
All strings with at least three as
BaBaBaB
bB | aB |

2. Give a simple description of the language generated by the grammar with production
S aA
A bS
S
L = {(ab)n : n>= 0 }
3. Find the grammar that generates the language
L = {wwR : w {a, b}+}.
S
aBa | bBb
B
bBb | aBa |

Format By : Mostafa Sayed (The Best)

Page 2

4. Consider the grammar G = ({A, B, C, D, E, S}, {a}, S, P), with productions


S
ABaC
Ba
aaB
BC
DC|E
aD
Da
AD
AB
aE
Ea
AE

Drive three different sentences in L(G). From these, make a conjecture about L(G).
S
S
S

aa,
aaaa,
aaaaaaaa.

L(G) = {an : n = 2i , i>=1}

Format By : Mostafa Sayed (The Best)

Page 3

Finite Automata Assignment


1. For = {a, b}, construct dfa that accept the sets of
a. All strings with no more than one a

a,b
b
q0

q1

q2

b. All strings with at least three as


b
q0

b
a

b
q1

a, b

q2

q3

2. Give a simple description of the language accepted by the following dfa


a

a,b

a
q0

q1

q2

q3

b
All strings of as and bs except that contains substring bba
3. Find a dfa that accept the language defined by the following nfa
q0

q1

q2

q3

1
1
q4

q5
1
1

q0

q1

q2

q3

q4

q5
1

4. Can you construct a dfa or nfa that accepts the following language
L = {wwR : w {a, b}+}.
No: Explain why? Because it is not a regular language, it a context-free language.

Format By : Mostafa Sayed (The Best)

Page 4

5. For = {a, b}, Convert the following nfa into an equivalent dfa.
b
q0

b
a

q1

{q1}

a,b

q2

a,b

{q0}

{q2}

{q1, q2}

Format By : Mostafa Sayed (The Best)

Page 5

Regular Languages Assignment

1. Give a regular expression for


a. L = {anbm, n >=4, m =< 3}
aaaaa*(, b, bb, bbb)
b. L = {abnw, n >=3, w {a, b}+}
abbbb*(a + b)(a + b)*
2. Find regular expression for the languages accepted by the following automata
a.
a

a,b

a
q0

q1

q2

q3

b
a*(ba)*bbb*
b.
q0

q1

q2

q3

1
1
q4

q5
1

111 + (11)*
3. Construct a dfa that accept the language generated by the grammar
S abA
A baB
B aA | bb
a
q0

ab

q1

ba

q2

bb

q3

a,b

q4
a,b

Format By : Mostafa Sayed (The Best)

Page 6

4. Find a regular grammar for the language L = {anbm: n + m is even}


S aaA | aA1 |
A aaA | bbB |
B bbB |
A1 aaA | bB
5. Show that there exits an algorithm for determining if L1 L2 for any regular languages L1
and L2
Since L1 and L2 are regular languages, L1 U L2 is also regular language.
If
L1 U L2 = L2
then
L1 L2
We determine the above equality using Theorem 4.7.
Then, that there exits an algorithm for determining if L1 L2

(Theorem 4.1)

6. Consider the following languages. For each, state whether or not it is regular. Then prove
your statement.
a. L = {anbm, n =< m }
This is not a regular language. To show that we need to use pumping lemma
1- The opponent: Given a positive integer m
2- We: choose w = aaaa a bbbb b
m
m
3- The opponent: has to decompose w into xyz, |xy| < m
a. xy contains all as, y has at least one a,
b. xy contains all bs, y has at least one b,
c. xy contains some as and some bs, y has as and bs, yi, i > 2, w does not
belong to the language.
d. xy contains some as and some bs, y has at least one b, yi, i =0, we have as
more than bs
4- We: show
a. yi, i > 2, we have as more than bs
b. yi, i =0, we have as more than bs
c. yi, i > 2, w does not belong to the language.
d. yi, i =0, we have as more than bs
Therefore, the language is not regular.

Format By : Mostafa Sayed (The Best)

Page 7

b. L = {ww, w {a, b}*}


This is not a regular language. To show that we need to use pumping lemma
1- The opponent: Given a positive integer m
2- We: choose w = = aaaa a bbbb b aaaa a bbbb b
m
m
m
m
3- The opponent: has to decompose w into xyz, |xy| < m
a. xy contains all as, y has at least one a,
b. xy contains all bs, y has at least one b,
c. xy contains some as and some bs, y has as and bs,
d. xy contains some as and some bs, y has at least one b,
e. So on
4- We: show
a. yi, i > 2, we have as in the left more than as in the right
b. yi, i > 2, we have bs in the left more than bs in the right
c. yi, i =0, we have as more than bs
d. yi, i > 2, w does not belong to the language.
e. yi, i =0, we have bs in the left more than bs in the right
f. So on
Therefore, the language is not regular.
c. L = {anbmcl, n + m + l = 10}
This language is regular, since it is finite.
d. L = {anbm: n + m is even}
This language is regular; there is a regular grammar for it (see problem 4).
e. L = {anbmcl, n =< 10, m =< 10, l >= 10}
This language is regular; there is a regular expression for it:
(, a, aa, aaa, aaaa, aaaaaaaaaa)( ,b,bb,bbb, bbbbbbbbbb)ccccccccccc*

Format By : Mostafa Sayed (The Best)

Page 8

Context-Free Language Assignment


7. Find context-free grammar for the following languages (n >= 0, m>=0, k>=0)
a. L = {anbm, n =< m }
S aSb | B | , B bB | b
b. L = {ww, w {a, b, c}*}
This language is not a context-free language. We cannot write context-free grammar for it.
c. L = {anbmck, k = n + m }
S aSc | aBc | , B bBc | .
8. Show a derivation tree for the string aabbbb with the grammar S AB| , A aB, B Sb.
S AB aBB aSbB aABbB aaBBbB aaSbBbB aabBbB aabSbbB
aabbbB aabbbSb aabbbb

9. Show that the following grammar is ambiguous S aSbS | bSaS |


S aSbS abSaSbS abaSbS ababS abab
S aSbS abS abaSbS ababS abab

Format By : Mostafa Sayed (The Best)

Page 9

Simplification of CFG Assignment


10. Eliminate all useless productions from the grammar
S a | aA | B | C , A aB |, BAa, C cCD, D ddd
S a | aA | B
A aB |,
BAa

11. Eliminate -productions from the grammar S AaB | aaB , A , B bbA |


S aB | aaB | a | aa
B bb
12. Transform the grammar S abAB, A bAB|, B Baa| A | into Chomsky normal form.
a. Eliminate -productions
S abAB | abA | abB | ab
A bA | bB | bAB | b
B Baa | A | aa.
b. Eliminate unit productions B A
S abAB | abA | abB | ab
A bA | bB | bAB | b
B Baa | bA | bB | bAB | b | aa.
c. Substitute terminals symbols with variables
Va a
Vb b
S Va Vb AB | Va Vb A | Va Vb B | Va Vb
A Vb A | Vb B | Vb AB | b
B B Va Va | Vb A | Vb B | Vb AB | b | Va Va
d.

Simplify several variables into two variables


Vd Va Vb
Ve Va Va
Vf AB
S Vd Vf | Vd A | Vd B | Va Vb
A Vb A | Vb B | Vb Vf | b
B B Ve | Vb A | Vb B| Vb Vf | b | Va Va
Va a
Vb b

Format By : Mostafa Sayed (The Best)

Page 10

Pushdown Automata
1.

Construct and draw an npda that accept the following languages:


a. L = L(a*b*)
a,z/z
b,z/z
q0

b,z/z

,z/z

q1

qf

We do not need to use the stack for this problem.


We need to have two different state for accepting as and bs
b.

L = {anbmcm+n: n >= 0, m >= 0}


a,z/0z
a,0/00

b,0/00
q0

c,0/

q1

q2

b,0/00

a,z/0z a,0/00
b,0/10
b,1/11
q0
c,1/ c,0/

c,0/

c,1/

,z/z

qf

c,0/
q1

,z/z

qf

In the first solution, we are using a single stack symbol with two states for accepting as then bs.
In the second solution, we are using two different stack symbols for accepting as and bs. But, we are using one
state to accept both of them.
c.

L = {a3nbn : n >= 0 }
a,z/1z
a,1/2
a,2/3

a,3/13
q0

b,3/
b,3/

q1

,z/z

qf

We may use ant three symbols to keep track of every three as. (e.g. x,y,z)
We may use just one symbol and keep adding it. But we need to remove three symbols for each b.

2.

Construct and draw an npda that accept the following languages:


S BB | b, B SB | a
Convert this grammar to Greibach normal form:
S aB | bBB | b
B bB | aBB | a

q0

,z/Sz

Format By : Mostafa Sayed (The Best)

a,S/B
b,S/BB
b,S/
q1

a,B/BB
b,B/B
a,B/
,z/

qf

Page 11

3.

Show that the following language is not a context-free language


L = {w {a, b, c}* : na(w) + nb(w) = 2nc(w)}

We need to use pumping lemma for context-free languages


1- The opponent: Given a positive integer m
2- We: choose w = aaaa a bbbb b cccc c
m
m
2m
3- The opponent: has to decompose w into uvxyz, |vxy| =< m, |vy| >= 1
e. vxy contains all as, v and y has at least one a,
f. vxy contains all bs, v and y has at least one b,
g. vxy contains some as and some bs, v at least one a and y has at least one b
h. so on
4- We: show
i. uvi xyiz, i > 2, we have as more than cs
j. uvi xyiz, i > 2, we have bs more than cs
k. uvi xyiz, i > 2, we have as and bs more than cs
l. so on
Therefore, the language is not a context-free language.

Format By : Mostafa Sayed (The Best)

Page 12

Turing Machine
1.

Construct Turing machines that accepts the following language on {x, y, z}.
a. L = {w: |w| is multiple of 4}
(Draw the transition diagram).

z/z,R

x/x,R
y/y,R
z/z,R

q0

y/y,R

q1

x/x,R

x/x,R
y/y,R
z/z,R

q2

c,0/
x/x,R
y/y,R
z/z,R

q3

/,L

qf
b.

L = {w: nx(w) = ny(w) = nz(w)} (Draw the transition diagram).

/,R
x/x,R b/b,R
c/c,R a/a,R y/b,R
qx
c/c,R a/a,R
b/b,R

y/y,R
x/x,R
qxy

a/a,R c/c,R
b/b,R

x/a,R
z/c,R
c/c,R a/a,R
c/c,R a/a,R b/b,R
c/c,L a/a,L b/b,L
b/b,R
y/y,R
y/y,R
z/z,R
x/x,L y/y,L z/z,L
y/b,R
z/c,R
qy
qyz
x/a,R
qxyz
z/c,R y/b,R

x/a,R

q1

/,R

z/z,R
x/a,R
b/b,R c/c,R a/a,R

x/a,R
qxz

qf

2.

b/b,R c/c,R a/a,R


z/z,R x/x,R

Construct Turing machines that computes f(x, y) = 3x + y (Draw the transition diagram).

Format By : Mostafa Sayed (The Best)

Page 13

3.

Construct Linear bounded automata for L = {wwR: w {a, b, c}+} (Draw the transition diagram).

c/c,L a/a,L

b/b,L

c/c,R a/a,R

b/b,R

qa
a/0,R

b/0,R

]/],L
c/0,R

qaa

b/b,R
c/c,R
a/a,R

0/0,R

q0

]/],L 0/0,L

qb

a/0,L

]/],L 0/0,L

c/c,R a/a,R
b/b,R

qbb

b/0,L

q2

c/0,L

]/],L 0/0,L
qc

Format By : Mostafa Sayed (The Best)

qc

qbb

Page 14

A Hierarchy of Formal Languages


1) Give language examples that demonstrate the subset relations in the following diagram. These examples should be
mutual exclusive (i.e. a CSL should not be CFL nor RL).

RE Language
CSL
CFL
RL

a)

R.E. Language:

b) CSL: anbncn
c)

CFL: anbn

d) RL: a*

2) Find a PC-solution for the following sequences:


X = { abb, aa, b, ba } and Y = {bbb, a, aba, a }.
X2
aa
a
Y2

X3
b
aba
Y3

X1
abb
bbb
Y1

Format By : Mostafa Sayed (The Best)

X4
ba
a
Y4

Page 15

Potrebbero piacerti anche