31
Pushdown Automata Lecture 21 Section 7.1 Robb T. Koether Hampden-Sydney College Wed, Oct 12, 2016 Robb T. Koether (Hampden-Sydney College) Pushdown Automata Wed, Oct 12, 2016 1 / 27

Pushdown Automata - Lecture 21 Section 7people.hsc.edu/faculty-staff/robbk/Coms461/Lectures... · 2016-10-13 · Pushdown Automata Lecture 21 Section 7.1 Robb T. Koether Hampden-Sydney

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Pushdown Automata - Lecture 21 Section 7people.hsc.edu/faculty-staff/robbk/Coms461/Lectures... · 2016-10-13 · Pushdown Automata Lecture 21 Section 7.1 Robb T. Koether Hampden-Sydney

Pushdown AutomataLecture 21Section 7.1

Robb T. Koether

Hampden-Sydney College

Wed, Oct 12, 2016

Robb T. Koether (Hampden-Sydney College) Pushdown Automata Wed, Oct 12, 2016 1 / 27

Page 2: Pushdown Automata - Lecture 21 Section 7people.hsc.edu/faculty-staff/robbk/Coms461/Lectures... · 2016-10-13 · Pushdown Automata Lecture 21 Section 7.1 Robb T. Koether Hampden-Sydney

Outline

1 Machines for CFGs

2 Pushdown Automata

3 ExamplesBalanced ParenthesesAlgebraic Expressions

4 Assignment

Robb T. Koether (Hampden-Sydney College) Pushdown Automata Wed, Oct 12, 2016 2 / 27

Page 3: Pushdown Automata - Lecture 21 Section 7people.hsc.edu/faculty-staff/robbk/Coms461/Lectures... · 2016-10-13 · Pushdown Automata Lecture 21 Section 7.1 Robb T. Koether Hampden-Sydney

Outline

1 Machines for CFGs

2 Pushdown Automata

3 ExamplesBalanced ParenthesesAlgebraic Expressions

4 Assignment

Robb T. Koether (Hampden-Sydney College) Pushdown Automata Wed, Oct 12, 2016 3 / 27

Page 4: Pushdown Automata - Lecture 21 Section 7people.hsc.edu/faculty-staff/robbk/Coms461/Lectures... · 2016-10-13 · Pushdown Automata Lecture 21 Section 7.1 Robb T. Koether Hampden-Sydney

Grammars vs. Machines

Definition (Context-free language)A context-free language (CFL) is the language L(G) of a context-freegrammar G.

Given a regular language, we can describe it by using a regulargrammar or a machine (a DFA).A context-free language can be described by using a context-freegrammar.Can it also be described by a machine?

Robb T. Koether (Hampden-Sydney College) Pushdown Automata Wed, Oct 12, 2016 4 / 27

Page 5: Pushdown Automata - Lecture 21 Section 7people.hsc.edu/faculty-staff/robbk/Coms461/Lectures... · 2016-10-13 · Pushdown Automata Lecture 21 Section 7.1 Robb T. Koether Hampden-Sydney

Machines for CFLs

If a machine is to process the string anbn, then it must be able to“remember” the number of a’s.We will use a stack to do this.As each a is read, push it onto the stack.When b is read, pop an a.When we are finished reading the string, the stack should beempty.

Robb T. Koether (Hampden-Sydney College) Pushdown Automata Wed, Oct 12, 2016 5 / 27

Page 6: Pushdown Automata - Lecture 21 Section 7people.hsc.edu/faculty-staff/robbk/Coms461/Lectures... · 2016-10-13 · Pushdown Automata Lecture 21 Section 7.1 Robb T. Koether Hampden-Sydney

Machines for CFLs

Each transition will include five parts.The symbol read.The symbol popped.The string pushed.Two states (the state we go from and the state we go to).

Such a machine is called a push-down automaton (PDA).

Robb T. Koether (Hampden-Sydney College) Pushdown Automata Wed, Oct 12, 2016 6 / 27

