meljun cortes automata lecture equivalence of pda and cfg part 2 4

23
Theory of Computation (With Automata Theory) * Property of STI Page 1 of 23 Equivalence of PDA and CFG (Part 2) EQUIVALENCE OF PUSHDOWN AUTOMATA AND CONTEXT-FREE GRAMMARS (Part 2) Context-Free Grammars to Pushdown Automata Introduction It was shown in the previous lesson that if a language is context-free, then some pushdown automaton recognizes it. This lesson will now prove that if a pushdown automaton recognizes some language, then that language is context- free.

Upload: meljun-cortes-mbampa

Post on 17-Jul-2016

219 views

Category:

Documents


1 download

DESCRIPTION

MELJUN CORTES Automata Lecture Equivalence of Pda and Cfg Part 2 4

TRANSCRIPT

Page 1: MELJUN CORTES Automata Lecture Equivalence of Pda and Cfg Part 2 4

Theory of Computation (With Automata Theory)

* Property of STI

Page 1 of 23

Equivalence of PDA and CFG (Part 2)

EQUIVALENCE OF PUSHDOWN AUTOMATA

AND CONTEXT-FREE GRAMMARS (Part 2)

Context-Free Grammars to Pushdown

Automata

Introduction

• It was shown in the previous

lesson that if a language is

context-free, then some

pushdown automaton

recognizes it.

• This lesson will now prove that

if a pushdown automaton

recognizes some language,

then that language is context-

free.

Page 2: MELJUN CORTES Automata Lecture Equivalence of Pda and Cfg Part 2 4

Theory of Computation (With Automata Theory)

* Property of STI

Page 2 of 23

Equivalence of PDA and CFG (Part 2)

• Assume that there is a PDA P

which recognizes a certain

language L.

• To determine if language L is a

context-free language, it must

be shown that there is some

context-free grammar that

describes it.

• A procedure should therefore

be created for constructing a

CFG G based on PDA P.

• In other words, convert the

pushdown automaton P into a

corresponding context-free

grammar G.

Page 3: MELJUN CORTES Automata Lecture Equivalence of Pda and Cfg Part 2 4

Theory of Computation (With Automata Theory)

* Property of STI

Page 3 of 23

Equivalence of PDA and CFG (Part 2)

• In order to simplify the

conversion of PDA P into a

corresponding CFG G, P must

be modified so that it will have

the following properties:

1. It must have only one final

state.

2. The stack becomes empty

when P goes to the final

state.

3. As P moves from one state

to another, it either pushes

a symbol onto the stack or

pops a symbol from the

stack. It cannot do both.

Page 4: MELJUN CORTES Automata Lecture Equivalence of Pda and Cfg Part 2 4

Theory of Computation (With Automata Theory)

* Property of STI

Page 4 of 23

Equivalence of PDA and CFG (Part 2)

For Property 1:

If the PDA has more than one

final state, the following

modification should be done:

qstart

Original PDA

`

qstart

Modified PDA

` qfinal

e, e → e

e, e → e

Page 5: MELJUN CORTES Automata Lecture Equivalence of Pda and Cfg Part 2 4

Theory of Computation (With Automata Theory)

* Property of STI

Page 5 of 23

Equivalence of PDA and CFG (Part 2)

For Property 2:

If the stack is not empty when

the PDA reaches the final

state, add transitions going

from the final state back to

itself that pops the remaining

contents of the stack except

for the stack empty symbol $.

Then, create a new final state

called qfinal and connect the

original final state to this new

state using a transition edge

with the label e, $ → e.

Page 6: MELJUN CORTES Automata Lecture Equivalence of Pda and Cfg Part 2 4

Theory of Computation (With Automata Theory)

* Property of STI

Page 6 of 23

Equivalence of PDA and CFG (Part 2)

For example, assume that

there is a PDA whose final

state is q4. When the PDA

reaches this state, the

contents of its stack are:

The following modifications to

the PDA should be done:

a

b

c

$

stack

contents

top of the

stack

e, a → e

q4 qfinal

e, b → e

e, c → e

e, $ → e

Page 7: MELJUN CORTES Automata Lecture Equivalence of Pda and Cfg Part 2 4

Theory of Computation (With Automata Theory)

* Property of STI

Page 7 of 23

Equivalence of PDA and CFG (Part 2)

For Property 3:

1st Case:

This is an example of a

situation wherein the PDA

