Normal Forms for Context-Free Grammars

Click here to load reader

  • date post

    31-Dec-2015
  • Category

    Documents

  • view

    43
  • download

    0

Embed Size (px)

description

CS 3240 – Chapter 6. Normal Forms for Context-Free Grammars. Topics. 6.1: Simplifying Grammars Substitution Removing useless variables Removing λ Removing unit productions 6.2: Normal Forms Chomsky Normal (CNF) 6.3: A Membership Algorithm CYK Algorithm - PowerPoint PPT Presentation

Transcript of Normal Forms for Context-Free Grammars

  • CS 3240 Chapter 6

    CS 3240 - Normal Forms for Context-Free Languages

  • 6.1: Simplifying GrammarsSubstitutionRemoving useless variablesRemoving Removing unit productions6.2: Normal FormsChomsky Normal (CNF)6.3: A Membership AlgorithmCYK AlgorithmAn example of bottom-up parsingCS 3240 - Normal Forms for Context-Free Languages*

    CS 3240 - Normal Forms for Context-Free Languages

  • Variables in CFGs can often be eliminatedIf they are not recursive, you can substitute their rules throughoutSee next slideCS 3240 - Normal Forms for Context-Free Languages*

    CS 3240 - Normal Forms for Context-Free Languages

  • CS 3240 - Normal Forms for Context-Free Languages*A a | aaA | abBcB abbA | b

    Just substitute directly for B:

    A a | aaA | ababbAc | abbc

    CS 3240 - Normal Forms for Context-Free Languages

  • A variable is useless if:It cant be reached from the start state, orIt never leads to a terminal stringDue to endless recursionBoth problems can be detected by a dependency graphSee next slideCS 3240 - Normal Forms for Context-Free Languages*

    CS 3240 - Normal Forms for Context-Free Languages

  • CS 3240 - Normal Forms for Context-Free Languages*S aSb | A | A aA

    A is useless (non-terminating):

    S aSb |

    CS 3240 - Normal Forms for Context-Free Languages

  • CS 3240 - Normal Forms for Context-Free Languages*S AA aA | B bA

    B is useless (non-reachable):

    S AA aA |

    CS 3240 - Normal Forms for Context-Free Languages

  • CS 3240 - Normal Forms for Context-Free Languages*Simplify the following:

    S aS | A | CA aB aaC aCb

    CS 3240 - Normal Forms for Context-Free Languages

  • CS 3240 - Normal Forms for Context-Free Languages*

    CS 3240 - Normal Forms for Context-Free Languages

  • CS 3240 - Normal Forms for Context-Free Languages*Simplify the following grammar:

    S AB | ACA aAb | bAa | aB bbA | aaB | ABC abCa | aDbD bD | aC

    CS 3240 - Normal Forms for Context-Free Languages

  • Any variable that can eventually terminate in the empty string is said to be nullableNote: a variable may be indirectly nullableIn general: if A V1V2Vn, and all the Vi are nullable, then A is also nullableSee Theorem 6.3CS 3240 - Normal Forms for Context-Free Languages*

    CS 3240 - Normal Forms for Context-Free Languages

  • CS 3240 - Normal Forms for Context-Free Languages*Consider the following grammar:

    S a | Xb | aYaX Y | Y b | X

    Which variables are nullable?

    How can we substitute the effect of before removing it?

    CS 3240 - Normal Forms for Context-Free Languages

  • First find all nullable variablesThen substitute (A + ) for every nullable variable A, and expandThen remove everywhere from the grammarWhats left is equivalent to the original grammarexcept the empty string may be lostwe wont worry about thatCS 3240 - Normal Forms for Context-Free Languages*

    CS 3240 - Normal Forms for Context-Free Languages

  • CS 3240 - Normal Forms for Context-Free Languages*Consider the following grammar (again):

    S a | Xb | aYaX Y | Y b | X

    How can we substitute the effect of before removing it?

    CS 3240 - Normal Forms for Context-Free Languages

  • S aSbS | bSaS |

    S aSa | bSb | XX aYb | bYaY aY | bY |

    CS 3240 - Normal Forms for Context-Free Languages*

    CS 3240 - Normal Forms for Context-Free Languages

  • Unit Productions often occur in chainsA B CMust maintain the effect of B and C when substituting for A throughoutProcedure:Find all unit chainsRebuild grammar by:Keeping all non-unit productionsKeeping only the effect of all unit productions/chainsCS 3240 - Normal Forms for Context-Free Languages*

    CS 3240 - Normal Forms for Context-Free Languages

  • CS 3240 - Normal Forms for Context-Free Languages*

    CS 3240 - Normal Forms for Context-Free Languages

  • 1) Determine all variables reachable by unit rules for each variable

    2) Keep all non-unit rules

    3) Substitute non-unit rules in place of each variable reachable by unit productionsCS 3240 - Normal Forms for Context-Free Languages*

    CS 3240 - Normal Forms for Context-Free Languages

  • CS 3240 - Normal Forms for Context-Free Languages*S aAA BBB aBb |

    Now remove nulls and see what happens.

    (Also see the solution for #15 in 6.1)

    CS 3240 - Normal Forms for Context-Free Languages

  • CS 3240 - Normal Forms for Context-Free Languages*S ABA BB aB | BB |

    CS 3240 - Normal Forms for Context-Free Languages

  • Do things in the following recommended order:Remove nullsRemove unit productionsRemove useless variablesSimplify by substitution as desiredCS 3240 - Normal Forms for Context-Free Languages*

    CS 3240 - Normal Forms for Context-Free Languages

  • Very important for our purposesAll CNF rules are of one of the following two forms:A c(a single terminal)A XY(exactly two variables)Must begin the transformation after simplifying the grammar (removing , all unit productions, useless variables, etc.)CS 3240 - Normal Forms for Context-Free Languages*

    CS 3240 - Normal Forms for Context-Free Languages

  • CS 3240 - Normal Forms for Context-Free Languages*Convert to CNF:

    S bA | aBA bAA | aS | aB aBB | bS | b

    (NOTE: already has no nulls or units)

    CS 3240 - Normal Forms for Context-Free Languages

  • CS 3240 - Normal Forms for Context-Free Languages*Convert the following grammar to CNF:

    S abABA bAB | B BAa | A |

    CS 3240 - Normal Forms for Context-Free Languages

  • CS 3240 - Normal Forms for Context-Free Languages*Convert the following grammar to CNF:

    S aS | bS | BB bb | C | C cC |

    CS 3240 - Normal Forms for Context-Free Languages

  • Single terminal character followed by zero or more variables (cV*, c )V aV aBCD allowed only in S Sometimes need to make up new variable namesCS 3240 - Normal Forms for Context-Free Languages*

    CS 3240 - Normal Forms for Context-Free Languages

  • CS 3240 - Normal Forms for Context-Free Languages*S ABA aA | bB | bB b

    Substitute for A in first rule (i.e., add B to each rule for A):

    S aAB | bBB | bB

    The other rules are okay

    CS 3240 - Normal Forms for Context-Free Languages

  • CS 3240 - Normal Forms for Context-Free Languages*S abSb |aa

    Add rules to generate a and b:

    S aBSB |aAA aB b

    CS 3240 - Normal Forms for Context-Free Languages

  • The parsing problemHow do we know if a string is generated by a given grammar?Bottom-up parsing (CYK Algorithm)An Example of Dynamic ProgrammingRequires Chomsky Normal Form (CNF)Start by considering A c rulesBuild up the parse tree from thereCS 3240 - Normal Forms for Context-Free Languages*

    CS 3240 - Normal Forms for Context-Free Languages

  • CS 3240 - Normal Forms for Context-Free Languages*S XYX XA | a | bY AY | aA a

    Does this grammar generate baaa?

    CS 3240 - Normal Forms for Context-Free Languages

  • CS 3240 - Normal Forms for Context-Free Languages*CNF yields binary trees. (Can you find a third parse tree?)

    CS 3240 - Normal Forms for Context-Free Languages

  • CS 3240 - Normal Forms for Context-Free Languages*

    CS 3240 - Normal Forms for Context-Free Languages

  • CS 3240 - Normal Forms for Context-Free Languages*

    CS 3240 - Normal Forms for Context-Free Languages

  • CS 3240 - Normal Forms for Context-Free Languages*

    CS 3240 - Normal Forms for Context-Free Languages

  • CS 3240 - Normal Forms for Context-Free Languages*

    CS 3240 - Normal Forms for Context-Free Languages

  • CS 3240 - Normal Forms for Context-Free Languages*

    CS 3240 - Normal Forms for Context-Free Languages

  • CS 3240 - Normal Forms for Context-Free Languages*Does the following grammar generate abbaab?

    S SAB | A aA | B bB |

    CS 3240 - Normal Forms for Context-Free Languages

    *C is a jail. B is non-reachable.

    S aS | AA a

    Same as

    S aS | a*D and C are useless. (Draw a dependency graph). Answer:

    S ABA aAb | bAa | aB bbA | aaB | AB*A dependency graph considering productions whose right-hand sides have no terminals is helpful in finding nullable variables.

    *S a | Xb | b | aYa | aaX YY b | X*The black represents the non-unit parts of the associated original rule. Then we add the non-unit rules for the places reachable by unit chains of the variable in question. In this case, only the first line survives.*The unit rule A B is introduced.**All nullable. Unit chains:

    S B bbS B C cC | cB C cC | c

    B ends up not reachable?*Another example: S aSbb | :

    S aSBB | B b*S => XY => XAY => XAAY => baaa*Show associated subtrees*Yes. Convert to CNF:

    S SZ | SA | SB | AB | XA | YB | a | bA XA | aB YB | bZ ABX aY b*