31
Theory of Computation (With Automata Theory) * Property of STI Page 1 of 31 Equivalence of PDA and CFG (Part 1) EQUIVALENCE OF PUSHDOWN AUTOMATA AND CONTEXT-FREE GRAMMARS (Part 1) Pushdown Automata and Context-Free Grammars Introduction A context-free grammar (CFG) can generate the strings of the context-free language it represents. A pushdown automaton (PDA) can recognize a context-free language. Are PDAs and CFGs equivalent?

Meljun cortes automata_lecture_equivalence of pda and cfg_part 1_2

Embed Size (px)

Citation preview

Page 1: Meljun cortes automata_lecture_equivalence of pda and cfg_part 1_2

Theory of Computation (With Automata Theory)

* Property of STI

Page 1 of 31

Equivalence of PDA and CFG (Part 1)

EQUIVALENCE OF PUSHDOWN AUTOMATA

AND CONTEXT-FREE GRAMMARS (Part 1)

Pushdown Automata and Context-Free

Grammars

Introduction

• A context-free grammar (CFG)

can generate the strings of the

context-free language it

represents.

• A pushdown automaton (PDA)

can recognize a context-free

language.

• Are PDAs and CFGs

equivalent?

Page 2: Meljun cortes automata_lecture_equivalence of pda and cfg_part 1_2

Theory of Computation (With Automata Theory)

* Property of STI

Page 2 of 31

Equivalence of PDA and CFG (Part 1)

• A context-free language is any

language that can be

generated by some context-

free grammar.

• Since PDAs recognize context-

free languages, can it be

claimed that a language is

context-free if and only if some

pushdown automaton

recognizes it?

• To answer the question, it

must be shown that:

1. If a language is context-

free, then some pushdown

automaton recognizes it.

2. If a pushdown automaton

recognizes some

language, then it is

context-free.

Page 3: Meljun cortes automata_lecture_equivalence of pda and cfg_part 1_2

Theory of Computation (With Automata Theory)

* Property of STI

Page 3 of 31

Equivalence of PDA and CFG (Part 1)

CFG to PDA

• A context-free language L has

a context-free grammar G that

can be used to generate or

derive the strings of the

language.

• To show that context-free

language L has some

pushdown automaton that

recognizes it, a procedure

must be established to convert

context-free grammar G of

language L into its equivalent

pushdown automaton P.

Page 4: Meljun cortes automata_lecture_equivalence of pda and cfg_part 1_2

Theory of Computation (With Automata Theory)

* Property of STI

Page 4 of 31

Equivalence of PDA and CFG (Part 1)

• Case Study:

Assume a certain context-free

language L1 is represented by

grammar G1:

S → AB

A → 0A 0

B → 0B1 ε

Recall how a grammar is used

to generate a string of the

language. To derive the string

0001 using the leftmost

derivation:

S → AB

→ 0AB

→ 00B

→ 000B1

→ 0001

Page 5: Meljun cortes automata_lecture_equivalence of pda and cfg_part 1_2

Theory of Computation (With Automata Theory)

* Property of STI

Page 5 of 31

Equivalence of PDA and CFG (Part 1)

• To show that language L1 has

a pushdown automaton that

recognizes it, a PDA P1 must

be constructed from grammar

G1. This PDA should be able

to determine if an input string

belongs to context-free

language L1.

• To do this, P1 must have the

capability to determine

whether there is a series of

substitutions using the rules of

grammar G1 that can generate

the input string.

• P1 should be able to “simulate”

the leftmost derivation of the

input string. The stack is used

to record the steps of the

derivation.

Page 6: Meljun cortes automata_lecture_equivalence of pda and cfg_part 1_2

Theory of Computation (With Automata Theory)

* Property of STI

Page 6 of 31

Equivalence of PDA and CFG (Part 1)

• As an example of how the

stack is used in simulating the

leftmost derivation of the string

0001 using grammar G1:

1. First, push the start

variable S onto the stack.

The stack contents will be:

2. Using the rule S → AB,

remove S from the stack

and replace it with AB.

The stack contents will

now be:

Stop of the

stack

A

B

top of the

stack

Page 7: Meljun cortes automata_lecture_equivalence of pda and cfg_part 1_2

Theory of Computation (With Automata Theory)

* Property of STI

Page 7 of 31

Equivalence of PDA and CFG (Part 1)

3. Using the rule A → 0A,

remove A from the stack

and replace it with 0A. The

stack contents will now be:

4. Remove 0 from the stack.

The stack contents will

now be:

A

B

