MTH401A Theory of Computation. Lecture 17

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

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

Lecture 12 Simplification of Context-Free Grammars and Normal Forms

CFGs and PDAs are Equivalent. We provide algorithms to convert a CFG to a PDA and vice versa.

Section 1 (closed-book) Total points 30

UNIT-VI PUSHDOWN AUTOMATA

(pp ) PDAs and CFGs (Sec. 2.2)

Theory of Computation - Module 3

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

Equivalence of CFG s and PDA s

(pp ) PDAs and CFGs (Sec. 2.2)

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

CSE 211. Pushdown Automata. CSE 211 (Theory of Computation) Atif Hasan Rahman

NPDA, CFG equivalence

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

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

Computability and Complexity

Pushdown Automata. Reading: Chapter 6

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

Homework. Context Free Languages. Announcements. Before We Start. Languages. Plan for today. Final Exam Dates have been announced.

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

Pushdown Automata. Notes on Automata and Theory of Computation. Chia-Ping Chen

Pushdown Automata (2015/11/23)

CS481F01 Prelim 2 Solutions

Lecture 17: Language Recognition

Theory of Computation Turing Machine and Pushdown Automata

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,

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

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

Context-Free Grammars: Normal Forms

Properties of Context-Free Languages. Closure Properties Decision Properties

CSE 105 THEORY OF COMPUTATION

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

Introduction to Theory of Computing

CPS 220 Theory of Computation

CS 373: Theory of Computation. Fall 2010

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

Miscellaneous. Closure Properties Decision Properties

Outline. CS21 Decidability and Tractability. Machine view of FA. Machine view of FA. Machine view of FA. Machine view of FA.

Simplification and Normalization of Context-Free Grammars

Context Free Grammars

THEORY OF COMPUTATION (AUBER) EXAM CRIB SHEET

Formal Languages and Automata

CPS 220 Theory of Computation Pushdown Automata (PDA)

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.

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

This lecture covers Chapter 7 of HMU: Properties of CFLs

AC68 FINITE AUTOMATA & FORMULA LANGUAGES DEC 2013

CSE 105 THEORY OF COMPUTATION

Computational Models - Lecture 4

Properties of Context-free Languages. Reading: Chapter 7

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

Theory of Computation (IV) Yijia Chen Fudan University

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

Pushdown Automata (Pre Lecture)

Einführung in die Computerlinguistik Kontextfreie Grammatiken - Formale Eigenschaften

Decidable and undecidable languages

Einführung in die Computerlinguistik

CPSC 313 Introduction to Computability

Foundations of Informatics: a Bridging Course

Computational Models: Class 5

Formal Definition of a Finite Automaton. August 26, 2013

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

Einführung in die Computerlinguistik

CDM Parsing and Decidability

Computational Models - Lecture 5 1

Accept or reject. Stack

Context-Free Grammar

Pushdown Automata (PDA) The structure and the content of the lecture is based on

5 Context-Free Languages

CISC4090: Theory of Computation

MA/CSSE 474 Theory of Computation

Theory of Computation

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

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

Computability Theory

Fundamentele Informatica II

Context Sensitive Grammar

Chomsky Normal Form and TURING MACHINES. TUESDAY Feb 4

Theory of Computation

Pushdown Automata: Introduction (2)

Undecidable Problems and Reducibility

Automata Theory (2A) Young Won Lim 5/31/18

Push-down Automata = FA + Stack

Context-Free Languages

Ogden s Lemma for CFLs

Context-Free Languages (Pre Lecture)

Properties of context-free Languages

Pushdown Automata. Pushdown Automata. Pushdown Automata. Pushdown Automata. Pushdown Automata. Pushdown Automata. The stack

SYLLABUS. Introduction to Finite Automata, Central Concepts of Automata Theory. CHAPTER - 3 : REGULAR EXPRESSIONS AND LANGUAGES

Languages, regular languages, finite automata

SCHEME FOR INTERNAL ASSESSMENT TEST 3

SFWR ENG 2FA3. Solution to the Assignment #4

Lecture 13: Turing Machine

Harvard CS 121 and CSCI E-207 Lecture 10: Ambiguity, Pushdown Automata

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.

CS Pushdown Automata

CSE 105 THEORY OF COMPUTATION

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

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

Transcription:

MTH401A Theory of Computation Lecture 17

Chomsky Normal Form for CFG s

Chomsky Normal Form for CFG s For every context free language, L, the language L {ε} has a grammar in which every production looks like, either 1. S AB, or 2. B a

Chomsky Normal Form for CFG s For every context free language, L, the language L {ε} has a grammar in which every production looks like, either 1. S AB, or 2. B a Further, the grammar has no useless symbols.

