automata assigment

Upload: abhay-singh

Post on 09-Jan-2016

249 views

Category:

Documents


0 download

DESCRIPTION

automata assigment

TRANSCRIPT

  • 09

    Sheet 1-8 Dr. Mostafa Aref Format By : Mostafa Sayed

  • Format By : Mostafa Sayed (The Best) Page 2

    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 BaB | B

    B bB |

    ii) All strings with at least three as

    S BaBaBaB

    B 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 3

    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 aa,

    S aaaa,

    S aaaaaaaa.

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

  • Format By : Mostafa Sayed (The Best) Page 4

    Finite Automata Assignment

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

    b a,b

    b

    q0 a q1 a q2

    b. All strings with at least three as

    b b b a, b

    q0 a q1 a q2 a q3

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

    a b a,b

    a

    q0 q1 b q2 a 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 1 q1 1 q2 1 q3

    1

    1

    q4 q5

    1

    1

    q0 1 q1 1 q2 1 q3 1 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 5

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

    b b

    q0 a q1 a,b q2

    b a,b

    {q0} {q1} a {q2}

    b a

    {q1, q2}

  • Format By : Mostafa Sayed (The Best) Page 6

    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 b a,b

    a

    q0 q1 b q2 a q3

    b

    a*(ba)*bbb*

    b. q0 1 q1 1 q2 1 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

    b a a,b

    q4

    a,b

  • Format By : Mostafa Sayed (The Best) Page 7

    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. (Theorem 4.1)

    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

    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 8

    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 9

    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 10

    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 11

    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 q1 ,z/z 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 b,0/00 c,0/ a,0/00

    q0 q1 q2 qf

    b,0/00 c,0/ ,z/z

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

    q0 c,1/ c,0/ q1 qf

    ,z/z

    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 = {a3n

    bn : n >= 0 }

    a,z/1z a,3/13 b,3/ a,1/2

    a,2/3 q0 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

    a,S/B a,B/BB

    b,S/BB b,B/B

    b,S/ a,B/

    q0 ,z/Sz q1 ,z/ qf

  • Format By : Mostafa Sayed (The Best) Page 12

    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 13

    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 y/y,R x/x,R

    c,0/ x/x,R x/x,R x/x,R

    q0 y/y,R q1 y/y,R q2 y/y,R q3

    z/z,R z/z,R z/z,R

    /,L

    qf

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

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

    c/c,R a/a,R y/b,R qxy

    qx

    x/a,R z/c,R

    c/c,R a/a,R x/a,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 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

    q1 qy qyz x/a,R qxyz

    z/c,R y/b,R

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

    qxz

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

    z/z,R x/x,R

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

  • Format By : Mostafa Sayed (The Best) Page 14

    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 ]/],L 0/0,L qaa

    a/0,R b/b,R a/0,L

    0/0,R c/c,R a/a,R

    q0 b/0,R qb ]/],L 0/0,L qbb b/0,L q2

    ]/],L c/c,R a/a,R c/0,L

    c/0,R b/b,R

    ]/],L 0/0,L

    qc qc qbb

  • Format By : Mostafa Sayed (The Best) Page 15

    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 X3 X1 X4

    aa b abb ba

    a aba bbb a

    Y2 Y3 Y1 Y4