P. 1
Ch6b

Ch6b

|Views: 0|Likes:
Formal Language
Formal Language

Categories:Types, School Work

See more
See less

08/01/2013

pdf

text

original

Formal Languages and Automata

Chapter 6 Simpliﬁcation of Context-Free Grammars and Normal Forms
Chuan-Ming Liu
cmliu@csie.ntut.edu.tw

Department of Computer Science and Information Engineering National Taipei University of Technology Taipei, TAIWAN

Mobile Computing and Software Engineering – p. 1/3

Objectives
Study several transformations and substitutions Investigate normal forms for context-free grammars (cfg’s) Chomsky normal form Greibach normal form Discuss useless, λ−, and unit- productions

Mobile Computing and Software Engineering – p. 2/3

Contents
Methods for Transforming Grammars Two Important Normal Forms A Membership Algorithm for cfg’s∗ .

Mobile Computing and Software Engineering – p. 3/3

4/3 . Mobile Computing and Software Engineering – p.Methods for Transforming Gramma The empty string plays a rather singular role in many theorems and proofs. We restrict our discussion to λ-free languages.

S. ˆ = (V. T. Suppose that P contains a production of the form A → x1 Bx2 . 5/3 . Assume that A and B are different variables and that B → y1 |y2 | · · · |yn is the set of all productions in P which have B as the left side.A Useful Substitution Rule Theorem: Let G = (V. P ˆ ) be the grammar in which P ˆ is Let G constructed by deleting A → x1 Bx2 from P . ˆ ) = L(G). L(G Mobile Computing and Software Engineering – p. Then. S. P ) be a context-free grammar (cfg). and adding to it A → x1 y1 x2 |x1 y2 x2 | · · · |x1 yn x2 . T.

