1 context-free grammars: introductiongoldberg/14-cc/notes/notes-cfg.pdfa production is useless if it...

25
Computing is recognizing/describing/generating/accepting a language. 1 Context-free grammars: introduction Two functions associated with languages: reading: for language recognizers; writing: for language generators One way to generate a language is to use “expressions,” for example, regular expressions. A different way is to use “substitution rules.” Generating L = {a} : rules application R1: S aS R2: S e rule #1 S aS rule #1 a 2 S rule #1 a 3 S rule #1 a 4 S rule #2 a 6 Generating L = {a} ∪{b} rules application R1: S M R2: S N R3: M aM R4: N bN R5: M e R6: N e rule #1 S M rule #3 aM ... ......... rule #5 a m rule #2 S N ... ......... rule #6 b n Problem 1 What does this grammar generate? 1

Upload: others

Post on 19-Sep-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Context-free grammars: introductiongoldberg/14-CC/Notes/notes-cfg.pdfA production is useless if it contains any useless variable. Theorem 11 For every grammar G, there is an equivalent

Computing is recognizing/describing/generating/accepting a language.

1 Context-free grammars: introduction

Two functions associated with languages:

reading: for language recognizers; writing: for language generators

One way to generate a language is to use “expressions,” for example, regular expressions. A differentway is to use “substitution rules.”

Generating L = {a}∗:

rules application

R1: S → aSR2: S → e

rule #1 S ⇒ aSrule #1 ⇒ a2Srule #1 ⇒ a3Srule #1 ⇒ a4Srule #2 ⇒ a6

Generating L = {a}∗ ∪ {b}∗

rules application

R1: S → MR2: S → NR3: M → aMR4: N → bNR5: M → eR6: N → e

rule #1 S ⇒ Mrule #3 ⇒ aM. . . . . . . . . . . .rule #5 ⇒ am

rule #2 S ⇒ N. . . . . . . . . . . .rule #6 ⇒ bn

Problem 1 What does this grammar generate?

1

Page 2: 1 Context-free grammars: introductiongoldberg/14-CC/Notes/notes-cfg.pdfA production is useless if it contains any useless variable. Theorem 11 For every grammar G, there is an equivalent

rules application

R1: S → aSbR2: S → e

rule #1 S ⇒ aSbrule #1 ⇒ aaSbbrule #1 ⇒ aaaSbbbrule #2 ⇒ aaabbb

Problem 2 Consider the grammar given in the table below. Show the derivation of the string

aboywithaflowerseesthegirl

How to improve the grammar so that it would generate words separated by blanks?

1 〈SENTENCE〉 → 〈NOUN -PHRASE〉〈V ERB-PHRASE〉

2 〈NOUN -PHRASE〉 → 〈CMPLX-NOUN〉 |3 〈CMPLX-NOUN〉〈PREP -PHRASE〉

4 〈V ERB-PHRASE〉 → 〈CMPLX-V ERB〉 |5 〈CMPLX-V ERB〉〈PREP -PHRASE〉

6 〈PREP -PHRASE〉 → 〈PREP 〉〈CMPLX-NOUN〉

7 〈CMPLX-NOUN〉 → 〈ARTICLE〉〈NOUN〉

8/9 〈CMPLX-V ERB〉 → 〈V ERB〉 |〈V ERB〉 〈NOUN〉-〈PHRASE〉

10/11 〈ARTICLE〉 → a|the

12/13/14 〈NOUN〉 → boy|girl|flower

15/16 〈V ERB〉 → likes|sees

17 〈PREP 〉 → with

Definition 3 A context-free grammar G is a quadruple (V,Σ, R, S) where

V set of variables (nonterminals)Σ set of terminals; Σ ∩ V = ∅R set of rules; a finite subset of V × (V ∪ Σ)∗

S start nonterminal.

2

Page 3: 1 Context-free grammars: introductiongoldberg/14-CC/Notes/notes-cfg.pdfA production is useless if it contains any useless variable. Theorem 11 For every grammar G, there is an equivalent

Notations:A →G u or A → u: (A,u) ∈ R

u ⇒ v: u yields v; ∃x, y,w ∈ (V ∪ Σ)∗, A ∈ V such thatu = xAy; v = xwy; A → w

⇒∗G: the reflexive, transitive closure of ⇒G

L(G): the language generated by G;L(G) = {w : w ∈ Σ∗&S ⇒∗

G w}

derivation v0 ⇒G v1 ⇒G v2 . . . ⇒G vp

p the length of the derivation

Problem 4 Consider the grammar G = (V,Σ, R, S) where V = {S,A}; Σ = {a, b}; and

R = {S → AA; A → AAA | bA | Ab | a}.

1. Which strings of L(G) can be produced by derivations of four or fewer steps?

2. Give at least three distinct derivations for the string babbab;

3. For any m,n, p ≥ 0, describe a derivation in G of the string bmabnabp;

4. Prove that L(G) comprises the set of all strings in Σ∗ that contain a positive even number ofa’s.

Definition 5 A parse (derivation) tree of a grammar G = (V,Σ, R, S) is a labeled rooted treeobtained through the use of the following operations:

• A one node tree labeled A where A ∈ V ; the single node of this tree is the root and the leaf;the yield is A.

• If A → w is a rule in G, then the root (resp. leaf) of this tree is the node labeled A (resp.w); the yield is w.

• If T1 . . . , Tn are parse trees with roots labeled A1, . . . , An and with yields w1, . . . , wn respec-tively and A → A1 . . . An is a rule in R, then the tree T with a root label A whose n subtreesare T1, . . . , Tn is also a parse tree. The leaves of T are the leaves of all Ti’s and the yield ofT is w1 . . . wn.