Chomsky Normal Form for CFG s Eliminating useless symbols : A symbol is useful if it appears in some derivation of some terminal string from the start symbol. It is useless, otherwise. Eliminate all useless symbols by : 1. Eliminate symbols that derive no terminal string. 2. Eliminate unreachable symbols.

Chomsky Normal Form for CFG s Example : Eliminate Variables S AB C A 0A 0 B 1B C 1 Basis : A and C are identified because of A 0 and C 1. Induction : S is identified because of S C. Nothing else can be identified. S C A 0A 0 C 1

Chomsky Normal Form for CFG s Algorithm to eliminate unreachable symbols : 1. Basis : We can reach S (the start symbol). 2. Induction : If we can reach A and A α then we can reach all symbols in α. 3. Algorithm : Remove from the grammar all symbols not reachable from S and all productions that involve these symbols.

Chomsky Normal Form for CFG s Example : Eliminate Useless Symbols S AB 0 A C 0 B 0B C 1 1. If we eliminate unreachable symbols first we would find that everything is reachable. 2. A, C, 1 would never get eliminated.

Chomsky Normal Form for CFG s Discovering Nullable Symbols : 1. To eliminate all ε- productions, we first need to discover all nullable variables (variables A such that A * ε). 2. Basis : If there is a production A ε then A is nullable. 3. Induction : If there is a production A α and all symbols in α are nullable then A is nullable.

Chomsky Normal Form for CFG s Example : Eliminate ε- Productions S ABC A 0A ε B 1B ε C ε 1. A, B, C, S are nullable. 2. New grammar S ABC AB AC BC A B C A 0A 0 B 1B 1

Chomsky Normal Form for CFG s Example : Eliminate ε- Productions S ABC A 0A ε B 1B ε C ε 1. A, B, C, S are nullable. 2. New grammar S ABC AB AC BC A B C A 0A 0 B 1B 1 C is now useless. Eliminate its productions

Chomsky Normal Form for CFG s Example : Eliminate ε- Productions S ABC A 0A ε B 1B ε C ε 1. A, B, C, S are nullable. 2. New grammar S ABC AB AC BC A B C A 0A 0 B 1B 1 C is now useless. Eliminate its productions

Chomsky Normal Form for CFG s Unit Productions : A unit production is one whose right side consists of exactly one variable. Algorithm : Find all pairs (A,B) such that A * B by a sequence of unit productions only. Basis : Pairs (A,A) are there. Induction : If we have found (A,B) and B C is a unit production then add (A,C).

Chomsky Normal Form for CFG s Example : Unit Productions S A B CC DD B BOZO CC ABC 0 - - -

Chomsky Normal Form for CFG s Example : Unit Productions S A B CC DD B BOZO CC ABC 0 - - - Unit Productions sequences S * B

Chomsky Normal Form for CFG s Example : Unit Productions S A B CC DD BOZO CC ABC 0 B BOZO CC ABC 0 - - - S A B CC DD B BOZO CC ABC 0 - - -

Chomsky Normal Form for CFG s Example : Unit Productions S A 11 A B 1 B S 0

Chomsky Normal Form for CFG s Example : Unit Productions S A 11 A B 1 B S 0 Unit Productions sequences S * A, S * B; A * B, A * S; B * S, B * A;

Chomsky Normal Form for CFG s Example 5 : Useless Symbols S A 11 1 0 A B 1 0 11 B S 0 11 1 S A 11 A B 1 B S 0

Chomsky Normal Form for CFG s Example 5 : Useless Symbols S A 11 1 0 A B 1 0 11 B S 0 11 1 A and B are useless symbols. No way to get them from S. S A 11 A B 1 B S 0

Chomsky Normal Form for CFG s Cleaning Up a Grammar : Theorem : If L is a CFL, then there is a CFG for L {ε} that has No useless symbols. No ε- productions. No unit productions. In other words, every right side is either a single terminal or has length 2.

Chomsky Normal Form for CFG s Chomsky Normal Form Steps: Step 1 : Clean up the grammar. Step 2 : For each right side that is not a single terminal, make it all variables. For each terminal symbol a, create a new variable A a and a production A a a. Replace a by A a in right sides of length 2. Step 3 : Break right sides of length longer than 2 into a chain of productions with right sides of two variables.

Chomsky Normal Form for CFG s Example : Terminals S 0A1B DEF A CC

Chomsky Normal Form for CFG s Example : Terminals S 0A1B DEF A CC Z 0 O 1

Chomsky Normal Form for CFG s Example : Terminals S ZAOB DEF A CC Z 0 O 1

Chomsky Normal Form for CFG s Example : Long productions S ZAOB DEF A CC Z 0 O 1