top of the

stack0

A

B

top of the

stack

string

derived = 0

Page 8: Meljun cortes automata_lecture_equivalence of pda and cfg_part 1_2

Theory of Computation (With Automata Theory)

* Property of STI

Page 8 of 31

Equivalence of PDA and CFG (Part 1)

5. Using the rule A → 0,

remove A from the stack

and replace it with 0. The

stack contents will now be:

6. Remove 0 from the stack.

The stack contents will

now be:

0

B

top of the

stack

string

derived = 0

Btop of the

stack

string

derived = 0 0

Page 9: Meljun cortes automata_lecture_equivalence of pda and cfg_part 1_2

Theory of Computation (With Automata Theory)

* Property of STI

Page 9 of 31

Equivalence of PDA and CFG (Part 1)

7. Using the rule B → 0B1,

remove B from the stack

and replace it with 0B1.

The stack contents will

now be:

8. Remove 0 from the stack.

The stack contents will

now be:

B

1

top of the

stack0 string

derived = 0 0

B

1

top of the

stack

string

derived = 0 0 0

Page 10: Meljun cortes automata_lecture_equivalence of pda and cfg_part 1_2

Theory of Computation (With Automata Theory)

* Property of STI

Page 10 of 31

Equivalence of PDA and CFG (Part 1)

9. Using the rule B → ,

remove B from the stack.

The stack contents will

now be:

10. Remove 1 from the stack.

The stack contents will

now be:

1top of the

stack

string

derived = 0 0 0

top of the

stack

string

derived = 0 0 0 1

Page 11: Meljun cortes automata_lecture_equivalence of pda and cfg_part 1_2

Theory of Computation (With Automata Theory)

* Property of STI

Page 11 of 31

Equivalence of PDA and CFG (Part 1)

• PDA P1 should perform the

following actions:

1. Before any computation

starts, P1 pushes the stack

empty symbol $ onto the

stack.

2. P1 pushes the start symbol

of the grammar onto the

stack.

3. Repeat the following

actions:

a. If the top of stack is a

variable, pop that

variable from the stack.

Then, select one of the

rules for that variable

and push the right-

hand side string of that

rule onto the stack.

Page 12: Meljun cortes automata_lecture_equivalence of pda and cfg_part 1_2

Theory of Computation (With Automata Theory)

* Property of STI

Page 12 of 31

Equivalence of PDA and CFG (Part 1)

b. If the top of stack is a

terminal, pop that

terminal from the stack.

Then, compare that

terminal with the next

symbol of the input

string. If they match,

repeat step 3 and

processing continues.

If they do not match,

reject the string.

c. If the top of stack is the

stack empty symbol $

and there are no more

input symbols, accept

the input string.

Page 13: Meljun cortes automata_lecture_equivalence of pda and cfg_part 1_2

Theory of Computation (With Automata Theory)

* Property of STI

Page 13 of 31

Equivalence of PDA and CFG (Part 1)

• The following will be

performed by P1 in order to

determine if the string 0001

belongs to language L1:

1. P1 will push the stack

empty symbol $ onto the

stack. The stack contents

will now be:

2. P1 will push the start

symbol S onto the stack.

The stack contents will

now be:

$top of the

stack

S

$

top of the

stack

Page 14: Meljun cortes automata_lecture_equivalence of pda and cfg_part 1_2

Theory of Computation (With Automata Theory)

* Property of STI

Page 14 of 31

Equivalence of PDA and CFG (Part 1)

3. Since the symbol at the top

of the stack is a variable

(S), pop it from the stack

and push the right-hand

side string of a rule for S (S

→ AB). The stack contents

will now be:

4. Since the symbol at the top

of the stack is a variable

(A), pop it from the stack

and push the right-hand

side string of a rule for A (A

→ 0A). The stack contents

will now be:

B

$

top of the

stackA

B

$

top of the

stack

A

0

Page 15: Meljun cortes automata_lecture_equivalence of pda and cfg_part 1_2

Theory of Computation (With Automata Theory)

* Property of STI

Page 15 of 31

Equivalence of PDA and CFG (Part 1)

5. Since the symbol at the top

of the stack is a terminal

(0), pop this from the stack

and compare it with the

first incoming input symbol

(0). Since they match, the

computation continues.

The stack contents will

now be:

6. Since the symbol at the top

of the stack is a variable

(A), pop it from the stack

and push the right-hand

side string of a rule for A (A

→ 0). The stack contents

will now be:

B

$

top of the

stackA

B

$

top of the

stack0

