Concordia University Department of Computer Science & Software Engineering

Similar documents
Section 1 (closed-book) Total points 30

60-354, Theory of Computation Fall Asish Mukhopadhyay School of Computer Science University of Windsor

TAFL 1 (ECS-403) Unit- III. 3.1 Definition of CFG (Context Free Grammar) and problems. 3.2 Derivation. 3.3 Ambiguity in Grammar

Theory of Computation - Module 3

Homework 4. Chapter 7. CS A Term 2009: Foundations of Computer Science. By Li Feng, Shweta Srivastava, and Carolina Ruiz

download instant at Assume that (w R ) R = w for all strings w Σ of length n or less.

Homework 4 Solutions. 2. Find context-free grammars for the language L = {a n b m c k : k n + m}. (with n 0,

Automata Theory Final Exam Solution 08:10-10:00 am Friday, June 13, 2008

Automata Theory CS F-08 Context-Free Grammars

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

CSE 468, Fall 2006 Homework solutions 1

Properties of Context-Free Languages

Lecture 11 Context-Free Languages

HW 3 Solutions. Tommy November 27, 2012

Properties of Context-free Languages. Reading: Chapter 7

AC68 FINITE AUTOMATA & FORMULA LANGUAGES JUNE 2014

VTU QUESTION BANK. Unit 1. Introduction to Finite Automata. 1. Obtain DFAs to accept strings of a s and b s having exactly one a.

Chapter 5: Context-Free Languages

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

EXAMPLE CFG. L = {a 2n : n 1 } L = {a 2n : n 0 } S asa aa. L = {a n b : n 0 } L = {a n b : n 1 } S asb ab S 1S00 S 1S00 100

CS375: Logic and Theory of Computing

AC68 FINITE AUTOMATA & FORMULA LANGUAGES DEC 2013

Lecture 12 Simplification of Context-Free Grammars and Normal Forms

Introduction to Theory of Computing

Fundamentele Informatica II

Recitation 4: Converting Grammars to Chomsky Normal Form, Simulation of Context Free Languages with Push-Down Automata, Semirings

Context-Free Grammars and Languages

Notes for Comp 497 (Comp 454) Week 10 4/5/05

ÖVNINGSUPPGIFTER I SAMMANHANGSFRIA SPRÅK. 17 april Classrum Edition

Definition: A grammar G = (V, T, P,S) is a context free grammar (cfg) if all productions in P have the form A x where

Parsing. Context-Free Grammars (CFG) Laura Kallmeyer. Winter 2017/18. Heinrich-Heine-Universität Düsseldorf 1 / 26

CS A Term 2009: Foundations of Computer Science. Homework 2. By Li Feng, Shweta Srivastava, and Carolina Ruiz.

Introduction to Formal Languages, Automata and Computability p.1/42

The Pumping Lemma for Context Free Grammars

Context Free Languages. Automata Theory and Formal Grammars: Lecture 6. Languages That Are Not Regular. Non-Regular Languages

Miscellaneous. Closure Properties Decision Properties

CSE 355 Test 2, Fall 2016

Computational Models - Lecture 4 1

SFWR ENG 2FA3. Solution to the Assignment #4

CS500 Homework #2 Solutions

This lecture covers Chapter 7 of HMU: Properties of CFLs

Computational Models - Lecture 4 1

CPSC 313 Introduction to Computability

Solutions to Problem Set 3

CS5371 Theory of Computation. Lecture 7: Automata Theory V (CFG, CFL, CNF)

Theory of Computation

Harvard CS 121 and CSCI E-207 Lecture 10: CFLs: PDAs, Closure Properties, and Non-CFLs

Context-Free Grammar

Theory of Computer Science

Finite Automata Theory and Formal Languages TMV026/TMV027/DIT321 Responsible: Ana Bove

INSTITUTE OF AERONAUTICAL ENGINEERING

1. (a) Explain the procedure to convert Context Free Grammar to Push Down Automata.

Notes for Comp 497 (454) Week 10

Problem Session 5 (CFGs) Talk about the building blocks of CFGs: S 0S 1S ε - everything. S 0S0 1S1 A - waw R. S 0S0 0S1 1S0 1S1 A - xay, where x = y.

CFG Simplification. (simplify) 1. Eliminate useless symbols 2. Eliminate -productions 3. Eliminate unit productions

Pushdown Automata. Reading: Chapter 6

CISC 4090 Theory of Computation

CS 373: Theory of Computation. Fall 2010

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

St.MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad

Chap. 7 Properties of Context-free Languages

Theory Of Computation UNIT-II

Context Free Languages and Grammars

Properties of context-free Languages

NPDA, CFG equivalence

October 6, Equivalence of Pushdown Automata with Context-Free Gramm

Solution. S ABc Ab c Bc Ac b A ABa Ba Aa a B Bbc bc.

(b) If G=({S}, {a}, {S SS}, S) find the language generated by G. [8+8] 2. Convert the following grammar to Greibach Normal Form G = ({A1, A2, A3},

Note: In any grammar here, the meaning and usage of P (productions) is equivalent to R (rules).

COSE212: Programming Languages. Lecture 1 Inductive Definitions (1)

Context Free Languages (CFL) Language Recognizer A device that accepts valid strings. The FA are formalized types of language recognizer.

HW6 Solutions. Micha l Dereziński. March 20, 2015

Non-context-Free Languages. CS215, Lecture 5 c

Context-Free Grammars: Normal Forms

Einführung in die Computerlinguistik

Problem 2.6(d) [4 pts] Problem 2.12 [3pts] Original CFG:

Author: Vivek Kulkarni ( )

Einführung in die Computerlinguistik

Theory of Computation (Classroom Practice Booklet Solutions)

Finite Automata and Formal Languages TMV026/DIT321 LP Useful, Useless, Generating and Reachable Symbols

FLAC Context-Free Grammars

How to write proofs - II

Properties of Context-Free Languages. Closure Properties Decision Properties

2.1 Solution. E T F a. E E + T T + T F + T a + T a + F a + a

C1.1 Introduction. Theory of Computer Science. Theory of Computer Science. C1.1 Introduction. C1.2 Alphabets and Formal Languages. C1.

Pushdown automata. Twan van Laarhoven. Institute for Computing and Information Sciences Intelligent Systems Radboud University Nijmegen

Context-Free Grammars. 2IT70 Finite Automata and Process Theory

Ogden s Lemma for CFLs

CPS 220 Theory of Computation

5.A Examples of Context-Free Grammars 3.1 and 3.2 of Du & Ko s book(pp )

Linear conjunctive languages are closed under complement

Theory of Computation (IV) Yijia Chen Fudan University

(pp ) PDAs and CFGs (Sec. 2.2)

CISC 4090 Theory of Computation

Context-free Grammars and Languages

Grammars and Context Free Languages

Chomsky Normal Form and TURING MACHINES. TUESDAY Feb 4

cse303 ELEMENTS OF THE THEORY OF COMPUTATION Professor Anita Wasilewska

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

Context-Free Grammars (and Languages) Lecture 7

Transcription:

Concordia University Department of Computer Science & Software Engineering COMP 335/4 Theoretical Computer Science Winter 2015 Assignment 3 1. In each case, what language is generated by CFG s below. Justify your claim (prove it!) (a) G with productions S asa bsb aab baa, A aaa bab a b ɛ Let s try a few derivations: S asa basab wsw R From here we can do wsw R waabw R, or wsw R wbaaw R. Clearly A u, where u = u R. So all in all we get L(G) = {wc 1 uc 2 w R : w, u {a, b}, u = u R, c 1, c 2 {a, b}, c 1 c 2 }. (b) G with productions S as bs a It is easily seen that L(G) = {wa : w {a, b} }. (c) S SS bs a It is not so easily seen that for this grammar G, we also have L(G) = L, where L = {wa : w {a, b} }. So let s prove it. Clearly L(G) L (All strings generated by G have to end in an a.) To see that L L(G) we show by an induction on w, that for any w {a, b}, we have wa L(G). Basis: w = 0. This means that w = ɛ. We have indeed S a. Induction hypothesis: For any w {a, b} where w n, we have wa L(G). Induction Step: Case 1: w = av, v n. By the IH we have S va. Then we can do the derivation S SS as ava. Case 2: w = bv, v n. By the IH we have S va. Now we can do the derivation S bs bva.

(d) G with productions S SaS b, S at bt ɛ, T as bs. Here again it might not be easy to see that L(G) = {a, b}, so we better prove it. First we note that it is obvious that L(G) {a, b} (Any string in L(G) is over {a, b}.) We will show on an induction on w, that if w {a, b}, then w L(G). The trick is that we need two IH s, namely L(G) {a, b}, and T x, where x is any string in {a, b} + Basis: w = 0. We have ɛ. x = 1. We have T as aɛ = a, and T bs bɛ = b, Induction hypothesis: For all w {a, b}, if w n, then w L(G). For all x {a, b} +, if x n, then T x. Induction Step: Case 1: w = av. By IH, we have S v. Then we can do the derivation S SaS ɛas av. Case 2: w = bv. By IH, we have T v. Now we can do the derivation S bt bv.

2. Find a CFG for each of the languages below. (a) L = {a n b m : n m 1} Here n m 1 (n m) (n < m 1) Hence the CFG: S A B λ A aab aa ab B abb Bb bb (b) L = {a n b m c k : n = m or m k} Here (n = m) (m k) (n = m) ((m < k) (m > k)) For (n = m): S A and A aab λ For (m > k): S B and B bbc bb b For (m < k): S C and C bcc Cc c Therefore, S A B C DB DC EA E where D aa a and E ce c (c) L = {w {a, b} : n a (w) n b (w)} L = L a L b, where L a = {w {a, b} : n a (w) > n b (w)}, and L b = {w {a, b} : n a (w) < n b (w)}, and L a can be gerenated by A a aa baa AAb AbA and L b by For L we can then use S A B. B b bb abb BBa BaB. (d) L, where L = {w {a, b} : w = a n b n, n 0} We have L = {w {a, b} : w = a n b m, n m} {wbau : w, u {a.b} }. We then get S A B C A aab aa a B abb Bb b C DbaD D ad bd λ.

3. In each case below, show that the grammar is ambiguous, and find an equivalent unambiguous grammar. (a) S SS ab a The grammar is ambiguous because, the string aaba can be obtained by two different leftmost derivations: S SS SSS ass aabs aaba S SS as ass aabs aaba An unambiguous version is: S Sa Sab a ab (b) S ABA, A aa ɛ, B bb ɛ The grammar is ambiguous because the string a has two leftmost derivations: S ABA aaba aɛba aɛɛa aɛɛɛ = a S ABA ɛba ɛɛa ɛɛa = a An unambiguous version is: S ABA AB BA A B λ A aa a B bb b (c) S asb aasb ɛ The grammar is ambiguous because, the string aaabb can be obtained by two leftmost derivations: S asb aaasbb aaaɛbb = aaabb S aasb aaasbb aaaɛbb = aaabb An unambiguous version is: S A ɛ A aab B ab B aabb aab

4. Design a PDA to accept each of the following languages. You may design your PDA to accept either by final state or empty stack, whichever is more convenient. (a) The set of strings over {0, 1} such that no prefix has more 1 s than 0 s. The PDA is 0, Z 0 0Z 0 0, 0 00 1, 0 ɛ start A 1, Z 0 Z 0 B ɛ, 0 0 ɛ, Z 0 Z 0 C (b) The set of strings with twice as many 0 s as 1 s. The PDA is 0, Z 0 0Z 0 0, 0 00 0, 1 ɛ start A ɛ, Z 0 Z 0 B 1, 1 111 1, Z 0 11Z 0 1, 0 1

(c) The set of strings over {a, b} that are not of the form ww, that is, not equal to any string repeated. The PDA is a, Z 0 XZ 0 a, X XX b, Z 0 XZ 0 b, X XX A a, Z 0 Z 0 a, X X B a, X ɛ a, Z 0 Y Z 0 b, Z 0 Y Z 0 b, X ɛ a, Y Y Y b, Y Y Y b, Z 0 Z 0 b, X X b, Z 0 Z 0 b, Y Y a, X ɛ a, Z 0 Y Z 0 b, Z 0 Y Z 0 D C a, Y ɛ b, Y ɛ a, Y Y Y b, Y Y Y b, X ɛ a, Z 0 Z 0 a, Y Y ɛ, Z 0 Z 0 E ɛ, Z 0 Z 0 F a, Y ɛ b, Y ɛ

5. Construct a PDA corresponding to the context-free grammar S SS {SX [SY ɛ X } Y ] Note that {, [, ], and ] are terminals. start 1 ɛ, Z 0 SZ 0 ɛ, S SS ɛ, S {SX ɛ, S [SY ɛ, S ɛ ɛ, X } ɛ, Y ] ɛ, { { ɛ, } } ɛ, [ ] ɛ, ] ]