3

Page 4: 1 Context-free grammars: introductiongoldberg/14-CC/Notes/notes-cfg.pdfA production is useless if it contains any useless variable. Theorem 11 For every grammar G, there is an equivalent

A A A

A A A

A

1 2 n

1 2 n

1 2 n

w w w1 2 n

w w w

w = w w . . . wn21

new yield

2 Leftmost derivations

Definition 6 A derivation is called leftmost(resp. rightmost) if the leftmost (resp. rightmost)nonterminal is always expanded.

Proposition 7 For every for w ∈ L(G), where G is a context-free grammar, there is a leftmostderivation of w.

Proof: Induction on the location of a non-leftmost step in a given derivation.

Given a non-leftmost derivation of a word w, we construct a new derivation of w for which a non-leftmost step, if any, is farther to the right than it was in the original derivation. We do it withoutincreasing the total length of the derivation.

We start by locating the 1st occurrence of a non-leftmost derivation.

S = w0 ⇒ w1 ⇒ . . . wk ⇒ wk+1 ⇒ . . . wn ∈ Σ∗

the first occurrence of a non-leftmost derivation

Initial derivation:

. . . ⇒ wk = αAβBγ ⇒ αAβδγ ⇒ . . . ⇒ αAǫ ⇒ αζǫ ⇒ . . .

B → δ βδγ ⇒ ǫ A → ζ

4

Page 5: 1 Context-free grammars: introductiongoldberg/14-CC/Notes/notes-cfg.pdfA production is useless if it contains any useless variable. Theorem 11 For every grammar G, there is an equivalent

New derivation:

. . . ⇒ wk = αAβBγ ⇒ αζβBγ ⇒ αζβδγ ⇒ . . . ⇒ αζǫ ⇒ . . .

A → ζ B → δ βδγ ⇒ ǫ

To construct a leftmost derivation, we repeat the transformation above as long as there are occur-rences of non-leftmost derivations.

Problem 8 Given a grammar

G : S → SS|bA|Ba|e; A → aa; B → bb

and a derivation

S ⇒ SS ⇒ SBa ⇒ SSBa ⇒ SSbba ⇒ SbAbba ⇒ Sbaabba ⇒ baabba,

construct the leftmost derivation of baabba.

Solution. The 1st occurrence of the non-leftmost step is in step 2:

. . . ⇒ SS ⇒ SBa ⇒ . . . .

Instead of expanding the 1st S we expanded the 2nd one. According to the algorithm from theproof, we find the step which expands the 1st S. In our example, this is step #3. Thus, by thealgorithm, we switch these two steps (#2 and #3). Remember, if the steps were not neighbors, butwere separated by other steps, we would in addition, have placed the separating steps after thesetwo. The resulting derivation is as follows:

S ⇒ SS ⇒ SSS ⇒ SSBa ⇒ SSbba ⇒ SbAbba ⇒ Sbaabba ⇒ baabba.

The new derivation is not leftmost, but the first occurrence of a non-leftmost step is now fartherfrom the beginning although the total number of steps didn’t change. The 1st non-leftmost stepis now #3: . . . ⇒ SSS ⇒ SSBa ⇒ . . .. Again, we find the step which expands the 1st S (as youcan see, the nonterminal which was supposed to be expanded, but wasn’t, happened to be S again).This time, the step dealing with the missed non-terminal is #7; it uses the rule S → e. Thus,according to the algorithm, in the new derivation, we preserve the first two steps, then execute step#7, followed by step #3, followed by the steps #4, 5, and 6. The result is:

S ⇒ SS ⇒ SSS ⇒ SS ⇒ SBa ⇒ Sbba ⇒ bAbba ⇒ baabba.

One can continue applying the algorithm pushing the non-leftmost steps to the right, until all ofthem are removed. On a side, one can also see that the final derivation can be simplified, givingrise to a shorter left-most derivation.

5

Page 6: 1 Context-free grammars: introductiongoldberg/14-CC/Notes/notes-cfg.pdfA production is useless if it contains any useless variable. Theorem 11 For every grammar G, there is an equivalent

3 Transforming CFG’s.

Theorem 9

Let a context-free grammar G(V,Σ, R, S) contain a production of the form

A → uBv, where A,B ∈ V and u, v ∈ (V ∪ Σ)∗

and let the set of all productions for substituting B 6= A is

B → w1|w2| . . . |wn.

Construct a new grammar H by removing A → uBv and adding A → uwiv (i = 1, . . . , n). Then

L(G) = L(H).

Definition 10

A variable A is useful for a grammar G, if at least one derivation of a string in L(G) contains A.Else, A is called useless. A production is useless if it contains any useless variable.

Theorem 11

For every grammar G, there is an equivalent grammar H (L(H) = L(G)), without useless variablesand useless productions.

Step 1.

Procedure Eliminate (G(V,Σ, R, S))V1 = ∅;while (not done)V1 = V1∪ {all productions in G of the form A → w where w ∈ (V1 ∪ Σ)∗}

Step 2.

Based on G1, construct a dependency graph, defined as follows:

the set of vertices are variables of the grammar;there is a directed edge (A,B) iff there is a production A → uBv.

Remove all variables that cannot be reached from S. Remove all productions containing removedvariables. The resulting grammar H has no useless variables or productions.

Definition 12

A production of the form A → ǫ is called a null-production (λ-production). A variable A for whichthe derivation A ⇒∗ ǫ, is called nullable.

Theorem 13

6

Page 7: 1 Context-free grammars: introductiongoldberg/14-CC/Notes/notes-cfg.pdfA production is useless if it contains any useless variable. Theorem 11 For every grammar G, there is an equivalent

