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

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

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 CS F-08 Context-Free Grammars

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

Automata Theory CS F-04 Non-Determinisitic Finite Automata

CS375 Midterm Exam Solution Set (Fall 2017)

Concordia University Department of Computer Science & Software Engineering

CS375: Logic and Theory of Computing

Talen en Compilers. Johan Jeuring , period 2. October 29, Department of Information and Computing Sciences Utrecht University

Context-Free Grammar

Lecture 12 Simplification of Context-Free Grammars and Normal Forms

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

SFWR ENG 2FA3. Solution to the Assignment #4

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

Theory of Computation - Module 3

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

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

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

CS 373: Theory of Computation. Fall 2010

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

INSTITUTE OF AERONAUTICAL ENGINEERING

CMSC 330: Organization of Programming Languages

CPSC 313 Introduction to Computability

St.MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad

AC68 FINITE AUTOMATA & FORMULA LANGUAGES JUNE 2014

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

Theory of Computer Science

CSCI 340: Computational Models. Regular Expressions. Department of Computer Science

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

Author: Vivek Kulkarni ( )

Context-Free Grammars: Normal Forms

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

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.

Solutions to Problem Set 3

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

Chapter 5. Finite Automata

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

Theory of Computation (Classroom Practice Booklet Solutions)

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

CSEP 590 Data Compression Autumn Arithmetic Coding

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

CS 133 : Automata Theory and Computability

Lecture 11 Context-Free Languages

Page 1. Compiler Lecture Note, (Regular Language) 컴파일러입문 제 3 장 정규언어. Database Lab. KHU

Section 1 (closed-book) Total points 30

5/10/16. Grammar. Automata and Languages. Today s Topics. Grammars Definition A grammar G is defined as G = (V, T, P, S) where:

Chapter 4. Regular Expressions. 4.1 Some Definitions

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

AC68 FINITE AUTOMATA & FORMULA LANGUAGES DEC 2013

컴파일러입문 제 3 장 정규언어

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

Nondeterministic Finite Automata and Regular Expressions

Grade 6 Math Circles October 20/21, Formalism and Languages: Beyond Regular Languages

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

Question Bank UNIT I

Computability and Complexity

Chomsky Normal Form and TURING MACHINES. TUESDAY Feb 4

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

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

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

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

CS500 Homework #2 Solutions

Formal Languages, Automata and Models of Computation

Math 280 Modern Algebra Assignment 3 Solutions

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

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

Automata and Languages

CS311 Computational Structures Regular Languages and Regular Expressions. Lecture 4. Andrew P. Black Andrew Tolmach

Context-free Languages and Pushdown Automata

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

Chapter 6. Properties of Regular Languages

Author: Vivek Kulkarni ( )

Theory of Computation Turing Machine and Pushdown Automata

A canonical semi-deterministic transducer

MTH401A Theory of Computation. Lecture 17

Properties of Context-free Languages. Reading: Chapter 7

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

Fundamentele Informatica II

Pushdown Automata. Reading: Chapter 6

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

This lecture covers Chapter 7 of HMU: Properties of CFLs

PART 3. CONTEXT FREE GRAMMARS AND PUSHDOWN AUTOMATA. FEATURES AND INDICES.

{a, b, c} {a, b} {a, c} {b, c} {a}

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

REGular and Context-Free Grammars

Homework 5 - Solution

CS Automata, Computability and Formal Languages

cse303 ELEMENTS OF THE THEORY OF COMPUTATION Professor Anita Wasilewska

Learning Context Free Grammars with the Syntactic Concept Lattice

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

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

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

Languages, regular languages, finite automata

Compiler Design. Spring Lexical Analysis. Sample Exercises and Solutions. Prof. Pedro C. Diniz

Lecture 1: Finite State Automaton

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

A Universal Turing Machine

Theory Of Computation UNIT-II

Transcription:

CS3133 - A Term 2009: Foundations of Computer Science Prof. Carolina Ruiz Homework 2 WPI By Li Feng, Shweta Srivastava, and Carolina Ruiz Chapter 4 Problem 1: (10 Points) Exercise 4.3 Solution 1: S is self recursive, so we need to introduce a new non recursive start symbol S with the associated rule S S. Note that S, A, and B are nullable variables. The regular expression is (ba) a. S S λ S BSA BS SA BA B S A B Bba ba A aa a Problem 2: (10 Points) Exercise 4.9 Solution 2: Note that: X CHAIN(X) S {S, A, C, B} A {A, B} B {B} C {C, B} Eliminating chain rules from the grammar results in: S aa a cc c bb b A aa a bb b B bb b C cc c bb b The regular expression for this grammar is: a + b c + b b + Problem 3: (10 Points) Exercise 4.15 Solution 3: TERM PREV {D, F, G} {D, F, G, A} {D, F, G} {D, F, G, A, S} {D, F, G, A} {D, F, G, A, S} {D, F, G, A, S} 1

