Pushdown Automata Chapters 14-18. Generators vs. Recognizers For Regular Languages: –regular expressions are generators –FAs are recognizers For Context-free.

Slides:



Advertisements
Similar presentations
Context-Free and Noncontext-Free Languages
Advertisements

The Pumping Lemma for CFL’s
Theory of Computation CS3102 – Spring 2014 A tale of computers, math, problem solving, life, love and tragic death Nathan Brunelle Department of Computer.
C O N T E X T - F R E E LANGUAGES ( use a grammar to describe a language) 1.
Pushdown Automata CPSC 388 Ellen Walker Hiram College.
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,
Pushdown Automata Chapter 12. Recognizing Context-Free Languages We need a device similar to an FSM except that it needs more power. The insight: Precisely.
CS 3240: Languages and Computation Properties of Context-Free Languages.
Pushdown Automata Part II: PDAs and CFG Chapter 12.
CS21 Decidability and Tractability
1 Introduction to Computability Theory Lecture12: Decidable Languages Prof. Amos Israeli.
Introduction to Computability Theory
1 Introduction to Computability Theory Lecture7: PushDown Automata (Part 1) Prof. Amos Israeli.
Courtesy Costas Busch - RPI1 NPDAs Accept Context-Free Languages.
CS 310 – Fall 2006 Pacific University CS310 Pushdown Automata Sections: 2.2 page 109 October 9, 2006.
CS Master – Introduction to the Theory of Computation Jan Maluszynski - HT Lecture 4 Context-free grammars Jan Maluszynski, IDA, 2007
Deterministic FA/ PDA Sequential Machine Theory Prof. K. J. Hintz Department of Electrical and Computer Engineering Lecture 4 Updated by Marek Perkowski.
January 14, 2015CS21 Lecture 51 CS21 Decidability and Tractability Lecture 5 January 14, 2015.
CS5371 Theory of Computation Lecture 9: Automata Theory VII (Pumping Lemma, Non-CFL)
Normal forms for Context-Free Grammars
Transparency No. P2C5-1 Formal Language and Automata Theory Part II Chapter 5 The Pumping Lemma and Closure properties for Context-free Languages.
CS5371 Theory of Computation Lecture 8: Automata Theory VI (PDA, PDA = CFG)
January 23, 2015CS21 Lecture 81 CS21 Decidability and Tractability Lecture 8 January 23, 2015.
CS 3240 – Chapter 8.  Is a n b n c n context-free? CS Properties of Context-Free Languages2.
January 15, 2014CS21 Lecture 61 CS21 Decidability and Tractability Lecture 6 January 16, 2015.
PZ03A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ03A - Pushdown automata Programming Language Design.
Today Chapter 2: (Pushdown automata) Non-CF languages CFL pumping lemma Closure properties of CFL.
FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY
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.
INHERENT LIMITATIONS OF COMPUTER PROGAMS CSci 4011.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 7 Mälardalen University 2010.
Pushdown Automata.
CSE 3813 Introduction to Formal Languages and Automata Chapter 8 Properties of Context-free Languages These class notes are based on material from our.
TM Design Universal TM MA/CSSE 474 Theory of Computation.
Pushdown Automata (PDAs)
CSCI 2670 Introduction to Theory of Computing September 22, 2005.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 11 Midterm Exam 2 -Context-Free Languages Mälardalen University 2005.
Cs3102: Theory of Computation Class 8: Non-Context-Free Languages Spring 2010 University of Virginia David Evans.
Context-Free and Noncontext-Free Languages Chapter 13 1.
DETERMINISTIC CONTEXT FREE LANGUAGES
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.
1Computer Sciences Department. Book: INTRODUCTION TO THE THEORY OF COMPUTATION, SECOND EDITION, by: MICHAEL SIPSER Reference 3Computer Sciences Department.
PZ03A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ03A - Pushdown automata Programming Language Design.
Non-CF Languages The language L = { a n b n c n | n  0 } does not appear to be context-free. Informal: A PDA can compare #a’s with #b’s. But by the time.
PushDown Automata. What is a stack? A stack is a Last In First Out data structure where I only have access to the last element inserted in the stack.
CS 203: Introduction to Formal Languages and Automata
Foundations of (Theoretical) Computer Science Chapter 2 Lecture Notes (Section 2.2: Pushdown Automata) Prof. Karen Daniels, Fall 2010 with acknowledgement.
Context-Free and Noncontext-Free Languages Chapter 13.
Pumping Lemma for CFLs. Theorem 7.17: Let G be a CFG in CNF and w a string in L(G). Suppose we have a parse tree for w. If the length of the longest path.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 9 Mälardalen University 2006.
Lecture 24: Models of Computation. When attempting to deal with theoretical issues concerning digital computers, we need a simplified definition of what.
CSCI 2670 Introduction to Theory of Computing September 23, 2004.
Chapter 8 Properties of Context-free Languages These class notes are based on material from our textbook, An Introduction to Formal Languages and Automata,
C SC 473 Automata, Grammars & Languages Automata, Grammars and Languages Discourse 04 Context-Free Grammars and Pushdown Automata.
Transparency No. P2C5-1 Formal Language and Automata Theory Part II Chapter 5 The Pumping Lemma and Closure properties for Context-free Languages.
January 20, 2016CS21 Lecture 71 CS21 Decidability and Tractability Lecture 7 January 20, 2016.
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.
Grammar Set of variables Set of terminal symbols Start variable Set of Production rules.
Lecture 6: Context-Free Languages
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,
Lecture 11  2004 SDU Lecture7 Pushdown Automaton.
Context-Free and Noncontext-Free Languages Chapter 13.
Closed book, closed notes
Properties of Context-Free Languages
PDAs Accept Context-Free Languages
Pushdown Automata.
Context-Free Grammars
Deterministic PDAs - DPDAs
Presentation transcript:

