12.07.2015 Views

Context-Free Grammars - Chomsky Normal Form - Hampden ...

Context-Free Grammars - Chomsky Normal Form - Hampden ...

Context-Free Grammars - Chomsky Normal Form - Hampden ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Homework Review<strong>Context</strong>-<strong>Free</strong><strong>Grammars</strong> -<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>Robb T.KoetherHomeworkReview<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>New start symbolEliminate all ε-rulesEliminate all unitrulesEliminate all mixedrulesEliminate all longrulesDerivations inCNFSolution(d) ab, ba, aab ∈ L(G).(e) a, b, ε /∈ L(G).(f) False, T aba.(g) True, T =⇒ ∗ aba.(h) False, T T.(i) True, T =⇒ ∗ T.TheMembershipProblem forCFGsAssignment


Homework Review<strong>Context</strong>-<strong>Free</strong><strong>Grammars</strong> -<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>Robb T.KoetherHomeworkReview<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>New start symbolEliminate all ε-rulesEliminate all unitrulesEliminate all mixedrulesEliminate all longrulesDerivations inCNFExercise 3, page 128.(j) True or False: XXX =⇒ ∗ aba.(k) True or False: X =⇒ ∗ aba.(l) True or False: T =⇒ ∗ XX.(m) True or False: T =⇒ ∗ XXX.(n) True or False: S =⇒ ∗ ε.TheMembershipProblem forCFGsAssignment


Homework Review<strong>Context</strong>-<strong>Free</strong><strong>Grammars</strong> -<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>Robb T.KoetherHomeworkReview<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>New start symbolEliminate all ε-rulesEliminate all unitrulesEliminate all mixedrulesEliminate all longrulesDerivations inCNFSolution(j) True, XXX =⇒ ∗ aba.(k) False, X ∗ aba.(l) True, T =⇒ ∗ XX.(m) True, T =⇒ ∗ XXX.(n) False, S ∗ ε.TheMembershipProblem forCFGsAssignment


Homework Review<strong>Context</strong>-<strong>Free</strong><strong>Grammars</strong> -<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>Robb T.KoetherHomeworkReview<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>New start symbolEliminate all ε-rulesEliminate all unitrulesEliminate all mixedrulesEliminate all longrulesSolution(o) The R must eventually produce X n SX n , for some n ≥ 0.(p) The S must produce either aTb or bTa.(q) The T must eventually produce X m , for some m ≥ 0.(r) Thus, so far, R produces X n aX m bX n or X n bX m aX n , forsome m, n ≥ 0.Derivations inCNFTheMembershipProblem forCFGsAssignment


Homework Review<strong>Context</strong>-<strong>Free</strong><strong>Grammars</strong> -<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>Robb T.KoetherHomeworkReview<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>New start symbolEliminate all ε-rulesEliminate all unitrulesEliminate all mixedrulesEliminate all longrulesDerivations inCNFSolution(o) Now X can be replaced with either a or b(p) Therefore, X n and X m can be any string in Σ ∗ .(q) Thus, the language is the set of all strings of the formuavbw or ubvaw, where u, v, w ∈ Σ ∗ and |u| = |w|.(r) It is not hard to see that this is the complement of theset {ww R | w ∈ Σ ∗ }.TheMembershipProblem forCFGsAssignment


<strong>Chomsky</strong> <strong>Normal</strong> <strong>Form</strong><strong>Context</strong>-<strong>Free</strong><strong>Grammars</strong> -<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>Robb T.KoetherHomeworkReview<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>New start symbolEliminate all ε-rulesEliminate all unitrulesEliminate all mixedrulesEliminate all longrulesDerivations inCNFDefinition (<strong>Chomsky</strong> <strong>Normal</strong> <strong>Form</strong>)A grammar is in <strong>Chomsky</strong> <strong>Normal</strong> <strong>Form</strong>, abbreviated CNF, ifeach rule is of the formA → BC, orA → a,where B and C are nonteriminals not equal to S and a is aterminal. Furthermore, the rule S → ε is allowed.TheMembershipProblem forCFGsAssignment


<strong>Chomsky</strong> <strong>Normal</strong> <strong>Form</strong><strong>Context</strong>-<strong>Free</strong><strong>Grammars</strong> -<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>Robb T.KoetherHomeworkReview<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>New start symbolEliminate all ε-rulesEliminate all unitrulesTheorem (<strong>Chomsky</strong> <strong>Normal</strong> <strong>Form</strong>)Every context-free language is generated by a grammar in<strong>Chomsky</strong> <strong>Normal</strong> <strong>Form</strong>.Eliminate all mixedrulesEliminate all longrulesDerivations inCNFTheMembershipProblem forCFGsAssignment