6. Consider the PDA P = {{q 0, q 1, q 2 }, {a}, {, Z 0 }, δ, q 0, Z 0, {q 2 }}, where δ(q 0, a, Z 0 ) = {(q 1, Z 0 )}, δ(q 1, a, ) = {(q 0, ɛ)}, and δ(q 0, ɛ, Z 0 ) = {(q 2, ɛ)}. Construct a CFG (using the method in the text) corresponding to P. a, Z 0 Z 0 q 0 start q 1 a, ɛ ɛ, Z 0 ɛ q 2 S [q 0 Z 0 q 0 ] [q 0 Z 0 q 1 ] [q 0 Z 0 q 2 ] [q 0 Z 0 q 0 ] ɛ[q 1 q 0 ][q 0 Z 0 q 0 ] ɛ[q 1 q 1 ][q 1 Z 0 q 0 ] ɛ[q 1 q 2 ][q 2 Z 0 q 0 ] [q 0 Z 0 q 1 ] ɛ[q 1 q 0 ][q 0 Z 0 q 1 ] ɛ[q 1 q 1 ][q 1 Z 0 q 1 ] ɛ[q 1 q 2 ][q 2 Z 0 q 1 ] [q 0 Z 0 q 2 ] ɛ[q 1 q 0 ][q 0 Z 0 q 2 ] ɛ[q 1 q 1 ][q 1 Z 0 q 2 ] ɛ[q 1 q 2 ][q 2 Z 0 q 2 ] [q 0 Z 0 q 2 ] ɛ [q 1 q 0 ] a By deleting useless symbols and productions, and by renaming the variables, we get: S T T AT T ɛ A a

