CSC 3130: Automata theory and formal languages

Preview:

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

Citation preview

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

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

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

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

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

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

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 $

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”

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

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}

Example 1

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

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

0, / 0

, / $q0 q1

1, / 10, 0 /

q2

1, 1 /

q3, $/ , /

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

Example 3

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

0, / 0

, / $q0 q1

1, / 1

q2

, $/

, /

q5 q4 q3, /

, /

0, 1 / 1, 0/

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 /

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 $

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

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

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

, $/

Main theorem

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

context-free grammar pushdown automaton

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

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

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

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

Description of PDA for CFGs

q0 q1 q2, / $S

a, a / for every terminal a

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

, $ /

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

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 /

, $ /

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 /

, $ /

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

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

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

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

$ $ $ $ $ $ $ $ $ $ $

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

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

Recommended