SFWR ENG 2FA3. Solution to the Assignment #4

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

1.[3] Give an unambiguous grammar that generates the set of all regular expressions on Ó = {a,b}. Justify your construction.

Theory of Computation - Module 3

Solution to CS375 Homework Assignment 11 (40 points) Due date: 4/26/2017

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

AC68 FINITE AUTOMATA & FORMULA LANGUAGES DEC 2013

Properties of Context-free Languages. Reading: Chapter 7

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

Theory of Computation

Computability and Complexity

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.

Lecture 12 Simplification of Context-Free Grammars and Normal Forms

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

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

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

This lecture covers Chapter 7 of HMU: Properties of CFLs

Concordia University Department of Computer Science & Software Engineering

INSTITUTE OF AERONAUTICAL ENGINEERING

Context-Free Grammar

AC68 FINITE AUTOMATA & FORMULA LANGUAGES JUNE 2014

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

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

1. Draw a parse tree for the following derivation: S C A C C A b b b b A b b b b B b b b b a A a a b b b b a b a a b b 2. Show on your parse tree u,

CS375: Logic and Theory of Computing

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

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

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

Theory of Computation Turing Machine and Pushdown Automata

Fundamentele Informatica II

cse303 ELEMENTS OF THE THEORY OF COMPUTATION Professor Anita Wasilewska

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

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

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

The Pumping Lemma for Context Free Grammars

Before We Start. The Pumping Lemma. Languages. Context Free Languages. Plan for today. Now our picture looks like. Any questions?

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

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.

Chomsky Normal Form and TURING MACHINES. TUESDAY Feb 4

CMSC 330: Organization of Programming Languages

Theory of Computer Science

CISC4090: Theory of Computation

Automata Theory - Quiz II (Solutions)

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

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

Author: Vivek Kulkarni ( )

St.MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad

CMSC 330: Organization of Programming Languages. Regular Expressions and Finite Automata

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

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

MTH401A Theory of Computation. Lecture 17

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

Context-Free Grammars: Normal Forms

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

Properties of context-free Languages

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

Theory of Computation

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

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

Computability and Complexity