Page 7: Pushdown Automata - Lecture 21 Section 7people.hsc.edu/faculty-staff/robbk/Coms461/Lectures... · 2016-10-13 · Pushdown Automata Lecture 21 Section 7.1 Robb T. Koether Hampden-Sydney

Machines for CFLs

The symbol read and the string pushed could be λ, but we mustpop exactly one symbol from the stack.

Them’s the rules.The current state and the symbols read and popped serve as“input.”The destination state and the string pushed serve as “output.”PDAs are inherently nondeterministic. They are sometimes calledNPDAs.There are also deterministic PDAs, called DPDAs.

Robb T. Koether (Hampden-Sydney College) Pushdown Automata Wed, Oct 12, 2016 7 / 27

Page 8: Pushdown Automata - Lecture 21 Section 7people.hsc.edu/faculty-staff/robbk/Coms461/Lectures... · 2016-10-13 · Pushdown Automata Lecture 21 Section 7.1 Robb T. Koether Hampden-Sydney

Machines for CFLs

The symbol read and the string pushed could be λ, but we mustpop exactly one symbol from the stack. Them’s the rules.

The current state and the symbols read and popped serve as“input.”The destination state and the string pushed serve as “output.”PDAs are inherently nondeterministic. They are sometimes calledNPDAs.There are also deterministic PDAs, called DPDAs.

Robb T. Koether (Hampden-Sydney College) Pushdown Automata Wed, Oct 12, 2016 7 / 27

Page 9: Pushdown Automata - Lecture 21 Section 7people.hsc.edu/faculty-staff/robbk/Coms461/Lectures... · 2016-10-13 · Pushdown Automata Lecture 21 Section 7.1 Robb T. Koether Hampden-Sydney

Machines for CFLs

The symbol read and the string pushed could be λ, but we mustpop exactly one symbol from the stack. Them’s the rules.The current state and the symbols read and popped serve as“input.”The destination state and the string pushed serve as “output.”

PDAs are inherently nondeterministic. They are sometimes calledNPDAs.There are also deterministic PDAs, called DPDAs.

Robb T. Koether (Hampden-Sydney College) Pushdown Automata Wed, Oct 12, 2016 7 / 27

Page 10: Pushdown Automata - Lecture 21 Section 7people.hsc.edu/faculty-staff/robbk/Coms461/Lectures... · 2016-10-13 · Pushdown Automata Lecture 21 Section 7.1 Robb T. Koether Hampden-Sydney

Machines for CFLs

The symbol read and the string pushed could be λ, but we mustpop exactly one symbol from the stack. Them’s the rules.The current state and the symbols read and popped serve as“input.”The destination state and the string pushed serve as “output.”PDAs are inherently nondeterministic. They are sometimes calledNPDAs.

There are also deterministic PDAs, called DPDAs.

Robb T. Koether (Hampden-Sydney College) Pushdown Automata Wed, Oct 12, 2016 7 / 27

Page 11: Pushdown Automata - Lecture 21 Section 7people.hsc.edu/faculty-staff/robbk/Coms461/Lectures... · 2016-10-13 · Pushdown Automata Lecture 21 Section 7.1 Robb T. Koether Hampden-Sydney

Machines for CFLs

The symbol read and the string pushed could be λ, but we mustpop exactly one symbol from the stack. Them’s the rules.The current state and the symbols read and popped serve as“input.”The destination state and the string pushed serve as “output.”PDAs are inherently nondeterministic. They are sometimes calledNPDAs.There are also deterministic PDAs, called DPDAs.

Robb T. Koether (Hampden-Sydney College) Pushdown Automata Wed, Oct 12, 2016 7 / 27

Page 12: Pushdown Automata - Lecture 21 Section 7people.hsc.edu/faculty-staff/robbk/Coms461/Lectures... · 2016-10-13 · Pushdown Automata Lecture 21 Section 7.1 Robb T. Koether Hampden-Sydney

Machine for {anbn | n ≥ 0}

Example (Machine for {anbn | n ≥ 0})

