Conversion of CFG to PDA Conversion of PDA to CFG

Slides:



Advertisements
Similar presentations
&& Department of nskinfo-i educationwww.nsksofttch.com CS2303-THEORY OF COMPUTATION uChapter: Closure.
Advertisements

Chapter 5 Pushdown Automata
1 Pushdown Automata (PDA) Informally: –A PDA is an NFA-ε with a stack. –Transitions are modified to accommodate stack operations. Questions: –What is a.
1 Lecture 32 Closure Properties for CFL’s –Kleene Closure construction examples proof of correctness –Others covered less thoroughly in lecture union,
1 Parse Trees Definitions Relationship to Left- and Rightmost Derivations Ambiguity in Grammars.
Pushdown Automata (PDA)
Pushdown Automata Part II: PDAs and CFG Chapter 12.
1 More Undecidable Problems Rice’s Theorem Post’s Correspondence Problem Some Real Problems.
PDAs => CFGs Sipser 2.2 (pages ). Last time…
PDAs => CFGs Sipser 2.2 (pages ). Last time…
Introduction to Computability Theory
Decidable languages Sipser 4.1 (pages ). CS 311 Mount Holyoke College 2 Hierarchy of languages All languages Turing-recognizable Turing-decidable.
Courtesy Costas Busch - RPI1 NPDAs Accept Context-Free Languages.
Normal Forms for CFG’s Eliminating Useless Variables Removing Epsilon
Fall 2004COMP 3351 NPDA’s Accept Context-Free Languages.
1 Context-Free Grammars Formalism Derivations Backus-Naur Form Left- and Rightmost Derivations.
1 Pushdown Automata Definition Moves of the PDA Languages of the PDA Deterministic PDA’s.
1 Module 31 Closure Properties for CFL’s –Kleene Closure construction examples proof of correctness –Others covered less thoroughly in lecture union, concatenation.
Nondeterministic Finite Automata
CS5371 Theory of Computation Lecture 8: Automata Theory VI (PDA, PDA = CFG)
January 15, 2014CS21 Lecture 61 CS21 Decidability and Tractability Lecture 6 January 16, 2015.
Fall 2006Costas Busch - RPI1 PDAs Accept Context-Free Languages.
Decision Properties of Regular Languages
1 PDAs Accept Context-Free Languages. 2 Context-Free Languages (Grammars) Languages Accepted by PDAs Theorem:
Finite State Machines Data Structures and Algorithms for Information Processing 1.
Properties of Context-Free Languages
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 8 Mälardalen University 2010.
1 Dr. torng CFG → PDA construction Shows that for any CFL L, there exists a PDA M such that L(M) = L The reverse is true, but we skip the proof Parsing.
Pushdown Automata.
Chapter 7 PDA and CFLs.
1 Parse Trees Definitions Relationship to lm and rm Derivations Ambiguity in Grammars.
Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.
1 Module 31 Closure Properties for CFL’s –Kleene Closure –Union –Concatenation CFL’s versus regular languages –regular languages subset of CFL.
1 Pushdown Automata Definition: relation to  -NFA Moves of the PDA Languages of the PDA Deterministic PDA’s Quiz Wednesday on CFGs lecture #3.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 11 Midterm Exam 2 -Context-Free Languages Mälardalen University 2005.
1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University 2010.
Pushdown Automata Chapters Generators vs. Recognizers For Regular Languages: –regular expressions are generators –FAs are recognizers For Context-free.
Closure Properties Lemma: Let A 1 and A 2 be two CF languages, then the union A 1  A 2 is context free as well. Proof: Assume that the two grammars are.
Introduction to Finite Automata
Foundations of (Theoretical) Computer Science Chapter 2 Lecture Notes (Section 2.2: Pushdown Automata) Prof. Karen Daniels, Fall 2010 with acknowledgement.
Grammars A grammar is a 4-tuple G = (V, T, P, S) where 1)V is a set of nonterminal symbols (also called variables or syntactic categories) 2)T is a finite.
C SC 473 Automata, Grammars & Languages Automata, Grammars and Languages Discourse 04 Context-Free Grammars and Pushdown Automata.
Formal Languages, Automata and Models of Computation
CSCI 2670 Introduction to Theory of Computing October 13, 2005.
1 Chapter 6 Pushdown automata Sagrada Familia ( 聖家 堂 ), Barcelona, Spain.
1 Alphabets: An Alphabet is a finite set of symbols. We will usually use  to denote the alphabet of input symbols or “terminal characters.” String: A.
1 Pushdown Automata Definition Moves of the PDA Languages of the PDA Deterministic PDA’s.
Donghyun (David) Kim Department of Mathematics and Physics North Carolina Central University 1 Chapter 2 Context-Free Languages Some slides are in courtesy.
CSCI 4325 / 6339 Theory of Computation Zhixiang Chen Department of Computer Science University of Texas-Pan American.
FORMAL LANGUAGES, AUTOMATA, AND COMPUTABILITY
1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 8 Mälardalen University 2011.
Pushdown Automata Chapter 12. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2) Say yes or no,
CS 154 Formal Languages and Computability March 15 Class Meeting Department of Computer Science San Jose State University Spring 2016 Instructor: Ron Mak.
About Grammars Hopcroft, Motawi, Ullman, Chap 7.1, 6.3, 5.4.
Costas Busch - LSU1 PDAs Accept Context-Free Languages.
1 Problem Session 3 Half(L) Things More Powerful Than a Turing Machine Some Concerns About Proofs.
Lecture 11  2004 SDU Lecture7 Pushdown Automaton.
Pushdown Automata.
Closed book, closed notes
Properties of Context-Free Languages
PDAs Accept Context-Free Languages
CSE 105 theory of computation
Theorem 29 Given any PDA, there is another PDA that accepts exactly the same language with the additional property that whenever a path leads to ACCEPT,
PDAs Accept Context-Free Languages
NPDAs Accept Context-Free Languages
Last Lecture We began to show CFL = PDA
Chapter 2 Context-Free Language - 01
CSE 105 theory of computation
FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY
Conversion of CFG to PDA Conversion of PDA to CFG
Presentation transcript:

