CDT314
This presentation is the property of its rightful owner.
Sponsored Links
1 / 104

CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 8 Mälardalen University PowerPoint PPT Presentation


  • 91 Views
  • Uploaded on
  • Presentation posted in: General

CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 8 Mälardalen University 2012. Content Context-Free Languages Push-Down Automata, PDA NPDA: Non-Deterministic PDA Formal Definitions for NPDAs NPDAs Accept Context-Free Languages Converting NPDA to Context-Free Grammar.

Download Presentation

CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 8 Mälardalen University

An Image/Link below is provided (as is) to download presentation

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 - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

CDT314FABER

Formal Languages, Automata and Models of Computation

Lecture 8

Mälardalen University

2012


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

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


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

Non-regular languages

Context-Free Languages

Regular Languages


Context free languages

Context-Free Languages

Based on C Busch, RPI, Models of Computation


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

stack

automaton

Context-Free Languages

Context-Free

Grammars

Pushdown

Automata

(CF grammars are

defined as generalized Regular Grammars)


Definition context free grammars

is string of variables and terminals

Definition: Context-Free Grammars

Grammar

Variables

Terminal

symbols

Start

variables

Productions of the form:


Pushdown automata pdas

Pushdown AutomataPDAs


Pushdown automaton pda

Pushdown Automaton - PDA

Input String

Stack

States


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

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 is 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 information (but it is not random access!).

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


The states

The States

Pop old- reading

stack symbol

Push new

- writing

stacksymbol

Input

symbol


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

input

stack

top

Replace

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


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

stack

top

Push

input


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

stack

top

Pop

input


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

input

stack

top

No Change


Npdas non deterministic push down automata

NPDAsNon-deterministic Push-Down Automata


Non determinism

Non-Determinism


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

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, depending on convention)


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

Example NPDA

is the language accepted by the NPDA:


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

Example NPDA

NPDAM

(Even-length palindromes)

Example :aabaaabbblbbbaaabaa


Pushing strings

Pushing Strings

Pop

symbol

Input

symbol

Push

string


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

Example

input

pushed

string

stack

top

Push


Another npda example

Another NPDA example

NPDAM


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

Execution Example

Time 0

Input

Stack

Current state


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

Time 1

Input

Stack


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

Time 2

Input

Stack


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

Time 3

Input

Stack


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

Time 4

Input

Stack


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

Time 5

Input

Stack


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

Time 6

Input

Stack


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

Time 7

Input

Stack

accept


Formal definitions for npdas

Formal Definitions for NPDAs


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

Transition function


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

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.


Formal definition

Final

states

States

Input

alphabet

Stack

start

symbol

Transition

function

Stack

alphabet

Formal Definition

Non-Deterministic Pushdown Automaton NPDA


Instantaneous description

Instantaneous Description

Current

stack

contents

Current

state

Remaining

input


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

Example

Instantaneous Description

Input

Time 4:

Stack


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

Example

Instantaneous Description

Input

Time 5:

Stack


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

We write

Time 4

Time 5


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

A computation example


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

A computation example


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

A computation example


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

A computation example


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

A computation example


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

A computation example


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

A computation example


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

A computation example


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

A computation example

For convenience we write


Formal definition1

Formal Definition

Language of NPDAM

Initial state

Final state


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

Example

NPDAM


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

NPDAM


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

Therefore:

NPDAM


Npdas accept context free languages

NPDAs Accept Context-Free Languages


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

Context-Free

Languages

(Grammars)

Languages

Accepted by

NPDAs

Theorem


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

Proof - Step 1:

Context-Free

Languages

(Grammars)

Languages

Accepted by

NPDAs

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


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

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 grammars to npdas

Converting Context-Free Grammarsto NPDAs


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

An example grammar:

What is the equivalent NPDA?


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

For eachproduction

add transition:

For eachterminal

add transition:

Grammar

NPDA


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

The NPDA simulates

the leftmost derivations of the grammar

L(Grammar) = L(NPDA)


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

Grammar:

A leftmost derivation:


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

