Transcript
Page 1: Equivalence of CFGs and PDAs

Equivalence of CFGs and PDAs

Section 3.4

Mon, Oct 24, 2005

Page 2: Equivalence of CFGs and PDAs

Every CFG is Equivalent to a PDA Theorem: Let G = {V, , R, S} be a context-free

grammar. There exists a PDA M such that L(M) = L(G).

Proof: Let M = {K, Σ, Γ, Δ, s, F}, where K = {s, f}. Γ = V. F = {f}. Δ = {((s, e, e), (f, S))}

{((f, e, A), (f, w)) | A w R}

{((f, a, a), (f, e)) | a }.

Page 3: Equivalence of CFGs and PDAs

Every CFG is Equivalent to a PDA A typical production rule is of the form

A w. A typical tape symbol is a. It is easy to see that this PDA accepts L(G).

se, e; S

a, a; e

e, A; w

f

Page 4: Equivalence of CFGs and PDAs

Example Design a PDA that accepts the language of the

grammar S AB A aA | e B aBb | e

Page 5: Equivalence of CFGs and PDAs

Example

e, S; ABe, A; aAe, A; e

e, B; aBbe, B; e

se, e; S

a, a; eb, b; e

f

Page 6: Equivalence of CFGs and PDAs

Example This PDA accepts the string aaaabb.

(s, aaaabb, e) (f, aaaabb, S)

(f, aaaabb, AB)

(f, aaaabb, aAB)

(f, aaabb, AB)

(f, aaabb, aAB)

(f, aabb, AB)

(f, aabb, B)

(f, aabb, aBb)

(f, abb, Bb)

Page 7: Equivalence of CFGs and PDAs

Example (f, abb, aBbb)

(f, bb, Bbb)

(f, bb, bb)

(f, b, b)

(f, e, e).

Page 8: Equivalence of CFGs and PDAs

Another Example Design a PDA that accepts that language of the

grammar E E + E | E * E | (E) | V V a | b | c

Page 9: Equivalence of CFGs and PDAs

Another Example

e, E; E + Ee, E; E * Ee, E; (E)e, E; V

se, e; E

a, a; eb, b; ec, c; e

f

e, V; ae, V; be, V; c

(, (; e), ); e+, +; e*, *; e

Page 10: Equivalence of CFGs and PDAs

Another Example Derive the string (a + b) * c.

(s, (a + b) * c, e) (f, (a + b) * c, E) (f, (a + b) * c, E * E) (f, (a + b) * c, (E) * E) (f, a + b) * c, E) * E) (f, a + b) * c, E + E) * E) (f, a + b) * c, V + E) * E) (f, a + b) * c, a + E) * E) (f, + b) * c, + E) * E) (f, b) * c, E) * E)

Page 11: Equivalence of CFGs and PDAs

Another Example (f, b) * c, N) * E)

(f, b) * c, b) * E)

(f, ) * c, ) * E)

(f, * c, * E)

(f, c, E)

(f, c, V)

(f, c, c)

(f, e, e)

Page 12: Equivalence of CFGs and PDAs

Nondeterminism Was nondeterminism used in the last example? Do CFGs generally lead to nondeterministic PDAs by

this method?

Page 13: Equivalence of CFGs and PDAs

Another Example Another, more intuitive, PDA that accepts the

language of E E + E | E * E | (E) | V V a | b | c

qs

(a, e; e)(b, e; e)(c, e; e)

(‘(’, e; ‘(’)

(+, e; e)(*, e; e)

(‘)’, ‘)’; e)

Page 14: Equivalence of CFGs and PDAs

Every PDA is Equivalent to a CFG Theorem: Let M = {K, Σ, Γ, Δ, s, F} be a push-down

automaton. There exists a CFG G such that L(G) = L(M).

Proof: Create a new start state s' and a new final state f ' and a new

symbol $. Create the transition ((s', e, e), (s, $)). Create the transitions ((f, e, $), (f ', e)) for every f F.

Page 15: Equivalence of CFGs and PDAs

Every PDA is Equivalent to a CFG

Next, rewrite the PDA as an equivalent simple PDA. For every transition ((q, a, ), (p, )) where q s,

. || 2.

Modify all transitions for which || 2. Modify all transitions for which || > 2. Modify all transitions for which = e. See pp. 140 – 141 for details.

