330 likes | 446 Views
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 FOR COMPLETE COVERAGE. Project 1. Pushdown Automata. Similar to finite automata, but for CFG’s
E N D
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 FOR COMPLETE COVERAGE
Pushdown Automata • Similar to finite automata, but for CFG’s • Finite automata are not adequate for CFG’s because we cannot keep track of what we’ve done • At any point, we only know the current state, not previous states • Need memory • PDA’s are finite automata with a stack
a a b b a a b b x y z FA and PDA Schematics State control FA State control PDA Stack: Infinite LIFO (last in first out) device
Formal Definition of PDA • A pushdown automaton is a 6-tuple (Q, Σ, , , q0, F), where Q, Σ, , and F are all finite sets, and • Q is the set of states • Σ is the set of input alphabet • is the stack alphabet • :Q×Σ× ,P(Q×) is the transition function • q0 is the start state and • FQ is the set of accept states
How does PDA work? • Start in start state • Read input character, perform state transition and update stack • Clone a machine if multiple transitions are possible • At end of input if any machine accepts and the stack is empty then accept
Example read ε & pop $ off stack read ε & push $ on stack read ε & push ε on stack read 0 & push 0 on stack read 1 & pop 0 off stack Language accepted: {0n1n | n 0}
Example ε,SSS ε,S(S) ε,S() • Read (()()) S $ ε,ε S$ ε,$ε qstart qloop qaccept (,(ε ),)ε
Example ( S ) $ ε,SSS ε,S(S) ε,S() • Read (()()) ε,ε S$ ε,$ε qstart qloop qaccept (,(ε ),)ε
Example S ) $ ε,SSS ε,S(S) ε,S() • Read (()()) ε,ε S$ ε,$ε qstart qloop qaccept (,(ε ),)ε (
Example SS ) $ ε,SSS ε,S(S) ε,S() • Read (()()) ε,ε S$ ε,$ε qstart qloop qaccept (,(ε ),)ε (
Example ( ) S ) $ ε,SSS ε,S(S) ε,S() • Read (()()) ε,ε S$ ε,$ε qstart qloop qaccept (,(ε ),)ε (
Example ) S ) $ ε,SSS ε,S(S) ε,S() • Read (()()) ε,ε S$ ε,$ε qstart qloop qaccept (,(ε ),)ε ((
Example S ) $ ε,SSS ε,S(S) ε,S() • Read (()()) ε,ε S$ ε,$ε qstart qloop qaccept (,(ε ),)ε (()
( ) ) $ Example ε,SSS ε,S(S) ε,S() • Read (()()) ε,ε S$ ε,$ε qstart qloop qaccept (,(ε ),)ε (()
) ) $ Example ε,SSS ε,S(S) ε,S() • Read (()()) ε,ε S$ ε,$ε qstart qloop qaccept (,(ε ),)ε (()(
) $ Example ε,SSS ε,S(S) ε,S() • Read (()()) ε,ε S$ ε,$ε qstart qloop qaccept (,(ε ),)ε (()()
$ Example ε,SSS ε,S(S) ε,S() • Read (()()) ε,ε S$ ε,$ε qstart qloop qaccept (,(ε ),)ε (()())
Example ε,SSS ε,S(S) ε,S() • Read (()()) ε,ε S$ ε,$ε qstart qloop qaccept (,(ε ),)ε (()())
More Examples • L= {aibjck, i=j or j=k>, and i,j,k>=0}. • L= {wwR | w in {0,1}* }
Differences between PDA’s and NFA’s • Transitions read one symbol of the string and push a string onto or pop a symbol off of the stack • Stack alphabet is not necessarily the same as the alphabet for the language • e.g., $ marks bottom of stack in previous (0n1n) example
Equivalence of PDA’s and CFG’s Theorem: A language is context free if and only if some pushdown automaton accepts it by e.s. Proved in two lemmas – one for the “if” direction and one for the “only if” direction We will only do the “only if” step – i.e., show that every context-free language has an associated PDA
CFG’s are recognized by PDA’s Lemma: If a language is context free, then some pushdown automaton accepts it by emp.st. Proof idea: Construct a PDA following CFG rules
Constructing the PDA • You can read any symbol in when that symbol is at the top of the stack • Transitions of the form a,aε • The rules will be pushed onto the stack – when a variable A is on top of the stack and there is a rule Aw, you pop A and push w • You accept only if the input is exhausted and the stack is empty
State control State control a b a b x B z t A t Idea of PDA construction for AxBz