Equivalence of CFGs and PDAs Section 3 4

  • Slides: 45
Download presentation
Equivalence of CFGs and PDAs Section 3. 4 Wed, Oct 29, 2004

Equivalence of CFGs and PDAs Section 3. 4 Wed, Oct 29, 2004

Every CFG is Equivalent to a PDA Theorem: Let G = {V, , R,

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). p Proof: Let M = {K, Σ, Γ, Δ , s, F}, where p n n 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 }.

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

Every CFG is Equivalent to a PDA A typical production rule is of the form A w. p A typical tape symbol is a. p It is easy to see that this PDA accepts L(G). p e, A; w s e, e; S f a, a; e

Example p Design a PDA that accepts the language of the grammar n n

Example p Design a PDA that accepts the language of the grammar n n n S AB A a. A | e B a. Bb | e

Example s e, e; S e, S; AB e, A; a. A e, A;

Example s e, e; S e, S; AB e, A; a. A e, A; e e, B; a. Bb e, B; e f a, a; e b, b; e

Example p This PDA accepts the string aaaabb. n (s, aaaabb, e) (f, aaaabb,

Example p This PDA accepts the string aaaabb. n (s, aaaabb, e) (f, aaaabb, S) (f, aaaabb, AB) (f, aaaabb, a. AB) (f, aabb, B) (f, aabb, a. Bb) (f, abb, Bb)

Example (f, abb, a. Bbb) (f, bb, bb) (f, b, b) (f, e, e).

Example (f, abb, a. Bbb) (f, bb, bb) (f, b, b) (f, e, e).

Another Example p Design a PDA that accepts that language of the grammar n

Another Example p Design a PDA that accepts that language of the grammar n n E E + E | E * E | (E) | V V a|b|c

Another Example e, E; E + E e, E; E * E e, E;

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

Another Example p Derive the string (a + b) * c. n (s, (a

Another Example p Derive the string (a + b) * c. n (s, (a + b) * c, 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)

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

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

Nondeterminism Was nondeterminism used in the first example? p Was nondeterminism used in the

Nondeterminism Was nondeterminism used in the first example? p Was nondeterminism used in the second example? p Do these grammars lead to nondeterministic PDAs? p

Every PDA is Equivalent to a CFG Theorem: Let M = {K, Σ, Γ,

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). p Proof: p n n n 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.

Every PDA is Equivalent to a CFG n n Next, rewrite the PDA as

Every PDA is Equivalent to a CFG n n Next, rewrite the PDA as an equivalent simple PDA. For every transition ((q, a, ), (p, )) where q s, . p | | 2. p n n Modify all transitions for which | | 2. Modify all transitions for which | | > 2. Modify all transitions for which = e. See pp. 140 – 141 for details.

Every PDA is Equivalent to a CFG The nonterminals in the CFG will be

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, $}. n p, A, q can be interpreted as “The problem of getting from state p to state q while popping A from the stack. ” n As we write the rules of the grammar, will assume that the initial move ((s', e, e), (s, $)) has been taken. n In other words, we begin in state s with $ (the bottom marker) on the stack. n

Every PDA is Equivalent to a CFG p The first rule is n n

Every PDA is Equivalent to a CFG p The first rule is n n 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 Σ*. Pop $ s f'

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

Every PDA is Equivalent to a CFG p The second set of rules. n n For each transition ((q, a, B), (r, C)) where a {e} and B, C {e}, and for each p K, q, B, p a r, 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 Pop B Push C p r Pop C

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

Every PDA is Equivalent to a CFG p The third set of rules. n For each transition ((q, a, B), (r, C 1 C 2)) where a {e} and B {e}, C 1, C 2 , and for each p, p' K, q, B, p a r, C 1, p' p', C 2, p. Pop B Push C 2 Push C 1 Pop B q p p' r Pop C 1 Pop C 2

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

Every PDA is Equivalent to a CFG p The fourth set of rules. n For each q K, q, e, q e. p The grammar generated by these rules has the same language as the original PDA.

Example p Use the algorithm to find a grammar for the language of the

Example p Use the algorithm to find a grammar for the language of the following PDA. a, e; a b, a; e e, e; e s p

Modify the PDA p Add the states s′ and f ′. a, e; a

Modify the PDA p Add the states s′ and f ′. a, e; a e, e; e e, e; $ s′ b, a; e s e, $; e p f′

Modify the PDA p Replace the transitions with simple transitions. a, a; aa a,

Modify the PDA p Replace the transitions with simple transitions. a, a; aa a, $; a$ e, e; $ s′ s b, a; e e, a; a e, $; $ e, $; e p f

The First Kind of Production p The first group: S s, $, f

The First Kind of Production p The first group: S s, $, f

The Second Kind of Production The second group, for transitions ((q, a, B), (r,

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

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

The Second Kind of Production p ((p, b, a), (p, e)) n n n p p, a, s b p, e, s p, a, p b p, e, p p, a, f b p, e, f ((p, e, $), (f ′, e)) n n n p, $, s f, e, s p, $, p f, e, p p, $, f f, e, f

The Third Kind of Production p p The third group, for transitions ((q, a,

The Third Kind of Production p p The third group, for transitions ((q, a, B), (r, C 1 C 2)): ((s, a, a), (s, aa)) n n n n n s, a, s a s, a, s a s, a, p p, a, s s, a, s a s, a, f f, a, s s, a, p a s, a, s s, a, p a s, a, p p, a, p s, a, p a s, a, f f, a, p s, a, f ′ a s, a, s s, a, f ′ a s, a, p p, a, f s, a, f ′ a s, a, f f, a, f

The Third Kind of Production p p The third group, for transitions ((q, a,

The Third Kind of Production p p The third group, for transitions ((q, a, B), (r, C 1 C 2)): ((s, a, $), (s, a$)) n n n n n s, $, s a s, a, s s, $, s a s, a, p p, $, s s, $, s a s, a, f f, $, s s, $, p a s, a, s s, $, p a s, a, p p, $, p s, $, p a s, a, f f, $, p s, $, f ′ a s, a, s s, $, f ′ a s, a, p p, $, f s, $, f ′ a s, a, f f, $, f

The Fourth Kind of Production p The fourth group: n n n s, e,

The Fourth Kind of Production p The fourth group: n n n s, e, s e p, e, p e f, e, f e

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

Simplify the Grammar p Give each “nonterminal” a simple name. n n n n n 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

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

Simplify the Grammar p Give each “nonterminal” a simple name. n n n n n 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

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

Simplify the Grammar p Give each “nonterminal” a simple name. n n n n n 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

Organize the grammar p Write the productions, using the simpler names, in alphabetical order

Organize the grammar p Write the productions, using the simpler names, in alphabetical order (except for S), grouping them according to the nonterminal on the left.

Organize the grammar n n n n n S L A D | a.

Organize the grammar n n n n n S L A D | a. AA | a. BD | a. CG B E | a. AB | a. BE | a. CH C F | a. AC | a. BF | a. CI D b. W E b. X F b. Y J M | a. AJ | a. BM | a. CP K N | a. AK | a. BN | a. CQ L O | a. AL | a. BO | a. CR

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

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

Simplify the grammar This algorithm typically produces a huge list of productions, most of

Simplify the grammar This algorithm typically produces a huge list of productions, most of which are unnecessary. p We will describe a procedure that will eliminate all unnecessary productions. p

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

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. p Remove all productions involving those nonterminals. p Repeat this until there is no change. p

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

Eliminate “Dead-End” Nonterminals Those nonterminals are initially G, H, I, P, Q, R, T, U, V, W, Y, Z, . p Then we also eliminate D, F, M, N. p The remaining productions are p n n n S L A a. AA B E | a. AB | a. BE C a. AC E b. X J a. AJ

Eliminate “Dead-End” Nonterminals n n n K a. AK L O | a. AL

Eliminate “Dead-End” Nonterminals n n n K a. AK L O | a. AL | a. BO O X e e

Eliminate Purely Recursive Nonterminals Identify all nonterminals with the property that whenever they appear

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. p Remove all productions involving those nonterminals. p Repeat this until there is no change. p

Eliminate Purely Recursive Nonterminals Thus, we should eliminate productions containing A, C, J, K.

Eliminate Purely Recursive Nonterminals Thus, we should eliminate productions containing A, C, J, K. p The remaining productions are p n n n n S L B E | a. BE E b. X L O | a. BO O X e e

Eliminate Trivial Nonterminals Identify all nonterminals whose only production is to replace them with

Eliminate Trivial Nonterminals Identify all nonterminals whose only production is to replace them with the empty string or a single terminal. p Substitute e into all right sides in which they appear. p Eliminate their productions. p Repeat this until there is no change. p

Eliminate Trivial Nonterminals Thus, we should initially eliminate T, X, . p Then eliminate

Eliminate Trivial Nonterminals Thus, we should initially eliminate T, X, . p Then eliminate E, O. p The remaining productions are p n n n S L B b | a. Bb L e | a. B

Eliminate Trivial Nonterminals Identify all nonterminals whose only production replaces them with another nonterminal.

Eliminate Trivial Nonterminals Identify all nonterminals whose only production replaces them with another nonterminal. p Make the substitution in all right sides. p Eliminate their productions. p The production for the start symbol S may be handled differently since we must retain S. p

Eliminate Trivial Nonterminals p The remaining productions are n n p S e |

Eliminate Trivial Nonterminals p The remaining productions are n n p S e | a. B B b | a. Bb This is a grammar for {anbn | n 0}.

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

Examples Let = {a, b}. p Find a grammar for the language {w * | in each prefix of w, #a #b}. p Find a grammar for the language {w * | #a = #b}. p Find a grammar for the language {w * | w = xx. R for some x *}. p Find a grammar for the language {w * | w = xay for some x, y * with |x| = |y|}. p