Sheet -8 Dr. Mostafa Aref Format By : Mostafa Sayed 09
Introduction Assignment. For = {a, } a) Write 0 strings of the following languages i) All strings with no more than one a,,, a, a, a, a, a, a, a ii) All strings with at least three a s aaa, aaa, aaa, aaaaa, aaa, aaa, aaa, aaa, aaa, aaa ) Write the grammar for the aove two languages i) All strings with no more than one a S B ii) S B BaB B B All strings with at least three a s BaBaBaB B ab 2. Give a simple description of the language generated y the grammar with production S aa A S S L = {(a) n : n>= 0 } 3. Find the grammar that generates the language L = {ww R : w {a, } + }. S aba B B B 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 aout L(G). S S S aa, aaaa, aaaaaaaa. L(G) = {a n : n = 2 i, i>=} Format By : Mostafa Sayed (The Best) Page 3
Finite Automata Assignment. For = {a, }, construct dfa that accept the sets of a. All strings with no more than one a q 0 a q a q 2 a,. All strings with at least three a s a, q 0 a q a q 2 a q 3 2. Give a simple description of the language accepted y the following dfa a a, a q 0 q q 2 a q 3 All strings of a s and s except that contains sustring a 3. Find a dfa that accept the language defined y the following nfa q 0 q q 2 q 3 q 4 q 5 q 0 q q 2 q 3 q 4 q 5 4. Can you construct a dfa or nfa that accepts the following language L = {ww R : w {a, } + }. 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, }, Convert the following nfa into an equivalent dfa. q 0 a q a, q 2 {q 0 } {q } a {q 2 } a, a {q, q 2 } Format By : Mostafa Sayed (The Best) Page 5
Regular Languages Assignment. Give a regular expression for a. L = {a n m, n >=4, m =< 3} aaaaa*(,,, ). L = {a n w, n >=3, w {a, } + } a*(a + )(a + )* 2. Find regular expression for the languages accepted y the following automata a. a*(a)** a a, a q 0 q q 2 a q 3. q 0 q q 2 q 3 + ()* q 4 q 5 3. Construct a dfa that accept the language generated y the grammar S aa A ab B aa a q 0 a q a q 2 q 3 a a, q 4 a, Format By : Mostafa Sayed (The Best) Page 6
4. Find a regular grammar for the language L = {a n m : n + m is even} S aaa aa A aaa B B B A aaa B 5. Show that there exits an algorithm for determining if L L 2 for any regular languages L and L 2 Since L and L 2 are regular languages, L U L 2 is also regular language. (Theorem 4.) If L U L 2 = L 2 then L L 2 We determine the aove equality using Theorem 4.7. Then, that there exits an algorithm for determining if L L 2 6. Consider the following languages. For each, state whether or not it is regular. Then prove your statement. a. L = {a n m, n =< m } This is not a regular language. To show that we need to use pumping lemma - The opponent: Given a positive integer m 2- We: choose w = aaaa a m m 3- The opponent: has to decompose w into xyz, xy < m a. xy contains all a s, y has at least one a,. xy contains all s, y has at least one, c. xy contains some a s and some s, y has a s and s, y i, i > 2, w does not elong to the language. d. xy contains some a s and some s, y has at least one, y i, i =0, we have a s more than s 4- We: show a. y i, i > 2, we have a s more than s. y i, i =0, we have a s more than s c. y i, i > 2, w does not elong to the language. d. y i, i =0, we have a s more than s Therefore, the language is not regular. Format By : Mostafa Sayed (The Best) Page 7
. L = {ww, w {a, } * } This is not a regular language. To show that we need to use pumping lemma - The opponent: Given a positive integer m 2- We: choose w = = aaaa a aaaa a m m m m 3- The opponent: has to decompose w into xyz, xy < m a. xy contains all a s, y has at least one a,. xy contains all s, y has at least one, c. xy contains some a s and some s, y has a s and s, d. xy contains some a s and some s, y has at least one, e. So on 4- We: show a. yi, i > 2, we have a s in the left more than a s in the right. yi, i > 2, we have s in the left more than s in the right c. y i, i =0, we have a s more than s d. y i, i > 2, w does not elong to the language. e. y i, i =0, we have s in the left more than s in the right f. So on Therefore, the language is not regular. c. L = {a n m c l, n + m + l = 0} This language is regular, since it is finite. d. L = {a n m : n + m is even} This language is regular; there is a regular grammar for it (see prolem 4). e. L = {a n m c l, n =< 0, m =< 0, l >= 0} This language is regular; there is a regular expression for it: (, a, aa, aaa, aaaa, aaaaaaaaaa)(,,,, )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 = {a n m, n =< m } S as B, B B. L = {ww, w {a,, c} * } This language is not a context-free language. We cannot write context-free grammar for it. c. L = {a n m c k, k = n + m } S asc abc, B Bc. 8. Show a derivation tree for the string aa with the grammar S AB, A ab, B S. S AB abb asb aabb aabbb aasbb aabb aasb aab aas aa 9. Show that the following grammar is amiguous S ass SaS S ass asass aass aas aa S ass as aass aas aa Format By : Mostafa Sayed (The Best) Page 9
Simplification of CFG Assignment 0. Eliminate all useless productions from the grammar S a aa B C, A ab, B Aa, C ccd, D ddd S a aa B A ab, B Aa. Eliminate -productions from the grammar S AaB aab, A, B A S ab aab a aa B 2. Transform the grammar S aab, A AB, B Baa A into Chomsky normal form. a. Eliminate -productions S aab aa ab a A A B AB B Baa A aa.. Eliminate unit productions B A S aab aa ab a A A B AB B Baa A B AB aa. c. Sustitute terminals symols with variales V a a V S V a V AB V a V A V a V B V a V A V A V B V AB B B V a V a V A V B V AB V a V a d. Simplify several variales into two variales V d V a V V e V a V a V f AB S V d V f Vd A V d B V a V A V A V B V V f B B V e V A V B V V f V a V a V a a V Format By : Mostafa Sayed (The Best) Page 0
Pushdown Automata. Construct and draw an npda that accept the following languages: a. L = L(a**) a,z/z,z/z q 0,z/z q,z/z q f We do not need to use the stack for this prolem. We need to have two different state for accepting a s and s. L = {a n m c m+n : n >= 0, m >= 0} a,z/0z,0/00 c,0/ a,0/00 q 0 q q 2 q f,0/00 c,0/,z/z a,z/0z a,0/00 c,/ c,0/,0/0,/ q 0 c,/ c,0/ q q f,z/z In the first solution, we are using a single stack symol with two states for accepting a s then s. In the second solution, we are using two different stack symols for accepting a s and s. But, we are using one state to accept oth of them. c. L = {a 3n n : n >= 0 } a,z/z a,3/3,3/ a,/2 a,2/3 q 0,3/ q,z/z q f We may use ant three symols to keep track of every three a s. (e.g. x,y,z) We may use just one symol and keep adding it. But we need to remove three symols for each. 2. Construct and draw an npda that accept the following languages: S BB, B SB a Convert this grammar to Greiach normal form: S ab BB B B abb a a,s/b,s/bb,s/ a,b/bb,b/b a,b/ q 0,z/Sz q,z/ q f Format By : Mostafa Sayed (The Best) Page
3. Show that the following language is not a context-free language L = {w {a,, c}* : n a (w) + n (w) = 2n c (w)} We need to use pumping lemma for context-free languages - The opponent: Given a positive integer m 2- We: choose w = aaaa a cccc c m m 2m 3- The opponent: has to decompose w into uvxyz, vxy =< m, vy >= e. vxy contains all a s, v and y has at least one a, f. vxy contains all s, v and y has at least one, g. vxy contains some a s and some s, v at least one a and y has at least one h. so on 4- We: show i. uv i xy i z, i > 2, we have a s more than c s j. uv i xy i z, i > 2, we have s more than c s k. uv i xy i z, i > 2, we have a s and s more than c s l. so on Therefore, the language is not a context-free language. Format By : Mostafa Sayed (The Best) Page 2
Turing Machine. 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 q 0 y/y,r q y/y,r q 2 y/y,r q 3 z/z,r z/z,r z/z,r /,L q f. L = {w: n x (w) = n y (w) = n z (w)} (Draw the transition diagram). /,R y/y,r a/a,r c/c,r x/x,r /,R x/x,r /,R c/c,r a/a,r y/,r q x q xy 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 /,R c/c,l a/a,l /,L /,R /,R y/y,r y/y,r z/z,r x/x,l y/y,l z/z,l y/,r z/c,r q q y q yz x/a,r q xyz z/c,r y/,r /,R z/z,r x/a,r x/a,r /,R c/c,r a/a,r q f q xz /,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 3
3. Construct Linear ounded automata for L = {ww R : w {a,, c} + } (Draw the transition diagram). c/c,l a/a,l /,L c/c,r a/a,r /,R q a ]/],L 0/0,L q aa a/0,r /,R a/0,l 0/0,R c/c,r a/a,r q 0 /0,R q ]/],L 0/0,L q /0,L q 2 ]/],L c/c,r a/a,r c/0,l c/0,r /,R ]/],L 0/0,L q c q c q Format By : Mostafa Sayed (The Best) Page 4
A Hierarchy of Formal Languages ) Give language examples that demonstrate the suset relations in the following diagram. These examples should e mutual exclusive (i.e. a CSL should not e CFL nor RL). RE Language CSL CFL RL a) R.E. Language: ) CSL: a n n c n c) CFL: a n n d) RL: a* 2) Find a PC-solution for the following sequences: X = { a, aa,, a } and Y = {, a, aa, a }. X2 X3 X X4 aa a a a aa a Y2 Y3 Y Y4 Format By : Mostafa Sayed (The Best) Page 5