1 / 46

Equivalence of CFGs and PDAs

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.

anitra
Download Presentation

Equivalence of CFGs and PDAs

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Equivalence of CFGs and PDAs Section 3.4 Mon, Oct 24, 2005

  2. 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  }.

  3. 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 Aw. • A typical tape symbol is a. • It is easy to see that this PDA accepts L(G).

  4. Example • Design a PDA that accepts the language of the grammar • S AB • A  aA | e • B  aBb | e

  5. 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

  6. 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)

  7. Example (f, abb, aBbb) (f, bb, Bbb) (f, bb, bb) (f, b, b) (f, e, e).

  8. Another Example • Design a PDA that accepts that language of the grammar • E E + E | E * E | (E) | V • V  a | b | c

  9. 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

  10. 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)

  11. 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)

  12. Nondeterminism • Was nondeterminism used in the last example? • Do CFGs generally lead to nondeterministic PDAs by this method?

  13. 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

  14. 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.

  15. 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.

  16. 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.

  17. 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  Σ*.

  18. 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 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. Pop B q Pop B Push C p r Pop C

  19. 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 ar, C1, p'p', C2, p. Pop B q Pop B Push C2 Push C1 p p' Pop C2 r Pop C1

  20. 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.

  21. 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.

  22. 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′.

  23. 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.

  24. The First Kind of Production • The first group: S s, $, f

  25. 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

  26. The Second Kind of Production • ((p, b, a), (p, e)) • 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)) • p, $, s f, e, s • p, $, p f, e, p • p, $, f f, e, f

  27. The Third Kind of Production • The third group, for transitions ((q, a, B), (r, C1C2)): • ((s, a, a), (s, aa)) • s, a, s as, a, ss, a, s • 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 • 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 • s, a, f ′ as, a, pp, a, f • s, a, f ′ as, a, ff, a, f

  28. The Third Kind of Production • The third group, for transitions ((q, a, B), (r, C1C2)): • ((s, a, $), (s, a$)) • s, $, s as, a, ss, $, 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 • s, $, p as, a, pp, $, p • s, $, p as, a, ff, $, p • s, $, f ′ as, a, ss, $, f • s, $, f ′ as, a, pp, $, f • s, $, f ′ as, a, ff, $, f

  29. The Fourth Kind of Production • The fourth group: • s, e, s e • p, e, p e • f, e, f e

  30. 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

  31. 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

  32. 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

  33. 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.

  34. 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

  35. Organize the grammar • M Z • N  • O  • T e • X e •  e

  36. 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.

  37. 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.

  38. 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

  39. Eliminate “Dead-End” Nonterminals • K aAK • L O | aAL | aBO • O  • X e •  e

  40. 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.

  41. 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

  42. 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.

  43. 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

  44. 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.

  45. Eliminate Trivial Nonterminals • The remaining productions are • S aB |e • B b | aBb • This is a grammar for {anbn | n 0}.

  46. 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|}.

More Related