460 likes | 667 Views
Equivalence of CFGs and PDAs. Section 3.4 Mon, Oct 24, 2005. 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.
E N D
Equivalence of CFGs and PDAs Section 3.4 Mon, Oct 24, 2005
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)) | Aw R} {((f, a, a), (f, e)) | a }.
e, A; w e, e; S s f a, a; e Every CFG is Equivalent to a PDA • A typical production rule is of the form Aw. • A typical tape symbol is a. • It is easy to see that this PDA accepts L(G).
Example • Design a PDA that accepts the language of the grammar • S AB • A aA | e • B aBb | e
f Example e, S; AB e, A; aA e, A; e e, B; aBb e, B; e e, e; S s a, a; e b, b; e
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)
Example (f, abb, aBbb) (f, bb, Bbb) (f, bb, bb) (f, b, b) (f, e, e).
Another Example • Design a PDA that accepts that language of the grammar • E E + E | E * E | (E) | V • V a | b | c
f 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 s (, (; e ), ); e +, +; e *, *; e a, a; e b, b; e c, c; e
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)
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)
Nondeterminism • Was nondeterminism used in the last example? • Do CFGs generally lead to nondeterministic PDAs by this method?
s (‘)’, ‘)’; e) (‘(’, e; ‘(’) (a, e; e) (b, e; e) (c, e; e) q (+, e; e) (*, e; e) Another Example • Another, more intuitive, PDA that accepts the language of • E E + E | E * E | (E) | V • V a | b | c
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 fF.
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.
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.
Pop $ s f ' 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 Σ*.
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. Pop B q Pop B Push C p r Pop C
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. Pop B q Pop B Push C2 Push C1 p p' Pop C2 r Pop C1
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.
a, e; a b, a; e e, e; e s p Example • Use the algorithm to find a grammar for the language of the following PDA.
a, e; a b, a; e e, e; e e, e; $ e, $; e s′ s p f ′ Modify the PDA • Add the states s′ and f′.
a, a; aa a, $; a$ b, a; e e, a; a e, $; $ e, e; $ e, $; e s′ s p f Modify the PDA • Replace the transitions with simple transitions.
The First Kind of Production • The first group: S s, $, f
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
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
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
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
The Fourth Kind of Production • The fourth group: • s, e, s e • p, e, p e • f, e, f e
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
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
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
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.
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
Organize the grammar • M Z • N • O • T e • X e • e
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.
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.
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
Eliminate “Dead-End” Nonterminals • K aAK • L O | aAL | aBO • O • X e • e
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.
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
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.
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
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.
Eliminate Trivial Nonterminals • The remaining productions are • S aB |e • B b | aBb • This is a grammar for {anbn | n 0}.
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|}.