<strong>Chomsky</strong> <strong>Normal</strong> <strong>Form</strong><strong>Context</strong>-<strong>Free</strong><strong>Grammars</strong> -<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>Robb T.KoetherHomeworkReview<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>New start symbolEliminate all ε-rulesEliminate all unitrulesEliminate all mixedrulesEliminate all longrulesDerivations inCNFOutline of proof.Begin with a grammar for the context-free language.Add a new start symbol S 0 .Eliminate all ε-rules A → ε.Eliminate all unit rules A → B.Eliminate all mixed rules.Eliminate all long rules.TheMembershipProblem forCFGsAssignment


New Start Symbol<strong>Context</strong>-<strong>Free</strong><strong>Grammars</strong> -<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>Robb T.KoetherHomeworkReview<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>New start symbolProof (New start symbol S 0 ).Add the rule S 0 → S.Eliminate all ε-rulesEliminate all unitrulesEliminate all mixedrulesEliminate all longrulesDerivations inCNFTheMembershipProblem forCFGsAssignment


Example<strong>Context</strong>-<strong>Free</strong><strong>Grammars</strong> -<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>Robb T.KoetherHomeworkReviewExample (New start symbol S 0 )Start with the grammar<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>New start symbolEliminate all ε-rulesEliminate all unitrulesEliminate all mixedrulesEliminate all longrulesDerivations inCNFTheMembershipProblem forCFGsAssignmentAdd the ruleS → SXS | εX → ab | εS 0 → S


Example<strong>Context</strong>-<strong>Free</strong><strong>Grammars</strong> -<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>Robb T.KoetherHomeworkReview<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>New start symbolEliminate all ε-rulesEliminate all unitrulesEliminate all mixedrulesEliminate all longrulesExample (New start symbol S 0 )We now have the grammarS 0 → SS → SXS | εX → ab | εDerivations inCNFTheMembershipProblem forCFGsAssignment


Eliminate All ε-Rules<strong>Context</strong>-<strong>Free</strong><strong>Grammars</strong> -<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>Robb T.KoetherHomeworkReview<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>New start symbolEliminate all ε-rulesEliminate all unitrulesProof (Eliminate all ε-rules).For each rule A → ε and each rule B → uAv (with A onthe right), add a rule B → uv.Eliminate the rule A → ε.Eliminate all mixedrulesEliminate all longrulesDerivations inCNFTheMembershipProblem forCFGsAssignment


Example<strong>Context</strong>-<strong>Free</strong><strong>Grammars</strong> -<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>Robb T.KoetherHomeworkReview<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>New start symbolEliminate all ε-rulesEliminate all unitrulesEliminate all mixedrulesEliminate all longrulesDerivations inCNFTheMembershipProblem forCFGsExample (Eliminate all ε-rules)Apply the rules S → ε and X → ε to the other rules,creating the rulesS 0 → εS → XS → SSS → XSS → SXS → S(Don’t bother keeping the last rule.)Assignment


Example<strong>Context</strong>-<strong>Free</strong><strong>Grammars</strong> -<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>Robb T.KoetherHomeworkReview<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>New start symbolEliminate all ε-rulesEliminate all unitrulesEliminate all mixedrulesEliminate all longrulesExample (Eliminate all ε-rules)Eliminate the rulesS → εX → εDerivations inCNFTheMembershipProblem forCFGsAssignment


Example<strong>Context</strong>-<strong>Free</strong><strong>Grammars</strong> -<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>Robb T.KoetherHomeworkReview<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>New start symbolEliminate all ε-rulesEliminate all unitrulesEliminate all mixedrulesEliminate all longrulesExample (Eliminate all ε-rules)We now haveS 0 → S | εS → SXS | SS | SX | XS | XX → abDerivations inCNFTheMembershipProblem forCFGsAssignment


Eliminate All Unit Rules<strong>Context</strong>-<strong>Free</strong><strong>Grammars</strong> -<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>Robb T.KoetherHomeworkReview<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>New start symbolEliminate all ε-rulesEliminate all unitrulesProof (Eliminate all unit rules).If A → B and B → u are rules, then add the rule A → u.Eliminate the rule A → B.Eliminate all mixedrulesEliminate all longrulesDerivations inCNFTheMembershipProblem forCFGsAssignment