performs both a pop and a

push operation in one state

transition. It popped b and

pushed c as it moved from

state qi to state qj.

qj

a, b → c

qi

Page 8: MELJUN CORTES Automata Lecture Equivalence of Pda and Cfg Part 2 4

Theory of Computation (With Automata Theory)

* Property of STI

Page 8 of 23

Equivalence of PDA and CFG (Part 2)

To ensure that the PDA will

perform only one operation for

each state transition, the PDA

will be modified as follows:

The pop operation was done in

one state transition (from qi to

qt) and the push operation was

done in a separate state

transition (from qt to qj).

Therefore, only one stack

operation was performed per

state transition.

qt

a, b → e

qi qj

e, e → c

Page 9: MELJUN CORTES Automata Lecture Equivalence of Pda and Cfg Part 2 4

Theory of Computation (With Automata Theory)

* Property of STI

Page 9 of 23

Equivalence of PDA and CFG (Part 2)

2nd Case:

This is an example of a

situation wherein the PDA did

not perform a single stack

operation in a state transition.

qj

a, e → e

qi

Page 10: MELJUN CORTES Automata Lecture Equivalence of Pda and Cfg Part 2 4

Theory of Computation (With Automata Theory)

* Property of STI

Page 10 of 23

Equivalence of PDA and CFG (Part 2)

To ensure that the PDA will

perform one stack operation

(push or pop) for each

transition, the PDA will be

modified as follows:

A dummy variable x was

pushed in one state transition

(from qi to qt) and it was

popped in a separate state

transition (from qt to qj).

Therefore, exactly one stack

operation was performed per

state transition.

qt

a, e → x

qi qj

e, x → e

Page 11: MELJUN CORTES Automata Lecture Equivalence of Pda and Cfg Part 2 4

Theory of Computation (With Automata Theory)

* Property of STI

Page 11 of 23

Equivalence of PDA and CFG (Part 2)

Conversion of the Modified PDA to

CFG

• Consider the following events

that can occur every time PDA

P processes an input string w:

1. PDA P starts at state qstart

with an empty stack.

2. As it starts processing the

input string w, the very first

action it performs on the

stack is a push operation.

3. As the input symbols

arrive, P moves from one

state to another while

pushing and popping

symbols whenever

necessary.

Page 12: MELJUN CORTES Automata Lecture Equivalence of Pda and Cfg Part 2 4

Theory of Computation (With Automata Theory)

* Property of STI

Page 12 of 23

Equivalence of PDA and CFG (Part 2)

4. If the string w is accepted

by P, the PDA will end up

in the final state qfinal with

an empty stack.

It is possible that in the

course of processing a

string, the stack may

become empty at some

point in time and may be

filled up again as input

symbols keep on arriving.

Eventually, the stack

becomes empty again as it

reaches the final state.

Page 13: MELJUN CORTES Automata Lecture Equivalence of Pda and Cfg Part 2 4

Theory of Computation (With Automata Theory)

* Property of STI

Page 13 of 23

Equivalence of PDA and CFG (Part 2)

• Therefore, the language of P is

composed of all strings that

cause the PDA to start at qstart

with an empty stack and end

at qfinal with an empty stack.

• This language will be called

Lstart, final.

• The grammar G should be

able to simulate these series

of events.

• In fact, the grammar G may

also be designed to simulate

the same series of events

between any two states (not

just the start and final states)

of the PDA P.

Page 14: MELJUN CORTES Automata Lecture Equivalence of Pda and Cfg Part 2 4

Theory of Computation (With Automata Theory)

* Property of STI

Page 14 of 23

Equivalence of PDA and CFG (Part 2)

• The figure below illustrates

this:

When the PDA is at state qi,

the stack is empty. During

processing, the stack gets

filled up. By the time the PDA

reaches state qj, the stack is

empty again.

These strings that were

processed are generated by

variable Aij.

PDA is at

state qi

stack height

input

string

PDA is at

state qj

string generated by Aij

Page 15: MELJUN CORTES Automata Lecture Equivalence of Pda and Cfg Part 2 4

Theory of Computation (With Automata Theory)

* Property of STI

Page 15 of 23

Equivalence of PDA and CFG (Part 2)

• The variable Aii can be used to

generate all the strings that

can cause the PDA to move

from state qi to state qi (back

to itself). The following

production should be included:

Aii → e

• The variable Astart,final

