Solutions to Problem Set 3

Similar documents
Lecture 11 Context-Free Languages

Automata Theory CS F-08 Context-Free Grammars

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

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

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

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

Context-free Grammars and Languages

Context-Free Grammars and Languages

Chapter 5: Context-Free Languages

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

Concordia University Department of Computer Science & Software Engineering

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

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

AC68 FINITE AUTOMATA & FORMULA LANGUAGES DEC 2013

Context-Free Grammars: Normal Forms

Theory of Computation (IV) Yijia Chen Fudan University

Lecture 12 Simplification of Context-Free Grammars and Normal Forms

Context Free Languages and Grammars

Chapter 1. Introduction

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

Einführung in die Computerlinguistik

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

Automata & languages. A primer on the Theory of Computation. Laurent Vanbever. ETH Zürich (D-ITET) October,

Part 3 out of 5. Automata & languages. A primer on the Theory of Computation. Last week, we learned about closure and equivalence of regular languages

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

Theory of Computer Science

Solution Scoring: SD Reg exp.: a(a

This lecture covers Chapter 5 of HMU: Context-free Grammars

CS375 Midterm Exam Solution Set (Fall 2017)

REGular and Context-Free Grammars

CS375: Logic and Theory of Computing

Grammars and Context Free Languages

Linear conjunctive languages are closed under complement

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

Context-Free Grammar

Formal Languages, Grammars and Automata Lecture 5

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

HW 3 Solutions. Tommy November 27, 2012

CA320 - Computability & Complexity

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

Context-Free Grammars (and Languages) Lecture 7

Theory of Computation (Classroom Practice Booklet Solutions)

UNIT II REGULAR LANGUAGES

Theory of Computation - Module 3

5 Context-Free Languages

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

AC68 FINITE AUTOMATA & FORMULA LANGUAGES JUNE 2014

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

Grammars and Context Free Languages

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

Section 1 (closed-book) Total points 30

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

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

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

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)

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

Harvard CS 121 and CSCI E-207 Lecture 9: Regular Languages Wrap-Up, Context-Free Grammars

Computability and Complexity

Fundamentele Informatica II

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

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

Miscellaneous. Closure Properties Decision Properties

10. The GNFA method is used to show that

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

CS 275 Automata and Formal Language Theory

Intro to Theory of Computation

CISC 4090 Theory of Computation

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

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

Part 4 out of 5 DFA NFA REX. Automata & languages. A primer on the Theory of Computation. Last week, we showed the equivalence of DFA, NFA and REX

Introduction to Theory of Computing

Chapter 4. Regular Expressions. 4.1 Some Definitions

CS500 Homework #2 Solutions

CS 154. Finite Automata vs Regular Expressions, Non-Regular Languages

Grammars and Context-free Languages; Chomsky Hierarchy

Comment: The induction is always on some parameter, and the basis case is always an integer or set of integers.

Computational Models - Lecture 4 1

CSE 355 Test 2, Fall 2016

FLAC Context-Free Grammars

Harvard CS 121 and CSCI E-207 Lecture 12: General Context-Free Recognition

CS 133 : Automata Theory and Computability

CPS 220 Theory of Computation

Chapter 1. Formal Definition and View. Lecture Formal Pushdown Automata on the 28th April 2009

CISC 4090 Theory of Computation

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

CPSC 313 Introduction to Computability

6.8 The Post Correspondence Problem

Languages. Languages. An Example Grammar. Grammars. Suppose we have an alphabet V. Then we can write:

Context-Free Grammars and Languages. Reading: Chapter 5

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

Computational Models - Lecture 5 1

Exam 1 CSU 390 Theory of Computation Fall 2007

CPS 220 Theory of Computation Pushdown Automata (PDA)

CSEP 590 Data Compression Autumn Arithmetic Coding

Parikh s theorem. Håkan Lindqvist

Syntactical analysis. Syntactical analysis. Syntactical analysis. Syntactical analysis

CISC4090: Theory of Computation

Transcription:

