csc 3130: automata theory and formal languages

33
CSC 3130: Automata theory and formal languages Andrej Bogdanov http://www.cse.cuhk.edu.hk/ ~andrejb/csc3130 The Chinese University of Hong Kong Pushdown automata Fall 2008

Upload: alaina

Post on 05-Jan-2016

66 views

Category:

Documents


4 download

DESCRIPTION

Fall 2008. The Chinese University of Hong Kong. CSC 3130: Automata theory and formal languages. Pushdown automata. Andrej Bogdanov http://www.cse.cuhk.edu.hk/~andrejb/csc3130. Motivation. We had two ways to describe regular languages How about context-free languages?. regular expression. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CSC 3130: Automata theory and formal languages

CSC 3130: Automata theory and formal languages

Andrej Bogdanov

http://www.cse.cuhk.edu.hk/~andrejb/csc3130

The Chinese University of Hong Kong

Pushdown automata

Fall 2008

Page 2: CSC 3130: Automata theory and formal languages

Motivation

• We had two ways to describe regular languages

• How about context-free languages?

regularexpression

DFANFA

syntactic computational

computational

CFG pushdown automaton

syntactic computational

computational

Page 3: CSC 3130: Automata theory and formal languages

Pushdown automata versus NFA

• Since context-free is more powerful than regular, pushdown automata must generalize NFAs

state control

0 1 0 0

input

NFA

Page 4: CSC 3130: Automata theory and formal languages

Pushdown automata

• A pushdown automaton has access to a stack, which is a potentially infinite supply of memory

state control

0 1 0 0

input

pushdown automaton (PDA)

stack

Page 5: CSC 3130: Automata theory and formal languages

Pushdown automata

• As the PDA is reading the input, it can push / pop symbols in / out of the stack

state control

0 1 0 0

input

pushdown automaton (PDA)

Z0 0 1stack

…1

Page 6: CSC 3130: Automata theory and formal languages

Rules for pushdown automata

• Multiple and -transitions are allowed

• Stack is always accessed from the top

• Each transition can pop and/or push into stack

• Transitions depend on input symbol and on last symbol popped from stack

• Automaton accepts if after reading whole input, it can reach an accepting state

Page 7: CSC 3130: Automata theory and formal languages

Example