If ǫ 6∈ L(G), then there is a grammar H such that

(1) L(G) = L(H), and(2) H has no null productions.

Definition 14

A production of the form A → B, where A,B ∈ V , is called a unit-production.

Theorem 15

If ǫ 6∈ L(G), then there is a grammar H such that

(1) L(G) = L(H), and(2) H has no unit productions.

Proof (idea). Find, for each variable A, all variables B 6= A, such that A ⇒∗ B. Include in H allnon-unit productions. Then for every pair A,B with A ⇒∗ B, add to H all rules A → w, for everyrule B → w currently in H.

4 Regular CFG’s.

Definition 16 A context free grammar is called regular if for every production T → w of G, allletters of w, with a possible exception for the last one, are terminals.

R ⊆ V × Σ∗(V ∪ {e}).

Example:V = {S, T}; Σ = {a, b}

S → abaTT → bbaS|aa

Theorem 17 A language is regular iff it is generated by a regular grammar.

Proof. We prove the statement by presenting two constructions. The first one constructs, fora given DFA, an equivalent regular grammar. The second one constructs, for a given regulargrammar, an equivalent NFA (non-deterministic finite automaton).

⇒ Let M = (Q,Σ, δ, q0, F ) be a DFA, then the regular grammar G = (V,Σ, R, S) is defined asfollows:

• set V of variables is the set Q of states in M ;

7

Page 8: 1 Context-free grammars: introductiongoldberg/14-CC/Notes/notes-cfg.pdfA production is useless if it contains any useless variable. Theorem 11 For every grammar G, there is an equivalent

• alphabet Σ is the same as for the DFA;

• the start variable S of G is q0;

• for every transaction q, p labeled with a ∈ Σ, i.e. q = δ(p, a), there is a rule p → aq in thegrammar;

• for every final state f ∈ F , there is a rule f → ǫ.

One can easily prove that the grammar generates the language L(M) which is accepted by M . Endof ⇒

⇐ Let G = (V,Σ, R, S) be a regular grammar. Before constructing an equivalent finite automaton,modify G as follows:

• replace every rule A → a1a2 · · · akB, where k ≥ 2 and B is a variable, with the rules A → a1A1,A1 → a2A2, . . . , Ak−1 → akB. Here A1, . . . , Ak are new variables.

• replace every rule A → a1a2 · · · ak, where k ≥ 2, with the rules A → a1A1, A1 → a2A2, . . . ,Ak−1 → ak. Here A1, . . . , Ak−1 are new variables.

Obviously, the new grammar is equivalent to the initial one. All rules in the new grammar G′ areof the form

A → aB or A → a,

where a is a terminal or the empty string ǫ, and B is a variable.

To construct an equivalent NFA,

• define every variable in V as a state of the new NFA;

• for every rule A → aB, introduce a transaction (A,B) labeled with a letter a;

• for every rule A → a (here a is either a letter or ǫ, the empty string), introduce a new stateF and a transaction (A,F ) labeled a; make F a final state;

• define the start variable to be the start state of the automaton.

One easily proves that the new NFA accepts the language generated by the grammar. End of ⇐

5 Chomsky Normal Form.

Definition 18 A context-free grammar is in Chomsky normal form if every rule is of the form

8

Page 9: 1 Context-free grammars: introductiongoldberg/14-CC/Notes/notes-cfg.pdfA production is useless if it contains any useless variable. Theorem 11 For every grammar G, there is an equivalent

A → BC A,B,C are variables; B,C 6= SA → a a is a terminal;S → ǫ S is the start variable.

Theorem 19 If L is context-free, then there is a context-free grammar in Chomsky normal formH which generates this language.

Step 1. Add a new start variable S0 and a new rule S0 → S, where S is the old start variable.

Step 2. For every A → ǫ, where A 6= S,

• remove it;

• ∀B → uAv, replace it with B → uv;/*If B → uAvAw, then we first add B → uvAw, then B → uAvw and then B → uvw.*/

• If A → B, add B → ǫ, unless this rule was previously removed.

Repeat step 2 until all ǫ-rules not involving S are eliminated.

Step 3. For every rule A → B and for every rule B → w,

• remove A → B;

• add A → w, unless that was previously removed.

Repeat step 3 until all unit rules are eliminated.

Step 4. For every rule A → u1u2 · · · uk, where k ≥ 3, replace it with the rules

A → u1A1; A1 → u2A2; . . . , Ak−2 → uk−1uk,

where Ai’s are new variables.

For k > 1, replace any terminal ui in the preceding rules with the new variable Ui and addthe rule Ui → ui.

6 Some problems for CFG’s and some solutions.

Problem 20 Construct four parse trees for the four derivations of the string babbab in the grammarG of problem 4 (section 1).

Problem 21 Consider the grammar G = (V,Σ, R, S) whereV = {S,A}; Σ = {a, b}; and

R = S → aAa | bAb | ǫA → SS.

9

Page 10: 1 Context-free grammars: introductiongoldberg/14-CC/Notes/notes-cfg.pdfA production is useless if it contains any useless variable. Theorem 11 For every grammar G, there is an equivalent

1. Which strings of L(G) can be produced by derivations of four or fewer steps?

2. Give a derivation of baabbb in G;

3. Construct the parse tree of the derivation from #2;

4. Describe L(G) in English?

Problem 22 Construct context-free grammars that generate each of the following languages:

1. L = {wcwR : w ∈ (a ∪ b)∗};

2. L = {wwR : w ∈ (a ∪ b)∗};

3. L = {w ∈ (a ∪ b)∗} : w = wR};

4. L = {ambn : m ≥ n};

5. L = {ambncpdq : m + n = p + q};

