cs 3240 chapter 7 l.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Pushdown Automata PowerPoint Presentation
Download Presentation
Pushdown Automata

Loading in 2 Seconds...

play fullscreen
1 / 57

Pushdown Automata - PowerPoint PPT Presentation


  • 268 Views
  • Uploaded on

CS 3240 – Chapter 7. Pushdown Automata. Where Are We?. A Pushdown Automaton Machine for Context-Free Languages. Take an FA and add a stack A restricted form of unbounded memory Operations: Pop Push Ignore ( λ ) (Pop always precedes push). PDA Operations.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Pushdown Automata' - hamlin


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
where are we
Where Are We?

CS 3240 - Introduction

a pushdown automaton machine for context free languages
A Pushdown AutomatonMachine for Context-Free Languages
  • Take an FA and add a stack
    • A restricted form of unbounded memory
  • Operations:
    • Pop
    • Push
    • Ignore (λ)
    • (Pop always precedes push)

CS 3240 - Pushdown Automata

pda operations
PDA Operations
  • Three items per transition edge
    • <read>,<pop>,<push>
    • any or all could be λ
  • A PDA accepts when:
    • An accepting state is reached, and
    • The stack is empty
      • Different from book!
  • The machine crashes when no move is possible
    • implicit jail

CS 3240 - Pushdown Automata

a pda for a n b n n 0
A PDA for anbn (n > 0)

A deterministic PDA (no choices; λ is okay sometimes). The stack alphabet (Γ = {X}) can differ from the input alphabet (Σ = {a, b}) .

Trace aabb…

CS 3240 - Pushdown Automata

tracing the pda state input stack
Tracing the PDA(<state> | <input> | <stack>)

q0 aabb λ

q0 abb X

q0 bb XX

q1 b X

q1 λ λ

Traditional notation (see book):

(q0,aabb, λ) ⊢ (q0,abb,X) ⊢ (q0,bb,XX) ⊢ (q1,b,X) ⊢ (q1,λ, λ)

Also: (q0,aabb,λ) ⊢*(q1,λ, λ) (⇒ aabb ∈ L)

CS 3240 - Pushdown Automata

a pda for a n b n n 07
A PDA for anbn (n ≥ 0)

CS 3240 - Pushdown Automata

a pda for a n b 2n n 0
A PDA for anb2n (n ≥ 0)

How would you do a2nbn?

CS 3240 - Pushdown Automata

a 2n b n
a2nbn

Each b must pop 2 X’s

CS 3240 - Pushdown Automata

a pda for wcw r
A PDA for wcwR

Is this deterministic?

Trace the string abaacaaba.

CS 3240 - Pushdown Automata

transition functions for pdas
Transition Functions for PDAs
  • Input: (state-1, input character, pop character)
  • Output: (state-2, push character(s))
  • Example: The edge (a,λ,a) on previous slide: δ(q0,a,λ) = (q0,a)
  • Exercise:
    • Rewrite wcwR in functional form

CS 3240 - Pushdown Automata

a pda for all palindromes
A PDA for All Palindromes

Is this deterministic?

Trace the string abaaaaaba.

Trace the string abaabaaba.

Trace the string abaaaaba.

CS 3240 - Pushdown Automata

a pda for a m b n m n 2m non deterministic
A PDA for ambn, m ≤ n ≤ 2mNon-deterministic

The grammar:

S → aSb | aSbb | λ

Trace aaabbbb

CS 3240 - Pushdown Automata

a pda for equal
A PDA for Equal

Trace ababbaab

CS 3240 - Pushdown Automata

a pda for n b n a 1
A PDA for nb = na + 1

CS 3240 - Pushdown Automata

a pda for twice b n b 2n a
A PDA for Twicebnb = 2na
  • Reasoning: There must be 2 b’s for every a. When we read an a, there are three possibilities:
  • Its 2 b’s have already been read
  • 1 b has been read and 1 remains
  • Both b’s are yet to be read

Try abababbbb.