Pushdown Automata Chapters 14-18

Generators vs. Recognizers For Regular Languages: –regular expressions are generators –FAs are recognizers For Context-free Languages –CFGs are generators –Pushdown Automata (PDAs) are recognizers

Languages generated by CFGs Languages defined by regular expressions All regular languages can be generated by CFGs and so can some non-regular languages

PDA vs. FA Add a stack Each transition can specify optional push and pop operations –using an independent alphabet –use Λ (or e) to ignore stack operations a,e/A means with input ‘a’, pop nothing, push an ‘A’ Can use accepting states for success –Or can just use an empty stack –We’ll do both simultaneously (usually)

Example: a n b n

Every input ‘a’ pushes a ‘A’ on the stack Every input ‘b’ pops an ‘A’ off the stack –if any other character appears, reject At end of input, the stack should be empty –else the count was off, and we should reject Code: anbn.cpp

Example: PalindromeX Accepts strings of the form wcw R Pops the first half onto the stack We need the middle delimiter to know when to start comparing in reverse –otherwise, the process would be non-deterministic –each popped character should match the corresponding input character Diagram on next slide Code: palindromex.cpp

PalindromeX

Example: EvenPalindrome No delimiter

Determinism vs. Non-determinism Deterministic if: –Only one transition exists for each combination of (q, a, X) = (state, input letter, stack top) –If a = e, then no other rule exists for that q and X Other, multiple moves for the same state/input/stack are possible As long as an acceptable path exists, the machine accepts the input Interesting note: –The class of languages accepted by NPDAs is larger than those accepted by DPDAs!

Languages accepted by nondeterministic PDA Languages accepted by deterministic PDA Languages accepted by FA or NFA or TG PDA more powerful than FA

CFG => PDA Has two states: –start, accepting Have an empty move from the start state to the accepting state that pushes the start non-terminal Cycle on the accepting state: –empty moves that replace variables with each of their rules –moves that consume each terminal

CFG => PDA Example S => e | (S) | SS

Derive (()) Using CFG (do a leftmost-derivation): –S => (S) =>((S)) => (()) PDA (non-deterministically) –do by hand, showing stack at each step

A DPDA for (…) Exercise: accept (( )( ))

CFG vs. PDA Any CFG can be represented by a PDA –But some CFLs require non-determinism unlike NFA’s => FAs => regular expressions –i.e., the languages accepted by DPDAs form a subset of those accepted by NPDAs Any PDA has a corresponding CFG –Lots of work to find!!!

Converting from PDA to CFG A PDA “consumes” a character A CFG “generates” a character We want to relate these two What happens when a PDA consumes a character? –It may change state –It may change the stack

Converting from PDA to CFG continued Suppose X is on the stack and ‘a’ is read What can happen to X? –It can be popped –It may replaced by one or more other stack symbols And so on… The stack grows and shrinks and grows and shrinks … –Eventually, as more input is consumed, the effect of having X on the stack must be erased (or we’ll never reach an empty stack!) –And the state may change many times –We must track all of this! (see picture next slide)

Observing a PDA

Converting from PDA to CFG continued Let the symbol represent the movement in a PDA that starts in state q and ends in state p –This will result in possibly many moves and stack changes –It represents moving from q to p while erasing the net effects of having A on the stack The symbol represents accepting a valid string (if f is a final state) These symbols will be our variables/non-terminals –Because they track the machine configuration that accepts strings –Our grammar will generate those strings