TERM = {S, A, F, G, D} Intermediate Grammar: S aa A aa ad D bd b F af ag a G a b REACH PREV NEW {S } {S, A} {S } {S } {S, A, D} {S, A} {A } {S, A, D} {S, A, D } {D } REACH = {S, A, D} Final Grammar: S aa A aa ad D bd b Regular Expression : aa + b + Problem 4: (10 Points) Exercise 4.19 Solution 4: Transform the rules in G to rules of the form S λ, A a, A w where w is a string with variables only S A AB B ABC a A A A a B B BC C b C A B C A a B b C c Then rewrite the above grammar such that each rule has exactly 2 variables on the right-hand side 2

S A T 1 AT 3 a T 1 AT 2 T 2 B B T 3 BC A A A a B B T 4 b T 4 BT 5 T 5 C C C A T 6 T 6 B C A a B b C c Problem 5: (10 Points) Exercise 4.23 Solution 5: Grammar after removing λ rules: S λ A ABa Ba AbA ba Ab b A Aa a B Bb BC C CB CA bb Grammar after removing chain rules: CHAIN(S) = {S, A} Now, let s look for useless symbols: S λ Aa a ABa Ba AbA ba Ab b A Aa a B Bb BC C CB CA bb TERM PREV {S, A} {S, A} {S, A} Grammar G T : TERM = {S, A} S λ Aa a AbA ba Ab b A Aa a REACH PREV NEW {S } {S, A} {S } {S } {S, A } {S, A} {A } REACH = {S, A} 3

Grammar G U : Final Grammar in Chomsky Normal Form: S λ Aa a AbA ba Ab b A Aa a S λ AA a AT 1 B A AB b T 1 B A A AA a A a B b Problem 6: (10 Points) Exercise 4.27 Solution 6: a.) aab (aba)b (ab ba) b abab b.) S A B A aab aabz Z abz baz ab ba B bb aba bby abay Y by b Problem 7: (10 Points) Exercise 4.30 Solution 7: S aab a A aab A AB aab A aa AB aa b A a B b 4

Chapter 5 Problem 8: (10 Points) Exercise 5.2 Solution 8: part a Figure 1: Chap 5 Question 2.a part b part c part d [q 0, babaab] [q 0, abaab] [q 1, baab] [q 2, aab] [q 1, ab] [q 1, b] [q 2, λ] [b (a(a ba) bb)] [b (a(a ba) bb)] (λ [a(a ba) ]) Problem 9: (10 Points) Exercise 5.3 Solution 9: part a δ a b q 0 q 1 q 0 q 1 q 1 q 2 q 2 q 0 q 1 part b Accepted strings are baab, abaaab. 5

part c [b [aa b(ba b) a]] aa b(ba b) Problem 10: (10 Points) Exercise 5.13 Solution 10: Figure 2: Chap5 Question 13 Problem 11: (10 Points) Exercise 5.23 Solution 11: part a part b δ a b q 0 {q 0, q 1 } q 1 {q 1, q 2 } q 2 {q 0, q 1 } [q 0, aaabb] [q 0, aaabb] [q 0, aaabb] [q 0, aabb] [q 0, aabb] [q 0, aabb] [q 0, abb] [q 0, abb] [q 0, abb] [q 0, bb] [q 1, bb] [q 1, bb] [q 1, b] [q 1, b] [q 1, λ] [q 2, λ] fails rejects accepts [q 0, aaabb] [q 0, aaabb] [q 0, aaabb] [q 0, aabb] [q 0, aabb] [q 1, aabb] [q 0, abb] [q 1, abb] [q 1, bb] [q 2, b] fails fails fails See the parallel execution tree in Figure 3 6

Figure 3: Chap5 Question 23(b): Parallel Execution Tree for input aaabb part c YES. part d [a [ab b(ab b) a]] ab b(ab b) Problem 12: (10 Points) Exercise 5.25 (d) Solution 12: See Figure 4. Problem 13: (10 Points) Exercise 5.36 Solution 13: part a part b Input transition function t for M. λ closure(q 0 ) = {q 0, q 2 } λ closure(q 1 ) = {q 1 } λ closure(q 2 ) = {q 2 } The input transition function of a state q on input symbol a is given by the following formula: 7

