Equivalence of CFGs and PDAs

  • Published on
    29-Jan-2016

  • View
    33

  • Download
    0

Embed Size (px)

DESCRIPTION

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. - PowerPoint PPT Presentation

Transcript

<ul><li><p>Equivalence of CFGs and PDAsSection 3.4Mon, Oct 24, 2005</p></li><li><p>Every CFG is Equivalent to a PDATheorem: 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}, whereK = {s, f}. = V.F = {f}. = {((s, e, e), (f, S))} {((f, e, A), (f, w)) | A w R} {((f, a, a), (f, e)) | a }.</p></li><li><p>Every CFG is Equivalent to a PDAA 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).</p></li><li><p>ExampleDesign a PDA that accepts the language of the grammarS ABA aA | eB aBb | e</p></li><li><p>Examplee, S; ABe, A; aAe, A; ee, B; aBbe, B; ese, e; Sa, a; eb, b; e</p></li><li><p>ExampleThis 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)</p></li><li><p>Example (f, abb, aBbb) (f, bb, Bbb) (f, bb, bb) (f, b, b) (f, e, e).</p></li><li><p>Another ExampleDesign a PDA that accepts that language of the grammarE E + E | E * E | (E) | VV a | b | c</p></li><li><p>Another Examplee, E; E + Ee, E; E * Ee, E; (E)e, E; Vse, e; Ea, a; eb, b; ec, c; ee, V; ae, V; be, V; c(, (; e), ); e+, +; e*, *; e</p></li><li><p>Another ExampleDerive 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) </p></li><li><p>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)</p></li><li><p>NondeterminismWas nondeterminism used in the last example?Do CFGs generally lead to nondeterministic PDAs by this method?</p></li><li><p>Another ExampleAnother, more intuitive, PDA that accepts the language ofE E + E | E * E | (E) | VV a | b | c</p></li><li><p>Every PDA is Equivalent to a CFGTheorem: 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.</p></li><li><p>Every PDA is Equivalent to a CFGNext, 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 || &gt; 2.Modify all transitions for which = e.See pp. 140 141 for details.</p></li><li><p>Every PDA is Equivalent to a CFGThe nonterminals in the CFG will be triples p, A, q, where p, q K and A {e, $}.p, A, q can be interpreted asThe 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.</p></li><li><p>Every PDA is Equivalent to a CFGThe first rule isS 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 *.</p></li><li><p>Every PDA is Equivalent to a CFGThe 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.qrpPop BPush CPop BPop C</p></li><li><p>Every PDA is Equivalent to a CFGThe 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.</p><p>qrpPop BPush C2Push C1Pop BPop C2p'Pop C1</p></li><li><p>Every PDA is Equivalent to a CFGThe 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.</p></li><li><p>ExampleUse the algorithm to find a grammar for the language of the following PDA.</p></li><li><p>Modify the PDAAdd the states s and f .</p></li><li><p>Modify the PDAReplace the transitions with simple transitions.</p></li><li><p>The First Kind of ProductionThe first group:S s, $, f</p></li><li><p>The Second Kind of ProductionThe second group, for transitions ((q, a, B), (r, C)):((s, e, a), (p, a))s, a, s p, a, ss, a, p p, a, ps, a, f p, a, f((s, e, $), (p, $))s, $, s p, $, ss, $, p p, $, ps, $, f p, $, f</p></li><li><p>The Second Kind of Production((p, b, a), (p, e))p, a, s bp, e, sp, a, p bp, e, pp, a, f bp, e, f((p, e, $), (f , e))p, $, s f, e, sp, $, p f, e, pp, $, f f, e, f</p></li><li><p>The Third Kind of ProductionThe third group, for transitions ((q, a, B), (r, C1C2)):((s, a, a), (s, aa))s, a, s as, a, ss, a, ss, a, s as, a, pp, a, ss, a, s as, a, ff, a, ss, a, p as, a, ss, a, ps, a, p as, a, pp, a, ps, a, p as, a, ff, a, ps, a, f as, a, ss, a, fs, a, f as, a, pp, a, fs, a, f as, a, ff, a, f</p></li><li><p>The Third Kind of ProductionThe third group, for transitions ((q, a, B), (r, C1C2)):((s, a, $), (s, a$))s, $, s as, a, ss, $, ss, $, s as, a, pp, $, ss, $, s as, a, ff, $, ss, $, p as, a, ss, $, ps, $, p as, a, pp, $, ps, $, p as, a, ff, $, ps, $, f as, a, ss, $, fs, $, f as, a, pp, $, fs, $, f as, a, ff, $, f</p></li><li><p>The Fourth Kind of ProductionThe fourth group:s, e, s ep, e, p ef, e, f e</p></li><li><p>Simplify the GrammarGive each nonterminal a simple name.A = s, a, sB = s, a, pC = s, a, fD = p, a, sE = p, a, pF = p, a, fG = f, a, sH = f, a, pI = f, a, f</p></li><li><p>Simplify the GrammarGive each nonterminal a simple name.J = s, $, sK = s, $, pL = s, $, fM = p, $, sN = p, $, pO = p, $, fP = f, $, sQ = f, $, pR = f, $, f</p></li><li><p>Simplify the GrammarGive each nonterminal a simple name.T = s, e, sU = s, e, pV = s, e, fW = p, e, sX = p, e, pY = p, e, fZ = f, e, s = f, e, p = f, e, f</p></li><li><p>Organize the grammarWrite the productions, using the simpler names, in alphabetical order (except for S), grouping them according to the nonterminal on the left.</p></li><li><p>Organize the grammarS LA D | aAA | aBD | aCGB E | aAB | aBE | aCHC F | aAC | aBF | aCID bWE bXF bYJ M | aAJ | aBM | aCPK N | aAK | aBN | aCQL O | aAL | aBO | aCR</p></li><li><p>Organize the grammarM ZN O T eX e e</p></li><li><p>Simplify the grammarThis algorithm typically produces a huge list of productions, most of which are unnecessary.We will describe a procedure that will eliminate all unnecessary productions.</p></li><li><p>Eliminate Dead-End NonterminalsIdentify 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.</p></li><li><p>Eliminate Dead-End NonterminalsThose 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 areS LA aAAB E | aAB | aBEC aACE bXJ aAJ</p></li><li><p>Eliminate Dead-End NonterminalsK aAKL O | aAL | aBOO X e e</p></li><li><p>Eliminate Purely Recursive NonterminalsIdentify 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.</p></li><li><p>Eliminate Purely Recursive NonterminalsThus, we should eliminate productions containing A, C, J, K.The remaining productions areS LB E | aBEE bXL O | aBOO X e e</p></li><li><p>Eliminate Trivial NonterminalsIdentify 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.</p></li><li><p>Eliminate Trivial NonterminalsThus, we should initially eliminate T, X, .Then eliminate E, O.The remaining productions areS LB b | aBbL e | aB</p></li><li><p>Eliminate Trivial NonterminalsIdentify 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.</p></li><li><p>Eliminate Trivial NonterminalsThe remaining productions areS aB | eB b | aBbThis is a grammar for {anbn | n 0}. </p></li><li><p>ExamplesLet = {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|}.</p></li></ul>