Module 32 - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Module 32

Description:

A BC. A a. Key ideas. Eliminating ?-productions (e.g. S ?) Eliminating unit productions (e.g. A B) 3 ... Bn with a series of productions: A B1Y1. Y1 B21Y2. Yn-2 Bn-1Bn ... – PowerPoint PPT presentation

Number of Views:14
Avg rating:3.0/5.0
Slides: 15
Provided by: erict9
Learn more at: http://www.cse.msu.edu
Category:
Tags: bc | module | series

less

Transcript and Presenter's Notes

Title: Module 32


1
Module 32
  • Chomsky Normal Form (CNF)
  • 4 step process

2
Chomsky Normal Form
  • A CFG is in Chomsky normal form (CNF) if every
    production is one of these two types
  • A ? BC
  • A ? a
  • Key ideas
  • Eliminating ?-productions (e.g. S ? ?)
  • Eliminating unit productions (e.g. A ? B)

3
Nullable Variables
  • A variable A in a CFG G (V, S, S, P) is defined
    as nullable if
  • Base case P contains the production A ? ?
  • Recursive case P contains the production
  • A ? B1B2 Bn and B1 through Bn are nullable
  • No other variables are nullable

4
Finding Nullable Variables
  • Initialize N0 to be the set of nullable variables
    by the base case definition
  • i 0
  • do
  • i i1
  • Ni Ni-1 union A P contains A ? a where a is
    a string in Ni-1
  • while Ni ? Ni-1
  • The final Ni is the set of nullable variables

5
Eliminating ?-productions
  • Given CFG G (V, S, S, P), construct a CFG G1
    (V, S, S, P1) as follows.
  • Initialize P1 P
  • Find set of all nullable variables N in G
  • For every production A ? a in P, add to P1 every
    production that can be obtained from this one by
    deleting from a one or more of the occurrences of
    nullable variables in a
  • Example A ? BBCD where B and C are nullable
    leads to
  • A ? BCD BCD BBD BD BD CD D
  • Clean up
  • Delete all ?-productions from P1
  • Delete any duplicate productions
  • Delete any productions of the form A ? A
  • Thm L(G1) L(G) ?

6
A-derivable Variables
  • B is A-derivable in a CFG G if and only if
  • A gtG B
  • Recursive definition A variable B in a CFG G
    (V, S, S, P) is defined as A-derivable if
  • Base case P contains the production A ? B or B
    A
  • Recursive case Variable C is A-derivable and P
    contains the production C ? B
  • No other variables are A-derivable
  • Easy to make into algorithm

7
Eliminating unit productions
  • Given CFG G (V, S, S, P), construct a CFG G1
    (V, S, S, P1) as follows.
  • Initialize P1 P
  • Find each A in V, find set of A-derivable
    variables in V
  • For each pair (A,B) such that B is A-derivable
    and every non-unit production B ? a in P, add
    production A ? a in P1
  • Clean up
  • Delete all unit productions from P1
  • Delete any duplicate productions
  • Thm L(G1) L(G) if G did not have any
    ?-productions

8
Making CNF grammar
  • First eliminate ?-productions
  • Then eliminate unit productions
  • For each terminal a in S, introduce a variable Xa
    with production rule Xa ? a
  • For each production of the form A ? a where
    terminal a appears and a gt 1, replace a with Xa
  • Finally, replace productions of the form A ? B1B2
    Bn with a series of productions
  • A ?B1Y1
  • Y1 ?B21Y2
  • ....
  • Yn-2 ?Bn-1Bn
  • Other methods can be used for this last step

9
Example Eliminate ?-productions
  • S ? AACD
  • A ? aAb ?
  • C ? aC a
  • D ? aDa bDb ?
  • Nullable variables A D
  • New grammar
  • S ? AACD ACD AAC CD AC C
  • A ? aAb ab
  • C ? aC a
  • D ? aDa bDb aa bb

10
Example Eliminate unit productions
  • S ? AACD ACD AAC CD AC C
  • A ? aAb ab
  • C ? aC a
  • D ? aDa bDb aa bb
  • New grammar
  • S ? AACD ACD AAC CD AC aC a
  • A ? aAb ab
  • C ? aC a
  • D ? aDa bDb aa bb

11
Example Add Xa and Xb
  • S ? AACD ACD AAC CD AC aC a
  • A ? aAb ab
  • C ? aC a
  • D ? aDa bDb aa bb
  • New grammar
  • S ? AACD ACD AAC CD AC XaC a
  • A ? XaAXb XaXb
  • C ? XaC a
  • D ? XaDXa XbDXb XaXa XbXb
  • Xa? a
  • Xb? b

12
Example Shorten long productions
  • S ? AACD ACD AAC CD AC XaC a
  • A ? XaAXb XaXb
  • C ? XaC a
  • D ? XaDXa XbDXb XaXa XbXb
  • Xa? a
  • Xb? b
  • Example replacement
  • S ? AACD becomes S ? AT1, T1 ? AT2, T2 ? CD

13
Observations
  • Consider a derivation from a CNF grammar G that
    begins
  • S gtG ABCD
  • How short can the final derived terminal string
    be?
  • Why?

14
Observation 2
  • A path in a parse tree has length x if it
    contains x variables
  • Consider a parse tree T for a string x and a CNF
    grammar G with m variables.
  • Suppose the longest path in T has length k. How
    long can this string x be?
  • Suppose string x has length 2m. How short can the
    longest path in T be?
Write a Comment
User Comments (0)
About PowerShow.com