λ, λ → λ

b, a → λa, λ → a

A machine for {anbn | n ≥ 0}: First attempt

Robb T. Koether (Hampden-Sydney College) Pushdown Automata Wed, Oct 12, 2016 8 / 27

Page 13: Pushdown Automata - Lecture 21 Section 7people.hsc.edu/faculty-staff/robbk/Coms461/Lectures... · 2016-10-13 · Pushdown Automata Lecture 21 Section 7.1 Robb T. Koether Hampden-Sydney

Machine for {anbn | n ≥ 0}

Example (Machine for {anbn | n ≥ 0})The first shortcoming is that we reach the final state withoutreading any b’s.Thus, this machine would accept, for example, aaaa, aaaab, andaaaabb, as well as aaaabbbb.We need to read all the b’s in one state and them move to a finalstate.

Robb T. Koether (Hampden-Sydney College) Pushdown Automata Wed, Oct 12, 2016 9 / 27

Page 14: Pushdown Automata - Lecture 21 Section 7people.hsc.edu/faculty-staff/robbk/Coms461/Lectures... · 2016-10-13 · Pushdown Automata Lecture 21 Section 7.1 Robb T. Koether Hampden-Sydney

Machine for {anbn | n ≥ 0}

Example (Machine for {anbn | n ≥ 0})A second shortcoming is that not every transition pops exactly onesymbol, as required.To make the initial transition, there must already be a symbol onthe stack.We will designate a special symbol, typically z, to be the startstack symbol.

Robb T. Koether (Hampden-Sydney College) Pushdown Automata Wed, Oct 12, 2016 10 / 27

Page 15: Pushdown Automata - Lecture 21 Section 7people.hsc.edu/faculty-staff/robbk/Coms461/Lectures... · 2016-10-13 · Pushdown Automata Lecture 21 Section 7.1 Robb T. Koether Hampden-Sydney

Machine for {anbn | n ≥ 0}

Example (Machine for {anbn | n ≥ 0})

λ, a → a

b, a → λa, z → aza, a → aa

λ, z → λ

A mahcine for {anbn | n ≥ 0}: Second attempt

Robb T. Koether (Hampden-Sydney College) Pushdown Automata Wed, Oct 12, 2016 11 / 27

Page 16: Pushdown Automata - Lecture 21 Section 7people.hsc.edu/faculty-staff/robbk/Coms461/Lectures... · 2016-10-13 · Pushdown Automata Lecture 21 Section 7.1 Robb T. Koether Hampden-Sydney

Machine for {anbn | n ≥ 0}

Example (Machine for {anbn | n ≥ 0})This machine still has one shortcoming.If we require the transition λ,a→ a to move from the first state tothe second state, then the machine will not accept λ.To remedy this, we could add a second possibility: λ, z→ z, but itwould be simpler just to add a transition λ, z→ z directly from thestart state to the final state.

Robb T. Koether (Hampden-Sydney College) Pushdown Automata Wed, Oct 12, 2016 12 / 27

Page 17: Pushdown Automata - Lecture 21 Section 7people.hsc.edu/faculty-staff/robbk/Coms461/Lectures... · 2016-10-13 · Pushdown Automata Lecture 21 Section 7.1 Robb T. Koether Hampden-Sydney

Machine for {anbn | n ≥ 0}

Example (Machine for {anbn | n ≥ 0})

λ, a → a

b, a → λa, z → aza, a → aa

λ, z → λ

λ, z → λ

A machine for {anbn | n ≥ 0}: Final attempt

Robb T. Koether (Hampden-Sydney College) Pushdown Automata Wed, Oct 12, 2016 13 / 27

Page 18: Pushdown Automata - Lecture 21 Section 7people.hsc.edu/faculty-staff/robbk/Coms461/Lectures... · 2016-10-13 · Pushdown Automata Lecture 21 Section 7.1 Robb T. Koether Hampden-Sydney

Outline

1 Machines for CFGs

2 Pushdown Automata

3 ExamplesBalanced ParenthesesAlgebraic Expressions

