38
Pushdown Automata Part I: PDAs Chapter 12 1

Pushdown Automata Part I: PDAs Chapter 12 1. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2)

Embed Size (px)

Citation preview

Page 1: Pushdown Automata Part I: PDAs Chapter 12 1. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2)

Pushdown Automata

Part I: PDAs

Chapter 121

Page 2: Pushdown Automata Part I: PDAs Chapter 12 1. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2)

Recognizing Context-Free Languages

Two notions of recognition:(1) Say yes or no, just like with FSMs

(2) Say yes or no, AND

if yes, describe the structure

a + b * c

2

Page 3: Pushdown Automata Part I: PDAs Chapter 12 1. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2)

Just Recognizing

We need a device similar to an FSM except that it needs more power.

The insight: Precisely what it needs is a stack, which gives it an unlimited amount of memory with a restricted structure.

Example: Bal (the balanced parentheses language)

(((()))

3

Page 4: Pushdown Automata Part I: PDAs Chapter 12 1. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2)

Definition of a Pushdown Automaton

M = (K, , , , s, A), where: K is a finite set of states is the input alphabet is the stack alphabet s K is the initial state A K is the set of accepting states, and is the transition relation. It is a finite subset of

(K ( {}) *) (K *)

state input or string of state string ofsymbols

symbolsto pop to

pushfrom top on

top of stack of

stack

4

Page 5: Pushdown Automata Part I: PDAs Chapter 12 1. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2)

Definition of a Pushdown Automaton

A configuration of M is an element of K * *.

The initial configuration of M is (s, w, ).

K : Current state

* : Input left to read

* : Stack content

Compare to the configuration of

FSM

5

Page 6: Pushdown Automata Part I: PDAs Chapter 12 1. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2)

Manipulating the Stack

c will be written as cab

a

b

If c1c2…cn is pushed onto the stack:

c1

c2

cn

cab c1c2…cncab

6

Page 7: Pushdown Automata Part I: PDAs Chapter 12 1. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2)

Yields

Let c be any element of {}, Let 1, 2 and be any elements of *, and Let w be any element of *.

Then, yields-in-one-step |-M is defined as (q1, cw, 1) |-M (q2, w, 2) iff ((q1, c, 1), (q2, 2)) .

Let |-M* be the reflexive, transitive closure of |-M.

C1 yields configuration C2 iff C1 |-M* C2

7

q1 q2

c/1/2If ((q1, c, 1), (q2, 2)) ,

Page 8: Pushdown Automata Part I: PDAs Chapter 12 1. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2)

Computations

A computation by M is a finite sequence of configurations C0, C1, …, Cn for some n 0 such that:

● C0 is an initial configuration,

● Cn is of the form (q, , ), for some state q KM and some string in *, and

● C0 |-M C1 |-M C2 |-M … |-M Cn.

8

Page 9: Pushdown Automata Part I: PDAs Chapter 12 1. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2)

Nondeterminism

If M is in some configuration (q1, s, ) it is possible that:

● contains exactly one transition that matches.

● contains more than one transition that matches.

● contains no transition that matches.

9

Page 10: Pushdown Automata Part I: PDAs Chapter 12 1. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2)

Accepting A computation C of M is an accepting computation iff:

● C = (s, w, ) |-M* (q, , ), and ● q A. M accepts a string w iff at least one of its computations accepts. Other paths may: ● Read all the input and halt in a nonaccepting state, ● Read all the input and halt in an accepting state with the stack not empty, ● Loop forever and never finish reading the input, or ● Reach a dead end where no more input can be read.

The language accepted by M, denoted L(M), is the set of all strings accepted by M.

10

Page 11: Pushdown Automata Part I: PDAs Chapter 12 1. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2)

Rejecting

A computation C of M is a rejecting computation iff:

● C = (s, w, ) |-M* (q, w, ), ● C is not an accepting computation, and ● M has no moves that it can make from (q, w, ).

M rejects a string w iff all of its computations reject.

So note that it is possible that, on input w, M neither accepts nor rejects.

11

Page 12: Pushdown Automata Part I: PDAs Chapter 12 1. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2)

A PDA for Balanced Parentheses

12

Page 13: Pushdown Automata Part I: PDAs Chapter 12 1. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2)

A PDA for Balanced Parentheses

M = (K, , , , s, A), where:K = {s} the states = {(, )} the input alphabet = {(} the stack alphabetA = {s} contains:

((s, (, **), (s, ( ))((s, ), ( ), (s, ))

**Important: This does not mean that the stack is empty

If input is (, what’s on stack top doesn’t matter and a ( is pushed to stack.

If input is ) and ( is on stack top, then ( is popped and nothing is pushed to stack.

13

Page 14: Pushdown Automata Part I: PDAs Chapter 12 1. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2)

A PDA for AnBn = {anbn: n 0}

14

Page 15: Pushdown Automata Part I: PDAs Chapter 12 1. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2)

A PDA for AnBn = {anbn: n 0}

M = (K, , , , s, A), where: K = {s, f} the states = {a, b} the input alphabet = {a} the stack alphabet A = {f} the accepting states contains: ((s, a, ), (s, a))

((s, b, a), (f, ))((f, b, a), (f, ))

15

Page 16: Pushdown Automata Part I: PDAs Chapter 12 1. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2)

A PDA for {wcwR: w {a, b}*}

16

Page 17: Pushdown Automata Part I: PDAs Chapter 12 1. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2)

M = (K, , , , s, A), where: K = {s, f} the states = {a, b, c} the input alphabet = {a, b} the stack alphabet A = {f} the accepting states contains: ((s, a, ), (s, a))

((s, b, ), (s, b))((s, c, ), (f, ))((f, a, a), (f, ))((f, b, b), (f, ))

A PDA for {wcwR: w {a, b}*}

17

Page 18: Pushdown Automata Part I: PDAs Chapter 12 1. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2)

A PDA for {anb2n: n 0}

18

Page 19: Pushdown Automata Part I: PDAs Chapter 12 1. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2)