How would you do Twicea?

CS 3240 - Pushdown Automata

what language is this
What Language Is This?

CS 3240 - Pushdown Automata

a i b j c k i j k 0 i j or i k
aibjck, i,j,k ≥ 0, i = jor i = k

CS 3240 - Pushdown Automata

a n or a n b n
anor anbn

CS 3240 - Pushdown Automata

formal definition of pdas
Formal Definition of PDAs
  • Q: set of states
  • ∑: input alphabet
  • Γ: stack alphabet
  • δ: Q x (∑∪λ) x (Γ∪λ) →finite subsets of Q x Γ*
  • q0: start state
  • F ⊆ Q: final states
  • (z ⋲ Γ: optional stack start symbol)

CS 3240 - Pushdown Automata

using stack start symbols
Using Stack Start Symbols
  • Not strictly necessary
    • Can serve as a “bottom marker” to allow detection of an empty stack
  • A convenience
    • Can make things easier to design/understand
    • Sometimes handy when seeking an associated CFG
    • Can eliminate some lambda pops
      • Handy later

CS 3240 - Pushdown Automata

a n b n with a start symbol
anbn with a Start Symbol

CS 3240 - Pushdown Automata

a pda for equal using a stack start symbol almost deterministic
A PDA for EqualUsing a Stack Start Symbol (“Almost” Deterministic)

CS 3240 - Pushdown Automata

xx r even palindrome
xxR (Even Palindrome)

CS 3240 - Pushdown Automata

a n b m n m 2n
anbm, n ≤ m≤ 2n

CS 3240 - Pushdown Automata

pdas and context free languages section 7 2
PDAs and Context-Free LanguagesSection 7.2
  • How do we know that PDAs are the correct machine model for CFLs?
  • 1) Must show that for any CFG, there is a PDA that accepts its language
  • 2) Must show that for any PDA, there is a CFG that generates its language
  • The first case is easier than the second!
  • It can be handy to go back and forth

CS 3240 - Pushdown Automata

from greibach form to pda
From Greibach Form to PDA
  • The leading character is read
  • The LHS variable is popped
  • The RHS string is pushed
  • Initial transition pushes S
  • See next slide

CS 3240 - Pushdown Automata

cfg to pda example from greibach form
CFG to PDA ExampleFrom Greibach Form

CS 3240 - Pushdown Automata

cfg to pda example from greibach form ii
CFG to PDA ExampleFrom Greibach Form II

CS 3240 - Pushdown Automata

cfg pda general case
CFG => PDAGeneral Case
  • Very trivial (Yay!)
  • But the (2-state) PDA is non-deterministic
    • State-2 is a final state
  • 1) Push S on the stack; move to state-2
  • 2) For every rule, have a transition that:
    • pops the left-hand side and pushes the right-hand side
  • 3) For every character, c, in Σ, have a rule that:
    • consumes and pops c simultaneously

CS 3240 - Pushdown Automata

without greibach form for lazy people
Without Greibach FormFor Lazy People

S → aSb | aSbb | λ

Trace aaabbbbb: (q0,aaabbbbb,λ) ⊢ (q1,aaabbbbb,S) ⊢ (q1,aaabbbbb,aSbb) ⊢ (q1,aabbbbb,Sbb) ⊢ (q1,aabbbbb,aSbbbb) ⊢ (q1,abbbbb,Sbbbb) ⊢ (q1,abbbbb,aSbbbbb) ⊢ (q1,bbbbb,Sbbbbb) ⊢

(q1,bbbbb,bbbbb) ⊢*(q1,λ,λ)

CS 3240 - Pushdown Automata

now you do one
Now You Do One

S → aSbSbS | bSaSbS | bSbSaS | λ

And trace bbabaabbb

CS 3240 - Pushdown Automata

one more
One More

S => (S) | SS | λ

Derive ()(()) from the grammar.

Then accept it by the machine.

Then find a one-state DPDA for this language.

