ppt for dbms

21
Examples for Context-free Language and Pumping Lemma CSC3130 Tutorial 5 Xiao Linfu [email protected] Department of Computer Science & Engineering Fall 2009

Transcript of ppt for dbms

  • Examples for Context-free Language and Pumping LemmaCSC3130 Tutorial 5Xiao Linfu [email protected]

    Department of Computer Science & EngineeringFall 2009

  • OutlineContext-free Languages, Context-free grammars (CFG), Push-down Automata (PDA)

    Pumping lemma

  • Relations

    Context free languageregular language

  • Relations

    Context-free Languages LContext-free Grammars GPush-down Automata ML = L(G)L = L(M)L(G) = L(M)

  • What we should know? Given a Language LNOT context-free proof by pumping lemmacontext-free design Pushdown Automata and context-free grammarNOT regular proof by pumping lemmaregular design DFA / NFA / RE

  • Example (I)Given the following CFG

    S X | Y X aXb | aX | a Y aYb | Yb | b(1) L(G) = ?(2) Design an equivalent PDA for it.

    ={a, b}

  • Example (I) --- solution: L(S)

    S X | YX aXb | aX | aY aYb | Yb | bTry to write some strings generated by it:SXaXbaaXbbaaaXbbaaaabbSYaYbaYbbaaYbbbaabbbbmore as than bsmore bs than asObservations: Start from S, we can enter two States X & Y, and X, Y are independent; In X state, always more a are generated; In Y state, always more b are generated.

    Ls = Lx U LyLx = { aibj; i>j }Lx = { aibj; i

  • Example (I) --- solution: PDA

    S X | YX aXb | aX | aY aYb | Yb | bL(S) = { aibj; ij }PDA = NFA + a stack (infinite memory)= { aibj; i>j } U { aibj; ij }LY = { aibj; i

  • Example (II)Given the following language:

    (1) design a CFG for it;(2) design a PDA for it.

    L = {0i1j: i j 2i, i=0,1,}, S = {0, 1}

  • Example (II) -- solution: CFGL = {0i1j: i j 2i, i=0,1,}, S = {0, 1}Consider two extreme cases:(a). if j = i, then L1 = { 0i1j: i=j }; (b). if j = 2i, then L2 = { 0i1j: 2i=j }.S 0S1S S 0S11S If i j 2i , then randomly choose red-rule or blue-rule in the generation.red-ruleblue-ruleS 0S1S 0S11S

  • Example (II) -- solution: CFGL = {0i1j: i j 2i, i=0,1,}, S = {0, 1} S 0S1 S 0S11 S Need to verify L = L(G)G =1). L(G) is a subset of L: The red-rule and blue-rule guarantee that in each derivation, the number of 1s generated is one or two times larger than that of 0s. So, L(G) is a subset of L.2). L is a subset of L(G): For any w = 0i1j, i j 2i, we use red-rule (2i - j) times and then blue-rule ( j - i ) times, i.e., S =*=> 02i-jS12i-j =*=> 02i-j0j-iS12(j-i)12i-j ==> 0i1j = w

  • Example (II) -- solution: PDAL = {0i1j: i j 2i, i=0,1,}, S = {0, 1}Similar idea: randomly choose two extreme cases

  • Example (III)Given the following language:

    (1). Design a CFG for it;(2). Design a PDA for it.

    L = { aibjckdl: i, j, k, l=0,1,; i+k=j+l }, where the alphabet = {a, b, c, d}

  • Example (III) solution: CFG

    L = { aibjckdl: i,j,k,l=0,1,; i+k=j+l }, Note that i + k = j + l ==> | i j | = | l k |. Assume n = i j = l k > 0, then i = n + j, l = n + k, and w = aibjckdl = anaj bj ckdkdn ajbjckdkThree blocks come from the same template:N x N xS aSd | XYX aXb | Y cYd | S-->anSdn--> anXYdn-->anajbjYdn-->anajbj ckbkdn= an+jbj ckbn+k(n+j) + k = j + (n+k)

  • Example (III) solution: PDA

    L = { aibjckdl: i,j,k,l=0,1,; i+k=j+l }, Main idea: use X to record an a or c; use Y to record an b or d.Compare #X and #Y: by cancellation.

    How to realize the comparison by cancellation?Action1: Push an X, when a or c was read;Action2: Pop an X (if any, otherwise push a Y), when b or d was read.Action3: Pop an Y (if any, otherwise push an X), when a or c was read.

  • Example (III) solution: PDA

    L = { aibjckdl: i,j,k,l=0,1,; i+k=j+l }, Action1: Push an X, when a or c was read;Action2: Pop an X (if any, otherwise push a Y), when b or d was read.Action3: Pop an Y (if any, otherwise push an X), when a or c was read.

  • OutlineContext-free Languages, Context-free grammars (CFG), Push-down Automata (PDA)Pumping Lemma

  • Pumping lemma for context-free languagesTheorem: For every context-free language L

    There exists a number n such that for every string z in L, we can write z = uvwxy where |vwx| n |vx| 1 For every i 0, the string uviwxiy is in L.wuyxv

  • Example(4)12L1 = {0n1n0n1n | n 0}choose nwrite z = uvwxyz = 0n1n0n1ni = 21. Neither v nor x can contain both 0 and 1We try to argue that z = uv2wx2y is NOT in L1

  • Example(5)12L2 = {0n#02n#3n | n 0 }choose nwrite z = uvwxyz = 0n#02n#03ni = 21. Neither v nor x can contain #2. 1:2:3 ratio cant be maintained since at least one segment NOT in v and x We try to argue that z = uv2wx2y is NOT in L2

  • End of this tutorial!Thanks for coming!