4 Assignment

Robb T. Koether (Hampden-Sydney College) Pushdown Automata Wed, Oct 12, 2016 14 / 27

Page 19: Pushdown Automata - Lecture 21 Section 7people.hsc.edu/faculty-staff/robbk/Coms461/Lectures... · 2016-10-13 · Pushdown Automata Lecture 21 Section 7.1 Robb T. Koether Hampden-Sydney

Pushdown Automaton

Definition (Pushdown automaton)A pushdown automaton, abbreviated PDA, is a septuple(Q,Σ, Γ, δ,q0, z,F ), where

Q is a finite set of states.Σ is a finite input alphabet.Γ is a finite stack alphabet.δ : Q × (Σ ∪ {λ})× Γ→ P ′(Q × Γ∗) is the transition function.q0 ∈ Q is the start state.z ∈ Γ is the start stack symbol.F ⊆ Q is the set of accept states.

where P ′ means the set of finite subsets.

Robb T. Koether (Hampden-Sydney College) Pushdown Automata Wed, Oct 12, 2016 15 / 27

Page 20: Pushdown Automata - Lecture 21 Section 7people.hsc.edu/faculty-staff/robbk/Coms461/Lectures... · 2016-10-13 · Pushdown Automata Lecture 21 Section 7.1 Robb T. Koether Hampden-Sydney

Example

Example (PDA for {anbn | n ≥ 0})The PDA that accepts

{anbn | n ≥ 0}

isQ = {q0,q1,q2}Σ = {a,b}Γ = {a, z}F = {q2}

Robb T. Koether (Hampden-Sydney College) Pushdown Automata Wed, Oct 12, 2016 16 / 27

Page 21: Pushdown Automata - Lecture 21 Section 7people.hsc.edu/faculty-staff/robbk/Coms461/Lectures... · 2016-10-13 · Pushdown Automata Lecture 21 Section 7.1 Robb T. Koether Hampden-Sydney

Example

Example (PDA for {anbn | n ≥ 0})and δ is given by

δ(q0,a, z) = {(q0,az)}δ(q0,a,a) = {(q0,aa)}δ(q0, λ, z) = {(q2, λ)}δ(q0, λ,a) = {(q1,a)}δ(q1,b,a) = {(q1, λ)}δ(q1, λ, z) = {(q2, λ)}

Robb T. Koether (Hampden-Sydney College) Pushdown Automata Wed, Oct 12, 2016 17 / 27

Page 22: Pushdown Automata - Lecture 21 Section 7people.hsc.edu/faculty-staff/robbk/Coms461/Lectures... · 2016-10-13 · Pushdown Automata Lecture 21 Section 7.1 Robb T. Koether Hampden-Sydney

Outline

1 Machines for CFGs

2 Pushdown Automata

3 ExamplesBalanced ParenthesesAlgebraic Expressions

4 Assignment

Robb T. Koether (Hampden-Sydney College) Pushdown Automata Wed, Oct 12, 2016 18 / 27

Page 23: Pushdown Automata - Lecture 21 Section 7people.hsc.edu/faculty-staff/robbk/Coms461/Lectures... · 2016-10-13 · Pushdown Automata Lecture 21 Section 7.1 Robb T. Koether Hampden-Sydney

Example

Example (Pushdown automaton)Design a PDA that accepts the language

{w | w contains an equal number of a’s and b’s}.

Robb T. Koether (Hampden-Sydney College) Pushdown Automata Wed, Oct 12, 2016 19 / 27

Page 24: Pushdown Automata - Lecture 21 Section 7people.hsc.edu/faculty-staff/robbk/Coms461/Lectures... · 2016-10-13 · Pushdown Automata Lecture 21 Section 7.1 Robb T. Koether Hampden-Sydney

Example

Example (Pushdown automaton)The strategy will be to keep the excess symbols, either a’s or b’s,on the stack.One state will represent an excess of a’s.Another state will represent an excess of b’s.We can tell when the excess switches from one symbol to theother because at that point the stack will be empty.In fact, when the stack is empty, we may return to the start state.

