= Languages Accepted by NPDAs Accept (Grammars) NPDAs - - PDF document

languages accepted by npdas accept grammars npdas context
SMART_READER_LITE
LIVE PREVIEW

= Languages Accepted by NPDAs Accept (Grammars) NPDAs - - PDF document

Review Languages and Grammars Alphabets, strings, languages Regular Languages CS 301 - Lecture 13 Deterministic Finite and Nondeterministic Automata Equivalence of NFA and DFA Converting NPDAs to Grammars Regular


slide-1
SLIDE 1

1

CS 301 - Lecture 13 Converting NPDAs to Grammars and Deterministic Pushdown Automata

Fall 2008

Review

  • Languages and Grammars

– Alphabets, strings, languages

  • Regular Languages

– Deterministic Finite and Nondeterministic Automata – Equivalence of NFA and DFA – Regular Expressions – Regular Grammars – Properties of Regular Languages – Languages that are not regular and the pumping lemma

  • Context Free Languages

– Context Free Grammars – Derivations: leftmost, rightmost and derivation trees – Parsing and ambiguity – Simplifications and Normal Forms – Nondeterministic Pushdown Automata – Converting Context Free Grammars into NPDAs

  • Today:

– Converting NPDAs to Context Free Grammars – Deterministic Pushdown Automata

NPDAs Accept Context-Free Languages

Context-Free Languages (Grammars) Languages Accepted by NPDAs

=

Theorem:

slide-2
SLIDE 2

2

Context-Free Languages (Grammars) Languages Accepted by NPDAs Proof - Step 1: Convert any context-free grammar to a NPDA with:

G M ) ( ) ( M L G L =

Last Time we showed how to convert A grammar to an NPDA….. Context-Free Languages (Grammars) Languages Accepted by NPDAs Proof - Step 2: Convert any NPDA to a context-free grammar with:

G M ) ( ) ( M L G L =

Converting NPDAs to Context-Free Grammars

Proof - step 2 For any NPDA M we will construct a context-free grammar with

G ) ( ) ( G L M L =

slide-3
SLIDE 3

3

Some Necessary Modifications

Modify (if necessary) the NPDA so that: 1) The stack is never empty 2) It has a single final state and empties the stack when it accepts a string 3) Has transitions in a special form

1) Modify the NPDA so that the stack is never empty

$

Stack

a

OK

$

OK NOT OK

$ a $

Introduce the new symbol to denote the bottom of the stack

#

$ a

#

$

# #

Original NPDA

# , → λ λ

At the beginning push into the stack

#

  • riginal

initial state new initial state

slide-4
SLIDE 4

4

i

q

j

q b a → $ ,

i

q

j

q

b a → # ,

In transitions: replace every instance of with

$ #

Example: if the automaton attempts to pop

  • r replace it will halt

$

Convert all transitions so that:

i

q

j

q s → ξ σ,

i

q

j

q

x x → , λ s → ξ σ, $ $ → , λ

{$} − Γ ∈ ∀x

Convert transitions as follows: halting state NPDA

f

q λ λ → x ,

λ λ λ → ,

Empty the stack

{$} − Γ ∈ ∀x

2) Modify the NPDA so that it empties the stack and has a unique final state

λ λ λ → , λ λ λ → ,

Old final states

λ λ → $ ,

slide-5
SLIDE 5

5

3) modify the NPDA so that transitions have the following forms:

i

q

j

q

λ σ → B , OR

i

q

j

q

CD B → , σ

symbols stack : , , D C B

i

q

j

q

y → λ σ, Convert:

i

q

j

q

τ τ σ y → , {$} − Γ ∈ ∀τ

i

q

j

q

B A → , σ Convert:

i

q

j

q

XB A → , σ λ σ → X , {$} − Γ ∈ X symbols

i

q

j

q

By A → , σ Convert:

i

q

j

q

y A → , σ BX X → , σ

{$} − Γ ∈ ∀X

symbols 2 | | ≥ y

Convert recursively

slide-6
SLIDE 6

6

λ λ → $ , q

f

