Chomsky Greibach Normal Forms Hector Miguel Chavez Western
Chomsky & Greibach Normal Forms Hector Miguel Chavez Western Michigan University
Presentation Outline • Introduction • Chomsky normal form • Preliminary simplifications • Final steps • Greibach Normal Form • Algorithm (Example) • Summary May 27, 2009 2
Introduction Grammar: G = (V, T, P, S) Terminals T = { a, b } Variables V = A, B, C Start Symbol Production May 27, 2009 S P=S→A 3
Introduction Grammar example S → a. BSc S → abc Ba → a. B Bb → bb S May 27, 2009 a. BSc L = { a n b n cn | n ≥ 1 } a. Babcc aa. Bbcc aabbcc 4
Introduction Context free grammar The head of any production contains only one non-terminal symbol S→P P → a. Pb P→ε May 27, 2009 L = { a nb n | n ≥ 0 } 5
Presentation Outline • Introduction • Chomsky normal form • Preliminary simplifications • Final simplification • Greibach Normal Form • Algorithm (Example) • Summary May 27, 2009 6
Chomsky Normal Form A context free grammar is said to be in Chomsky Normal Form if all productions are in the following form: A → BC A→α • A, B and C are non terminal symbols • α is a terminal symbol May 27, 2009 7
Presentation Outline • Introduction • Chomsky normal form • Preliminary simplifications • Final steps • Greibach Normal Form • Algorithm (Example) • Summary May 27, 2009 8
Preliminary Simplifications There are three preliminary simplifications May 27, 2009 1 Eliminate Useless Symbols 2 Eliminate ε productions 3 Eliminate unit productions 9
Preliminary Simplifications Eliminate Useless Symbols We need to determine if the symbol is useful by identifying if a symbol is generating and is reachable • X is generating if X * ω for some terminal string ω. • X is reachable if there is a derivation X * αXβ for some α and β May 27, 2009 10
Preliminary Simplifications Example: Removing non-generating symbols S → AB | a A→b Initial CFL grammar S → AB | a A→b Identify generating symbols S→a A→b May 27, 2009 Remove non-generating 11
Preliminary Simplifications Example: Removing non-reachable symbols May 27, 2009 S→a A→b Identify reachable symbols S→a Eliminate non-reachable 12
Preliminary Simplifications The order is important. Looking first for non-reachable symbols and then for non-generating symbols can still leave some useless symbols. S → AB | a A→b May 27, 2009 S→a A→b 13
Preliminary Simplifications Finding generating symbols If there is a production A → α, and every symbol of α is already known to be generating. Then A is generating S → AB | a A→b May 27, 2009 We cannot use S → AB because B has not been established to be generating 14
Preliminary Simplifications Finding reachable symbols S is surely reachable. All symbols in the body of a production with S in the head are reachable. S → AB | a A→b May 27, 2009 In this example the symbols {S, A, B, a, b} are reachable. 15
Preliminary Simplifications There are three preliminary simplifications May 27, 2009 1 Eliminate Useless Symbols 2 Eliminate ε productions 3 Eliminate unit productions 16
Preliminary Simplifications Eliminate ε Productions • • In a grammar ε productions are convenient but not essential If L has a CFG, then L – {ε} has a CFG A * ε Nullable variable May 27, 2009 17
Preliminary Simplifications If A is a nullable variable • Whenever A appears on the body of a production A might or might not derive ε S → ASA | a. B A→B|S B→b|ε May 27, 2009 Nullable: {A, B} 18
Preliminary Simplifications Eliminate ε Productions • • Create two version of the production, one with the nullable variable and one without it Eliminate productions with ε bodies S → ASA | a. B A→B|S B→b|ε May 27, 2009 S → ASA | a. B | AS | SA | S | a A→B|S B→b 19
Preliminary Simplifications Eliminate ε Productions • • Create two version of the production, one with the nullable variable and one without it Eliminate productions with ε bodies S → ASA | a. B A→B|S B→b|ε May 27, 2009 S → ASA | a. B | AS | SA | S | a A→B|S B→b 20
Preliminary Simplifications Eliminate ε Productions • • Create two version of the production, one with the nullable variable and one without it Eliminate productions with ε bodies S → ASA | a. B A→B|S B→b|ε May 27, 2009 S → ASA | a. B | AS | SA | S | a A→B|S B→b 21
Preliminary Simplifications There are three preliminary simplifications May 27, 2009 1 Eliminate Useless Symbols 2 Eliminate ε productions 3 Eliminate unit productions 22
Preliminary Simplifications Eliminate unit productions A unit production is one of the form A → B where both A and B are variables Identify unit pairs A * B A → B, B → ω, then A → ω May 27, 2009 23
Preliminary Simplifications Example: T = {*, +, (, ), a, b, 0, 1} I → a | b | Ia | Ib | I 0 | I 1 F → I | (E) T→F|T*F E→T|E+T Basis: (A, A) is a unit pair of any variable A, if A * A by 0 steps. May 27, 2009 Pairs Productions ( E, E ) E→E+T ( E, T ) E→T*F ( E, F ) E → (E) ( E, I ) E → a | b | Ia | Ib | I 0 | I 1 ( T, T ) T→T*F ( T, F ) T → (E) ( T, I ) T → a | b | Ia |Ib | I 0 | I 1 ( F, F ) F → (E) ( F, I ) F → a | b | Ia | Ib | I 0 | I 1 ( I, I ) I → a | b | Ia | Ib | I 0 | I 1 24
Preliminary Simplifications Example: Pairs Productions … … ( T, T ) T→T*F ( T, F ) T → (E) ( T, I ) T → a | b | Ia |Ib | I 0 | I 1 … … I → a | b | Ia | Ib | I 0 | I 1 E → E + T | T * F | (E ) | a | b | la | lb | l 0 | l 1 T → T * F | (E) | a | b | Ia | Ib | I 0 | I 1 F → (E) | a | b | Ia | Ib | I 0 | I 1 May 27, 2009 25
Presentation Outline • Introduction • Chomsky normal form • Preliminary simplifications • Final steps • Greibach Normal Form • Algorithm (Example) • Summary May 27, 2009 26
Final Simplification Chomsky Normal Form (CNF) Starting with a CFL grammar with the preliminary simplifications performed 1. Arrange that all bodies of length 2 or more to consists only of variables. 2. Break bodies of length 3 or more into a cascade of productions, each with a body consisting of two variables. May 27, 2009 27
Final Simplification Step 1: For every terminal α that appears in a body of length 2 or more create a new variable that has only one production. E → E + T | T * F | (E ) | a | b | la | lb | l 0 | l 1 T → T * F | (E) | a | b | Ia | Ib | I 0 | I 1 F → (E) | a | b | Ia | Ib | I 0 | I 1 I → a | b | Ia | Ib | I 0 | I 1 E → EPT | TMF | LER | a | b | l. A | l. B | l. Z | l. O T → TMF | LER | a | b | IA | IB | IZ | IO F → LER | a | b | IA | IB | IZ | IO I → a | b | IA | IB | IZ | IO A→a B→b Z→ 0 O→ 1 P→+ M→* L→( R→) May 27, 2009 28
Final Simplification Step 2: Break bodies of length 3 or more adding more variables E → EPT | TMF | LER | a | b | l. A | l. B | l. Z | l. O T → TMF | LER | a | b | IA | IB | IZ | IO C 1 → PT F → LER | a | b | IA | IB | IZ | IO C 2 → MF I → a | b | IA | IB | IZ | IO C 3 → ER A→a B→b Z→ 0 O→ 1 P→+ M→*L→( R→) May 27, 2009 29
Presentation Outline • Introduction • Chomsky normal form • Preliminary simplifications • Final steps • Greibach Normal Form • Algorithm (Example) • Summary May 27, 2009 30
Greibach Normal Form A context free grammar is said to be in Greibach Normal Form if all productions are in the following form: A → αX • A is a non terminal symbols • α is a terminal symbol • X is a sequence of non terminal symbols. It may be empty. May 27, 2009 31
Presentation Outline • Introduction • Chomsky normal form • Preliminary simplifications • Final steps • Greibach Normal Form • Algorithm (Example) • Summary May 27, 2009 32
Greibach Normal Form Example: S → XA | BB B → b | SB X→b A→a S = A 1 X = A 2 A = A 3 B = A 4 A 1 → A 2 A 3 | A 4 A 4 → b | A 1 A 4 A 2 → b A 3 → a CNF New Labels Updated CNF May 27, 2009 33
Greibach Normal Form Example: A 1 → A 2 A 3 | A 4 A 4 → b | A 1 A 4 A 2 → b A 3 → a First Step A i → A j Xk j > i Xk is a string of zero or more variables A 4 → A 1 A 4 May 27, 2009 34
Greibach Normal Form Example: First Step A i → A j Xk j > i A 4 → A 1 A 4 → A 2 A 3 A 4 | A 4 A 4 A 4 | b A 4 → b. A 3 A 4 | A 4 A 4 A 4 | b May 27, 2009 A 1 → A 2 A 3 | A 4 A 4 → b | A 1 A 4 A 2 → b A 3 → a 35
Greibach Normal Form Example: A 1 → A 2 A 3 | A 4 A 4 → b. A 3 A 4 | A 4 A 4 A 4 | b A 2 → b A 3 → a Second Step Eliminate Left Recursions A 4 → A 4 A 4 A 4 May 27, 2009 36
Greibach Normal Form Example: Second Step Eliminate Left Recursions A 4 → b. A 3 A 4 | b. A 3 A 4 Z | b. Z Z → A 4 A 4 | A 4 A 4 Z May 27, 2009 A 1 → A 2 A 3 | A 4 A 4 → b. A 3 A 4 | A 4 A 4 A 4 | b A 2 → b A 3 → a 37
Greibach Normal Form Example: A 1 → A 2 A 3 | A 4 A 4 → b. A 3 A 4 | b. A 3 A 4 Z | b. Z Z → A 4 A 4 | A 4 A 4 Z A 2 → b A 3 → a May 27, 2009 A → αX GNF 38
Greibach Normal Form Example: A 1 → A 2 A 3 | A 4 A 4 → b. A 3 A 4 | b. A 3 A 4 Z | b. Z Z → A 4 A 4 | A 4 A 4 Z A 2 → b A 3 → a A 1 → b. A 3 | b. A 3 A 4 A 4 | b. A 3 A 4 ZA 4 | b. ZA 4 Z → b. A 3 A 4 A 4 | b. A 3 A 4 ZA 4 | b. ZA 4 May 27, 2009 39
Greibach Normal Form Example: A 1 → b. A 3 | b. A 3 A 4 A 4 | b. A 3 A 4 ZA 4 | b. ZA 4 → b. A 3 A 4 | b. A 3 A 4 Z | b. Z Z → b. A 3 A 4 A 4 | b. A 3 A 4 ZA 4 | b. ZA 4 A 2 → b A 3 → a Grammar in Greibach Normal Form May 27, 2009 40
Presentation Outline Summary (Some properties) • Every CFG that doesn’t generate the empty string can be simplified to the Chomsky Normal Form and Greibach Normal Form • The derivation tree in a grammar in CNF is a binary tree • In the GNF, a string of length n has a derivation of exactly n steps • Grammars in normal form can facilitate proofs • CNF is used as starting point in the algorithm CYK May 27, 2009 41
Presentation Outline References [1] Introduction to Automata Theory, Languages and Computation, John E. Hopcroft, Rajeev Motwani and Jeffrey D. Ullman, 2 nd edition, Addison Wesley 2001 (ISBN: 0 -201 -44124 -1) [2] CS-311 HANDOUT, Greibach Normal Form (GNF), Humaira Kamal, http: //suraj. lums. edu. pk/~cs 311 w 02/GNF-handout. pdf [3] Conversion of a Chomsky Normal Form Grammar to Greibach Normal Form, Arup Guha, http: //www. cs. ucf. edu/courses/cot 4210/spring 05/lectures/Lec 14 Greiba ch. ppt May 27, 2009 42
Test Questions 1. Convert the following grammar to the Chomsky Normal Form. S→P P → a. Pb | ε 2. Is the following grammar context-free? S → a. BSc | abc Ba → a. B Bb → bb 3. 4. Prove that the language L = { anbncn | n ≥ 1 } is not context-free. Convert the following grammar to the Greibach Normal Form. S -> a | CD | CS A -> a | b | SS C -> a D -> AS May 27, 2009 43
Presentation Outline Thank You! May 27, 2009 44
- Slides: 44