Converting from PDA to CFG continued Consider the transition ((q,a,X),(p,Y)) –This means that a is consumed, X is popped, we move to state p, and subsequent processing must erase Y and its subsequent effects A corresponding grammar rule is: – => a (?’s represent the same state) –We don’t know where we’ll eventually end up –But we know we immediately go through p –So we entertain all possibilities

From Transitions to Grammar Rules 1) S => for all final states, f 2) => λ for all states, q –These serve as terminators 3) For transitions ((q,a,X),(p,Y)): – => a for all states, r 4) For transitions ((q,a,X),(p,Y 1 Y 2 )): – => a for all states, r, s –And so on for longer pushed strings

Theoretical Results Pumping Lemma for CFGs Closure properties –different from regular languages! Decidability –we won’t cover most of this (Chapter 18) –you’ll get the important stuff in Compilers –need determinism to do efficient parsing

Infinite CFLs How can you tell if a CFG generates an infinite language? CFLPL-1.PDF

Parse Trees from CNF What do CNF Parse Trees Look Like? Relate depth of tree to length of possible strings CFLPL-2.PDF CFLPL-3.PDF CFLPL-4.PDF

CNF Parse Trees vs. Strings We want to go the other way: –determine the possible depths of CNF trees from strings of a given length CFLPL-5.PDF CFLPL-6.PDF CFLPL-7.PDF

The Pumping Lemma for CFGs Similar to the one for regular languages Based on self-embedding (a type of loop) –For sufficiently-long strings (≥ p = 2 v ), a non-terminal will be a descendant of itself in the parse Because the language resulting from never reusing non- terminals is finite –leads to repetition properties, similar to loops in FAs Every string of sufficient length from an infinite CFL can be written as uvxyz, and pumped as uv i xy i z, which string is also in the same CFL –|x| > 0, |v| + |y| > 0, |vxy| <= p (= 2 v )

a n b n a n is not context-free Intuitively: You’ve already used up the stack to coordinate the a n b n prefix Must consider all cases for a proof –CFLPL-8.PDFCFLPL-8.PDF

ww is not Context Free CFLPL-9.PDF

Closure Properties of CFLs CFLs are closed under union, concatenation, and Kleene Star CFLs are not closed under intersection or complement! But the intersection of a CFL and a Regular language is a CFL

Union of CFLs Let S 1 be the start symbol for L 1, and S 2 for L 2 Just have a new start symbol point to the OR of the old ones: S => S 1 | S 2 S 1 => … S 2 => …

Concatenation of CFLs S => S 1 S 2 S 1 => … S 2 => …

Kleene Star of CFLs Rename the old start non-terminal to S 1 S => S 1 S | Λ S 1 => …

Intersection of CFLs Let L 1 = a n b n a m Let L 2 = a n b m a m (The CFGs for the above are on page 385) L 1 ∩ L 2 = a n b n a n –We already showed this is not context free

Complement of CFLs Proof by contradiction, derived from the result of intersection, because: L 1 ∩ L 2 = (L 1 ' + L 2 ')' Since the intersection is not closed, but union is, then the complement cannot be.

Complements of DCFLs These are closed under complement Just invert the acceptability conditions, similar to FAs –String is in L' if either an accept state is not reached or the stack is not empty So, you would think that DCFLs are also closed under intersection, but they’re not, because…

DCFLs not Closed under Union! Consider: L 1 = {a i b j c k | i = j} L 2 = {a i b j c k | j = k} Each of these is DCF –(Show this!) The union is not! –It requires non-determinism –It’s CF, but not DCF

Another Interesting Fact DCFLs always have an associated CFG that is unambiguous

Closure Properties of CFLs Summary Closed under Union, Concatenation, Kleene Star Not closed under intersection, complement CFL ∩ Regular = CFL DCFLs are closed under complement –But not union!

Decidability Unanswerable questions Answerable questions

Undecidable Questions Do 2 arbitrary CFGs generate the same language? Is a CFG ambiguous? Is a given CFL’s complement also CF? Is the intersection of 2 given CFLs CF? Do 2 CFLs have a common word?

Decidable Questions Does a CFG generate any words? –Substitute each “terminating production” (RHS is all terminals) throughout and see what happens “back substitution method” Example, page 405 Is a non-terminal ever used? (p ) Is a CFL finite or infinite? (p )

CYK Algorithm Answers the question: “Is this string accepted by this grammar?” –A “dynamic programming” algorithm –Works backwards in stages There are better ways of parsing –Take the compiler class to learn those