Page 16: Meljun cortes automata_lecture_equivalence of pda and cfg_part 1_2

Theory of Computation (With Automata Theory)

* Property of STI

Page 16 of 31

Equivalence of PDA and CFG (Part 1)

7. Since the symbol at the top

of the stack is a terminal

(0), pop this from the stack

and compare it with the

second incoming input

symbol (0). Since they

match, the computation

continues. The stack

contents will now be:

8. Since the symbol at the top

of the stack is a variable

(B), pop it from the stack

and push the right-hand

side string of a rule for B (B

→ 0B1). The stack

contents will now be:

B

$

top of the

stack

1

$

top of the

stack

B

0

Page 17: Meljun cortes automata_lecture_equivalence of pda and cfg_part 1_2

Theory of Computation (With Automata Theory)

* Property of STI

Page 17 of 31

Equivalence of PDA and CFG (Part 1)

9. Since the symbol at the top

of the stack is a terminal

(0), pop this from the stack

and compare it with the

third incoming input symbol

(0). Since they match, the

computation continues.

The stack contents will

now be:

10. Since the symbol at the top

of the stack is a variable

(B), replace it with the

string at the right-hand side

of a rule for B (B → ).

The stack contents will

now be:

1

$

top of the

stackB

1

$

top of the

stack

Page 18: Meljun cortes automata_lecture_equivalence of pda and cfg_part 1_2

Theory of Computation (With Automata Theory)

* Property of STI

Page 18 of 31

Equivalence of PDA and CFG (Part 1)

11. Since the symbol at the top

of the stack is a terminal

(1), pop this from the stack

and compare it with the

fourth incoming input

symbol (1). Since they

match, the computation

continues. The stack

contents will now be:

12. Since the stack is empty

and there are no more

input symbols left, the

string is accepted by PDA

P1.

$top of the

stack

Page 19: Meljun cortes automata_lecture_equivalence of pda and cfg_part 1_2

Theory of Computation (With Automata Theory)

* Property of STI

Page 19 of 31

Equivalence of PDA and CFG (Part 1)

• One problem in the series of

actions outlined is that there

will be situations where there

are several choices on which

rule to use in substituting a

certain variable.

However, since a PDA is

nondeterministic, it can

therefore make guesses on

which rule to use.

In other words, the PDA will try

to use all possible rules for

substituting a certain variable.

If the string is part of the

language, then one (or more)

copies of the PDA will end up

in a final state.

Page 20: Meljun cortes automata_lecture_equivalence of pda and cfg_part 1_2

Theory of Computation (With Automata Theory)

* Property of STI

Page 20 of 31

Equivalence of PDA and CFG (Part 1)

• Observe that the PDA to be

constructed requires that an

entire string is pushed onto the

stack.

Example:

In step 8 of the previous

example, when the symbol

at the top of the stack was

B, it was popped out of the

stack and the string 0B1

was pushed onto the stack.

1

$

B

0

B

$

before after

Page 21: Meljun cortes automata_lecture_equivalence of pda and cfg_part 1_2

Theory of Computation (With Automata Theory)

* Property of STI

Page 21 of 31

Equivalence of PDA and CFG (Part 1)

This is equivalent to the

transition label , B → 0B1.

This is shown in the state

diagram below:

(qi, , B) = (qj, 0B1)

However, PDAs can only push

one symbol onto the stack as it

moves from one state to

another.

qj

qi

e, B → 0B1

Page 22: Meljun cortes automata_lecture_equivalence of pda and cfg_part 1_2

Theory of Computation (With Automata Theory)

* Property of STI

Page 22 of 31

Equivalence of PDA and CFG (Part 1)

The pushing of an entire string

onto the stack can be

implemented by introducing

intermediate states between

states qi and qj.

(qi, , B) = (qx, 1)

(qx , ) = (qy, B)

(qy , ) = (qj, 0)

qj

qi

qxe, B → 1

e, e → B

e, e → 0qy

Page 23: Meljun cortes automata_lecture_equivalence of pda and cfg_part 1_2

Theory of Computation (With Automata Theory)

* Property of STI

Page 23 of 31

Equivalence of PDA and CFG (Part 1)

• The PDA to be constructed will

have three major states

representing the actions it has

to perform in determining

whether a string belongs to a

certain language.

1. qstart – this is the start state

where the stack empty

symbol $ and then the start

variable S are pushed onto

the stack.

2. qloop – this state is where

the major processing

occurs.

3. qfinal – the final or accept

state. The PDA goes to

this state when the stack is

empty and there are no