Chomsky Normal Form for CFG s Example : Long productions S ZAOB DEF A CC Z 0 O 1 Too long.

Chomsky Normal Form for CFG s Example : Long productions S ZAOB DEF A CC Z 0 O 1 Too long. New group of symbols.

Chomsky Normal Form for CFG s Example : Long productions S ZM DEF A CC Z 0 O 1 M AOB

Chomsky Normal Form for CFG s Example : Long productions S ZM DEF A CC Z 0 O 1 M AOB New group of symbols.

Chomsky Normal Form for CFG s Example : Long productions S ZM DH A CC Z 0 O 1 M AN N OB H EF

Chomsky Normal Form for CFG s Chomsky Normal Form : CFG is said to be in a Chomsky Normal Form if the grammar has no useless symbols and every production is of one of these two forms : 1. A BC (right side is two variables), 2. A a (right side is a single terminal). Theorem : If L is a CFL, then L {ε} has a CFG in Chomsky normal form.

Pushdown Machines

Pushdown Machines 0 1 0 1 1 1 0 1 0 0 0 0 0 0 0

Pushdown Machines 0 1 0 1 1 1 0 1 0 0 0 0 0 0 0 Finite State Machine

Pushdown Machines 0 1 0 1 1 1 0 1 0 0 0 0 0 0 0 Finite State Machine

Pushdown Machines 0 1 0 1 1 1 0 1 0 0 0 0 0 0 0

Pushdown Machines 0 1 0 1 1 1 0 1 0 0 0 0 0 0 0 Finite Pushdown State Machine

Formalism : Pushdown Machines A pushdown machine is described by: 1. A finite set of states (Q, typically). 2. An input alphabet (Σ, typically). 3. A stack alphabet (Γ, typically). 4. A transition function (δ, typically). 5. A start state (q 0, in Q, typically). 6. A start symbol (Z 0 in Γ, typically). 7. A set of final states (F Q, typically).

Convention : Pushdown Machines a, b, c, are input symbols. Sometimes, we also allow ε as a possible value., X, Y, Z are stack symbols., w, x, y, z are strings of input symbols. α, β, γ, are strings of stack symbols.

Pushdown Machines The Transition Function :

Pushdown Machines The Transition Function : Takes three input arguments. 1. A state, in Q. 2. An input, which is either a symbol in Σ or ε. 3. A stack symbol in Γ.

Pushdown Machines The Transition Function : Takes three input arguments. 1. A state, in Q. 2. An input, which is either a symbol in Σ or ε. 3. A stack symbol in Γ. δ(q, a, Z) is a set of zero or more actions of the form (p, α). p is a state and α is a string of stack symbols.

Pushdown Machines Actions of The Pushdown Machine : If δ(q, a, Z) contains (p, α) among its actions, then one thing the pushdown machine can do in state q, with a at the front of the input, and Z on top of the stack is : 1. Change the state to p. 2. Remove a from the front of the input (a may be ε). 3. Replace Z on the top of the stack by α.

Example : Pushdown Machines Construct a pushdown machine to accept {0 n 1 n : n 1}.

Example : Pushdown Machines Construct a pushdown machine to accept {0 n 1 n : n 1}. The states :

Example : Pushdown Machines Construct a pushdown machine to accept {0 n 1 n : n 1}. The states : 1. q = start state. We are in state q if we have seen only 0 s so far.

Example : Pushdown Machines Construct a pushdown machine to accept {0 n 1 n : n 1}. The states : 1. q = start state. We are in state q if we have seen only 0 s so far. 2. p = we have seen at least one 1 and may now proceed only if the inputs are 1 s.

Example : Pushdown Machines Construct a pushdown machine to accept {0 n 1 n : n 1}. The states : 1. q = start state. We are in state q if we have seen only 0 s so far. 2. p = we have seen at least one 1 and may now proceed only if the inputs are 1 s. 3. f = final state; accept.

Pushdown Machines Example (continued) : The stack symbols :

Pushdown Machines Example (continued) : The stack symbols : 1. Z 0 = start symbol. Also marks the bottom of the stack.

Pushdown Machines Example (continued) : The stack symbols : 1. Z 0 = start symbol. Also marks the bottom of the stack. 2. X = marker, used to count the number of 0 s seen on the input.

Pushdown Machines Example (continued) : The transitions :

Pushdown Machines Example (continued) : The transitions : 1. δ(q, 0, Z 0 ) = {(q, XZ 0 )}.

Pushdown Machines Example (continued) : The transitions : 1. δ(q, 0, Z 0 ) = {(q, XZ 0 )}. 2. δ(q, 0, X) = {(q, XX)}. These two rules make sure that one X is pushed into the stack for each 0 read from the input.

