1 Lecture 32 CFG --> PDA construction –Shows that for any CFL L, there exists a PDA M such that L(M) = L –The reverse is true as well, but we do not prove.

Slides:



Advertisements
Similar presentations
1 Chapter Parsing Techniques. 2 Section 12.3 Parsing Techniques We know (via a theorem) that the context- free languages are exactly those languages.
Advertisements

Theory of Computation CS3102 – Spring 2014 A tale of computers, math, problem solving, life, love and tragic death Nathan Brunelle Department of Computer.
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,
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,
Nathan Brunelle Department of Computer Science University of Virginia Theory of Computation CS3102 – Spring 2014 A tale.
1 Module 20 NFA’s with -transitions –NFA- ’s Formal definition Simplifies construction –LNFA- –Showing LNFA  is a subset of LNFA (extra credit) and therefore.
142 Parsing start (a, Z 0 /aZ 0 ) ( a, a/aa ) (b, a/  ) ( , Z 0 /Z 0 ) For a given CFG G, parsing a string w is to check if w  L(G) and, if it is,
Pushdown Automata Part II: PDAs and CFG Chapter 12.
1 Module 32 Pushdown Automata (PDA’s) –definition –Example We define configurations and computations of PDA’s We define L(M) for PDA’s.
1 Module 19 LNFA subset of LFSA –Theorem 4.1 on page 131 of Martin textbook –Compare with set closure proofs Main idea –A state in FSA represents a set.
1 Module 21 Closure Properties for LFSA using NFA’s –From now on, when I say NFA, I mean any NFA including an NFA- unless I add a specific restriction.
Why the algorithm works! Converting an NFA into an FSA.
Module 28 Context Free Grammars Definition of a grammar G
CS Master – Introduction to the Theory of Computation Jan Maluszynski - HT Lecture 4 Context-free grammars Jan Maluszynski, IDA, 2007
1 Module 33 Pushdown Automata (PDA’s) –Another example.
1 Module 34 CFG --> PDA construction –Shows that for any CFL L, there exists a PDA M such that L(M) = L –The reverse is true as well, but we do not prove.
Lecture 18 NFA’s with -transitions –NFA- ’s Formal definition Simplifies construction –LNFA- –Showing LNFA  is a subset of LNFA and therefore a subset.
1 Lecture 36 Attempt to prove that CFL’s are closed under intersection –Review previous constructions –Translate previous constructions to current setting.
1 Module 31 Closure Properties for CFL’s –Kleene Closure construction examples proof of correctness –Others covered less thoroughly in lecture union, concatenation.
1 Lecture 19 Closure Properties for LFSA using NFA’s –union second proof –concatenation –Kleene closure.
1 Lecture 20 LNFA subset of LFSA –Theorem 4.1 on page 105 of Martin textbook –Compare with set closure proofs Main idea –A state in FSA represents a set.
MA/CSSE 474 Theory of Computation
Context-Free Grammars Chapter 3. 2 Context-Free Grammars and Languages n Defn A context-free grammar is a quadruple (V, , P, S), where  V is.
FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY
Finite State Machines Data Structures and Algorithms for Information Processing 1.
CS 3240: Languages and Computation Pushdown Automata & CF Grammars NOTE: THESE ARE ONLY PARTIAL SLIDES RELATED TO WEEKS 9 AND 10. PLEASE REFER TO THE TEXTBOOK.
Nathan Brunelle Department of Computer Science University of Virginia Theory of Computation CS3102 – Spring 2014 A tale.
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 (PDA) Part 2 While I am distributing graded exams: Design a PDA to accept L = { a m b n : m  n; m, n > 0} MA/CSSE 474 Theory of Computation.
Pushdown Automata.
Chapter 7 PDA and CFLs.
1 Theory of Digital Computation Course material for undergraduate students on IT Department of Computer Science University of Veszprem Veszprem, Hungary.
Context-free Grammars Example : S   Shortened notation : S  aSaS   | aSa | bSb S  bSb Which strings can be generated from S ? [Section 6.1]
TM Design Universal TM MA/CSSE 474 Theory of Computation.
Pushdown Automata CS 130: Theory of Computation HMU textbook, Chap 6.
Pushdown Automata (PDAs)
Lecture # 19. Example Consider the following CFG ∑ = {a, b} Consider the following CFG ∑ = {a, b} 1. S  aSa | bSb | a | b | Λ The above CFG generates.
Part VII. Models for Context-Free Languages 1/50.
1 Module 31 Closure Properties for CFL’s –Kleene Closure –Union –Concatenation CFL’s versus regular languages –regular languages subset of CFL.
1 Section 12.3 Context-Free Parsing We know (via a theorem) that the context-free languages are exactly those languages that are accepted by PDAs. When.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 11 Midterm Exam 2 -Context-Free Languages Mälardalen University 2005.
1 A well-parenthesized string is a string with the same number of (‘s as )’s which has the property that every prefix of the string has at least as many.
Chapter 7 Pushdown Automata
1 Pushdown Automata There are context-free languages that are not regular. Finite automata cannot recognize all context-free languages.
Three Basic Concepts Languages Grammars Automata.
Lecture 14UofH - COSC Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 14.
Transparency No. 1 Formal Language and Automata Theory Homework 5.
FORMAL LANGUAGES, AUTOMATA, AND COMPUTABILITY
1 A well-parenthesized string is a string with the same number of (‘s as )’s which has the property that every prefix of the string has at least as many.
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,
1 Chapter Pushdown Automata. 2 Section 12.2 Pushdown Automata A pushdown automaton (PDA) is a finite automaton with a stack that has stack operations.
CS 154 Formal Languages and Computability March 10 Class Meeting Department of Computer Science San Jose State University Spring 2016 Instructor: Ron Mak.
1 Section 12.2 Pushdown Automata A pushdown automaton (PDA) is a finite automaton with a stack that has stack operations pop, push, and nop. PDAs always.
CS 154 Formal Languages and Computability March 15 Class Meeting Department of Computer Science San Jose State University Spring 2016 Instructor: Ron Mak.
Lecture 11  2004 SDU Lecture7 Pushdown Automaton.
Pushdown Automata - like NFA-  but also has a stack - transition takes the current state, the current input symbol, and the top-of-the-stack symbol (which.
Chapter 1 INTRODUCTION TO THE THEORY OF COMPUTATION.
Pushdown Automata.
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,
CSE 105 theory of computation
Pushdown Automata PDAs
Pushdown Automata Reading: Chapter 6.
Intro to Theory of Computation
Chapter Thirteen: Stack Machines
Chapter 5 Pushdown Automata
Key to Homework #8 1. For each of the following context-free grammars (a) and (b) below, construct an LL(k) parser with minimum k according to the following.
Pushdown Automata (PDA) Part 2
More About Nondeterminism
Presentation transcript:

1 Lecture 32 CFG --> PDA construction –Shows that for any CFL L, there exists a PDA M such that L(M) = L –The reverse is true as well, but we do not prove that here

2 CFL subset LPDA Let L be an arbitrary CFL Let G be the CFG such that L(G) = L –G exists by definition of L is CF Construct a PDA M such that L(M) = L –M is constructed from CFG G Argue L(M) = L There exists a PDA M such that L(M) = L L is in LPDA –By definition of L in LPDA

3 Visualization CFL LPDA CFG’s PDA’s L L G M Let L be an arbitrary CFL Let G be the CFG such that L(G) = L G exists by definition of L is CF Construct a PDA M such that L(M) = L M is constructed from CFG G Argue L(M) = L There exists a PDA M such that L(M) = L L is in LPDA By definition of L in LPDA

4 Algorithm Specification Input –CFG G Output –PDA M such that L(M) = L(G) CFG GPDA M A

5 Construction Idea The basic idea is to have a 2-phase PDA –Phase 1: Derive all strings in L(G) on the stack nondeterministically Do not process any input while we are deriving the string on the stack –Phase 2: Match the input string against the derived string on the stack –This is a deterministic process Move to an accepting state only when the stack is empty

6 Illustration Input Grammar PALG –V = {S} –  = {a,b} –S = S –P: S --> aSa | bSb | a | b | What is L(PALG)? –The set of all palindromes over {a,b} 1. Derive all strings in L(G) on the stack 2. Match the derived string against input (q 0, aabaa, Z) /* put S on stack */ (q 1, aabaa, SZ) /* derive aabaa on stack */ (q 1, aabaa, aSaZ) (q 1, aabaa, aaSaaZ) (q 1, aabaa, aabaaZ) /* match stack vs input */ (q 1, abaa, abaaZ) (q 1, baa, baaZ) (q 1, aa, aaZ) (q 1, a, aZ) (q 1,, Z) (q 2,, Z) Illustration of how the PDA might work, though not completely accurate.

7 Construction Input Grammar –G=(V, , S, P) Output PDA –M=(Q, , , q 0, Z, F,  ) –Q = {q 0, q 1, q 2 } –  =  –  = V union  union {Z} –Z = Z –q 0 = q 0 –F = {q 2 }  : –  (q 0,, Z) = (q 1, SZ) –  (q 1,, Z) = (q 2, Z) –For all productions A -->   q 1,, A) = (q 1,  ) –For all a in   q 1, a, a) = (q 1, )