Example<strong>Context</strong>-<strong>Free</strong><strong>Grammars</strong> -<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>Robb T.KoetherHomeworkReview<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>New start symbolEliminate all ε-rulesEliminate all unitrulesEliminate all mixedrulesEliminate all longrulesDerivations inCNFTheMembershipProblem forCFGsExample (Eliminate all unit rules)Add the rulesS 0 → SXS | SS | SX | XS | X | abS → abEliminate the rulesS 0 → SS → XAssignment


Example<strong>Context</strong>-<strong>Free</strong><strong>Grammars</strong> -<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>Robb T.KoetherHomeworkReview<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>New start symbolEliminate all ε-rulesEliminate all unitrulesEliminate all mixedrulesEliminate all longrulesExample (Eliminate all unit rules)We now haveS 0 → SXS | SS | SX | XS | ab | εS → SXS | SS | SX | XS | abX → abDerivations inCNFTheMembershipProblem forCFGsAssignment


Eliminate All Mixed Rules<strong>Context</strong>-<strong>Free</strong><strong>Grammars</strong> -<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>Robb T.KoetherHomeworkReview<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>New start symbolEliminate all ε-rulesEliminate all unitrulesEliminate all mixedrulesEliminate all longrulesDerivations inCNFDefinition (Mixed rule)A mixed rule is a rule whose right-hand side has length atleast 2 and contains at least one terminal.Proof (Eliminate all mixed rules).Add rulesA → afor all terminals a appearing in mixed rules.Replace a with A in those rules.TheMembershipProblem forCFGsAssignment


Example<strong>Context</strong>-<strong>Free</strong><strong>Grammars</strong> -<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>Robb T.KoetherHomeworkReview<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>New start symbolEliminate all ε-rulesEliminate all unitrulesEliminate all mixedrulesEliminate all longrulesDerivations inCNFExample (Eliminate all mixed rules)Add the rulesA → aB → bReplace the string ab with AB.TheMembershipProblem forCFGsAssignment


Example<strong>Context</strong>-<strong>Free</strong><strong>Grammars</strong> -<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>Robb T.KoetherHomeworkReview<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>New start symbolEliminate all ε-rulesEliminate all unitrulesEliminate all mixedrulesEliminate all longrulesDerivations inCNFExample (Eliminate all mixed rules)We now haveS 0 → SXS | SS | SX | XS | AB | εS → SXS | SS | SX | XS | ABX → ABA → aB → bTheMembershipProblem forCFGsAssignment


Eliminate All Long Rules<strong>Context</strong>-<strong>Free</strong><strong>Grammars</strong> -<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>Robb T.KoetherHomeworkReview<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>New start symbolEliminate all ε-rulesEliminate all unitrulesDefinition (Mixed rule)A long rule is a rule whose right-hand side has length atleast 3.Eliminate all mixedrulesEliminate all longrulesDerivations inCNFTheMembershipProblem forCFGsAssignment


Eliminate All Long Rules<strong>Context</strong>-<strong>Free</strong><strong>Grammars</strong> -<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>Robb T.KoetherHomeworkReview<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>New start symbolEliminate all ε-rulesEliminate all unitrulesEliminate all mixedrulesEliminate all longrulesDerivations inCNFTheMembershipProblem forCFGsProof (Eliminate all long rules).Replace the long rulesA → B 1 B 2 . . . B k , (k ≥ 3)withA → B 1 C 1C 1 → B 2 C 2C 2 → B 3 C 3.C k−2 → B k−2 C k−2C k−1 → B k−1 B kAssignment


Example<strong>Context</strong>-<strong>Free</strong><strong>Grammars</strong> -<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>Robb T.KoetherHomeworkReview<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>New start symbolExample (Eliminate all long rules)ReplaceS 0 → SXSS → SXSEliminate all ε-rulesEliminate all unitrulesEliminate all mixedrulesEliminate all longrulesDerivations inCNFTheMembershipProblem forCFGsAssignmentwithS 0 → SYS → SYY → XS