6. L = {w ∈ {a, b}∗ : na(w) = nb(w)};(na(w) denotes the number of occurrences of letter a in the string w)

7. L = {w ∈ {a, b}∗ : na(w) = 2nb(w)};

8. L = {uawb : u,w ∈ {a, b}∗&|w| = |u|};

Solutions to some problems:

• Construct a CFG that generates

L = {w ∈ {a, b}∗ : na(w) = nb(w)}.

Solution. The grammar which generates the language is as follows:

G : S → aSb|bSa|SS|e

Call property A of a string w to have na(w) = nb(w). It is easy to see that every string w ∈ L(G)has property A. The reverse statement—every string with property A is in L—is also true but theproof is not evident.

We prove it by induction on the length of |w|. Obviously, every string with property A must havean even length. Thus, the base of the induction is two strings of length 2. It is easy to see thatthey are in L.

Assume that the reverse statement is true for all strings of length n ≥ 2, and let |w| > n. If the firstand the last letters of w are distinct, w = σ1w1σ2, σ1 6= σ2, then using the appropriate production,we reduce the statement to that on w1 which is correct by the induction hypothesis.

10

Page 11: 1 Context-free grammars: introductiongoldberg/14-CC/Notes/notes-cfg.pdfA production is useless if it contains any useless variable. Theorem 11 For every grammar G, there is an equivalent

Let us now consider the case of the first and the last letters of w being equal, say w = aw1a.Moving from left to right, we add 1 for every a and subtract 1 for every b. After the first a, thecounter is 1; since at the end it must be 0, its value just before the last a must be -1.Since every step changes the counter by 1, there must be a moment at which the value of thecounter is 0. Therefore, w can be presented as w1w2 so that both w1w2 where w1 and w2 haveproperty A.To generate w using G, we use S → SS and derivations of w1 and w2 (the latter exist by inductionhypothesis).

• Construct a CFG that generates

L = {w ∈ {a, b}∗ : na(w) = 2nb(w)}.

Solution. The grammar which generates the language is as follows:

G : S → aaSb|aSab|abSa|aSba|bSaa|baSa|SS|aSbSa|e

Call B the property wa = 2wb of a word w. We use again induction on |w|. It is easy see that theonly case that is not trivial is the case of w of the type aaw1aa or bbw1bb. Consider the formerone. Starting with a 0, move from left to right adding 1 for each a and subtracting 2 after each b.After the second letter, the counter equals 2, and before the last two a’s it is -2.If for some intermediate position, the counter = 0, we follow the proof to the previous problem.Since for our case the counter changes by 1 and 2, if it is never 0, we prove (make it exact !) thattwo consecutive values of the counter are 1 and -1, implying that w = aw1bw1a where both w1 andw2 have property B. Then we use S → aSbSa to finish the proof.

7 Parsing: algorithmic aspects

Two algorithmic problems related to representing a language by a grammar:

Problem #1: (the parsing problem):given a grammar G and a string w, is w ∈ L(G)?

Problem #2: given a grammar G, is L(G) = ∅?

Parsing is the process of determining if a string of terminals (in the Compiler Theory, they arecalled tokens) can be generated by a given grammar. Most parsing methods fall into one of the twoclasses: top-down and down-up methods. Both construct the parse tree of the input string, butdo it in the opposite directions. The former starts at the root and proceeds towards leaves, whilethe latter starts at the leaves and proceeds to the root.

An exhaustive top-down parsing: systematically construct all possible leftmost derivationsand see if any of them match w (the breadth-first search algorithm).

Example 2311

Page 12: 1 Context-free grammars: introductiongoldberg/14-CC/Notes/notes-cfg.pdfA production is useless if it contains any useless variable. Theorem 11 For every grammar G, there is an equivalent

Grammar G : S → SS|aSb|bSa|ǫ; string w = aabb

Round 1: S ⇒ SS; S ⇒ aSb; S ⇒ bSa; S ⇒ ǫthe last two productions can be removed

Round 2: S ⇒ SS ⇒ SSS;S ⇒ SS ⇒ aSbS;S ⇒ SS ⇒ bSaS;S ⇒ SS ⇒ S;S ⇒ aSb ⇒ aSSb;S ⇒ aSb ⇒ aaSbb;S ⇒ aSb ⇒ abSab;S ⇒ aSb ⇒ ab;

· · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · ·

Round 3: S ⇒ aSb ⇒ aaSbb;S ⇒ aabb.

Idea for parsing: list out all derivations (= parse trees) that produce strings of length 1, 2, . . . , |w|.

Caveat: some of the rules are of the form A → α, where |α| ≤ 1, which may create infinite numberof derivations even though they produce a finite number of strings; thus, the algorithm mustunderstand how to eliminate repetitions.

New idea: modify G so that the resulting grammar G′ generates the same language but does nothave rules of the type A → ǫ and A → B.

Caveat: It is not always possible, since if ǫ ∈ L(G), then G must have a rule A → ǫ for someA ∈ V .

Final idea: construct a new grammar G′ such that

• L(G) = L(G′);

• G′ does not have any rule A → ǫ and A → B, where A,B ∈ V ;

• if ǫ ∈ L(G) then S → ǫ is allowed.

Question: why does the final idea work?

Answer: Let every rule of grammar L be of the form A → u where u is either a terminal symbol,or |u| > 1, with only exception for S → ǫ. Then after each derivation step, different fromS → ǫ, either the derived string becomes longer, or the number of its terminals becomesbigger. Thus, any derivation of w 6= ǫ has at most 2|w| − 1 steps. Consequently, we onlyneed to examine parse trees whose size is bounded by 2|w| − 1. Therefore, for every w, thealgorithm determines in a finite number of steps whether w ∈ L(G).