A PDA for {anb2n: n 0}

M = (K, , , , s, A), where: K = the states = the input alphabet = the stack alphabet A = the accepting states contains:

19

Page 20: Pushdown Automata Part I: PDAs Chapter 12 1. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2)

Exploiting NondeterminismA PDA M is deterministic iff: ● M contains no pairs of transitions that compete with each other,

and ● Whenever M is in an accepting configuration it has no available

moves.

But many useful PDAs are not deterministic.

20

Page 21: Pushdown Automata Part I: PDAs Chapter 12 1. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2)

A PDA for PalEven ={wwR: w {a, b}*}

S S aSaS bSb

A PDA:

21

Page 22: Pushdown Automata Part I: PDAs Chapter 12 1. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2)

A PDA for PalEven ={wwR: w {a, b}*}

S S aSaS bSb

A PDA:

22

Page 23: Pushdown Automata Part I: PDAs Chapter 12 1. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2)

A PDA for {w {a, b}* : #a(w) = #b(w)}

23

Page 24: Pushdown Automata Part I: PDAs Chapter 12 1. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2)

A PDA for {w {a, b}* : #a(w) = #b(w)}

24

Page 25: Pushdown Automata Part I: PDAs Chapter 12 1. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2)

More on NondeterminismAccepting Mismatches

L = {ambn : m n; m, n > 0}

Start with the case where n = m:

a//a

b/a/

b/a/

1 2

25

Page 26: Pushdown Automata Part I: PDAs Chapter 12 1. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2)

More on NondeterminismAccepting Mismatches

L = {ambn : m n; m, n > 0}

Start with the case where n = m:

a//a

b/a/

b/a/

● If stack and input are empty, halt and reject.

● If input is empty but stack is not (m > n) (accept):

● If stack is empty but input is not (m < n) (accept):

1 2

26

Page 27: Pushdown Automata Part I: PDAs Chapter 12 1. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2)

More on NondeterminismAccepting Mismatches

L = {ambn : m n; m, n > 0}

a//a

b/a/

b/a/

● If input is empty but stack is not (m > n) (accept):

a//a

b/a/

b/a/

/a/

/a/

1 2

21 3

27

Page 28: Pushdown Automata Part I: PDAs Chapter 12 1. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2)

More on NondeterminismAccepting Mismatches

L = {ambn : m n; m, n > 0}

a//a

b/a/

b/a/

● If stack is empty but input is not (m < n) (accept):

a//a

b/a/

b/a/

1 2

21 4

b//

b//

28

Page 29: Pushdown Automata Part I: PDAs Chapter 12 1. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2)

Putting It TogetherL = {ambn : m n; m, n > 0}

● Jumping to the input clearing state 4: Need to detect bottom of stack.

● Jumping to the stack clearing state 3: Need to detect end of input.

29

Page 30: Pushdown Automata Part I: PDAs Chapter 12 1. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2)

The Power of Nondeterminism

Consider AnBnCn = {anbncn: n 0}.

PDA for it?

30

Page 31: Pushdown Automata Part I: PDAs Chapter 12 1. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2)

The Power of Nondeterminism

Consider AnBnCn = {anbncn: n 0}.

Now consider L = AnBnCn. L is the union of two languages:

1. {w {a, b, c}* : the letters are out of order}, and

2. {aibjck: i, j, k 0 and (i j or j k)} (in other words, unequal numbers of a’s, b’s, and c’s).

31

Page 32: Pushdown Automata Part I: PDAs Chapter 12 1. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2)

A PDA for L = AnBnCn What is it?

32

Page 33: Pushdown Automata Part I: PDAs Chapter 12 1. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2)

Are the Context-Free Languages Closed Under Complement?

AnBnCn is context free.

If the CF languages were closed under complement, then

AnBnCn = AnBnCn

would also be context-free.

But we will prove that it is not.

33

Page 34: Pushdown Automata Part I: PDAs Chapter 12 1. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2)

L = {anbmcp: n, m, p 0 and n m or m p}

S NC /* n m, then arbitrary c'sS QP /* arbitrary a's, then p mN A /* more a's than b'sN B /* more b's than a'sA aA aAA aAbB bB BbB aBbC | cC /* add any number of c'sP B' /* more b's than c'sP C' /* more c's than b'sB' b B' bB'B' bB'cC' c | C'cC' C'cC' bC'cQ | aQ /* prefix with any number of a's 34

Page 35: Pushdown Automata Part I: PDAs Chapter 12 1. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2)

Reducing Nondeterminism

and show where the competitions are

● Jumping to the input clearing state 4: Need to detect bottom of stack, so push # onto the stack before we start.● Jumping to the stack clearing state 3: Need to detect end of input. Add to L a termination character (e.g., $) 35

Page 36: Pushdown Automata Part I: PDAs Chapter 12 1. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2)

Reducing Nondeterminism

● Jumping to the input clearing state 4:

36

Page 37: Pushdown Automata Part I: PDAs Chapter 12 1. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2)

Reducing Nondeterminism

● Jumping to the stack clearing state 3:

37

Page 38: Pushdown Automata Part I: PDAs Chapter 12 1. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2)

More on PDAs A PDA for {wwR : w {a, b}*}:

What about a PDA to accept {ww : w {a, b}*}?

38