more input symbols.

Page 24: Meljun cortes automata_lecture_equivalence of pda and cfg_part 1_2

Theory of Computation (With Automata Theory)

* Property of STI

Page 24 of 31

Equivalence of PDA and CFG (Part 1)

Transition Function of the PDA

to be constructed:

The start state qstart will have a

transition edge to state qloop

with the label

(qstart, , ) = (qloop, S$)

The transition edges for state

qloop should be able to handle

the following:

1. The top of the stack

symbol is a variable.

2. The top of the stack

symbol is a terminal.

3. The top of the stack

symbol is the stack empty

symbol $.

Page 25: Meljun cortes automata_lecture_equivalence of pda and cfg_part 1_2

Theory of Computation (With Automata Theory)

* Property of STI

Page 25 of 31

Equivalence of PDA and CFG (Part 1)

If the top of the stack symbol is

a variable A, there should be

a transition:

(qloop, , A) = (qloop, w)

where w is the right-hand side

string of the rule A → w used

to replace A.

If the top of the stack symbol is

a terminal a, there should be

a transition:

(qloop, a, a) = (qloop, )

If the top of the stack symbol is

the stack empty symbol $,

there should be a transition:

(qloop, , $) = (qfinal, )

Page 26: Meljun cortes automata_lecture_equivalence of pda and cfg_part 1_2

Theory of Computation (With Automata Theory)

* Property of STI

Page 26 of 31

Equivalence of PDA and CFG (Part 1)

The state diagram for the PDA

P to be constructed from

context-free grammar G will

be:

qstart

e, A → w

e, e → S$

qfinal

qloop

a, a → e

e, $ → e

For rule A → w

For terminal a

Page 27: Meljun cortes automata_lecture_equivalence of pda and cfg_part 1_2

Theory of Computation (With Automata Theory)

* Property of STI

Page 27 of 31

Equivalence of PDA and CFG (Part 1)

The state diagram for PDA P1

constructed from context-free

grammar G1 will be:

qstart

e, S → AB

e, e → S$

qfinal

qloop

0, 0 → ee, $ → e 1, 1 → e

e, A → 0A

e, A → 0

e, B → 0B1

e, B → e

Page 28: Meljun cortes automata_lecture_equivalence of pda and cfg_part 1_2

Theory of Computation (With Automata Theory)

* Property of STI

Page 28 of 31

Equivalence of PDA and CFG (Part 1)

Revising PDA P1 so that

intermediate states are used to

ensure that only one symbol is

pushed onto the stack per

state transition:

qstart

e, e → $

0, 0 → e

1, 1 → e

e, A → 0

e, B → ee, $ → e

qloop

qfinal

e, S →

B

e, e → A

e, A →

A

e, e → 0

e, B → 1 e, e → B e, e → 0

e, e → S

Page 29: Meljun cortes automata_lecture_equivalence of pda and cfg_part 1_2

Theory of Computation (With Automata Theory)

* Property of STI

Page 29 of 31

Equivalence of PDA and CFG (Part 1)

• Example:

Convert the following context-

free grammar G2 to a PDA P2:

S → 0T1 1

T → T0 ε

Initial PDA P2:

qstart

e, S → 0T1

e, e → S$

qfinal

qloop

0, 0 → e

e, $ → e1, 1 → e

e, S → 1

e, T → T0

e, T → e

Page 30: Meljun cortes automata_lecture_equivalence of pda and cfg_part 1_2

Theory of Computation (With Automata Theory)

* Property of STI

Page 30 of 31

Equivalence of PDA and CFG (Part 1)

Revising PDA P2 so that

intermediate states are used to

ensure that only one symbol is

pushed onto the stack per

state transition:

qstart

e, e → $

0, 0 → e

1, 1 → e

e, S → 1

e, T → e

e, $ → e

qloop

qfinal

e, S →

1

e, e → T

e, T →

0

e, e → Te, e → S

e, e → 0

Page 31: Meljun cortes automata_lecture_equivalence of pda and cfg_part 1_2

Theory of Computation (With Automata Theory)

* Property of STI

Page 31 of 31

Equivalence of PDA and CFG (Part 1)

• Since there is now a

procedure to convert a CFG to

a PDA, it can therefore be

concluded that If a language is

context free, then some PDA

recognizes it

• Exercises:

Convert the following context-

free grammars to PDAs:

1. Grammar G3

S → AB

A → aAb ε

B → cB ε

2. Grammar G4

S → SS (S) ε

3. Grammar G5

S → ε 0 1 0S0 1S1