Page 16: Equivalence of CFGs and PDAs

Every PDA is Equivalent to a CFG

The nonterminals in the CFG will be triples p, A, q, where p, q K and A {e, $}.

p, A, q can be interpreted as

“The problem of getting from state p to state q while popping A from the stack.”

As we write the rules of the grammar, will assume that the initial move ((s', e, e), (s, $)) has been taken.

In other words, we begin in state s with $ (the bottom marker) on the stack.

Page 17: Equivalence of CFGs and PDAs

Every PDA is Equivalent to a CFG The first rule is

S s, $, f '. This represents the problem of getting from s to f ', popping

$ along the way. In other words, (s, w, $) * (f ', v, e) for some w, v Σ*.

s f 'Pop $

Page 18: Equivalence of CFGs and PDAs

Every PDA is Equivalent to a CFG The second set of rules.

For each transition ((q, a, B), (r, C)) where a {e} and B, C {e}, and for each p K,

q, B, p ar, C, p. This represents replacing the problem of getting from q to

p, popping B, with the problem of getting from r to p, reading a and popping C.

q

r

pPop BPush C

Pop B

Pop C

Page 19: Equivalence of CFGs and PDAs

Every PDA is Equivalent to a CFG The third set of rules.

For each transition ((q, a, B), (r, C1C2)) where a {e} and B {e}, C1, C2 , and for each p, p' K,

q, B, p ar, C1, p'p', C2, p.

q

r

pPop BPush C2

Push C1

Pop B

Pop C2

p'

Pop C1

Page 20: Equivalence of CFGs and PDAs

Every PDA is Equivalent to a CFG The fourth set of rules.

For each q K,

q, e, q e.

The grammar generated by these rules has the same language as the original PDA.

Page 21: Equivalence of CFGs and PDAs

Example Use the algorithm to find a grammar for the language

of the following PDA.

ps

a, e; a

e, e; e

b, a; e

Page 22: Equivalence of CFGs and PDAs

Modify the PDA Add the states s′ and f ′.

ps

a, e; a

e, e; e

b, a; e

s′ f ′e, e; $ e, $; e

Page 23: Equivalence of CFGs and PDAs

Modify the PDA Replace the transitions with simple transitions.

ps

a, a; aaa, $; a$

e, a; ae, $; $

b, a; e

s′ fe, e; $ e, $; e

Page 24: Equivalence of CFGs and PDAs

The First Kind of Production The first group:

S s, $, f

Page 25: Equivalence of CFGs and PDAs

The Second Kind of Production The second group, for transitions ((q, a, B), (r, C)): ((s, e, a), (p, a))

s, a, s p, a, s s, a, p p, a, p s, a, f p, a, f

((s, e, $), (p, $)) s, $, s p, $, s s, $, p p, $, p s, $, f p, $, f

Page 26: Equivalence of CFGs and PDAs

The Second Kind of Production ((p, b, a), (p, e))

p, a, s bp, e, s p, a, p bp, e, p p, a, f bp, e, f

((p, e, $), (f ′, e)) p, $, s f, e, s p, $, p f, e, p p, $, f f, e, f

Page 27: Equivalence of CFGs and PDAs

The Third Kind of Production The third group, for transitions ((q, a, B), (r, C1C2)):

((s, a, a), (s, aa)) s, a, s as, a, ss, a, s s, a, s as, a, pp, a, s s, a, s as, a, ff, a, s s, a, p as, a, ss, a, p s, a, p as, a, pp, a, p s, a, p as, a, ff, a, p s, a, f ′ as, a, ss, a, f s, a, f ′ as, a, pp, a, f s, a, f ′ as, a, ff, a, f

Page 28: Equivalence of CFGs and PDAs

The Third Kind of Production The third group, for transitions ((q, a, B), (r, C1C2)):

((s, a, $), (s, a$)) s, $, s as, a, ss, $, s s, $, s as, a, pp, $, s s, $, s as, a, ff, $, s s, $, p as, a, ss, $, p s, $, p as, a, pp, $, p s, $, p as, a, ff, $, p s, $, f ′ as, a, ss, $, f s, $, f ′ as, a, pp, $, f s, $, f ′ as, a, ff, $, f

Page 29: Equivalence of CFGs and PDAs

The Fourth Kind of Production The fourth group:

s, e, s e p, e, p e f, e, f e

Page 30: Equivalence of CFGs and PDAs

Simplify the Grammar Give each “nonterminal” a simple name.

A = s, a, s B = s, a, p C = s, a, f D = p, a, s E = p, a, p F = p, a, f G = f, a, s H = f, a, p I = f, a, f

Page 31: Equivalence of CFGs and PDAs

Simplify the Grammar Give each “nonterminal” a simple name.

J = s, $, s K = s, $, p L = s, $, f M = p, $, s N = p, $, p O = p, $, f P = f, $, s Q = f, $, p R = f, $, f

Page 32: Equivalence of CFGs and PDAs

Simplify the Grammar Give each “nonterminal” a simple name.

T = s, e, s U = s, e, p V = s, e, f W = p, e, s X = p, e, p Y = p, e, f Z = f, e, s = f, e, p = f, e, f

Page 33: Equivalence of CFGs and PDAs

Organize the grammar Write the productions, using the simpler names, in

alphabetical order (except for S), grouping them according to the nonterminal on the left.

Page 34: Equivalence of CFGs and PDAs

Organize the grammar S L A D | aAA | aBD | aCG B E | aAB | aBE | aCH C F | aAC | aBF | aCI D bW E bX F bY J M | aAJ | aBM | aCP K N | aAK | aBN | aCQ L O | aAL | aBO | aCR

Page 35: Equivalence of CFGs and PDAs

Organize the grammar M Z N O T e X e e

Page 36: Equivalence of CFGs and PDAs

Simplify the grammar This algorithm typically produces a huge list of

productions, most of which are unnecessary. We will describe a procedure that will eliminate all

unnecessary productions.

Page 37: Equivalence of CFGs and PDAs

Eliminate “Dead-End” Nonterminals Identify all nonterminals that do not appear on the left

side of any production or that do not appear on the right side of any production.

Remove all productions involving those nonterminals.

Repeat this until there is no change.

Page 38: Equivalence of CFGs and PDAs

Eliminate “Dead-End” Nonterminals Those nonterminals are initially G, H, I, P, Q, R, T,

U, V, W, Y, Z, . Then we also eliminate D, F, M, N. The remaining productions are

S L A aAA B E | aAB | aBE C aAC E bX J aAJ

Page 39: Equivalence of CFGs and PDAs

Eliminate “Dead-End” Nonterminals

K aAK L O | aAL | aBO O X e e

Page 40: Equivalence of CFGs and PDAs

Eliminate Purely Recursive Nonterminals Identify all nonterminals with the property that

whenever they appear on the left side of a production, they also appear on the right side.

Remove all productions involving those nonterminals.

Repeat this until there is no change.

Page 41: Equivalence of CFGs and PDAs

Eliminate Purely Recursive Nonterminals Thus, we should eliminate productions containing A,

C, J, K. The remaining productions are

S L B E | aBE E bX L O | aBO O X e e

Page 42: Equivalence of CFGs and PDAs

Eliminate Trivial Nonterminals Identify all nonterminals whose only production is to

replace them with the empty string or a single terminal.

Substitute e into all right sides in which they appear. Eliminate their productions. Repeat this until there is no change.

Page 43: Equivalence of CFGs and PDAs

Eliminate Trivial Nonterminals Thus, we should initially eliminate T, X, . Then eliminate E, O. The remaining productions are

S L B b | aBb L e | aB

Page 44: Equivalence of CFGs and PDAs

Eliminate Trivial Nonterminals Identify all nonterminals whose only production

replaces them with another nonterminal. Make the substitution in all right sides. Eliminate their productions. The production for the start symbol S may be handled

differently since we must retain S.

Page 45: Equivalence of CFGs and PDAs

Eliminate Trivial Nonterminals The remaining productions are

S aB | e B b | aBb

This is a grammar for {anbn | n 0}.

Page 46: Equivalence of CFGs and PDAs

Examples Let = {a, b}. Find a grammar for the language

{w * | in each prefix of w, #a #b}. Find a grammar for the language

{w * | #a = #b}. Find a grammar for the language

{w * | w = xxR for some x *}. Find a grammar for the language

{w * | w = xay for some x, y * with |x| = |y|}.