q a, $ → 0$ a, 0 → 00 a,1→ λ b, $ →1$ b,1→ 11 b, 0 → λ

} : { ) (

b a

n n w M L = =

Example of a NPDA in correct form:

symbol stack initial : $

The Grammar Construction

) (

j iBq

q

In grammar :

G

Terminals: Input symbols of NPDA states Stack symbol Variables:

i

q

j

q λ → B a,

For each transition We add production

a Bq q

j i

→ ) (

For each transition We add productions

) )( ( ) (

k l l j k i

Dq q Cq q a Bq q →

i

q

j

q CD B a → ,

For all possible states in the automaton

l k q

q ,

slide-7
SLIDE 7

7

Start Variable:

) $ (

f

  • q

q

Stack bottom symbol Start state final state Example:

λ λ → $ , q

f

q a, $ → 0$ a, 0 → 00 a,1→ λ b, $ →1$ b,1→ 11 b, 0 → λ a q q → ) 1 (

Grammar production: Example:

λ λ → $ , q

f

q a, $ → 0$ a, 0 → 00 a,1→ λ b, $ →1$ b,1→ 11 b, 0 → λ ) $ )( 1 ( | ) $ )( 1 ( ) $ ( ) $ )( 1 ( | ) $ )( 1 ( ) $ (

f f f f f f f

q q q q b q q q q b q q q q q q b q q q q b q q → →

Grammar productions: Example:

λ λ → $ , q

f

q a, $ → 0$ a, 0 → 00 a,1→ λ b, $ →1$ b,1→ 11 b, 0 → λ

Grammar production:

λ → ) $ ( 0

f

q q

slide-8
SLIDE 8

8

) $ )( 1 ( | ) $ )( 1 ( ) $ ( ) $ )( 1 ( | ) $ )( 1 ( ) $ (

f f f f f f f

q q q q b q q q q b q q q q q q b q q q q b q q → → ) 1 )( 1 ( | ) 1 )( 1 ( ) 1 ( ) 1 )( 1 ( | ) 1 )( 1 ( ) 1 (

f f f f f f f

q q q q b q q q q b q q q q q q b q q q q b q q → → ) $ )( ( | ) $ )( ( ) $ ( ) $ )( ( | ) $ )( ( ) $ (

f f f f f f f

q q q q a q q q q a q q q q q q a q q q q a q q → →

Resulting Grammar:

able start vari : ) $ ( 0

f

q q ) )( ( | ) )( ( ) ( ) )( ( | ) )( ( ) (

f f f f f f f

q q q q a q q q q a q q q q q q a q q q q a q q → → b q q a q q → → ) ( ) 1 ( λ → ) $ ( 0

f

q q

Derivation of string

abba

⇒ ) $ ( 0

f

q q ⇒ ) $ )( (

f

q q q q a ⇒ ) $ ( 0

f

q q ab ⇒ ) $ )( 1 (

f

q q q q abb ⇒ ) $ ( 0

f

q q abba

abba

In general:

w Aq q

j i ∗

⇒ ) (

if and only if the NPDA goes from to by reading string and the stack doesn’t change below and then is removed from stack i

q

j

q A

A

w

slide-9
SLIDE 9

9

Therefore:

w q q

f ∗

⇒ ) $ ( 0

if and only if

w is accepted by the NPDA

Therefore: For any NPDA there is a context-free grammar that accepts the same language Context-Free Languages (Grammars) Languages Accepted by NPDAs

Deterministic PDA DPDA

Deterministic PDA: DPDA

q1 q2 w b a → ,

1

q

2

q w b → , λ

Allowed transitions: (deterministic choices)

slide-10
SLIDE 10

10

Allowed transitions:

q1 q2

1

, w b → λ q3

2

, w c → λ q1 q2

1

, w b a → q3

2

, w c a →

(deterministic choices)

q1 q2

1

, w b a → q3

2

, w b a →

Not allowed: (non deterministic choices)

q1 q2

1

, w b → λ q3

2

, w b a →

DPDA example

a, λ → a b, a → λ q0 q1 q2 q3 b, a → λ λ, $ → $

} : { ) ( ≥ = n b a M L

n n

a, λ → a

} : { ) ( ≥ = n b a M L

n n The language is deterministic context-free

slide-11
SLIDE 11

11

Definition: A language is deterministic context-free if there exists some DPDA that accepts it

L Example of Non-DPDA (NPDA)

λ, $ → $ q1 q2 b b a a → → λ λ , , λ, λ → λ q0 λ λ → → b b a a , ,

} { ) (

R

ww M L =

λ, $ → $ q1 q2 b b a a → → λ λ , , λ, λ → λ q0 λ λ → → b b a a , ,

Not allowed in DPDAs

NPDAs Have More Power than DPDAs

slide-12
SLIDE 12

12

Deterministic Context-Free Languages (DPDA) Context-Free Languages NPDAs Since every DPDA is also a NPDA It holds that: We will actually show: We will show that there exists a context-free language which is not accepted by any DPDA

L

Deterministic Context-Free Languages (DPDA) Context-Free Languages (NPDA) The language is:

} { } {

2n n n n

b a b a L ∪ = ≥ n

We will show:

L

  • is context-free

L

  • is not deterministic context-free

} { } {

2n n n n

b a b a L ∪ =

Language is context-free

L

Context-free grammar for :

L

2 1 | S

S S → λ |

1 1

b aS S → λ |

2 2

bb aS S →

} {

n nb

a } {

2n nb

a } { } {

2n n n n

b a b a ∪

slide-13
SLIDE 13

13 } { } {

2n n n n

b a b a L ∪ =

is not deterministic context-free

Theorem:

The language (there is no DPDA that accepts )

L

Proof: Assume for contradiction that

} { } {

2n n n n

b a b a L ∪ =

is deterministic context free Therefore: there is a DPDA that accepts

M L

DPDA with

M

n nb

a

n

b

accepts n nb

a

2 accepts n nb

a } { } { ) (

2n n n n

b a b a M L ∪ =

DPDA with

M

n nb

a

n

b } { } { ) (

2n n n n

b a b a M L ∪ =

Such a path exists because of the determinism

M

slide-14
SLIDE 14

14

The language is not context-free

} {

n n n

c b a

(we will prove this at a later class using pumping lemma for context-free languages)

Fact 1: Regular languages

* *b a

Context-free languages

n nb

a

The language is not context-free

} {

n n n

c b a L ∪

}) { } { (

2n n n n

b a b a L ∪ =

Fact 2:

(we can prove this using pumping lemma for context-free languages - next lecture)

We will construct a NPDA that accepts:

} {

n n n

c b a L ∪

}) { } { (

2n n n n

b a b a L ∪ =

which is a contradiction! Modify M

M′

n

c

n nc

a

} { } { ) (

2n n n n

b a b a M L ∪ = } { } { ) (

2n n n n

c a c a M L ∪ = ′

Replace with

b c

n nb

a

n

b

M

slide-15
SLIDE 15

15

The NPDA that accepts

} {

n n n

c b a L ∪

n nb

a

n

b

n

c

M′ M

n nc

a λ λ Connect final states of with final states of M′ M Since is accepted by a NPDA

} {

n n n

c b a L ∪

it is context-free Contradiction! (since is not context-free)

} {

n n n

c b a L ∪

Therefore:

} { } {

2n n n n

b a b a L ∪ =

There is no DPDA that accepts End of Proof Not deterministic context free

What’s Next

  • Read

– Linz Chapter 1,2.1, 2.2, 2.3, (skip 2.4), 3, 4, 5, 6.1, 6.2, (skip 6.3), 7.1, 7.2, and 7.3 (skip 7.4) – JFLAP Chapter 1, 2.1, (skip 2.2), 3, 4, 5, 6, 7

  • Next Lecture Topics

– Midterm Review

  • Midterm in Lecture on Tuesday 10/14

– Covers Linz 1.1, 1.2, 2.1, 2.2, 2.3, (skip 2.4), 3, 4, 5, 6.1, 6.2 and JFLAP 1, 2.1, (skip 2.2), 3, 4, 6.1 – Closed book, but you may bring one sheet of 8.5 x 11 inch paper with any notes you like. – Midterm will take the full lecture

  • Homework

– Homework due Thursday