Review • Languages and Grammars – Alphabets, strings, languages • Regular Languages CS 301 - Lecture 10 – Deterministic Finite and Nondeterministic Automata – Equivalence of NFA and DFA Chomsky and Greibach – Regular Expressions – Regular Grammars – Properties of Regular Languages Normal Forms – Languages that are not regular and the pumping lemma • Context Free Languages Fall 2008 – Context Free Grammars – Derivations: leftmost, rightmost and derivation trees – Parsing and ambiguity – Simplifying Context Free Grammars • Today: – More Simplifications – Normal Forms Which Variables are Nullable? Nullable Variables 0) Nullable Variables = For every production production : A λ − → λ Add to 1) For every variable check each production Nullable Variable: A ⇒ … ⇒ λ Add to if all 2) If step 1 added any to , repeat step 1 1
Removing Nullable Variables Final Grammar Example Grammar: S aMb → S aMb → S aMb → Substitute S ab → M aMb → M aMb → M → λ M aMb → M → λ M → λ M ab → Nullable variable Unit-Productions Removing Unit Productions Observation: A → A Unit Production: A → B Is removed immediately (a single variable in both sides) 2
Example Grammar: S aA S aA → → S aA | aB → A a A a → → A a Substitute → A B A B → → A → B B A | B → B A B A → → B bb → B bb B bb → → S aA | aB S aA | aB S aA | aB → → → S aA | aB | aA → A a A a A a → Remove → → Substitute A a → B → B B → A B A | B B A B A → → → B bb → B bb B bb B bb → → → 3
Remove repeated productions Removing All Final grammar S aA | aB | aA S aA | aB → → • Step 1: Remove Nullable Variables A a A a → → • Step 2: Remove Unit-Productions B bb B bb → → • Step 3: Remove Useless Variables Chomsky Normal Form Normal Forms Each productions has form: for A → a A → BC or Context-free Grammars variable variable terminal 4
Examples: Conversion to Chomsky S AS S AS Normal Form → → S ABa S a S AAS → → → • Example: A aab A SA A SA → → → B Ac A b A aa → → → Not Chomsky Chomsky Not Chomsky Normal Form Normal Form Normal Form V Introduce intermediate variable: 1 T , T , T Introduce variables for terminals: a b c S AV → 1 S ABT → S ABT a → V BT a → 1 a A T T T → A T T T a a b → A T T T S ABa a a b → → a a b B AT → B AT c → B AT A aab c → → c T a → T a a → T a B Ac a → → a T b → T b → b T b b → b T c → T c → c T c c → c 5
V Introduce intermediate variable: Final grammar in Chomsky Normal Form: 2 S AV → S AV 1 → 1 S AV → 1 V BT → V BT 1 a → 1 a V BT → 1 a A T V → A T V a 2 → a 2 Initial grammar A T T T → a a b V T T → V T T 2 a b → 2 a b B AT S ABa → → c B AT → B AT c → c T a A aab → → a T a → T a a → a T b B Ac → → b T b → T b → b b T c → c T c → T c c → c The Procedure In general: First remove: From any context-free grammar (which doesn’t produce ) λ Nullable variables not in Chomsky Normal Form Unit productions we can obtain: An equivalent grammar in Chomsky Normal Form 6
Replace any production A C C C → 1 2 n a Then, for every symbol : with A C V → 1 1 Add production T a → a V C V → 1 2 2 … a T V C C In productions: replace with − → n 2 n 1 n a − T New variable: a V , V , … , V New intermediate variables: 1 2 n 2 − Observations Theorem: For any context-free grammar • Chomsky normal forms are good (which doesn’t produce ) λ for parsing and proving theorems there is an equivalent grammar in Chomsky Normal Form • It is very easy to find the Chomsky normal form for any context-free grammar 7
Examples: Greibach Normal Form S cAB → All productions have form: S abSb → A aA | bB | b → S aa → A a V V V k 0 → ≥ B b 1 2 k → Greibach Not Greibach symbol variables Normal Form Normal Form Conversion to Greibach Normal Form: Theorem: For any context-free grammar (which doesn’t produce ) λ there is an equivalent grammar S aT ST → b b S abSb in Greibach Normal Form → S aT → a S aa → T a → a T b → b Greibach Normal Form 8
Observations What’s Next • Read • Greibach normal forms are very good – Linz Chapter 1,2.1, 2.2, 2.3, (skip 2.4), 3, 4, 5, 6.1, 6.2, (skip 6.3), and 7.1 for parsing – JFLAP Chapter 1, 2.1, (skip 2.2), 3, 4, 5, 6.1, 7 • Next Lecture Topics from Chapter 7.1 – Nondeterminstic Pushdown Automata • Quiz 2 in Recitation on Wednesday 10/1 – Covers Linz 2, 3, 4 and JFLAP 3, 4 – Closed book, but you may bring one sheet of 8.5 x 11 inch paper with any notes you like. – Quiz will take the full hour • It is hard to find the Greibach normal • Homework form of any context-free grammar – Homework Due Today – New Homework Available Friday Morning – New Homework Due Next Thursday 9
Recommend
More recommend