b. 6/3 . B }.Example 1 Consider G = ({A. c}. P ) with A → a|aaA|abBc. Mobile Computing and Software Engineering – p. How about A = B ? 2. A. Consider the productions associated with B after the substitution. The substitution rule we discussed here needs that A and B are distinct. {a. B → abbA|b ˆ as described in the above theorem? What is G Note: 1.

A variable A is said to be useful if and only if there exists at least one w ∈ L(G) such that S =⇒∗ xAy =⇒∗ w. y ∈ (V ∪ T )∗ . S. Deﬁnition: (Useless Variable) A variable is not useful is called useless. with x. P ) be a context-free grammar (cfg).Removing Useless Productions Deﬁnition: (Useful Variable) Let G = (V. A production is useless if it involves any useless variable. 7/3 . Mobile Computing and Software Engineering – p. T.

S }. B. it can not derive a terminal string. Mobile Computing and Software Engineering – p. A → aA|λ. P ) with S → A. B → bA B is useless and B → bA is a useless production. {a. 8/3 . S. 2.Example 2 Consider G = ({A. it can not be reached from the start symbol. Note: Two reasons why a variable is useless 1. b}.

b}. {a. B. S }.Example 3 Eliminate useless variables and productions from G = ({A. S. 9/3 . C. aa. Mobile Computing and Software Engineering – p. aCb. P ) with P consisting of S A B C Solution: → → → → aS |A|C. a.

P ) be a context-free grammar. T. Then there exists an equivalent grammar ˆ = (V ˆ. P ˆ ) that does not contain any useless G variables or productions.Rule 1 Theorem: Let G = (V.T ˆ . 10/3 . S. Mobile Computing and Software Engineering – p. S.

Deﬁnition: (Nullable Variable) Any variable A for which the derivation A =⇒∗ λ is possible is called nullable. 11/3 . Mobile Computing and Software Engineering – p.Removing λ-Productions Deﬁnition: (λ-production) Any production of a cfg of the form A→λ is call a λ-production.

Example 4 Consider the grammar S → aS1 b. S1 → aS1 b|λ. which generates the λ-free language {an bn : n ≥ 1}. S1 → λ can be removed by adding S → ab. S1 → ab. Mobile Computing and Software Engineering – p. 12/3 .

P ˆ ) that does not contain any G λ-productions. Mobile Computing and Software Engineering – p. P ) be a context-free grammar with λ ∈ L(G). T. S. Then there exists an equivalent grammar ˆ = (V ˆ.T ˆ . 13/3 . S.Rule 2 Theorem: Let G = (V.

d.Example 5 Find a cfg without λ-productions equivalent to the grammar deﬁned by S A B C D → → → → → ABaC. D|λ. b|λ. 14/3 . Mobile Computing and Software Engineering – p. BC.

15/3 .Removing Unit-Productions Deﬁnition: (Unit-Productions) Any production of a cfg of the form A→B where A. Mobile Computing and Software Engineering – p. is call a unit-production. B ∈ V .

S.Rule 3 Theorem: Let G = (V. Then there exists an ˆ = (V ˆ. Mobile Computing and Software Engineering – p. 16/3 .T ˆ . T. S. P ˆ ) that does not contain equivalent cfg G any unit-productions. P ) be a context-free grammar without λ-productions.

Mobile Computing and Software Engineering – p. A → a|bc|B.Example 6 Remove all unit-productions from S → Aa|B. B → A|bb. 17/3 .

λ-productions. Mobile Computing and Software Engineering – p. Then there exists a cfg that generates L and that does not have any useless productions. 18/3 .Theorem Let L be a context free language that does not contain λ. or unit-productions.

Mobile Computing and Software Engineering – p.Contents Methods for Transforming Grammars Two Important Normal Forms A Membership Algorithm for cfg’s∗ . 19/3 .

Chomsky Normal Form Note: the string on the right of a production consist of no more than two symbols. Mobile Computing and Software Engineering – p. Deﬁnition: A cfg is in Chomsky normal formChomsky normal form if all productions are of the form A → BC or A → a. 20/3 . C ∈ V and a ∈ T . B. where A.

Example 7 The grammar S → AS |a. 21/3 . Mobile Computing and Software Engineering – p. A → SA|b is in Chmosky normal form. The grammar S → AS |AAS. A → SA|aa is not in Chmosky normal form.

22/3 . Mobile Computing and Software Engineering – p. P ) with λ ∈ L(G) has an ˆ = (V ˆ. P ˆ ) in Chomsky equivalent grammar G normal form. T.Theorem Any cfg G = (V. S.T ˆ. S.

Example 8 Convert the grammar with productions S → abA. B → Ac to Chmosky normal form. A → aab. 23/3 . Mobile Computing and Software Engineering – p.

Deﬁnition: A cfg is in Chomsky normal form if all productions are of the form A → ax. but Greibach normal form does not put the restriction that the pair (A. Mobile Computing and Software Engineering – p.Greibach Normal Form Note: We put restrictions on the positions in which terminals and variables can appear. 24/3 . Note: the form A → ax is common to both Greibach normal form and s-grammar. a) occur at most once. where x ∈ V ∗ and a ∈ T .

25/3 . Mobile Computing and Software Engineering – p. A → aA|bB |b. B → b is not in Greibach normal form.Example 9 The grammar S → AB.

26/3 . Mobile Computing and Software Engineering – p.Example 10 Convert the grammar S → abSb|aa to Greibach normal form.

P ˆ ) in Greibach equivalent grammar G normal form. T. S. S.Theorem Any cfg G = (V. 27/3 . Mobile Computing and Software Engineering – p.T ˆ. P ) with λ ∈ L(G) has an ˆ = (V ˆ.

Contents Methods for Transforming Grammars Two important Normal Forms A Membership Algorithm for cfg’s∗ . 28/3 . Mobile Computing and Software Engineering – p.

CYK Algorithm An algorithm to verify if a given string belongs to the language generated by some given cfg. S. The given cfg G = (V. 29/3 . w ∈ L(G) ⇔ S ∈ V1n . Clearly. According to the dynamic programming algorithmic design paradigm. Mobile Computing and Software Engineering – p. Given a string w = a1 a2 · · · an . T. P ) is in Chomsky normal form. and subsets Vij = {A ∈ V : A ⇒∗ wij } ⊆ V . we deﬁne substrings wij = ai · · · aj .

C ∈ Vk+1j } Mobile Computing and Software Engineering – p..j −1} {A : A → BC. C ⇒∗ wk+1j . A ⇒∗ wij ⇐⇒ ∃ a production A → BC . with B ∈ Vik . 30/3 . For j > i.Computing Vij Consider two forms deﬁned in Chomsky normal form: For each i. Vij = k∈{i. In other words. for some i ≤ k < j .. with B ⇒∗ wik ...i+1. A ∈ Vii ⇐⇒ A → ai .

.n . . . V23 . V24 . Vn−2. Compute V12 . Mobile Computing and Software Engineering – p. . . . . . V22 . Vn−1. . . . . 31/3 . Compute V13 .n . Vnn .Bottom-up Approach Use a Bottom-up approach to compute all Vij with the equations discussed Compute V11 .

Algorithm . 32/3 . w ) 1 for i ← 1 to n £ n = |w|.Pseudocode M EMBERSHIP (G.j 11 then Vij = Vij ∪ {A} 12 if S ∈ V1n 13 then w ∈ L(G) Mobile Computing and Software Engineering – p. 2 do if A → ai exists 3 then Vii = A 4 else Vii = ∅ 5 for l ← 2 to n 6 do for i ← 1 to n − l + 1 7 do j ← i + l − 1 8 for k ← i to j − 1 9 do if A → BC exists 10 for B ∈ Vik and C ∈ Vk+1.

B → AB |b. Mobile Computing and Software Engineering – p.Example 11 Determine whether the string w = aabbb is in the language generated by the grammar S → AB. 33/3 . A → BB |a.

scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->