Pushdown Machines Example (continued) : The transitions : 1. δ(q, 0, Z 0 ) = {(q, XZ 0 )}. 2. δ(q, 0, X) = {(q, XX)}. These two rules make sure that one X is pushed into the stack for each 0 read from the input. 3. δ(q, 1, X) = {(p, ε)}. When we see a 1, go to state p and pop one X.

Pushdown Machines Example (continued) : The transitions : 1. δ(q, 0, Z 0 ) = {(q, XZ 0 )}. 2. δ(q, 0, X) = {(q, XX)}. These two rules make sure that one X is pushed into the stack for each 0 read from the input. 3. δ(q, 1, X) = {(p, ε)}. When we see a 1, go to state p and pop one X. 4. δ(p, 1, X) = {(p, ε)}. Pop one X per 1.

Pushdown Machines Example (continued) : The transitions : 1. δ(q, 0, Z 0 ) = {(q, XZ 0 )}. 2. δ(q, 0, X) = {(q, XX)}. These two rules make sure that one X is pushed into the stack for each 0 read from the input. 3. δ(q, 1, X) = {(p, ε)}. When we see a 1, go to state p and pop one X. 4. δ(p, 1, X) = {(p, ε)}. Pop one X per 1. 5. δ(p, ε, Z 0 ) = {(f, Z 0 )}. Accept at bottom.

Pushdown Machines Example (continued) : In action Z 0 0 0 0 1 1 1 q

Pushdown Machines Example (continued) : In action Z 0 0 0 0 1 1 1 q δ(q, 0, Z 0 )

Pushdown Machines Example (continued) : In action Z 0 0 0 0 1 1 1 q δ(q, 0, Z 0 ) = {(q, XZ 0 )}

Pushdown Machines Example (continued) : In action X 0 0 1 1 1 Z 0 q

Pushdown Machines Example (continued) : In action X 0 0 1 1 1 Z 0 q δ(q, 0, X)

Pushdown Machines Example (continued) : In action X 0 0 1 1 1 Z 0 q δ(q, 0, X) = {(q, XX)}

Pushdown Machines Example (continued) : In action X X 0 1 1 1 Z 0 q

Pushdown Machines Example (continued) : In action X X 0 1 1 1 Z 0 q δ(q, 0, X)

Pushdown Machines Example (continued) : In action X X 0 1 1 1 Z 0 q δ(q, 0, X) = {(q, XX)}

Pushdown Machines Example (continued) : In action X X X 1 1 1 Z 0 q

Pushdown Machines Example (continued) : In action X X X 1 1 1 Z 0 q δ(q, 1, X)

Pushdown Machines Example (continued) : In action X X X 1 1 1 Z 0 q δ(q, 1, X) = {(p, ε)}

Pushdown Machines Example (continued) : In action X X 1 1 Z 0 p

Pushdown Machines Example (continued) : In action X X 1 1 Z 0 p δ(p, 1, X)

Pushdown Machines Example (continued) : In action X X 1 1 Z 0 p δ(p, 1, X) = {(p, ε)}

Pushdown Machines Example (continued) : In action X 1 Z 0 p

Pushdown Machines Example (continued) : In action X 1 Z 0 p δ(p, 1, X)

Pushdown Machines Example (continued) : In action X 1 Z 0 p δ(p, 1, X) = {(p, ε)}

Pushdown Machines Example (continued) : In action Z 0 p

Pushdown Machines Example (continued) : In action Z 0 p δ(p, ε, Z 0 )

Pushdown Machines Example (continued) : In action Z 0 p δ(p, ε, Z 0 ) = {(f, Z 0 )}

Pushdown Machines Example (continued) : In action Z 0 f

Pushdown Machines Instantaneous Descriptions : We can formalize the pictures just seen with an instantaneous description (ID). An ID is a 3-tuple (q, w, α), where: 1. q is the current state. 2. w is the remaining input. 3. α is the stack contents, top at the left.

Pushdown Machines The Goes-To relation: To say that ID I can become ID J in one move of the pushdown machine, we write I J.

Pushdown Machines The Goes-To relation: To say that ID I can become ID J in one move of the pushdown machine, we write I J. Formally, (q, aw, Xα) (p, w, βα), for any w and α, if δ(q, a, X) contains (p, β).

Pushdown Machines The Goes-To relation: To say that ID I can become ID J in one move of the pushdown machine, we write I J. Formally, (q, aw, Xα) (p, w, βα), for any w and α, if δ(q, a, X) contains (p, β). Extend to *, meaning zero or more moves by Basis : I * I. Induction : If I * J and J K, then I * K.