- 40 Views
- Uploaded on
- Presentation posted in: General

CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 8 MÃ¤lardalen University

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

CD5560

FABER

Formal Languages, Automata and Models of Computation

Lecture 8

MÃ¤lardalen University

2010

ContentContext-Free LanguagesPush-Down Automata, PDANPDA: Non-Deterministic PDAFormal Definitions for NPDAs NPDAs Accept Context-Free LanguagesConverting NPDA to Context-Free Grammar

Non-regular languages

Context-Free Languages

Regular Languages

Context-Free Languages

stack

automaton

Context-Free Languages

Context-Free

Grammars

Pushdown

Automata

(CF grammars are

defined as generalized Regular Grammars)

is string of variables and terminals

Grammar

Variables

Terminal

symbols

Start

variables

Productions of the form:

is string of terminals

Grammar

Variables

Terminal

symbols

Start

variables

Right or Left Linear Grammars. Productions of the form:

or

Pushdown AutomataPDAs

Input String

Stack

States

The Stack

A PDA can write symbols on stack and read them later on.

POP reading symbol PUSH writing symbol

All access to the stack only on the top!

(Stack top is written leftmost in the string, e.g. yxz)

A stack is valuable as it can hold an unlimitedamount of (certain kind of) information.

The stack allows pushdown automata to recognize some non-regular languages.

Pop old- reading

stack symbol

Push new

- writing

stacksymbol

Input

symbol

input

stack

top

Replace

(An alternative is to start and finish with empty stack or a stack bottom symbol such as $)

stack

top

Push

input

stack

top

Pop

input

input

stack

top

No Change

Example 3.7 Salling:

A PDA for simple nested parenthesis strings

Time 0

Input

Stack

Example 3.7

Time 1

Input

Stack

Example 3.7

Time 2

Input

Stack

Stack

Example 3.7

Time 3

Input

Stack

Example 3.7

Time 4

Input

Stack

Example 3.7

Time 5

Input

Example 3.7

Time 6

Input

Stack

Example 3.7

Time 7

Input

Stack

NPDAsNon-deterministic Push-Down Automata

A string is accepted if:

- All the input is consumed
- The last state is a final state
- Stack is in the initial condition
- (either: empty (when we started with empty stack),
- or: bottom symbol reached, or similar)

Example NPDA

is the language accepted by the NPDA:

Example NPDA

NPDAM

(Even-length palindromes)

Pop

symbol

Input

symbol

Push

string

Example

input

pushed

string

stack

top

Push

NPDAM

Execution Example

Time 0

Input

Stack

Current state

Time 1

Input

Stack

Time 2

Input

Stack

Time 3

Input

Stack

Time 4

Input

Stack

Time 5

Input

Stack

Time 6

Input

Stack

Time 7

Input

Stack

accept

Formal Definitions for NPDAs

Transition function

Transition function

new state

current state

current stack top

new stack top

current input symbol

An unspecified transition function is to the null set and represents a dead configuration for the NPDA.

Final

states

States

Input

alphabet

Stack

start

symbol

Transition

function

Stack

alphabet

Non-Deterministic Pushdown Automaton NPDA

Current

stack

contents

Current

state

Remaining

input

Example

Instantaneous Description

Input

Time 4:

Stack

Example

Instantaneous Description

Input

Time 5:

Stack

We write

Time 4

Time 5

A computation example

A computation example

A computation example

A computation example

A computation example

A computation example

A computation example

A computation example

For convenience we write

Language of NPDAM

Initial state

Final state

Example

NPDAM

NPDAM

Therefore:

NPDAM

NPDAs Accept Context-Free Languages

Context-Free

Languages

(Grammars)

Languages

Accepted by

NPDAs

Theorem

Proof - Step 1:

Context-Free

Languages

(Grammars)

Languages

Accepted by

NPDAs

Convert any context-free grammarGto a NPDA Mwith L(G) = L(M)

Proof - Step 2:

Context-Free

Languages

(Grammars)

Languages

Accepted by

NPDAs

Convert any NPDA M to a context-free grammarGwith L(M) = L(G)

Converting Context-Free Grammarsto NPDAs

An example grammar:

What is the equivalent NPDA?

Grammar

NPDA

The NPDA simulates

leftmost derivations of the grammar

L(Grammar) = L(NPDA)

Grammar:

A leftmost derivation:

NPDA execution:

Time 0

Input

Stack

Start

Time 1

Input

Stack

Time 2

Input

Stack

Time 3

Input

Stack

Time 4

Input

Stack

Time 5

Input

Stack

Time 6

Input

Stack

Time 7

Input

Stack

Time 8

Input

Stack

Time 9

Input

Stack

Time 10

Input

Stack

accept

In general

Given any grammarG

we can construct a NPDAMwith

Constructing NPDAM from grammarG

Top-down parser

For any production

For any terminal

Grammar G generates string w

if and only if

NPDA M accepts w

For any context-free language

there is an NPDA

that accepts the same language

Context-Free

Languages

(Grammars)

Languages

Accepted by

NPDAs

Which means

Converting NPDAstoContext-Free Grammars

For any NPDA M

we will construct

a context-free grammar G with

The grammar simulates the machine

A derivation in Grammar

variables

terminals

Input processed

Stack contents

in NPDA M

- First we modify the NPDA so that
- It has a single final state qf and
- It empties the stack
- when it accepts the input

Original NPDA

Empty Stack

Second we modify the NPDA transitions.

All transitions will have form:

or

which means that each move

increases/decreases stack by a single symbol.

- Those simplifications do not affect generality of our argument.
- It can be shown that for any NPDA there exists an equivalent one having above two properties
- i.e.
- the equivalent NPDA with a single final state which empties its stack when it accepts the input, and which for each move increases/decreases stack by a single symbol.

Example of a NPDA in an appropriate form

In grammarG

Stack symbol

Variables:

states

Terminals:

Input symbols of NPDA

For each transition:

we add production:

For each transition:

we add production:

for all statesqk , ql

Stack bottom symbol

Start Variable

Start state

(Single) Final state

Example

Grammar production:

Grammar productions:

Grammar production:

Resulting Grammar

Resulting Grammar, cont.

Resulting Grammar, cont.

Derivation of string

In general, in grammar:

if and only if

is accepted by the NPDA

Explanation

By construction of Grammar:

if and only if

in the NPDA going from qito qj

the stack doesnâ€™t change below

and A is removed from stack

Context-Free

Languages

(Grammars)

Languages

Accepted by

NPDAs

We have shown the procedure to convert

any NPDA Mto a context-free

grammar G with L(M) = L(G)

which means

Context-Free

Languages

(Grammars)

Languages

Accepted by

NPDAs

Therefore

END OF PROOF

Example (Sudkamp 8.1.2)

Language consisting solely of aâ€™s or an equal number of aÂ´s and bÂ´s.