8 Examples

9 Palindromes PALG: –V = {S} –  = {a,b} –S = S –P: S --> aSa | bSb | a | b | Output PDA M=(Q, ,q 0,Z,F,  ) – Q = {q 0, q 1, q 2 } –  = {a,b,S,Z} – q 0 = q 0 – Z = Z – F = {q 2 }  : –  (q 0,, Z) = (q 1, SZ) –  (q 1,, Z) = (q 2, Z) –Production Transitions  q 1,, S) = (q 1, aSa)  q 1,, S) = (q 1, bSb)  q 1,, S) = (q 1, a)  q 1,, S) = (q 1, b)  q 1,, S) = (q 1, ) –Matching transitions  q 1, a, a) = (q 1, )  q 1, b, b) = (q 1, )

10 Palindrome Transition Table Transition Current Input Top of Next Stack Number State Symbol Stack State Update q 0 Z q 1 SZ 2 q 1 Z q 2 Z 3 q 1 S q 1 aSa 4 q 1 S q 1 bSb 5 q 1 S q 1 a 6 q 1 S q 1 b 7 q 1 S q 1 8 q 1 a a q 1 9 q 1 b b q 1

11 Partial Computation Tree (q 0, aba, Z) (q 1, aba, SZ) (q 1, aba, aSaZ) (other branches not shown) (q 1, ba, SaZ) (q 1, ba, baZ) (other branches not shown) (q 1, a, aZ) (q 1,, Z) (q 2,, Z) On your own, draw computation trees for other strings not in the language and see that they are not accepted.

