the pumping lemma for context free grammars cpennycu/2018/assets/fa-toc-8.pdfآ  example...

Download The Pumping Lemma For Context Free Grammars cpennycu/2018/assets/fa-ToC-8.pdfآ  Example applications

Post on 10-May-2020

0 views

Category:

Documents

0 download

Embed Size (px)

TRANSCRIPT

  • The Pumping Lemma For Context Free Grammars

    Because the first Pumping Lemma got lonely.

  • Building Intuition

    What is the Pumping

    Lemma for Regular

    Languages?

    How is the Pumping

    Lemma for Regular

    Languages used?

    What is an example of a non-Context Free Language?

  • Building Intuition, Part 2: Examples Context Free Language

    ● {anbn | n ≥ 1} ● {aibjck | i = j or i = k} ● {wwR | w ∈ {0,1}*}

    Non-Context Free Language

    ● {anbncn | n ≥ 1} ● {aibjck | 0 ≤ i ≤ j ≤ k} ● {ww | w ∈ {0,1}*}

  • Building Intuition, Part 3: Pictures DFA Diagram CFG Parse Tree

  • Pumping Lemma for Context Free Languages If A is a Context Free Language, then there is a number p (the

    pumping length) where if s is any string in A of length at least p,

    then s may be divided into 5 pieces, s = uvxyz, satisfying the

    following conditions:

    a. For each i ≥ 0, uv

    i

    xy

    i

    z ∈ A,

    b. |vy| > 0, and

    c. |vxy| ≤ p.

  • Pumping Lemma (CFL) Proof Let G be a CFG for CFL A. Let b be the maximum

    number of symbols in the RHS of a rule (assume at

    least 2). In any parse tree, a node can have at most

    b children. I.e., at most b

    h

    nodes are within h steps

    to the start variable. (Implies height h limits string

    to maximum length of b

    h

    .)

    |V| is number of variables in G. The pumping

    length p = b

    |V| + 1

    . If s ∈ A and |s| ≥ p, parse tree

    must be |V| + 1 high, because b

    |V| + 1

    ≥ b

    |V|

    + 1.

    �휏 is the smallest (least number of nodes) parse tree of s. �휏 is at least |V| + 1 high, so the longest path from root is at least |V| + 1.

    That path has at least |V| + 2 nodes; one at a

    terminal, all others are variables. Hence, that path

    has at least |V| + 1 variables. With G having only

    |V| variables, some variable R appears more than

    once on that path. For convenience, we select R to

    be a variable that repeats among the lowest |V| + 1

    variables on this path.

    Divide s into uvxyz.

    Each occurrence of R has a subtree. The upper

    occurrence is larger and generates vxy. The lower

    is smaller and generates x.

  • Pumping Lemma (CFL) Proof (cont.) Both subtrees are generated by R, so one may be

    substituted for the other and still be a valid parse

    tree. Replacing the smaller with the larger yields

    uv

    i

    xy

    i

    z at each i > 1. Replacing larger by the

    smaller yields uxz. This establishes condition 1 of

    the lemma.

    v and y are not both ε. If they were, the parse tree obtained by pumping down would have fewer

    nodes than �휏, but it would still generate s. This is not possible because �휏 is a parse tree for s with the least number of nodes, and is a contradiction. This

    establishes condition 2.

    In �휏, the upper R generates vxy. R was chosen to fall within the bottom |V| + 1 variables on the path,

    and we chose the longest path in the parse tree, so

    the upper R is at most |V| + 1 high. This height can

    generate a string of at most b

    |V| + 1

    = p. This

    establishes condition 3. ∎

  • Example applications of the Pumping Lemma (CFL) B = {an bn cn | n ≥ 0}

    Is this Language a Context Free Language?

    ● If Context Free, build a CFG or PDA ● If not Context Free, prove with Pumping

    Lemma

    Proof by Contradiction:

    Assume B is a CFL, then Pumping Lemma

    must hold.

    p is the pumping length given by the PL.

    Choose s to be ap bp cp.

    Because s ∈ B and |s| ≥ p, PL guarantees s can be

    split into 5 pieces, s = uvxyz, where for any i ≥ 0,

    uv

    i

    xy

    i

    z ∈ B. Consider 2 cases:

    1. v and y contain one type of symbol. uv

    2

    xy

    2

    z

    cannot contain equal numbers of a’s, b’s and

    c’s. Thus a contradiction via condition 1 of PL.

    2. Either v or y contains more than one type of

    symbol. uv

    2

    xy

    2

    z may have same number of

    a’s, b’s, and c’s, but will be out of order, which

    is also a contradiction.

    Contradiction is unavoidable, thus B is not Context

    Free.

  • Example applications of the Pumping Lemma (CFL) C = {a

    i

    b

    j

    c

    k

    | 0 ≤ i ≤ j ≤ k}

    Is this Language a Context Free Language?

    ● If Context Free, build a CFG or PDA ● If not Context Free, prove with Pumping

    Lemma

    Proof by Contradiction:

    Assume C is a CFL, then Pumping Lemma

    must hold.

    p is the pumping length given by the PL.

    Choose s to be ap bp cp.

    Because s ∈ C and |s| ≥ p, PL guarantees s can be

    split into 5 pieces, s = uvxyz, where for any i ≥ 0,

    uv

    i

    xy

    i

    z ∈ C. Consider 2 cases:

    1. v and y contain one type of symbol.

    a. a’s not in vy. Pump down

    b. b’s not in vy. If a’s in vy, pump up. If c’s in vy,

    pump down.

    c. c’s not in vy. Pump up.

    2. Either v or y contains more than one type of

    symbol. uv

    2

    xy

    2

    z will have a’s, b’s, and c’s, but

    will be out of order, which is also a

    contradiction.

    Contradiction is unavoidable, thus C is not Context

    Free.

  • Example applications of the Pumping Lemma (CFL) D = {ww | w ∈ {0,1}*}

    Is this Language a Context Free Language?

    ● If Context Free, build a CFG or PDA ● If not Context Free, prove with Pumping

    Lemma

    Proof by Contradiction:

    Assume D is a CFL, then Pumping Lemma

    must hold.

    p is the pumping length given by the PL.

    Choose s to be 0p 1p 0p 1p.

    Because s ∈ D and |s| ≥ p, PL guarantees s can be

    split into 5 pieces, s = uvxyz, where for any i ≥ 0,

    uv

    i

    xy

    i

    z ∈ D. Remember condition 3: |vxy| ≤ p

    1. vxy does not straddle midpoint of s. Pumping

    renders s not in the form ww.

    2. vxy straddles midpoint of s. Pumping renders

    s in the form 0p 1i 0j 1p, which is not in the

    form ww.

    Contradiction is unavoidable, thus D is not Context

    Free.

    Note: 0p 1 0p 1 is not a good choice for s!

  • Tools to prove that a

    Language is not

    Context Free:

    The Pumping Lemma

    Tools to prove that a

    Language is

    Context Free:

    Context Free Grammar

    Pushdown Automata

Recommended

View more >