Context free grammars chomsky normal form
Download
1 / 27

Context-Free Grammars Chomsky Normal Form - PowerPoint PPT Presentation


  • 220 Views
  • Uploaded on

Context-Free Grammars – Chomsky Normal Form. Lecture 16 Section 2.1 Wed, Sep 26, 2007. 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.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Context-Free Grammars Chomsky Normal Form' - Olivia


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Context free grammars chomsky normal form

Context-Free Grammars – Chomsky Normal Form

Lecture 16

Section 2.1

Wed, Sep 26, 2007


Chomsky normal form
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.


Chomsky normal form1
Chomsky Normal Form

  • Theorem: Every context-free language is generated by a grammar in CNF.


Chomsky normal form2
Chomsky Normal Form

  • Constructive proof (outline):

    • Add a new start symbol S0.

    • Eliminate all  rules A.

    • Eliminate all unit rules AB.

    • Convert all remaining rules to the proper form.


Chomsky normal form3
Chomsky Normal Form

  • Proof (detailed):

  • Add a new start symbol S0.

    • Add the rule S0S.

  • Eliminate all  rulesA.

    • For each rule A and each rule B  uAv, add a rule B  uv.

    • Eliminate the rule A.


Example
Example

  • Start with the grammar

    • SSXS | 

    • X  ab | 

  • Add the rule

    • S0S


Example1
Example

  • We now have

    • S0S

    • SSXS | 

    • X  ab | 


Example2
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.


Example3
Example

  • Eliminate the rules

    • S

    • X  


Example4
Example

  • Add the rule

    • S0

      because in the original, S could be replaced by .


Example5
Example

  • We now have

    • S0S | 

    • S  SXS | SS | SX | XS | X

    • X  ab


Chomsky normal form4
Chomsky Normal Form

  • Proof (detailed):

  • Eliminate all unit rulesAB.

    • If AB and Bu are rules, then add the rule Au.

    • Eliminate the rule AB.


Chomsky normal form5
Chomsky Normal Form

  • Add the rules

    • S  ab

    • S0SXS | SS | SX | XS | X | ab

  • Eliminate the rules

    • S0S

    • S  X


Example6
Example

  • We now have

    • S0SXS | SS | SX | XS | ab | 

    • S  SXS | SS | SX | XS | ab

    • X  ab


Chomsky normal form6
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.


Example7
Example

  • Add the rules

    • A a

    • B b

      and rewrite the string ab as AB.


Example8
Example

  • We now have

    • S0SXS | SS | SX | XS | AB | 

    • S  SXS | SS | SX | XS | AB

    • X  AB

    • A a

    • B b


Chomsky normal form7
Chomsky Normal Form

  • Finally, eliminate all long rules.

  • Break all strings of length  2 into a series of strings of length 2.


Chomsky normal form8
Chomsky Normal Form

  • Replace the rule

    • A B1B2…Bk

      with

    • A  B1C1

    • C1  B2C2

    • Ck – 2  Bk – 2Ck – 2

    • Ck – 1  Bk – 1Bk


Example9
Example

  • Replace

    • S0SXS

    • S  SXS

  • with

    • S0SY

    • S  SY

    • Y XS


Example10
Example

  • The final result is

    • S0SY | SS | SX | XS | AB | 

    • S  SY | SS | SX | XS | AB

    • X  AB

    • Y XS

    • A a

    • B b


A derivation in cnf
A Derivation in CNF

  • Use this grammar in CNF to derive the string ababab.

  • S0 SY  SXS  ABXS

     ABABS  ABABAB

     aBABAB  abABAB

     abaBAB  ababAB

    ababaB  ababab.


Cnf derivations
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.


The membership problem
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.


Example11
Example

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


A tree of all possible derivations
A Tree of all Possible Derivations

S0

SY

SS

AB

SY

SS

SY

SS

AB

SY

a

b

XS

AB

AB

SS

etc.


Example12
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.


ad