The implementation of the idea for parsing.

Eliminate all rules A → ǫ except for S → ǫ.For every rule A → ǫ, if there is a rule B → uAv, add B → uv to the set of rules. Repeatuntil no new rule can be added. Then remove all rules of the form A → ǫ, except S → ǫ, if itis in the grammar.

12

Page 13: 1 Context-free grammars: introductiongoldberg/14-CC/Notes/notes-cfg.pdfA production is useless if it contains any useless variable. Theorem 11 For every grammar G, there is an equivalent

For the resulting grammar G′, L(G) = L(G′). (Why ?)

Eliminate all rules A → B.

• For every two variables A and B, determine if A ⇒∗ B; (How?).

• Whenever B → u is a rule in G and A ⇒∗ B, add A → u. Repeat until no new rule canbe added. Eliminate all rules A → B.

For the resulting grammar G′′, L(G) = L(G′′). (Why ?)

Problem #2: Given a grammar G, is L(G) = ∅?

Answer. If grammar G generates a nonempty language, then there is a parse tree whose yield is aterminal string and whose height ≤ |V |. This is because every parse tree of height > |V | has a pathwith two nodes labeled with the same variable. Then, a smaller parse tree can be constructed byexcising the part of the tree between the nodes with the same label. The process can be repeateduntil a parse tree of height ≤ |V | is constructed with the terminal yield.

Thus, to answer the question, it is sufficient to search through all parse trees of height ≤ |V |; ifnone of them yields a terminal string, the language is empty.

����������������������������������������������������������������������������������������������������������������������������������������������������������

����������������������������������������������������������������������������������������������������������������������������������������������������������A

A

A

SS

The solutions above to both problems are only of a theoretical value; a practical solution (when itis available) is based on the notion of a pushdown automaton.

13

Page 14: 1 Context-free grammars: introductiongoldberg/14-CC/Notes/notes-cfg.pdfA production is useless if it contains any useless variable. Theorem 11 For every grammar G, there is an equivalent

8 Ambiguity

Steve saw a man with a telescope

Two ways to parse (to understand) the sentence:

Steve used a telescope to see the man The man was with the telescope

A grammar is said to be ambiguous if two or more parse (derivation) trees exist for the samestring. A grammar which is not ambiguous is called unambiguous.

Example 24 S → SbS | ScS | a; S ⇒∗G abaca.

derivation #1: S ⇒ SbS ⇒ abS ⇒ abScS ⇒ abacS ⇒ abaca

derivation #2: S ⇒ ScS ⇒ SbScS ⇒ abScS ⇒ abacS ⇒ abaca

cS S

S

bS S

S c S

aa

a

a

bS S

a

a

S

Example 25 S → aSb | SS | ǫ; S ⇒∗G abaca.

An unambiguous equivalent of the grammar: S → aTb | SS; T → S | ǫ;

Example 26 The grammar below describes a fragment of a programming language concerned witharithmetic expressions.

A → I := E; I → a|b|c; E → E + E|E ∗ E|( E )|I.

A ⇒G a := b + c ∗ a

14

Page 15: 1 Context-free grammars: introductiongoldberg/14-CC/Notes/notes-cfg.pdfA production is useless if it contains any useless variable. Theorem 11 For every grammar G, there is an equivalent

:=

*a

I

A

E

E + E

E * E

IIb

I

c a

a

:=

A

I E

E E

IE+E

I I a

cb

An unbiguous version of the grammar for arithmetic expressions.

A → I := E; I → a|b|c; E → T | E + T ; T → F | T ∗ F ; F → I | ( E ).

a

I

A

:= E

+E T

T * FI

F I

aI

c

b

Example 27S → if C then S | if C then S else S | statement; C → condition

The grammar is ambiguous; consider the string

if C1 then if C2 then S1 else S2.

(if a = b then if b = c then x = 1 else x = 2)

15

Page 16: 1 Context-free grammars: introductiongoldberg/14-CC/Notes/notes-cfg.pdfA production is useless if it contains any useless variable. Theorem 11 For every grammar G, there is an equivalent

if C

S

condition then if condition

C

S

then statement

S

else

S

statement

S

if C

thencondition if condition

C S

statementthen else

S

statement

S

An unambiguous equivalent grammar (idea):

S → M | UM → if C then M else M | otherU → if C then S | if C then M else U

9 Pushdown Automata: finite automata + memory

Definition 28 A pushdown automaton M is a 6-tuple (Q, Σ, Γ, δ, q0, F ), where

16

Page 17: 1 Context-free grammars: introductiongoldberg/14-CC/Notes/notes-cfg.pdfA production is useless if it contains any useless variable. Theorem 11 For every grammar G, there is an equivalent

Q : a finite set of states;

Σ : a finite alphabet of input symbols;

Γ : a finite alphabet of stack symbols;

q0 ∈ Q : the initial state;

F ⊆ Q : a set of final states;

δ : the transition function;

Q × (Σ ∪ ǫ) × (Γ ∪ ǫ) → P(Q × (Γ ∪ ǫ)).

An input string w to a PDA M is considered to be written on the input tape as

w = w1w2 · · ·wm,

where wi ∈ Σ ∪ ǫ ≡ Σǫ.

Given the current state q ∈ Q, the current letter u ∈ Σ ∪ ǫ of the input, and the current lettera ∈ Γ ∪ ǫ ≡ Γǫ at the top of the stack, if

(q′, b) ∈ δ(q, u, a),

then M may select q as its next state and replace the top letter in the stack with b. If δ(q, u, a) = ∅,no action of M is possible.

A computation by M begins in the start state with an empty stack.

If the input string w is written asw = w1w2 · · ·wm,