generates all the strings that

can cause the PDA to start at

state qstart with an empty stack

and end in state qfinal, also with

an empty stack. Astart,final will

be the start variable of

grammar G.

Page 16: MELJUN CORTES Automata Lecture Equivalence of Pda and Cfg Part 2 4

Theory of Computation (With Automata Theory)

* Property of STI

Page 16 of 23

Equivalence of PDA and CFG (Part 2)

• While processing an input

string, two things may happen:

1. The stack becomes empty

in the middle of the

processing.

At some point in time

during the processing, the

stack becomes empty (say

at state qx). Then, as input

symbols continue to arrive,

it gets filled up again as the

PDA resumes moving from

one state to another. And,

by the time the stack

reaches state qj, it

becomes empty again.

Page 17: MELJUN CORTES Automata Lecture Equivalence of Pda and Cfg Part 2 4

Theory of Computation (With Automata Theory)

* Property of STI

Page 17 of 23

Equivalence of PDA and CFG (Part 2)

This is shown in the figure

below:

From this, the following

production should be

added:

Aij → Aix Axj

PDA is at

state qi

stack height

input

string

PDA is at

state qj

string

generated

by Aix

string

generated

by Axj

PDA is at

state qx

Page 18: MELJUN CORTES Automata Lecture Equivalence of Pda and Cfg Part 2 4

Theory of Computation (With Automata Theory)

* Property of STI

Page 18 of 23

Equivalence of PDA and CFG (Part 2)

2. The stack never becomes

empty in the middle of

processing.

This means that the

symbol that was pushed at

the beginning (when the

PDA moved from state qi

to some intermediate state

qx) is exactly the same

symbol that was popped at

the end (when the PDA

moved from some

intermediate state qy to

state qj).

qi qx qy qj

stack is

empty

stack is

empty

b, c → ea, e → c

Page 19: MELJUN CORTES Automata Lecture Equivalence of Pda and Cfg Part 2 4

Theory of Computation (With Automata Theory)

* Property of STI

Page 19 of 23

Equivalence of PDA and CFG (Part 2)

From this, the following

production should be

added:

Aij → aAxyb

where a is the input symbol

read when the PDA moved

from state qi to state qx and

b is the input symbol read

when the PDA moved from

state qy to state qj. Bear in

mind that a or b can be the

empty string e.

Page 20: MELJUN CORTES Automata Lecture Equivalence of Pda and Cfg Part 2 4

Theory of Computation (With Automata Theory)

* Property of STI

Page 20 of 23

Equivalence of PDA and CFG (Part 2)

• The formal procedure for the

construction of grammar G

from PDA P is outlined below:

1. Introduce the variable

Astart,final as the start

variable of grammar G.

2. For every state qi of PDA

P, introduce the following

variables with productions

or rules:

Aii → e

3. For every state qi, qj, and

qx of PDA P, introduce the

following variables with

productions or rules:

Aij → Aix Axj

Page 21: MELJUN CORTES Automata Lecture Equivalence of Pda and Cfg Part 2 4

Theory of Computation (With Automata Theory)

* Property of STI

Page 21 of 23

Equivalence of PDA and CFG (Part 2)

4. For every state qi, qx, qy,

and qj in the PDA P, if the

following transitions exist:

introduce the following

variables with productions

or rules:

Aij → aAxyb

qi qx

qy qjb, c → e

a, e → c

Page 22: MELJUN CORTES Automata Lecture Equivalence of Pda and Cfg Part 2 4

Theory of Computation (With Automata Theory)

* Property of STI

Page 22 of 23

Equivalence of PDA and CFG (Part 2)

Formal Theorem of PDA and CFG

Equivalence

• Theorem 7:

A language is context-free if

and only if some pushdown

automaton recognizes it.

• Lemma 7.1

If a language is context-free,

then some pushdown

automaton recognizes it.

• Lemma 7.2

If a pushdown automaton

recognizes some language,

then it is context-free.

Page 23: MELJUN CORTES Automata Lecture Equivalence of Pda and Cfg Part 2 4

Theory of Computation (With Automata Theory)

* Property of STI

Page 23 of 23

Equivalence of PDA and CFG (Part 2)

• Example:

Convert the following PDA into

its corresponding context-free

grammar:

q1 q2

q3

e, e → $

0, e → 0

1, 0 → e

1, 0 → e

e, $ → e

q0