CS 3240 - Pushdown Automata

pda cfg
PDA => CFG
  • We need to relate PDA movement to a CFG production rule somehow
  • Key: consuming a character in the PDA corresponds to generating a character in the CFG
  • The stack contents must also be part of all this

CS 3240 - Pushdown Automata

special case
Special Case
  • PDAs of the form below have a natural translation to a CFG
    • the reverse of CFG-to-PDA conversion
    • (conveniently) call the stack start symbol S
    • Translate each transition in state-2 into a rule:
      • c,X,Y ⟺ X → cY (i.e., <pop> → <char> <push>)
      • (X cannot be λ)

CS 3240 - Pushdown Automata

pda to cfg example equal
PDA-to-CFG ExampleEQUAL

S → aXS | bYS | λ

X → aXX | bYX | b

Y → bYY | aXY | a

Derive abbbaaba

CS 3240 - Pushdown Automata

exercise
Exercise

Go back and find grammars for slides 22,24-25.

Also, work backward from the (()) DPDA to a grammar.

CS 3240 - Pushdown Automata

pda cfg general case
PDA => CFGGeneral Case
  • We must also track state changes
  • Our variables must therefore contain state and stack information
  • Very tricky!

CS 3240 - Pushdown Automata

pda cfg continued
PDA => CFGContinued
  • Suppose X is on the stack and ‘a’ is read
  • What can happen to X?
    • It will be popped (and possibly put back)
    • It may be replaced by one or more other stack symbols
      • Which symbols will also later be popped, replaced, etc…
      • The stack grows and shrinks and grows and shrinks …
    • Eventually, as more input is consumed, the effect of having pushed X on the stack must be erased (or we’ll never reach an empty stack!)
    • And the state may change many times!

CS 3240 - Pushdown Automata

observing a pda source aho et al
Observing a PDA(Source: Aho et al)

… (qi, x1x2…xk, Y1Y2…Yk) ⊢*(qj, x2…xk, Y2…Yk) ⊢*(qn, xk, Yk) ⊢*(qf, λ, λ)

CS 3240 - Pushdown Automata

converting from pda to cfg
Converting from PDA to CFG
  • Let the symbol <qAp> represent the sequence of movements in a PDA that:
    • starts in state q
    • eventually ends in state p
    • eventually removes A and its after-effects from the stack
  • The symbol <sλf> then represents accepting a valid string (if s is the start state and f is a final state)
  • These symbols will be our variables
    • Because they track the machine configuration that accepts strings
    • Just as the grammar will generate those strings

CS 3240 - Pushdown Automata

converting from pda to cfg continued
Converting from PDA to CFGcontinued
  • Consider the transition δ(q,a,X) = (p,Y)
    • This means that a is consumed, X is popped, we move directly to state p, and subsequent processing must eventually erase Y and its follow-on effects
  • A corresponding grammar rule is:
    • <qX?> → a<pY?> (?’s represent the same state)
    • We don’t know where we’ll eventually end up when the stack is back to where it was before the X was first pushed
    • All we know is we immediately go throughp
    • So we entertain all possibilities (see 3 and 4 on next slide)

CS 3240 - Pushdown Automata

from transitions to grammar rules 4 types of rules generated
From Transitions to Grammar Rules4 types of rules generated
  • 1) S → <sλf> for all final states, f
  • 2) <qλq> → λ for all states, q
    • These will serve as terminators
  • 3) For transitions δ(q,a,X) = (p,Y):
    • <qXr> → a<pYr> for all states, r
  • 4) For transitions δ(q,a,X) = (p,Y1Y2):
    • <qXr> → a<pY1s><sY2r> for all states, r, s
    • And so on, for longer pushed strings

CS 3240 - Pushdown Automata

preparing a grammar for conversion
Preparing a Grammar for Conversion
  • The process requires:
    • No multiple pops
      • So spread them out over multiple states if needed
  • Also, for every lambda-pop c,λ,X:
    • Add an equivalent rule for each letter of Γ (the stack alphabet) as follows:
      • (c,X,XX)
      • (c,Y,XY)
      • etc.
    • And keep the original lambda pop!!!

