Presentation is loading. Please wait.

Presentation is loading. Please wait.

Context-Free Grammars – Chomsky Normal Form Lecture 16 Section 2.1 Wed, Sep 26, 2007.

Similar presentations


Presentation on theme: "Context-Free Grammars – Chomsky Normal Form Lecture 16 Section 2.1 Wed, Sep 26, 2007."— Presentation transcript:

1 Context-Free Grammars – Chomsky Normal Form Lecture 16 Section 2.1 Wed, Sep 26, 2007

2 Chomsky Normal Form A context-free grammar is in Chomsky Normal Form (CNF) if each rule is of the form A  BC, or A  a, where B and C are not S. Furthermore, the rule S   is allowed.

3 Chomsky Normal Form Theorem: Every context-free language is generated by a grammar in CNF.

4 Chomsky Normal Form Constructive proof (outline): Add a new start symbol S 0. Eliminate all  rules A  . Eliminate all unit rules A  B. Convert all remaining rules to the proper form.

5 Chomsky Normal Form Proof (detailed): Add a new start symbol S 0. Add the rule S 0  S. Eliminate all  rules A  . For each rule A   and each rule B  uAv, add a rule B  uv. Eliminate the rule A  .

6 Example Start with the grammar S  SXS |  X  ab |  Add the rule S 0  S

7 Example We now have S 0  S S  SXS |  X  ab | 

8 Example Apply the rules S   and X   to the other rules, creating the rules S  X S  SS S  XS S  SX S  S Don’t bother with the last rule.

9 Example Eliminate the rules S   X  

10 Example Add the rule S 0   because in the original, S could be replaced by .

11 Example We now have S 0  S |  S  SXS | SS | SX | XS | X X  ab

12 Chomsky Normal Form Proof (detailed): Eliminate all unit rules A  B. If A  B and B  u are rules, then add the rule A  u. Eliminate the rule A  B.

13 Chomsky Normal Form Add the rules S  ab S 0  SXS | SS | SX | XS | X | ab Eliminate the rules S 0  S S  X

14 Example We now have S 0  SXS | SS | SX | XS | ab |  S  SXS | SS | SX | XS | ab X  ab

15 Chomsky Normal Form Eliminate all mixed rules. Add rules A  a for all terminals appearing in strings of length  2. Then replace a with A in those strings.

16 Example Add the rules A  a B  b and rewrite the string ab as AB.

17 Example We now have S 0  SXS | SS | SX | XS | AB |  S  SXS | SS | SX | XS | AB X  AB A  a B  b

18 Chomsky Normal Form Finally, eliminate all long rules. Break all strings of length  2 into a series of strings of length 2.

19 Chomsky Normal Form Replace the rule A  B 1 B 2 …B k with A  B 1 C 1 C 1  B 2 C 2 … C k – 2  B k – 2 C k – 2 C k – 1  B k – 1 B k

20 Example Replace S 0  SXS S  SXS with S 0  SY S  SY Y  XS

21 Example The final result is S 0  SY | SS | SX | XS | AB |  S  SY | SS | SX | XS | AB X  AB Y  XS A  a B  b

22 A Derivation in CNF Use this grammar in CNF to derive the string ababab. S 0  SY  SXS  ABXS  ABABS  ABABAB  aBABAB  abABAB  abaBAB  ababAB  ababaB  ababab.

23 CNF Derivations Theorem: If a grammar G is in CNF and a string w in L(G) has length n, then w will be derived from G in exactly 2n – 1 steps.

24 The Membership Problem This theorem allows us to determine whether a given string is derivable from a given grammar. This is called the Membership Problem.

25 Example Show that the string abba is not derivable from the grammar of the previous example.

26 A Tree of all Possible Derivations S0S0 SYSSAB  abSYSSABSY SS ABXSABSS SY etc.

27 Example Put the grammar E  E + E | E * E | (E) | a | b | c into CNF. Then show that the string c++ is not derivable from it.


Download ppt "Context-Free Grammars – Chomsky Normal Form Lecture 16 Section 2.1 Wed, Sep 26, 2007."

Similar presentations


Ads by Google