Top Banner

of 7

MELJUN CORTES Automata Lecture Chomsky Normal Form 2

Aug 07, 2018

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 8/21/2019 MELJUN CORTES Automata Lecture Chomsky Normal Form 2

    1/16

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 1 of 16

    Chomsky Normal Form

    CHOMSKY NORMAL

    FORM

    Chomsky Normal Form

      Introduction

    •   Simplified versions of context-

    free grammars are very useful

    in analyzing the grammar,

    constructing proofs about

    certain properties of thegrammar, testing if a particular 

    string is a member of the

    language of the grammar, etc.

    •   These simplified versions arecalled normal fo rms .

    •   One popular normal form of 

    context-free grammars is the

    Chomsky Normal Form.

  • 8/21/2019 MELJUN CORTES Automata Lecture Chomsky Normal Form 2

    2/16

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 2 of 16

    Chomsky Normal Form

     Formal Definition of the ChomskyNormal Form

    •   A context-free grammar is in the

    Chom sky norm al fo rm    if every

    rule is of the form

     A → BC

     A → a

    where:

    1. A, B, and C  are any variables

    2. a is any terminal

    3. B and C  may not be the start

    variable

    4. the rule S    , where S  is the

    start variable, is allowed

  • 8/21/2019 MELJUN CORTES Automata Lecture Chomsky Normal Form 2

    3/16

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 3 of 16

    Chomsky Normal Form

    •   Example:

    Grammar   G1:

    S  → XY 0

    X  → YY 1

    Y  → 0

    Grammar   G2:

    S  → XS XYY 0X 0

    X  → SX 11

    Y  → 1

    Grammar    G1   is in the

    Chomsky normal form while

    grammar  G2 is not.

  • 8/21/2019 MELJUN CORTES Automata Lecture Chomsky Normal Form 2

    4/16

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 4 of 16

    Chomsky Normal Form

    •   Procedure for converting acontext-free grammar into the

    Chomsky Normal Form:

    Case Study: Convert the

    following grammar   G3 into theChomsky normal form.

    S ASA 0B

     A B S

    B 1

    Step 1:

    Create a new start variable   S 0and the production   S 0   S ,

    where   S   is the original start

    variable of the grammar being

    converted.

  • 8/21/2019 MELJUN CORTES Automata Lecture Chomsky Normal Form 2

    5/16

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 5 of 16

    Chomsky Normal Form

    S0   S

    S ASA   0B

     A B S

    B 1

    Step 2:

    For every variable   A that is not

    a start variable, remove all   Arules (the -p roduc tions  ).

    Then, for each occurrence of  A

    on right hand side of a

    production, add a new rulewith the  A deleted.

    For example, if there is a rule

    B  →  AC , add the rule  B  →  C .

  • 8/21/2019 MELJUN CORTES Automata Lecture Chomsky Normal Form 2

    6/16

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 6 of 16

    Chomsky Normal Form

    If there is a rule   B   →   A, addthe rule   B   → ε, unless   B   → ε

    was previously removed.

    Removing  B   :

    S0   S

    S ASA   0B 0

     A B S

    B 1

    Removing   A   :

    S0

      S

    S ASA   0B 0SA   AS S

     A B S

    B 1

  • 8/21/2019 MELJUN CORTES Automata Lecture Chomsky Normal Form 2

    7/16

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 7 of 16

    Chomsky Normal Form

    Step 3:

    Remove all rules of the form   A

    B (the un it p roduc tions ).

    For each rule   B   →   w  (where   w 

    is a string of variables and

    terminals) that appears, add

    the rule   A   →   w  unless   A   →   w 

    was previously removed.

    Rules of the form   A A can just simply be removed since

    these are useless productions.

  • 8/21/2019 MELJUN CORTES Automata Lecture Chomsky Normal Form 2

    8/16

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 8 of 16

    Chomsky Normal Form

    Removing  S S :

    S0   S

    S ASA   0B 0SA   AS

     A B S

    B 1

    Removing  A B:

    S0   S

    S ASA   0B 0SA   AS

     A S 1

    B 1

  • 8/21/2019 MELJUN CORTES Automata Lecture Chomsky Normal Form 2

    9/16

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 9 of 16

    Chomsky Normal Form

    Removing  A S :

    S0   S

    S ASA   0B 0SA   AS

     A 1 ASA 0B 0SA   AS

    B 1

    Removing  S S 0:

    S0   ASA   0B 0SA   AS

    S ASA   0B 0SA   AS

     A 1 ASA 0B 0SA   AS

    B 1

  • 8/21/2019 MELJUN CORTES Automata Lecture Chomsky Normal Form 2

    10/16

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 10 of 16

    Chomsky Normal Form

    Step 4:

    Convert all the rules into the

    proper form by removing the

    rules of the form:

     A w1w2…wk

    where   k    ≥   3 and   w i   is a

    variable or terminal

    and adding the rules   A

    w 1T 1,   T 1   w 2T 2,   T 2   w 3T 3,

    and so on and so forth (where

    T 1,   T 2, and   T 3   are new

    variables).

    If   w i   is a terminal, replace it

    with a new variable   T i and add

    the rule  T i    w i .

  • 8/21/2019 MELJUN CORTES Automata Lecture Chomsky Normal Form 2

    11/16

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 11 of 16

    Chomsky Normal Form

    The resulting grammar    G3   in

    Chomsky normal form will be

    S0   AT1   T2B 0 SA   AS

    S AT1   T2B 0 SA   AS

     A 1 AT1   T2B 0 SA AS

    T1   SA

    T2   0

    B 1

  • 8/21/2019 MELJUN CORTES Automata Lecture Chomsky Normal Form 2

    12/16

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 12 of 16

    Chomsky Normal Form

    •   Example:

    Convert the following grammar 

    G4   into the Chomsky normal

    form.

     A BAB B

    B 00

    Step 1: Add  S 0   A

    S0   A

     A BAB B

    B 00

  • 8/21/2019 MELJUN CORTES Automata Lecture Chomsky Normal Form 2

    13/16

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 13 of 16

    Chomsky Normal Form

    Step 2: Remove -productions

    Remove  A   :

    S0   A

     A   BAB B BB

    B 00

    Remove  B   :

    S0   A

     A   BAB B BB AB BA

    B 00

  • 8/21/2019 MELJUN CORTES Automata Lecture Chomsky Normal Form 2

    14/16

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 14 of 16

    Chomsky Normal Form

    Step 3: Remove unit-productions

    Remove  A B:

    S0   A

     A BAB BB AB

    BA 00

    B 00

    Remove  S 0   A:

    S0   BAB BB AB

    BA 00

     A BAB BB AB

    BA 00

    B 00

  • 8/21/2019 MELJUN CORTES Automata Lecture Chomsky Normal Form 2

    15/16

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 15 of 16

    Chomsky Normal Form

    Step 4: Convert all the rules into

    the proper form.

    The resulting grammar    G4   in

    Chomsky normal form will be:

    S0   BT1   BB AB BA   T2T2

     A BT1   BB AB BA   T2T2

    B T2T2

    T1   AB

    T2   0

  • 8/21/2019 MELJUN CORTES Automata Lecture Chomsky Normal Form 2

    16/16

    Theory of Computation (With Automata Theory)

    * Property of STI 

    Page 16 of 16

    Chomsky Normal Form

    •   Formal Theorem on ChomskyNormal Form of Context-Free

    Grammars:

    Theo rem 6 :

    Any con tex t-free grammar  

    can be converted in to i ts  

    Chomsky normal fo rm .

    •   Exercise

    Convert the following context-

    free grammar into its Chomsky

    normal form:

    S   → ASB

     A   → 0AS 0

    B   → S1S A 00