CS 3240 - Pushdown Automata

convert pda for a n b n n 0
Convert PDA for anbn (n > 0)
  • Call the start state, s, and the final state, f.
  • Add the following transition on s: (a,X,XX)
  • 1) S → <sλf>
  • 2) <sλs> → λ; <fλf> → λ
  • Now go through each transition…

CS 3240 - Pushdown Automata

converting transitions to rules
Converting Transitions to Rules
  • δ(s,a,λ) = (s,X):
    • <sλs> → a<sXs>
    • <sλf> → a<sXf>
  • δ(s,a,X) = (s,XX):
    • <sXs> → a<sXs><sXs>
    • <sXf> → a<sXs><sXf>
    • <sXs> → a<sXf><fXs>
    • <sXf> → a<sXf><fXf>

CS 3240 - Pushdown Automata

converting transitions to rules continued
Converting Transitions to RulesContinued
  • δ(s,b,X) = (f,λ):
    • <sXs> → b<fλs>
    • <sXf> → b<fλf>
  • δ(f,b,X) = (f,λ):
    • <fXs> → b<fλs>
    • <fXf> → b<fλf>

CS 3240 - Pushdown Automata

what s left
What’s Left?
  • S → <sλf>
  • <sλs> → a<sXs> | λ
  • <fλf> → λ
    • <sλf> → a<sXf>
    • <sXs> → a<sXs><sXs>
    • <sXf> → a<sXs><sXf> | a<sXf><fXf> | b<fλf>
    • <fXf> → b<fλf>

CS 3240 - Pushdown Automata

simplify substitute lambda for f f
SimplifySubstitute lambda for <fλf>
  • S → <sλf>
  • <sλs> → a<sXs> | λ
    • <sλf> → a<sXf>
    • <sXs> → a<sXs><sXs>
    • <sXf> → a<sXs><sXf> | a<sXf><fXf> | b
    • <fXf> → b

CS 3240 - Pushdown Automata

simplify s s is unreachable sxs doesn t terminate
Simplify<sλs> is unreachable; <sXs> doesn’t terminate
  • S → <sλf>
    • <sλf> → a<sXf>
    • <sXf> → a<sXf><fXf> | b
    • <fXf> → b

CS 3240 - Pushdown Automata

simplify eliminate unit production s s f
SimplifyEliminate unit production S → <sλf> → …
  • S → a<sXf>
    • <sXf> → a<sXf><fXf> | b
    • <fXf> → b

CS 3240 - Pushdown Automata

simplify substitute fxf b
SimplifySubstitute <fXf> → b
  • S → a<sXf>
    • <sXf> → a<sXf>b | b

CS 3240 - Pushdown Automata

simplify rename
SimplifyRename
  • S → aX
    • X → aXb | b
    • Done! (finally :-)

CS 3240 - Pushdown Automata

exercise54
Exercise
  • Find a CFG for nb = na+ 1 using the generic conversion process

CS 3240 - Pushdown Automata

deterministic pdas
Deterministic PDAs
  • A PDA is deterministic if there are no choices:
    • Depends on a state’s <character, pop symbol> pair:
    • 1) δ(q,a,X) has only one choice (a could be λ), and
    • 2) If a is not λ, then there is no δ(q,λ,X) edge, and
    • 3) if X is not λ, then there is no δ(q,a,λ) edge.
  • NOTE: Some PDAs are inherently non-deterministic (no deterministic equivalent, e.g., wwR)

CS 3240 - Pushdown Automata

examples
Examples
  • Revisit previous PDAs and check for determinacy

CS 3240 - Pushdown Automata

a hierarchy of languages
A Hierarchy of Languages

Languages accepted bynondeterministic PDA

Languages accepted by DFA

Languages accepted bydeterministic PDA

CS 3240 - Pushdown Automata