12 {a n b n | n >= 0} Grammar G: –V = {S} –  = {a,b} –S = S –P: S --> aSb | Output PDA M=(Q, ,q 0,Z,F,  ) – Q = {q 0, q 1, q 2 } –  = {a,b,S,Z} – q 0 = q 0 – Z = Z – F = {q 2 }  : –  (q 0,, Z) = (q 1, SZ) –  (q 1,, Z) = (q 2, Z) –Production Transitions  q 1,, S) = (q 1, aSb)  q 1,, S) = (q 1, ) –Matching transitions  q 1, a, a) = (q 1, )  q 1, b, b) = (q 1, )

13 {a n b n | n >= 0} Transition Table Transition Current Input Top of Next Stack Number State Symbol Stack State Update q 0 Z q 1 SZ 2 q 1 Z q 2 Z 3 q 1 S q 1 aSb 4 q 1 S q 1 5 q 1 a a q 1 6 q 1 b b q 1

14 Partial Computation Tree (q 0, aabb, Z) (q 1, aabb, SZ) (q 1, aabb, aSbZ) (other branch not shown) (q 1, abb, SbZ) (q 1, abb, aSbbZ) (other branch not shown) (q 1, bb, SbbZ) (q 1, bb, bbZ) (other branch not shown) (q 1, b, bZ) (q 1,, Z) (q 2,, Z)

15 {a i b j | i = j or i = 2j} Grammar G: –V = {S,T,U} –  = {a,b} –S = S –P: S --> T | U T --> aTb | U --> aaUb | Output PDA M=(Q, ,q 0,Z,F,  ) – Q = {q 0, q 1, q 2 } –  = {a,b,S,T,U,Z} – q 0 = q 0 – Z = Z – F = {q 2 }  –  (q 0,, Z) = (q 1, SZ) –  (q 1,, Z) = (q 2, Z) –Production Transitions  q 1,, S) = (q 1, T)  q 1,, S) = (q 1, U)  q 1,, T) = (q 1, aTb)  q 1,, T) = (q 1, )  q 1,, U) = (q 1, aaUb)  q 1,, U) = (q 1, ) –Matching transitions  q 1, a, a) = (q 1, )  q 1, b, b) = (q 1, )

16 {a i b j | i = j or i = 2j} Transition Table Transition Current Input Top of Next Stack Number State Symbol Stack State Update q 0 Z q 1 SZ 2 q 1 Z q 2 Z 3 q 1 S q 1 T 4 q 1 S q 1 U 5 q 1 T q 1 aTb 6 q 1 T q 1 7 q 1 U q 1 aaUb 8 q 1 U q 1 9 q 1 a a q 1 10 q 1 b b q 1

17 Partial Computation Tree (q 0, aab, Z) (q 1, aab, SZ) (q 1, aab, UZ) (other branch not shown) (q 1, aab, aaUbZ) (other branch not shown) (q 1, ab, aUbZ) (q 1, b, UbZ) (q 1, b, bZ) (other branch not shown) (q 1,, Z) (q 2,, Z)

18 Things you should be able to do You should be able to execute this algorithm –Given any CFG, construct an equivalent PDA You should understand the idea behind this algorithm –Derive string on stack and then match it against input You should understand how this construction can help you design PDA’s You should understand that it can be used in many-one reductions between decision problems about CFL’s.