NPDA execution:

Time 0

Input

Stack

Start


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

Time 1

Input

Stack


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

Time 2

Input

Stack


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

Time 3

Input

Stack


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

Time 4

Input

Stack


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

Time 5

Input

Stack


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

Time 6

Input

Stack


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

Time 7

Input

Stack


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

Time 8

Input

Stack


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

Time 9

Input

Stack


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

Time 10

Input

Stack

accept


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

In general

Given any grammarG

we can construct a NPDAMwith


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

Constructing NPDAM from grammarG

Top-down parser

For any production

For any terminal


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

Grammar Ggenerates string w

if and only if

NPDA Maccepts w


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

For any context-free language

there is an NPDA

that accepts the same language


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

Which means

Languages

Accepted by

NPDAs

Context-FreeLanguages(Grammars)


Converting npdas to context free grammars

Converting NPDAstoContext-Free Grammars


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

For any NPDA M

we will construct

a context-free grammar G with


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

The grammar simulates the machine

A derivation in Grammar

variables

terminals

Input processed

Stack contents

in NPDA M


Some simplifications

Some Simplifications

  • 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


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

Second we modify the NPDA transitions.

All transitions will have form:

or

which means that each move

increases/decreases stack by a single symbol.


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

  • Thosesimplificationsdo not affect generality of our argument.

  • It can be shown that for any NPDA there exists an equivalent one having the 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.


The grammar construction

The Grammar Construction

In grammarG

Stack symbol

Variables:

states

Terminals:

Input symbols of NPDA


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

For each transition:

we add production:


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

For each transition:

we add production:

for all statesqk , ql


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

Stack bottom symbol

Start Variable

Start state

(Single) Final state


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

  • From NPDA to CFG, in short:

  • When we write a grammar, we can use any variable names we choose. As in programming languages, we like to use "meaningful" variable names.

  • Translating an NPDA into a CFG, we will use variable names that encode information about both the state of the NPDA and the stack contents. Variable names will have the form [qiAqj], where qi and qj are states and A is a variable.

  • The "meaning" of the variable [qiAqj] is that the NPDA can go from state qi with Ax on the stack to state qj with x on the stack. Each transition of the form (qi, a, A) = (qj,l) results in a single grammar rule.


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

  • From NPDA to CFG

  • Each transition of the form (qi, a, A) = (qj, BC) results in a multitude of grammar rules, one for each pair of states qx and qy in the NPDA.

  • This algorithm results in a lot of useless (unreachable) productions, but the useful productions define the context-free grammar recognized by the NPDA.

http://www.seas.upenn.edu/~cit596/notes/dave/npda-cfg6.htmlhttp://www.cs.duke.edu/csed/jflap/tutorial/pda/cfg/index.html using JFLAP


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

For any NPDA

there is an context-free grammar

that generates the same language.


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

Context-Free

Languages

(Grammars)

Languages

Accepted by

NPDAs

We have the procedure to convert

any NPDA Mto a context-free

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

which means:


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

We have already shown that for any context-free language

there is an NPDA

that accepts the same language. That is:

Languages

Accepted by

NPDAs

Context-FreeLanguages(Grammars)


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

Context-Free

Languages

(Grammars)

Languages

Accepted by

NPDAs

Therefore:

END OF PROOF


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

An example of a NPDA in an appropriate form


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

Example

Grammar production:


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

Grammar productions:


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

Grammar production:


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

Resulting Grammar


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

Resulting Grammar, cont.


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

Resulting Grammar, cont.


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

Derivation of string


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

In general, in grammar:

if and only if

is accepted by the NPDA


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

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


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

Example (Sudkamp 8.1.2)

Language consisting solely of a’s or an equal number of a´s and b´s.


Cdt314 faber formal languages automata and models of computation lecture 8 m lardalen university

Concerning examination in the course:

Exercises  are voluntary

Labs are voluntary

Midterms are voluntary

Lectures are voluntary…

All of them are recommended!

JFLAP demo

http://www.cs.duke.edu/csed/jflap/movies


  • Login