7. Use the Pumping Lemma for CFL s to show that none of the following languages are context-free. ***Notice: Answers are simplified *** (a) L 1 = {ww : w {a, b} } z = a n b n a n b n is in the language. Now we show by pumping lemma for CFG s that the language can not be generated by any CFG. It is easy to see, since vwx n, it does not contain the whole z So we have the following cases vwx = a p ; p n vwx = a p b q ; p + q n vwx = b q ; q n vwx = b q a p ; q + p n and there is at least an a n and b n which would not be affected by pumping lemma, while as a result the generated string by pumping lemma will not be in the language. (b) L 2 = {a n b k : 0 n k 2 } In this case z = a n2 b n is in the language and we have the following cases for pumping lemma: vwx = a p ; p n vwx = a p b q ; p + q n vwx = b q ; q n By pumping the first and last cases easily resulting in a string which is not in the language. But for the second case, we have uv n+1 wxn + 1y = a n2 +np b n+nq implies that n 2 + np = (n(1 + q)) 2 = n 2 (1 + 2q + q 2 ) = n 2 + 2n 2 q + n 2 q 2 that is, np = 2n 2 q + n 2 q 2, thus, p = 2nq + nq 2. Now let check what happen for uv 2 wx2y = a n2 +p b n+q. That is n 2 + p = n 2 + 2nq + nq 2 = n 2 + 2nq + q 2 = (n + q) 2, which requires n = 1 however, we can not restrict n to be equal 1. (c) L 3 = {a n b m c k : 0 n < m, n k m} z = a n b n c n+1 is in the language. So we have the following cases for vwx: vwx = a p ; p n vwx = a p b q ; p + q n vwx = b q ; q n vwx = b q c r ; q + r n vwx = c r ; r n In first three cases uv 2 wx 2 y will not be in the language. In last two cases uv 0 wx 0 y will not be in the language.