Figure 4: Chap5 Question 25 (d) Hence, t(q, a) = λ closure(δ(q j, a)) q j λ closure(q) t(q 0, a) = λ closure(δ(q 0, a)) λ closure(δ(q 2, a)) = λ closure(q 0 ) λ closure( ) = {q 0, q 2 } = {q 0, q 2 } t(q 0, b) = λ closure(δ(q 0, b)) λ closure(δ(q 2, b)) = λ closure( ) λ closure(q 1, q 2 ) = {q 1, q 2 } = {q 1, q 2 } t(q 0, c) = λ closure(δ(q 0, c)) λ closure(δ(q 2, c)) = λ closure(q 1 ) λ closure( ) = {q 1 } = {q 1 } t(q 1, a) = λ closure(δ(q 1, a)) = t(q 1, b) = λ closure(δ(q 1, b)) = t(q 1, c) = λ closure(δ(q 1, c)) = λ closure({q 1 }) = {q 1 } part c t(q 2, a) = λ closure(δ(q 2, a)) = t(q 2, b) = λ closure(δ(q 2, b)) = λ closure({q 1, q 2 }) = {q 1, q 2 } t(q 2, c) = λ closure(δ(q 2, c)) = 8

State Symbol λ closure of NFA Transition Next State λ closure({q 0 }) a λ closure(δ(q 0, a)) = λ closure({q 0 }) = {q 0, q 2 } {q 0, q 2 } = {q 0, q 2 } = {q 0, q 2 } λ closure(δ(q 2, a)) = λ closure( ) = [Same as t(q 0, a) t(q 2, a)] b λ closure(δ(q 0, b)) = λ closure( ) = {q 1, q 2 } = {q 1, q 2 } λ closure(δ(q 2, b)) = λ closure({q 1, q 2 }) = {q 1, q 2 } [Same as t(q 0, b) t(q 2, b)] c λ closure(δ(q 0, c)) = λ closure({q 1 }) = {q 1 } {q 1 } = {q 1 } λ closure(δ(q 2, c)) = λ closure( ) = [Same as t(q 0, c) t(q 2, c)] {q 1, q 2 } a λ closure(δ(q 1, a)) = λ closure( ) = = λ closure(δ(q 2, a)) = λ closure( ) = [Same as t(q 1, a) t(q 2, a)] b λ closure(δ(q 1, b)) = λ closure( ) = {q 1, q 2 } = {q 1, q 2 } λ closure(δ(q 2, b)) = λ closure({q 1, q 2 }) = {q 1, q 2 } [Same as t(q 1, b) t(q 2, b)] c λ closure(δ(q 1, c)) = λ closure({q 1 }) = {q 1 } {q 1 } = {q 1 } λ closure(δ(q 2, c)) = λ closure( ) = [Same as t(q 1, c) t(q 2, c)] {q 1 } a λ closure(δ(q 1, a)) = λ closure( ) = [Same as t(q 1, a)] b λ closure(δ(q 1, b)) = λ closure( ) = [Same as t(q 1, b)] c λ closure(δ(q 1, c)) = λ closure({q 1 }) = {q 1 } {q 1 } [Same as t(q 1, c)] See the state diagram of the resulting DFA in Figure 5. Figure 5: Chap5 Question 36 (c) part d a [b [c (b b)]c ] A good exercise is to determine whether or not the regular expression above is equivalent to a b c. Problem 14: (10 Points) Exercise 5.41 Solution 14: See the constructed NFA in Figure 6. 9

Figure 6: Chap5 Question 41 NFA Input transition function t for the NFA λ closure(q 0 ) = {q 0, q 2 } λ closure(q 1 ) = {q 1 } λ closure(q 2 ) = {q 2 } λ closure(q 3 ) = {q 3 } t(q 0, a) = {q 1 }, t(q 1, a) =, t(q 2, a) =, t(q 3, a) = {q 2 } t(q 0, b) = {q 3 }, t(q 1, b) = {q 0, q 2 }, t(q 2, b) = {q 3 }, t(q 3, b) = Constructing the states and transitions of an equivalent DFA: State Symbol Next State λ closure({q 0 }) a t(q 0, a) t(q 2, a) = {q 1 } = {q 1 } = {q 0, q 2 } b t(q 0, b) t(q 2, b) = {q 3 } {q 3 } = {q 3 } {q 1 } a t(q 1, a) = b t(q 1, b) = {q 0, q 2 } {q 3 } a t(q 3, a) = {q 2 } b t(q 3, b) = {q 2 } a t(q 2, a) = b t(q 2, b) = {q 3 } The resulting DFA, equivalent to the original NFA, is depicted in Figure 7. Figure 7: Chap5 Question 41 DFA 10