then for every i ∈ [1,m − 1],(ri+1, b) ∈ δ(ri, wi+1, a),

where {ri} is the sequences of states; a, b are stack symbols.

A string w is accepted by M if at the end of w M enters a final state.

The definition of a PDA does not include a requirement of an empty stack at the end of computationneither does it contain a formal mechanism to allow the PDA to test for an empty stack. However,it is always possible to achieve that the stack is empty at the end by introducing a special symbol$ which is initially placed on the stack. If the PDA ever sees the $ again, it “knows” that the stackis effectively empty. Thus, we will always design pushdown automata that empty the stack by thetime they accept the string.

17

Page 18: 1 Context-free grammars: introductiongoldberg/14-CC/Notes/notes-cfg.pdfA production is useless if it contains any useless variable. Theorem 11 For every grammar G, there is an equivalent

Definition 29 The language L(M) accepted by M is the set of all strings accepted by M .

If (q, b) ∈ δ(p, u, a), we say that (p, u, a), (q, b) is a transition of M . A transition (p, u, a), (q, b) isrepresented by a diagram composed of two nodes labeled p and q, respectively, and an arrow (p, q)labeled with u, a → b.

p qu,a b

Since δ(q, u, a) ⊆ Q × Γ ∪ ǫ, at every particular step, there can be more than one applicabletransitions; if δ(q, u, a) = ∅, then no transition is applicable.

The transition function δ is completely described by the set ∆ of its applicable transitions.

If M performs transition (q, u, a)(p, ǫ), we say that M pops a from the stack. The transition iscalled a pop. If M performs transition (q, u, ǫ)(q, b), we say that M pushes b onto the stack; thetransition is called a push.

Example 30 G = (Q,Σ,Γ, δ, q1, F ), where Q = {q1, q2, q3, q4}; Σ = {0, 1}; Γ = {0, $}; and F ={q1, q4}. What is the language recognized by this pushdown automaton?

e1,0

e1,0

e,e e,$ e$q q

1 2

0,e 0

q4q 3

Example 31 What is the language recognized by the pushdown automaton described below?

Q = {q0, q1, q2, f}Σ = {a, b, c} The transition function δ is represented by theΓ = {a, b, $} diagram below. For all triples (q, u, a) that are notF = {f} shown on the diagram, δ(q, u, a) = ∅.

10 Pushdown Automata and Context-free Grammars

Theorem 32 The class of languages accepted by pushdown automata is exactly the class of context-free languages.

18

Page 19: 1 Context-free grammars: introductiongoldberg/14-CC/Notes/notes-cfg.pdfA production is useless if it contains any useless variable. Theorem 11 For every grammar G, there is an equivalent

=⇒: Each context-free language is accepted by a PDA.

⇐=: Each language accepted by a PDA is context-free.

Proving =⇒:

∀G = (V,Σ, R, S),∃M = ({p, q, . . .},Σ, V ∪ Σ,∆, p, {q})such that L(G) = L(M).

∆ : (p, ǫ, ǫ)(q, S)(q, ǫ, A)(q, w), ∀A → w(q, a, a)(q, ǫ), ∀a ∈ Σ.

How to interpret a transition (q, u, a)(q, w), where w is a string?

p q qp

e,e b

abcu,a

e,ecu,a

a

Interpretation

For every string that can be derived by a grammar G, the new PDA accepts the string by “mim-icking” its leftmost derivation. The end of the derivation corresponds to the moment the PDAfinished reading the input-string and its stack is empty.

Example 33G : V = {S, T}; Σ = {a, b}

R : {S → abTa;T → ba : . . .}

Derivations in G Transitions in MS ⇒ abTa (p, ǫ, ǫ)(q, S); (q, ǫ, S)(q, abTa);

(q, a, a)(q, ǫ); (q, b, b)(q, ǫ);⇒ abbaa (q, ǫ, T )(q, ba);

(q, b, b)(q, ǫ); (q, a, a)(q, ǫ); (q, a, a)(q, ǫ).

Proving ⇐=:

Given PDA M , construct a grammar G, such that L(G) = L(M).

First, we modify M so that it satisfies the following conditions:

1. the final state of M is unique; (easy)

2. when M accepts a string, its stack is empty; (easy)

3. each transition is either a push or a pop.

Let (p, u, a)(q, b) be a transition with a 6= ǫ and b 6= ǫ. Replace it with (p, u, a)(qnew, ǫ)&(qnew, ǫ, ǫ)(q, b),where qnew is a new state.

Design G = (V,Σ, R, q0) by:

19

Page 20: 1 Context-free grammars: introductiongoldberg/14-CC/Notes/notes-cfg.pdfA production is useless if it contains any useless variable. Theorem 11 For every grammar G, there is an equivalent

V = {Ap,q} (∀p, q ∈ Q); Apq → aArsb ∀(p, a, ǫ)(r, t) & (s, b, t)(q, ǫ)Σ set of terminals; Apq → AprArq, ∀p, q, r ∈ QAq0,f the start variable; App → ǫ. ∀p ∈ Q.

Proof. We prove a more general statement:

∀p, q ∈ Q(M), the set L(Ap,q) of strings generated from Apq is the set Σpq of stringsthat take M from state p to state q, starting and ending with the empty stack.

Claim 1. We prove that every w ∈ L(Ap,q) is also a string in Σpq by induction on the length ofthe derivation of w.

If the number of steps in the derivation of w is 1, then the rule used can only be App → ǫ. Obviously,ǫ ∈ Σp,p, since it takes M from p to p with the stack empty at the beginning and the end.

Assuming the statement is correct for all derivations with the number of steps ≤ k ≥ 0, and let thenumber of steps in the derivation of wnew is k + 1.