Conversion of CFG to PDA Conversion of PDA to CFG Equivalence of PDA, CFG Conversion of CFG to PDA Conversion of PDA to CFG

Overview When we talked about closure properties of regular languages, it was useful to be able to jump between RE and DFA representations. Similarly, CFG’s and PDA’s are both useful to deal with properties of the CFL’s.

Overview – (2) Also, PDA’s, being “algorithmic,” are often easier to use when arguing that a language is a CFL. Example: It is easy to see how a PDA can recognize balanced parentheses; not so easy as a grammar. But all depends on knowing that CFG’s and PDA’s both define the CFL’s.

Converting a CFG to a PDA Let L = L(G). Construct PDA P such that N(P) = L. P has: One state q. Input symbols = terminals of G. Stack symbols = all symbols of G. Start symbol = start symbol of G.

Intuition About P Given input w, P will step through a leftmost derivation of w from the start symbol S. Since P can’t know what this derivation is, or even what the end of w is, it uses nondeterminism to “guess” the production to use at each step.

Intuition – (2) At each step, P represents some left-sentential form (step of a leftmost derivation). If the stack of P is , and P has so far consumed x from its input, then P represents left-sentential form x. At empty stack, the input consumed is a string in L(G).

Transition Function of P δ(q, a, a) = (q, ε). (Type 1 rules) This step does not change the LSF represented, but “moves” responsibility for a from the stack to the consumed input. If A ->  is a production of G, then δ(q, ε, A) contains (q, ). (Type 2 rules) Guess a production for A, and represent the next LSF in the derivation.

Proof That L(P) = L(G) We need to show that (q, wx, S) ⊦* (q, x, ) for any x if and only if S =>*lm w. Part 1: “only if” is an induction on the number of steps made by P. Basis: 0 steps. Then  = S, w = ε, and S =>*lm S is surely true.

Induction for Part 1 Consider n moves of P: (q, wx, S) ⊦* (q, x, ) and assume the IH for sequences of n-1 moves. There are two cases, depending on whether the last move uses a Type 1 or Type 2 rule.

Use of a Type 1 Rule The move sequence must be of the form (q, yax, S) ⊦* (q, ax, a) ⊦ (q, x, ), where ya = w. By the IH applied to the first n-1 steps, S =>*lm ya. But ya = w, so S =>*lm w.

Use of a Type 2 Rule The move sequence must be of the form (q, wx, S) ⊦* (q, x, A) ⊦ (q, x, ), where A ->  is a production and  = . By the IH applied to the first n-1 steps, S =>*lm wA. Thus, S =>*lm w = w.

Proof of Part 2 (“if”) We also must prove that if S =>*lm w, then (q, wx, S) ⊦* (q, x, ) for any x. Induction on number of steps in the leftmost derivation. Ideas are similar; read in text.

Proof – Completion We now have (q, wx, S) ⊦* (q, x, ) for any x if and only if S =>*lm w. In particular, let x =  = ε. Then (q, w, S) ⊦* (q, ε, ε) if and only if S =>*lm w. That is, w is in N(P) if and only if w is in L(G).

From a PDA to a CFG Now, assume L = N(P). We’ll construct a CFG G such that L = L(G). Intuition: G will have variables generating exactly the inputs that cause P to have the net effect of popping a stack symbol X while going from state p to state q. P never gets below this X while doing so.

Variables of G G’s variables are of the form [pXq]. This variable generates all and only the strings w such that (p, w, X) ⊦*(q, ε, ε). Also a start symbol S we’ll talk about later.

Productions of G Each production for [pXq] comes from a move of P in state p with stack symbol X. Simplest case: δ(p, a, X) contains (q, ε). Then the production is [pXq] -> a. Note a can be an input symbol or ε. Here, [pXq] generates a, because reading a is one way to pop X and go from p to q.

Productions of G – (2) Next simplest case: δ(p, a, X) contains (r, Y) for some state r and symbol Y. G has production [pXq] -> a[rYq]. We can erase X and go from p to q by reading a (entering state r and replacing the X by Y) and then reading some w that gets P from r to q while erasing the Y. Note: [pXq] =>* aw whenever [rYq] =>* w.

Productions of G – (3) Third simplest case: δ(p, a, X) contains (r, YZ) for some state r and symbols Y and Z. Now, P has replaced X by YZ. To have the net effect of erasing X, P must erase Y, going from state r to some state s, and then erase Z, going from s to q.

Picture of Action of P p X a w x r Y Z w x s Z x q

Third-Simplest Case – Concluded Since we do not know state s, we must generate a family of productions: [pXq] -> a[rYs][sZq] for all states s. [pXq] =>* awx whenever [rYs] =>* w and [sZq] =>* x.

Productions of G: General Case Suppose δ(p, a, X) contains (r, Y1,…Yk) for some state r and k > 3. Generate family of productions [pXq] -> a[rY1s1][s1Y2s2]…[sk-2Yk-1sk-1][sk-1Ykq]

Completion of the Construction We can prove that (q0, w, Z0)⊦*(p, ε, ε) if and only if [q0Z0p] =>* w. Proof is in text; it is two easy inductions. But state p can be anything. Thus, add to G another variable S, the start symbol, and add productions S -> [q0Z0p] for each state p.