Solution Scoring: SD Reg exp.: a(a

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

CS 373: Theory of Computation. Fall 2010

CSE 468, Fall 2006 Homework solutions 1

In English, there are at least three different types of entities: letters, words, sentences.

Theory of Computation (Classroom Practice Booklet Solutions)

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

CISC 4090 Theory of Computation

Simplification of CFG and Normal Forms. Wen-Guey Tzeng Computer Science Department National Chiao Tung University

Simplification of CFG and Normal Forms. Wen-Guey Tzeng Computer Science Department National Chiao Tung University

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

CSE 105 Homework 1 Due: Monday October 9, Instructions. should be on each page of the submission.

CISC 4090 Theory of Computation

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

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

Notes for Comp 497 (454) Week 10

Computational Models - Lecture 4

CSE 355 Test 2, Fall 2016

Plan for 2 nd half. Just when you thought it was safe. Just when you thought it was safe. Theory Hall of Fame. Chomsky Normal Form

5 Context-Free Languages

Introduction to Theory of Computing

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

An automaton with a finite number of states is called a Finite Automaton (FA) or Finite State Machine (FSM).

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

GEETANJALI INSTITUTE OF TECHNICAL STUDIES, UDAIPUR I

Theory Bridge Exam Example Questions

Chap. 7 Properties of Context-free Languages

Please give details of your answer. A direct answer without explanation is not counted.

PUSHDOWN AUTOMATA (PDA)

I have read and understand all of the instructions below, and I will obey the University Code on Academic Integrity.

Properties of Context-Free Languages

Automata Theory CS F-08 Context-Free Grammars

Context-free Languages and Pushdown Automata

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

Automata: a short introduction

Ogden s Lemma. and Formal Languages. Automata Theory CS 573. The proof is similar but more fussy. than the proof of the PL4CFL.

Lecture 17: Language Recognition

CPSC 313 Introduction to Computability

FABER Formal Languages, Automata. Lecture 2. Mälardalen University

CS311 Computational Structures More about PDAs & Context-Free Languages. Lecture 9. Andrew P. Black Andrew Tolmach

Computational Models - Lecture 4 1

Transcription:

SFWR ENG 2FA3. Solution to the Assignment #4 Total = 131, 100%= 115 The solutions below are often very detailed on purpose. Such level of details is not required from students solutions. Some questions have more than one solution. If you think your solution has been marked wrongly, write a short memo stating where marking in wrong and what you think is right, and resubmit to me during class, office hours, or just slip under the door to my office. 1.[20] (a)[2] In the left automaton the states 7,8 are not accessible, in the right automaton all states are accessible. (b)[10] Left automaton: 1 2 3 4 5 2 x 3 x x 4 x x 5 x x x 6 x x x x 1

Hence the equivalence classes are {1,6},{2,5},{3,4}. Right automaton: 1 2 3 4 5 6 7 2 3 x x 4 x x 5 x x x x 6 x x x x 7 x x x x 8 x x x x x Hence the equivalence classes are {1, 2},{3, 4, 8},{5, 6, 7}. (c)[8] Minimum state left automaton Minimum state right automaton 2.[16] Consider the following context-free grammar G: R XRX S S at b bta T XT X X ε X a b Answer the following questions: 1. [1] What are the variables/nonterminals of G? Answer: N = {R, X, S, T }. 2. [1] What are the terminals of G? Answer: Σ = {a,b}. 3. [1] What is the start variable of G? Answer: This is ambiguous question with two correct answers. One is S as most textbooks say that S denotes the start 2

symbol. The other is R as customarily the start symbol is usually the top right variable. Those who will take 3RA3 next term will hear more on the requirements ambiguity problem. 4. [1] Give three strings in L(G). Answer. If S is the start symbol: ab,ba,aab; if R is the start symbol: ab,ba,aab. 5. [1] Give three strings not in L(G). Answer. If S is the start symbol: a,b,ε; if R is the start symbol: a,b,ε. 6. [1] True or false: T aba. Answer: No. 7. [1] True or false: T aba. Answer: Yes, as T XT X at X axx abx aba. 8. [1] True or false: T T. Answer: Yes, as includes the identity relation. 9. [1] True or false: XXX aba. Answer: Yes, as XXX axx abx aba. 10. [1] True or false: X aba. Answer: No. 11. [1] True or false: T XX. Answer: Yes, as T XT X XX. 12. [1] True or false: T XXX. Answer: Yes, as T XT X XXX. 13. [1] True or false: S ε. Answer: No. 14. [3] Give a description in English of L(G). Answer: If S is the start symbol, R is useless symbol and L(G) is the set of all strings over a,b that have length bigger or equal 2, and the first symbol is always different than the last one. If R is the start symbol, L(G) consists of all strings over a,b that are not palindromes. 3.[10] Give context-free grammars generating the following languages: (a) [5] (b) [5] The set of strings over the alphabet {a,b} with more a s than b s. S TaT T T T at b bta a ε T generates all strings with at least as many a s as b s, and S forces an extra a. {a m b n c p d q m + n = p + q}. S asd A B A aac C B bbd C C bcc ε For all productions, we generate one a or b on left side, and one c or d on the right side, which can guarantee that m + n = p + q. Variable A cover cases that m q, while variable B cover cases that m q. 3

4.[10] Exercise 3 (page 306 of the Kozen textbook). The simplest and most straightforward grammar G is: S ε (S) [S] SS. We have to show that L(G) = PAREN 2. First we show that L(G) PAREN 2. The proof is by induction on the length of derivation. If S x, then x = ε PAREN 2. Suppose that for every x {(,),[,]} such that if S k x, where 1 k n, then x PAREN 2 (strong induction!). Consider the case S n+1 y, where x {(,),[,]}. We have two cases: S n v(s)w y = v()w, or S n v[s]w z = v[]w, where v,w {(,),[,]}, and S k v, S m w where k < n and m < n. Hence, by inductive assumption v,w PAREN 2. If v,w PAREN 2, then by (ii) and (iii) of the definition of PAREN 2, we immediately get v()w PAREN 2 and v[]w PAREN 2. This means L(G) PAREN 2. Now we have to show that PAREN 2 L(G). The proof by the inductive definition of PAREN 2. Clearly ε L(G) so we are done with (i) of the definition of PAREN 2. Let x L(G), i.e S x {(,),[,]}. Consider (x) and [x]. We have S (S) (x), so (x) L(G). Similarly S (S) [x], so [x] L(G). Hence we are done with (ii) of the definition of PAREN 2. Let x,y L(G), i.e S x {(,),[,]} and S y {(,),[,]}. In this case: S SS xs xy, so xy L(G). So we are done with (iii) of the definition of PAREN 2. Hence PAREN 2 L(G), i.e. L(G) = PAREN 2. 5.[5] Eliminate all useless productions from the below grammar. S a aa B C A ab ε B Aa C ccd D d What language does this grammar generate? Step 1. There is only one variable from which we cannot derive a sequence of terminals, namely: C After erasing it we get: S a aa B A ab ε B Aa D d 4

Step 2. There is only one variable that does not appear in any string derived from S, namely: D After erasing it we get: S a aa B A ab ε B Aa The grammar generates the language containing only odd number of a s, i.e. {a 2k+1 k 0}. 6.[5] Eliminate all ε-productions from the below grammar. S ABaC A BC B b ε C D ε D d In the first step we conclude that B, C can eventually generate ε (they are often called nullable). Then, since A BC we conclude that A can also generate ε. It is easy to observe that only A,B,C can generate ε. Hence: S ABaC BaC AaC ABa ac Ba Aa a A BC B C B b C D D d 7.[5] Eliminate all unit-productions from the below grammar. S Aa A a bc B B A bb We have A B and B A, so actually A is the same as B. Therefore we can change: A a bc B B A bb into just 5

A a bc bb Hence the final grammar is: S Aa A a bc bb 8.[10] Convert the below grammar into Chomsky Normal Form. S abab A bab ε B BAa A ε First we have to get rid of all ε-productions. The obtained grammar is: S abab aba abb ab A bab ba bb b B BAa Aa Ba a A Next we have to get rid of all unit-productions. The result is: S abab aba abb ab A bab ba bb b B BAa Aa Ba a bab ba bb b Now, we may produce a Chomsky normal form: S X a X bab X a X ba X a X bb X a X b X a a X b b X bab X a X AB X ba X b A X bb X b B X AB AB A X b X AB X b A X b B b B X BA X a AX a BX a a X b X AB X a A X b B b X BA BA 8a.[10] Convert the below grammar into Graibach Normal Form. S ABb a A aaa b B bab bbb ABB a 6

First substitute A by aaa b in B ABB. The result is B bab bbb a aaabb bbb, i.e. B bab bbb a aaabb. Now we substitute A by aaa b in S ABb. The result is S aaabb baab a Hence now we have: S aaabb baab a A aaa b B bab bbb a aaabb which is almost Graibach. We just need to add C a a, C b b, and replace a and b that are not first by C a and C b, respectively. Final result: S ac a ABC b baac b a A ac a A b B bac b bbb a ac a ABB C a a C b b 9.[10] Construct a pushdown automaton accepting: {a n b m 2n = 3m n 0 m 0} You can choose either acceptance with the empty stack of final state. The below solution has in fact both. Note that the language in question is {ε,a 3 b 2,a 6 b 4,...}, i.e. {a 3k b 2k k 0} Thus, for each symbol a of the input, we push two copies of symbol a into the stack (this is done when in state s). Then, for each symbol b we read from the input, we match it with three a s in the stack (using states q 1,q 2,q 3,q 4 ). The automaton looks as follows: M = (Q,Σ,Γ,δ,s,,F), where Q = {s,q 1,q 2,q 3,q 4,r}, Σ = {a,b}, Γ = {a,b, }, F = {r}, and the relation δ is the following: 7

{(s, ε, ),(r, ε), just to accept empty string (s,a, ),(s,aa ), first push of aa (s,a,a),(s,aa), remaining pushes of aa, note that for example aa on the input tape results in aaaa on the stack! (s,b,a),(q 1,ε), first encounter of b (q 1,ε,a),(q 2,ε), the remaining 3 steps deal with matching 3 a s with 2 b s (q 2,ε,a),(q 3,ε), (q 3,b,a),(q 4,ε), (q 4,b,a),(q 1,ε), (q 3,ε, ),(r,ε)} just to go to the final state 10.[10] Construct a pushdown automaton accepting: {a i b i c k i, j,k 0 i + k = j} You can choose either acceptance with the empty stack of final state. The below solution has in fact both. The basic idea for this problem is to use different states to enforce the order of the occurrences of the letters a,b and c, and use the stack to meet the requirement of i + k = j.the automaton looks as follows: M = (Q,Σ,Γ,δ,s,,F), where Q = {s,q, p,r}, Σ = {a,b,c}, Γ = {a,b,c, }, F = {r}, and the relation δ is the following: {(s,a, ),(s,a ), (s,b, ),(s,b ), (s,a,a),(s,aa), (s,b,a),(q,ε), (q,b,a),(q,ε), (q,b, ),(q,b ), (q,b,b),(p,bb), (p,c,b),(p,ε), (p,ε, ),(r,ε)} 11.[10] Show that the following language is not context-free: {a i b i2 i > 0} Assume L = {a n b i2 i > 0} is context free, then pumping lemma hold. Let p be the number in pumping lemma. We take z = a p2 b p. Clearly, z p and z L. Therefore, z = uvwxy and vx 1, vwx p, and for all i 0, uv i wx i y L. Let us consider two cases, Case 1. vx = a k, i.e. it contains no b. Then, z = uv 2 wx 2 y = a q+k b p, and k1. Therefore, z / L, a contradiction. 8

Case 2. vx = a j b k, and k1, i.e. it contains at least one b. Then, consider z = uv 0 wx 0 y. We have: # a (z ) = q j = p 2 j, and 1 j p 1 (# a (z ) is the number of a in z), and # b (z ) = p k, and 1 k p. So, (# b (z)) 2 = (p k) 2 = p 2 2kp + k 2. Clearly kp j > k 2 kp, because kp j p (p 1) = 1, and k 2 kp = k(k p) 0. Now, it is easy to verify that, p 2 j > p 2 2kp + k 2. Therefore, z / L, contradiction. 12.[10] Describe a Turing Machine that accept {a 2n n 0}. Solution. The idea is the following: for each input string w 1. Sweep left to right across the tape, crossing off every other a. 2. If in stage 1 the tape contained a single a, accept. 3. If in stage 1 the tape contained more than a single a and the number of a was odd, reject. 4. Return the head to the left-hand end of the tape. 5. Go to 1. The formal description could be the following: M = (Q,Σ,Γ,,,δ,q 1,q accept,q re ject ), where Q = (q 1,q 2,q 3,q 4,q 5,t,r) Σ = {a} Γ = {a,x,, } q 1 is the start state. and the δ is given by the below table δ a x q 1 (q 1,,R) (q 1,,R) (q re ject,,r) (q re ject,x,r) q 2 (q re ject,,r) (q 3,x,R) (q accept,,r) (q 2,x,R) q 3 (q re ject,,r) (q 4,a,R) (q 5,,L) (q 3,x,R) q 4 (q re ject,,r) (q 3,x,R) (q re ject,,r) (q 4,x,R) q 5 (q re ject,,r) (q 5,a,L) (q 2,,L) (q 5,x,L) 9