Case 1: the first step in the derivation of wnew is Apq ⇒ aAr,sb. Then wnew = aw′b and thefollowing two transitions must exist in M : (p, a, ǫ)(r, t) and (s, b, t)(q, ǫ).

Case 2: the first step in the derivation of wnew is Apq ⇒ AprArq. Then wnew = w′w′′, where w′

and w′′ are two strings generated in G from Apr and Ar, q, respectively.

In both case, the statement about wnew follows from that for w′ (case 1) and for w′&w′′ (Case 2).

Claim 2. We prove that Σpq ⊆ L(Apq) by induction on the length of w.

If w ∈ Σpq, that is w can bring M from p to q with the empty stack at the beginning and the end,then the first (resp. last) transition of w is a push (resp. pop).

Let |w| = 0. Then w = ǫ and the path starts and ends at p. The derivation of w is App → ǫ.

Let our statement be correct for strings of length k ≥ 0 and let string w of length k+1 can bring Mfrom p to q with the empty stack at the beginning and the end. If moving M from p to q containsa moment at which the stack is empty, the first step of a derivation of w in G is Apq → AprAr,q.

Else, let (p, a, ǫ)(r, t) and (q, b, s)(q, ǫ) be the first and the last steps, respectively, in the path fromp to q according to w. Since during no intermediate step the stack becomes empty, the symbol tpushed into the stack the first step is the symbol which is popped during the last step. Thus, s = t,and wnew = aw′b. Furthermore, w′ takes M from r to s with the stack being empty initially andat the end. Since |w′| = k − 1, it can be derived from Lr,s. Then the first step of the derivation forw is

Apq ⇒ aAr,sb.

The derivations that follow are those used for w′.

20

Page 21: 1 Context-free grammars: introductiongoldberg/14-CC/Notes/notes-cfg.pdfA production is useless if it contains any useless variable. Theorem 11 For every grammar G, there is an equivalent

11 Languages that are not context-free.

The pumping lemma for context-free languages is a technique for proving that certain languagesare not context-free. It states that every context-free language has a special value called pumpinglength such that all sufficiently long strings in the languages can by “pumped”. The pumpingtheorem is typically used to prove that some languages are not CFL: for this purpose, one provesthat a given language has no finite pumping length.

Theorem 34 (The Pumping Theorem) Let G be a context-free grammar which describes an infinitelanguage. Then there is a number p depending on G only such that any string w ∈ L(G) of length> p can be rewritten as uvxyz so that

• |v| + |y| > 0;

• |vxy| ≤ p;

• uvixyiz ∈ L(G) ∀i ≥ 0.

Important comment: the theorem asserts the existence of a sub-division into five substringsthat satisfy the three conditions above; it does not indicate what they are, or how many exist. Notevery subdivision would satisfy the three conditions.

Proof. Let an infinite CFL L be generated by a CFG G(V,Σ, R, S) and let b be the maximumlength of the string in the right-hand side of any rule. Since L is infinite, b ≥ 2.

Claim. In any parse tree of a string w ∈ L, the number of nodes on the k-th level(k ≥ 0) of the tree does not exceed bk.

This claim is easily proved by induction of k. Indeed, for k = 0, it is trivially correct.If the claim is correct for some level k, then the size of the level k +1 can increase by atmost b times, since every node may have at most b children (for some nodes the numberof children is smaller than b, if the the corresponding rule has fewer than b letters in itsright-hand side.).

Set p = b|V |+2 and let w ∈ L with |w| > p. Take the parse tree for w with the smallest numberof nodes. The height of the tree is at least |V | + 2, and so is the length of the longest path ofthis tree. The labels of the nodes of this path are variables, except for the leaf which is labeledwith a terminal. Since there are |V | + 2 labels of non-leaf nodes and V variables, by pigeonholeprinciple, some variable A appear more than once on the path. Select A to be the variable thatrepeats among the lowest |V | + 1 variables on the path. Split w into five parts u, v, x, y, and zaccording to the figure below:

21

Page 22: 1 Context-free grammars: introductiongoldberg/14-CC/Notes/notes-cfg.pdfA production is useless if it contains any useless variable. Theorem 11 For every grammar G, there is an equivalent

vu x y z

A

S

w = u v x y z

| w | > b

A

|V|+2

Both subtrees vxy and x are generated by the some variable A, so we may substitute one for theother and still obtain a valid tree. Repeating the substitution i times, we get i “pumped” parsedtrees of strings in L. This satisfies the first condition of the lemma.

S

u v y z

yv

A

A

A

x

To satisfy the second condition, we must check, that v and y are not both empty strings. Indeed,if they were, the subtree generated by the upper A would be a path and a tree generated from thelast vertex of the path; the labels of the first and the last vertices of the path would be A. Butthen, we would be able to construct a smaller parse tree for w by simply removing the edges of thepath. This would contradict the minimality of the initial parse tree for w.

To show that the third condition of the theorem holds, notice that in the parse tree, the upperoccurrence of A generates vxy. By our choice, both occurrences of A are among the bottom |V |+2nodes of the longest path of the tree. So the height of the subtree generated by A is at most |V |+2.This implies that the length of the yield of this subtree is at most b|V |+2 = p. This completes theproof of the pumping theorem.

Problem 35

22

Page 23: 1 Context-free grammars: introductiongoldberg/14-CC/Notes/notes-cfg.pdfA production is useless if it contains any useless variable. Theorem 11 For every grammar G, there is an equivalent

• Prove that L = {anbncn : n ≥ 0} is not context-free.

• Prove that L = {ww : w ∈ (a ∪ b)∗} is not context-free.

12 Stronger implementations of the pumping idea