Example<strong>Context</strong>-<strong>Free</strong><strong>Grammars</strong> -<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>Robb T.KoetherHomeworkReview<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>New start symbolEliminate all ε-rulesEliminate all unitrulesEliminate all mixedrulesEliminate all longrulesDerivations inCNFTheMembershipProblem forCFGsExample (Eliminate all long rules)The final result isS 0 → SY | SS | SX | XS | AB | εS → SY | SS | SX | XS | ABX → ABY → XSA → aB → bwhich is in <strong>Chomsky</strong> <strong>Normal</strong> <strong>Form</strong>.Assignment


A Derivation in CNF<strong>Context</strong>-<strong>Free</strong><strong>Grammars</strong> -<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>Robb T.KoetherHomeworkReview<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>New start symbolEliminate all ε-rulesEliminate all unitrulesEliminate all mixedrulesEliminate all longrulesDerivations inCNFExample (A CNF derivation)Use this grammar in CNF to derive the string ababab.S 0 ⇒ SY⇒ SXS⇒ ABXS⇒ ABABS⇒ ABABABTheMembershipProblem forCFGsAssignment


A Derivation in CNF<strong>Context</strong>-<strong>Free</strong><strong>Grammars</strong> -<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>Robb T.KoetherExample (A CNF derivation)HomeworkReview<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>New start symbolEliminate all ε-rulesEliminate all unitrulesEliminate all mixedrulesEliminate all longrulesDerivations inCNFTheMembershipProblem forCFGsAssignment.⇒⇒⇒⇒⇒⇒aBABABabABABabaBABababABababaBababab.


CNF Derivations<strong>Context</strong>-<strong>Free</strong><strong>Grammars</strong> -<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>Robb T.KoetherHomeworkReview<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>New start symbolEliminate all ε-rulesEliminate all unitrulesTheoremIf a grammar G is in CNF and a string w in L(G) has lengthn, then w is derived from G in exactly 2n − 1 steps.Eliminate all mixedrulesEliminate all longrulesDerivations inCNFTheMembershipProblem forCFGsAssignment


The Membership Problem for CFGs<strong>Context</strong>-<strong>Free</strong><strong>Grammars</strong> -<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>Robb T.KoetherHomeworkReview<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>New start symbolEliminate all ε-rulesEliminate all unitrulesEliminate all mixedrulesEliminate all longrulesDefinition (The Membership Problem for CFGs)Given a CFG G and a string w, can w be derived from G?The previous theorem allows us to solve theMembership Problem.Derivations inCNFTheMembershipProblem forCFGsAssignment


Example<strong>Context</strong>-<strong>Free</strong><strong>Grammars</strong> -<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>Robb T.KoetherHomeworkReview<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>New start symbolEliminate all ε-rulesEliminate all unitrulesEliminate all mixedrulesEliminate all longrulesExample (The Membership Problem for CFGs)Show that the string abba is not derivable from thegrammar of the previous example.Draw a tree of all possible derivations of strings up tolength 4.This will involve up to 7 steps (but no more).Derivations inCNFTheMembershipProblem forCFGsAssignment


Example<strong>Context</strong>-<strong>Free</strong><strong>Grammars</strong> -<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>Robb T.KoetherExample (The Membership Problem for CFGs)S 0HomeworkReview<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>New start symbolEliminate all ε-rulesEliminate all unitrulesEliminate all mixedrulesEliminate all longrulesSY:SS:SX XS:AB:SYX SSX SXX XSX ABX SAB: : : : : :Derivations inCNFTheMembershipProblem forCFGsAssignmentSYAB SSAB SXAB XSAB: : : :ABABabab


Example<strong>Context</strong>-<strong>Free</strong><strong>Grammars</strong> -<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>Robb T.KoetherHomeworkReview<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>New start symbolEliminate all ε-rulesEliminate all unitrulesEliminate all mixedrulesEliminate all longrulesDerivations inCNFExample (The Membership Problem for CFGs)Put the grammarE → E + E | E ∗ E | (E) | a | b | cinto CNF.Show that the string c++ is not derivable from it.TheMembershipProblem forCFGsAssignment


Assignment<strong>Context</strong>-<strong>Free</strong><strong>Grammars</strong> -<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>Robb T.KoetherHomeworkReview<strong>Chomsky</strong><strong>Normal</strong> <strong>Form</strong>New start symbolEliminate all ε-rulesEliminate all unitrulesHomeworkRead Section 2.1, pages 106 - 109.Exercise 14, page 129.Eliminate all mixedrulesEliminate all longrulesDerivations inCNFTheMembershipProblem forCFGsAssignment

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!