L = {0n#1n: n ≥ 0}state control

0 0 0 #

input

$ a astack

a …

1 1 1read 0push a

read #

read 1pop a

pop $

push $

Page 8: CSC 3130: Automata theory and formal languages

Shorthand notation

read, pop / push

read 0push a

read #

read 1pop a

pop $

push $

0, / a

#, /

1, a /

, $ /

, / $

convention:we use $ for“end of stack”

Page 9: CSC 3130: Automata theory and formal languages

Formal definition

A pushdown automaton is (Q, , , , q0, F):– Q is a finite set of states; is the input alphabet; is the stack alphabet

– q0 in Q is the initial state;

– F Q is a set of final states; is the transition function

: Q ( {}) ( {}) → subsets of Q ( {})stateinput symbol pop symbol statepush symbol

Page 10: CSC 3130: Automata theory and formal languages

Example

: Q ( {}) ( {}) → subsets of Q ( {})

(q1, 0, ) = {(q1, a)} (q1, 1, ) = ∅

(q1, #, ) = {(q1, )} (q1, 0, ) = ∅

...

0, / a

#, /

1, a /

, $ /

, / $

q0

q1

q2

q3

= {0, 1, #}

= {$, a}

Page 11: CSC 3130: Automata theory and formal languages

Example 1

L = {wwR: w∈*} = {0, 1}

, 00, 0110 ∈ L1, 011, 010 ∉ L

0, / 0

, / $q0 q1

1, / 10, 0 /

q2

1, 1 /

q3, $/ , /

Page 12: CSC 3130: Automata theory and formal languages

Example 2

L = {w: w = wR, w∈*} = {0, 1}

, 1, 00, 010, 0110 ∈ L

011 ∉ L

0, / 0

, / $q0 q1

1, / 10, 0 /

q2

1, 1 /

q3, $/

, / 0, / 1, /

0110110110 011010110orx x xRxR

Page 13: CSC 3130: Automata theory and formal languages

Example 3

L = {0n1m0m1n | n 0, m 0} = {0, 1}

0, / 0

, / $q0 q1

1, / 1

q2

, $/

, /

q5 q4 q3, /

, /

0, 1 / 1, 0/

Page 14: CSC 3130: Automata theory and formal languages

Example 4

L = {w: w has same number 0s and 1s} = {0, 1}

Strategy:Stack keeps track of excess of 0s or 1s

If at the end, stack is empty, number is equal

0, / 0

, / $q0 q1

1, / 1

q3, $/

0, 1 / 1, 0 /

Page 15: CSC 3130: Automata theory and formal languages

Example 4

L = {w: w has same number 0s and 1s} = {0, 1}

0, / 0

, / $q0 q1

1, / 1

q3, $/

0, 1 / 1, 0 /

w = 001110 read stack0 $00 $001 $01 $1 $10 $

Page 16: CSC 3130: Automata theory and formal languages

Example 5

L = {w: w has two 0-blocks with same number of 0s

Strategy:Guess where first block starts

Record 0s on stack

01011, 001011001, 1001010100101001000, 01111allowed not allowed

Guess where second block starts

Match 0s from stack

Page 17: CSC 3130: Automata theory and formal languages

Example 5

L = {w: w has two 0-blocks with same number of 0s

Strategy:Guess where first block starts

Record 0s on stack

Guess where second block startsMatch 0s from stack

It either starts at the beginning, or after some 1After that we must see at least one 0

Then you see a 1, or a pattern 1(0+1)*1

Accept if stack is empty either after next 1,or if you reach end of string

Page 18: CSC 3130: Automata theory and formal languages

Example 5

L = {w: w has two 0-blocks with same number of 0s

0, /

, / $q0 q1 q2

1, /

1, /

0, / 0

q3

0, / 0

, / $

q5

1, / q4

1, /

1, /

0, / 1, /

0, 0/

1, $/ q6

0, / 1, /

q7

, $/

Page 19: CSC 3130: Automata theory and formal languages

Main theorem

A language L is context-free if and only if it is accepted by some pushdown automaton.

context-free grammar pushdown automaton

Page 20: CSC 3130: Automata theory and formal languages

A convention

• Sometimes we denote “transitions” by:

• This will mean:

a, b/ c1c2c3

q0 q1

, / c2

q0 q1a, b/ c1 , / c3

intermediatestates

pop b, then push c1, c2, and c3

Page 21: CSC 3130: Automata theory and formal languages

From CFGs to PDAs

• Idea: Use PDA to simulate derivations

A → 0A1A → BB → #

A 0A1 00A11 00B11 00#11

PDA control:

CFG:

write start variable

stack:

$A

replace production in reverse $1A0

pop terminals and match $1A

e, e / A

0, 0 / e

e, A / 1A0

input:

00#11

00#11

0#11

replace production in reverse $11A0e, A / 1A0 0#11

pop terminals and match $11A0, 0 / e #11

replace production in reverse $11Be, A / B #11

Page 22: CSC 3130: Automata theory and formal languages

From CFGs to PDAs

• If, after reading whole input, PDA ends up with an empty stack, derivation must be valid

• Conversely, if there is no valid derivation, PDA will get stuck somewhere– Either unable to match next input symbol,– Or match whole input but stack non empty

Page 23: CSC 3130: Automata theory and formal languages

Description of PDA for CFGs

• Push $ onto stack

• Repeat the following steps:– If the top of the stack is a variable A:

Choose a rule A → and substitute A with – If the top of the stack is a terminal a:

Read next input symbol and compare to aIf they don’t match, reject (die)

– If top of stack is $, go to accept state

Page 24: CSC 3130: Automata theory and formal languages

Description of PDA for CFGs

q0 q1 q2, / $S

a, a / for every terminal a

, A / k...1for every production A → 1...k

, $ /

Page 25: CSC 3130: Automata theory and formal languages

From PDAs to CFGs

• First, we simplify the PDA:– It has a single accept state qf

– It empties its stack before accepting– Each transition is either a push, or a pop, but

not both

context-free grammar pushdown automaton

Page 26: CSC 3130: Automata theory and formal languages

Simplifying the PDA

• First, we simplify the PDA:– It has a single accept state qf

– It empties its stack before accepting– Each transition is either a push, or a pop, but not both

0, / a

1, a / b

1, b /

q1 q2

q3

, / , /

q0, / $

qf

, b / , a /

, $ /

Page 27: CSC 3130: Automata theory and formal languages

Simplifying the PDA

• First, we simplify the PDA:– It has a single accept state qf

– It empties its stack before accepting– Each transition is either a push, or a pop, but not both

0, / a

1, a / b

1, b /

q1 q2

q3

, / , /

q0, / $

qf

, b / , a /

, $ /

Page 28: CSC 3130: Automata theory and formal languages

Simplifying the PDA

• First, we simplify the PDA:– It has a single accept state qf

– It empties its stack before accepting– Each transition is either a push, or a pop, but not both

0, / a

1, a /

1, b /

q1 q2

q3

, / x

q0, / $

qf, b / , a /

, $ /

q13

, x /

, / xq23

, x /

q12, / b

Page 29: CSC 3130: Automata theory and formal languages

From PDAs to CFGs

• We look at the stack in an accepting computation:

a

$ $

a

$

a

$

a

$

a

$

a

$

a

$

a

$

a

$ $

b a c c c

a

portions that preserve the stack

q3 q1 q7 q0 q1 q2 q1 q3 q7

A03 = {x: x leads from q0 to q3 and preserves stack}

1 1 0 1 0 0input

state

stack

qfq0 q1

0

q1

1

Page 30: CSC 3130: Automata theory and formal languages

From PDAs to CFGs

a a a a a a a a a

b a c c c

a

q0 q1 q3 q1 q7 q1 q2 q1 q7

1 1 0 1 0 00input

state

stack

A11

A03

0 A03 → 0A11

q0 q3 qf

$ $ $ $ $ $ $ $ $ $ $

q1

1

Page 31: CSC 3130: Automata theory and formal languages

From PDAs to CFGs

a a a a a a a a a

b a c c c

a

q3 q1 q7 q1 q2 q1 q7

1 1 0 1 0 0input

state

stack

A03

A13

A13 → A10A03

q0 q3

A10

qfq0 q1

0

q1

1

$ $ $ $ $ $ $ $ $ $ $

Page 32: CSC 3130: Automata theory and formal languages

From PDAs to CFGs

qi

qj

a, / t

b, t /

qi’

qj’ Aij → aAi’j’b

qi qj qk Aik → AijAjk

qi Aii →

variables:Aij

start variable:A0f

Page 33: CSC 3130: Automata theory and formal languages

Example

start variable: A03

productions:

A00 → A00A00

A00 → A01A10A00 → A03A30A01 → A01A11

A01 → A02A21

A00 →

...

A11 → A22 →

A12 → 0A121A12 → 0A11

A33 →

0, / a

, / $ , $ /

1, a /

q0 q2 q3q1, a /

A03 → A12