Here we consider strings in a CFL language L and mark some letters of the string. We show thatthere is a positive integer p such that if w ∈ L and p of its characters are marked, then w is“pumpable” in such a way that one of the pumped strings contains a marked character.

Definition 36 If some letters of a string w are marked and u is a substring of w, then the weightof u (w.r.t. the marking of w) is the number of marked symbols in u. The weight is denotedweight(u).

Lemma 37 (Ogden’s Derivation-Pumping Lemma.) Let G be a context-free grammar. Then thereis a positive integer p such that for any w ∈ L(G), if at least p characters of w are marked, thenthere is a variable A and splitting w = uvxyz of w such that

1. weight(v) + weight(y) ≥ 1;

2. weight(vxy) ≤ p;

3. S ⇒∗ uAz;

4. A ⇒∗ vAy;

5. A ⇒∗ x;

6. ∀i ≥ 0, A ⇒∗ vixyi.

Theorem 38 (Ogden’s Pumping Theorem.) Let L be a CFL. Then there is a positive integer p(depending on L) such that for any w ∈ L, if at least p characters of w are marked, then there existsplitting w = uvxyz such that

1. weight(v) + weight(y) ≥ 1;

2. weight(vxy) ≤ p;

3. ∀i ≥ 0, uvixyiz ∈ L.

Example 39 Let L = {aibjck : i 6= j and j = k }. Then L is not context-free.

23

Page 24: 1 Context-free grammars: introductiongoldberg/14-CC/Notes/notes-cfg.pdfA production is useless if it contains any useless variable. Theorem 11 For every grammar G, there is an equivalent

Solution. Assume that L is a CFL and let p be the pumping number as in Ogden’s theorem.Consider

w = ap!+pbpcp,

where p! = 1 × 2 × . . . × p. Mark all b’s. Then w = uvxyz for some u, v, x, y, z satisfying Ogden’stheorem, in particular, one of v or y overlaps with the b-substring of w. (Notice, that this overlapis not guaranteed by the original pumping theorem.)

Claim: For some m ≤ p, v = bm and y = cm. Indeed

1. v and y must belong to either a∗, or b∗, or c∗, since otherwise pumping would create stringsnot even in a∗b∗c∗;

2. if v = ar, for some r, then y would be the one to contain marked symbols (they are b’s only),and pumping would increase the number of b’s without increasing the number of c’s; thiswould yield a string not in L;

3. if v = cr, then y would contain only c’s and pumping would increase the number of c’s withoutincreasing the number of b’s;

4. the remaining option for v is to be bm; in this case y must be cm so as to guarantee that theincrease of b’s equals that of c’s.

Now let us pump i times for i = p!/m + 1. Then

uvixyiz = uvvp!/mxyp!/myz = uvbp!xcp!yz = ap+p!bp+p!cp+p!.

The resulting string is not in L, thus the initial assumption that L is a CFL is incorrect.

Theorem 40 There exists an inherently ambiguous context-free language.

Proof. We will show that for any grammar generating L = {aibjck : i = j or j = k}, there is astring of the form ambmcm that can be parsed in two different ways.

Let G be a grammar for L and let p > 1 be the pumping number for G as in Ogden’s lemma.Let w0 = apbpcp+p!. Mark all b’s. By Ogden’s lemma, G contains a variable A and a splittingw0 = uvxyz such that

1. S ⇒∗ uAz; and

2. ∀i ≥ 0, A ⇒∗ vixyi.

The only way to pump without getting strings out of L is if v and y are of the form v = ak andy = bk for 1 ≤ k ≤ p (if v or y contained more than one distinct character, pumping would yield astring not even of the form a∗b∗c∗; furthermore, |v| and |y| must be equal, for otherwise pumpingdown produces a string with all different numbers of a’s, b’s, and c’s).

24

Page 25: 1 Context-free grammars: introductiongoldberg/14-CC/Notes/notes-cfg.pdfA production is useless if it contains any useless variable. Theorem 11 For every grammar G, there is an equivalent

Letting i = p!/k + 1, we see that A ⇒ ap!+kxyp!+k, so

S ⇒∗ u A z ⇒∗ u ap!+k x bp!+k z ⇒∗ u ap! v x bp!y z ⇒∗ ap+p! bp+p! cp+p!

Thus, there is a derivation of ap+p!bp+p!cp+p! in which ap!+kxbp!+k is a phrase; (a phrase is definedto be the yield of a subtree of a parse tree provided the subtree’s root is a variable.)

If we apply the same argument to w′ = ap+p!bpcp to derive ap+p!bp+p!cp+p!, we discover that thisderivation contains a phrase bp!+lx′cp!+l for some l ≥ 0.

Each of the two phrases, ap!+kxbp!+k and bp!+lx′cp!+l, contains at least p! + 1 b’s, and the stringsderived contain just p! + p b’s; thus each of the phrases contain more than a half of the b’s that arederived. Hence, the two phrases overlap. The 1st string contain some a’s but no c’s; the 2nd stringcontains some c’s but no a’s. Therefore neither phrase is a substring of the other, Thus, the parsetrees are different, and the grammar is ambiguous.

Problem 41 Prove that the following languages are not CFLs:

1. {aibjck : i < j < k}; ({aibjck : j < i = k});

2. {ai2};

3. {ai : i is composite};Hint: let q be the least prime greater than (p!+1)!+1, and show that in fact q > (p!+1)!+p!+1.Then let i = q − p! and apply the pumping theorem.

4. L = {aibjcidj : i, j ≥ 0}.

Problem 42 Prove that L = {aibjck : i 6= j and j = k} satisfies the conclusions of the originalpumping theorem (we already know that L is not CFL which was proved using Ogden’s pumpingtheorem.)

25