Robb T. Koether (Hampden-Sydney College) Pushdown Automata Wed, Oct 12, 2016 20 / 27

Page 25: Pushdown Automata - Lecture 21 Section 7people.hsc.edu/faculty-staff/robbk/Coms461/Lectures... · 2016-10-13 · Pushdown Automata Lecture 21 Section 7.1 Robb T. Koether Hampden-Sydney

Example

Example (Pushdown automaton)

a, z → azb, a → λ

b, b → bb

λ, z → λ

λ, z → λ

b, z → bza, b → λ

a, a → aa

Robb T. Koether (Hampden-Sydney College) Pushdown Automata Wed, Oct 12, 2016 21 / 27

Page 26: Pushdown Automata - Lecture 21 Section 7people.hsc.edu/faculty-staff/robbk/Coms461/Lectures... · 2016-10-13 · Pushdown Automata Lecture 21 Section 7.1 Robb T. Koether Hampden-Sydney

Outline

1 Machines for CFGs

2 Pushdown Automata

3 ExamplesBalanced ParenthesesAlgebraic Expressions

4 Assignment

Robb T. Koether (Hampden-Sydney College) Pushdown Automata Wed, Oct 12, 2016 22 / 27

Page 27: Pushdown Automata - Lecture 21 Section 7people.hsc.edu/faculty-staff/robbk/Coms461/Lectures... · 2016-10-13 · Pushdown Automata Lecture 21 Section 7.1 Robb T. Koether Hampden-Sydney

Examples

Example (Pushdown automata)Let Σ = {a, (, )}. Design a PDA whose language is

{w ∈ Σ∗ | w contains balanced parentheses}.

Robb T. Koether (Hampden-Sydney College) Pushdown Automata Wed, Oct 12, 2016 23 / 27

Page 28: Pushdown Automata - Lecture 21 Section 7people.hsc.edu/faculty-staff/robbk/Coms461/Lectures... · 2016-10-13 · Pushdown Automata Lecture 21 Section 7.1 Robb T. Koether Hampden-Sydney

Outline

1 Machines for CFGs

2 Pushdown Automata

3 ExamplesBalanced ParenthesesAlgebraic Expressions

4 Assignment

Robb T. Koether (Hampden-Sydney College) Pushdown Automata Wed, Oct 12, 2016 24 / 27

Page 29: Pushdown Automata - Lecture 21 Section 7people.hsc.edu/faculty-staff/robbk/Coms461/Lectures... · 2016-10-13 · Pushdown Automata Lecture 21 Section 7.1 Robb T. Koether Hampden-Sydney

Examples

Example (Pushdown automata)Let Σ = {a,b,c,+,×, (, )}. Design a PDA whose language is

{w ∈ Σ∗ | w is a valid algebraic expression}.

Robb T. Koether (Hampden-Sydney College) Pushdown Automata Wed, Oct 12, 2016 25 / 27

Page 30: Pushdown Automata - Lecture 21 Section 7people.hsc.edu/faculty-staff/robbk/Coms461/Lectures... · 2016-10-13 · Pushdown Automata Lecture 21 Section 7.1 Robb T. Koether Hampden-Sydney

Outline

1 Machines for CFGs

2 Pushdown Automata

3 ExamplesBalanced ParenthesesAlgebraic Expressions

4 Assignment

Robb T. Koether (Hampden-Sydney College) Pushdown Automata Wed, Oct 12, 2016 26 / 27

Page 31: Pushdown Automata - Lecture 21 Section 7people.hsc.edu/faculty-staff/robbk/Coms461/Lectures... · 2016-10-13 · Pushdown Automata Lecture 21 Section 7.1 Robb T. Koether Hampden-Sydney

Assignment

AssignmentSection 7.1 Exercises 1, 3, 6bcdfj.

Robb T. Koether (Hampden-Sydney College) Pushdown Automata Wed, Oct 12, 2016 27 / 27