V22.0453-001 Theory of Computation October 8, 2003 TA: Nelly Fazio Solutions to Problem Set 3 Problem 1 We have seen that a grammar where all productions are of the form: A ab, A c (where A, B non-terminals, and a, c terminals) defines a regular language, and that from the grammar we can build directly an FSA that recognizes the corresponding language. Show that if all the productions have the form: A Ba, A c such a grammar also represents a regular language. A grammar G = (V, Σ, R, S) where all the productions in R are of the form A ab and A c is called left grammar. A grammar G = (V, Σ, R, S) where all the productions in R are of the form A Ba and A c is called right grammar. We have seen that a left grammar define a regular language. We want to show that a right grammar define a regular language, too. Let G = (V, Σ, R, S) be a left grammar. Consider the grammar G = (V, Σ, R, S), where R is obtained reverting all the productions in R i.e., R = {A w A w r is in R}. Note that G is a right grammar, since it was obtained reverting all the productions of the left grammar G. Moreover, it holds that L(G) = L(G ) r. We already proved that the language generated by a left grammar is a regular set: thus, L(G ) is a regular set. We also know that the set of regular languages is closed under reversal: hence, L(G ) r is also a regular set. Since L(G ) r = L(G), we proved that any right grammar generates a regular language. PS3-1

Problem 2 Describe the language generated by the following grammars (upper-case letters are non-terminals, lower case terminals): 1. S SaS b 2. S asb bsa ǫ 1. L 1 = L(b(ab) ), i.e. L 1 is the language described by the regular expression b(ab) which is equivalent to (ba) b. 2. Given a string w, let s denote with w R the reverse of w (e.g. if w = bba then w R = abb). Besides, given a string w, let s denote with w the complement of w obtained from w by swapping a s with b s (e.g. if w = bba then w = aab). Now, we can define our language over the alphabet Σ = {a, b} as: L 2 = {z Σ w Σ : z = w w R } PS3-2

Problem 3 The following grammar is not regular but it generates a regular language. Write the regular expression that describes it: S SSS a ab First, observe that the only production that introduce non-terminals in the sentential form is the production S SSS. Thus, for any derivation we can rearrange the order in which the productions are applied so that all the applications of S SSS came first, followed by application of S a ab, as needed. Then, we consider the set of sentential form derivable just from the production S SSS. We can notice that after t derivations, we have exactly 3 + 2(t 1) = 1 + 2t S s in the sentential form; thus, the set of sentential forms obtainable with application of the production S SSS is A = {S c c = 1 + 2t} = {S(SS) c c = t}. From what said above, it is clear that all the strings generated by the grammar are those obtained by applying the productions S a ab to a sentential form in A. Hence, the language generated by the grammar is the language described by the regular expression: (a ab)((a ab)(a ab)) PS3-3

Problem 4 Show that the following grammar is ambiguous, by producing one string in the language that has two different parse trees: S as asbs ǫ Consider the string aaba and the two parse trees in the figure below: 1 S S a S a S b S a S b S a S a S ǫ a S ǫ ǫ 2 ǫ PS3-4

Problem 5 Show that the language generated by the grammar: S asbs bsas ǫ is the set of all strings with an equal number of a s and b s. You might want to prove this by induction over the length of the string, or by reasoning over the sentential forms that derive a string. We first show that each string generated by this grammar has the same number of a s and b s. Proceed by induction over the length N of the derivation. Case base: N = 1. The only string derivable with a derivation of length N = 1 is ǫ (S ǫ): it trivially holds that the number of a s and b s in ǫ is the same. Inductive step: Assume that all the strings w derivable from S with a derivation of length at most N have an equal number of a s and b s. Consider now a string w derivable from S with a derivation of length N + 1. Clearly, the first production used in the derivation that yields w is either S asbs or S bsas. In either cases, the remaining N productions can be ideally split into two derivations: one that generates a string w 1 from the first S and another that generates a string w 2 from the second S. By induction, it clearly holds that in both w 1 and w 2 the number of a s is equal to the number of b s. Moreover, since w is either aw 1 bw 2 or bw 1 aw 2, we get that the number of a s in w is equal to the number of b s. To complete the proof, we still need to show that any string w with an equal number of a s and b s can be derived from S. Proceed by induction on the length of w. Case base: w = 0, i.e. w = ǫ. It is clear that w can be derived from s (S ǫ). Inductive step: Assume that all the strings w with an equal number of a s and b s and of length up to 2N are derivable from S. Consider now a string w with an equal number of a s and b s of length 2(N + 1). W.l.o.g. assume that the first symbol in w is an a. Let 2 j 2N + 2 be the first index such that the substring of w from position 1 to position j has an equal number of a s and b s. Notice that such j always exists, since in the worst case j = 2N + 2 will do it. If j = 2N + 2, then this means that w is of the form a N b N and thus can be derived from S by the derivation S asbs asb aasbsb aasbb... a N b N. Otherwise (i.e. j < 2N +2), notice that, since the symbol at position j must be a b, w can be written as w = aw 1 bw 2, where w 1 and w 2 are both strings of length at most N, each having an equal number of a s and b s. Thus, by induction, both w 1 and w 2 can be derived from S from which it follows that w can also be derived from S. PS3-5