8. Convert the following grammar into Chomsky normal form S aa abb A aaa ɛ B bb bbc C C B S aa abb A aaa ɛ B bb bbc C C B Eliminate useless symbols: S aa A aaa ɛ Eliminate the ɛ-production: S aa a A aaa aa Eliminate unit product rules S aa a A aaa aa Break bodies of length more than two S aa a A ax aa X aa Change variables: S UA U A UX UU X UA U a

9. (a) Show that the language L = {a n b n : a, b {a, b}, n is not a multiple of 5} is context-free. Let and L 1 = {a n b n : n 0} L 2 = {w {a, b} : w is not a multiple of 10}. It is clear that L 1 is context-free, and L 2 is a regular language. Furthermore, we have L = L 1 L 2, since the intersection of a context-free language and a regular language is context-free, then L is context-free. (b) Let L = {a n b n : n 0}, and M = {a 2m b 2p : m 0, p 0}. Construct a PDA for L and a DFA 1 for M. Then use the Cartesian construction to obtain a PDA for L M. DFA for M: a start A B a b b C D PDA for L: a, X XX a, Z 0 XZ 0 b b, X ɛ ɛ, X X ɛ, Z 0 Z 0 ɛ, Z 0 Z 0 start 1 2 3 Finally L M: state input stack new state new stack (A, 1) a Z 0 (B, 1) Z 0 (A, 1) a X (B, 1) XX (A, 1) ɛ Z 0 (A, 2) Z 0 (A, 1) ɛ X (A, 2) XX (A, 2) b XX (C, 2) X (A, 2) ɛ Z 0 (A, 3) Z 0 (C, 2) b XX (D, 2) X (D, 2) b XX (C, 2) X (D, 2) ɛ Z 0 (D, 3) Z 0 1 Leave out the trap state