CSE322 Normal Forms: CNF & GNF Lecture Notes PDF

Document Details

IntimateIndigo3153

Uploaded by IntimateIndigo3153

Lovely Professional University

Tags

formal languages context-free grammars normal forms computer science

Summary

These lecture notes cover normal forms, specifically Chomsky Normal Form (CNF) and Greibach Normal Form (GNF), for context-free grammars (CFGs). They explain how to convert a CFG to these normal forms and discuss elimination of useless and epsilon productions. The presentation includes examples and steps to transform grammars.

Full Transcript

CSE322 Normal forms: CNF & GNF Lecture #28-29 Presentation outline Introduction Chomsky normal form Preliminary simplifications Final steps Greibach Normal Form Algorithm (Example) Summary Grammar: G = (V, T, P, S) Terminal TT =...

CSE322 Normal forms: CNF & GNF Lecture #28-29 Presentation outline Introduction Chomsky normal form Preliminary simplifications Final steps Greibach Normal Form Algorithm (Example) Summary Grammar: G = (V, T, P, S) Terminal TT = ={{ a, a, b b}} s Variabl V V== A, A, B, B, C C es Start S S Symbol Production PP = =SS→ →AA Grammar example S → aBSc S → abc LL = ={ab { a b cc || n nn nn nn n Ba → aB ≥ ≥1 1} } Bb → bb S  aBSc   aBabcc  aaBbcc aabbcc Context free grammar The head of any production contains only one non-terminal symbol S→P LL = ={ab { a b || n nn nn n≥≥ P → aPb P→ε 0 0} } Introduction Chomsky normal form Preliminary simplifications Final simplification Greibach Normal Form Algorithm (Example) Summary 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 Introduction Chomsky normal form Preliminary simplifications Final steps Greibach Normal Form Algorithm (Example) Summary There are three preliminary simplifications Eliminate Eliminate Useless Useless 1 Symbols Symbols Eliminate Eliminate εε 2 productions productions Eliminate Eliminate unit unit 3 productions productions 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 β Example: Removing non-generating symbols SS→→ AB AB || aa Initial CFL AA→→bb grammar SS→→ AB AB || aa Identify generating AA→→bb symbols S S→→ aa Remove non-generating A A→→b b Preliminary Simplifications Example: Removing non-reachable symbols S S→→ aa Identify reachable A A→→b b symbols S S→→ aa Eliminate non-reachable Preliminary Simplifications The order is important. Looking first for non-reachable symbols and then for non-generating symbols can still leave some useless symbols. SS→→ AB AB || S S→→ aa aa A A→→b b AA→→bb 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 SS→→ AB AB || We cannot use S → AB aa because B has not been AA→→bb established to be generating Finding reachable symbols S is surely reachable. All symbols in the body of a production with S in the head are reachable. SS→→ AB AB || In this example the aa symbols {S, A, B, a, b} AA→→bb are reachable. There are three preliminary simplifications Eliminate Eliminate Useless Useless 1 Symbols Symbols Eliminate Eliminate εε 2 productions productions Eliminate Eliminate unit unit 3 productions productions Eliminate ε Productions In a grammar ε productions are convenient but not essential If L has a CFG, then L – {ε} has a CFG * εε A  A Nullable variable If A is a nullable variable Whenever A appears on the body of a production A might or might not derive ε S → ASA | aB Nullable: A→B|S {A, B} B→b|ε Eliminate ε Productions Create two version of the production, one with the nullable variable and one without it Eliminate productions with ε bodies S → ASA | S → ASA | aB | AS | SA | S aB |a A→B|S A→B|S B→b|ε B→b Eliminate ε Productions Create two version of the production, one with the nullable variable and one without it Eliminate productions with ε bodies S → ASA | S → ASA | aB | AS | SA | S aB |a A→B|S A→B|S B→b|ε B→b Eliminate ε Productions Create two version of the production, one with the nullable variable and one without it Eliminate productions with ε bodies S → ASA | S → ASA | aB | AS | SA | S aB |a A→B|S A→B|S B→b|ε B→b There are three preliminary simplifications Eliminate Eliminate Useless Useless 1 Symbols Symbols Eliminate Eliminate εε 2 productions productions Eliminate Eliminate unit unit 3 productions productions 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 A → B, B → ω, then A →ω Example: T = {*, +, (, ), a, b, Pairs Productions 0, 1} ( E, E ) E→E+T I → a | b | Ia | Ib | I0 | ( E, T ) E→T*F I1 ( E, F ) E → (E) F → I | (E) ( E, I ) E → a | b | Ia | Ib | I0 | I1 T→F|T*F ( T, T ) T→T*F E→T|E+T ( T, F ) T → (E) Basis: (A, A) is a ( T, I ) T → a | b | Ia |Ib | I0 | I1 unit pair of any ( F, F ) F → (E) *  variable A, if ( F, I ) F → a | b | Ia | Ib | I0 | I1 A A by 0 ( I, I ) I → a | b | Ia | Ib | I0 | I1 steps. Example: Pairs Productions … … ( T, T ) T→T*F ( T, F ) T → (E) ( T, I ) T → a | b | Ia |Ib | I0 | I1 … … I → a | b | Ia | Ib | I0 | I1 E → E + T | T * F | (E ) | a | b | la | lb | l0 | l1 T → T * F | (E) | a | b | Ia | Ib | I0 | I1 F → (E) | a | b | Ia | Ib | I0 | I1 Introduction Chomsky normal form Preliminary simplifications Final steps Greibach Normal Form Algorithm (Example) Summary 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. 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 | l0 | l1 T → T * F | (E) | a | b | Ia | Ib | I0 | I1 F → (E) | a | b | Ia | Ib | I0 | I1 I → a | b | Ia | Ib | I0 | I1 E → EPT | TMF | LER | a | b | lA | lB | lZ | lO 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→) Step 2: Break bodies of length 3 or more adding more variables E → EPT | TMF | LER | a | b | lA | lB | lZ | lO C → PT T → TMF | LER | a | b | IA | IB | IZ | IO 1 F → LER | a | b | IA | IB | IZ | IO C2 → MF I → a | b | IA | IB | IZ | IO C3 → ER A → aB → bZ → 0O → 1 P → +M → * L → ( R → ) Introduction Chomsky normal form Preliminary simplifications Final steps Greibach Normal Form Algorithm (Example) Summary 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. Introduction Chomsky normal form Preliminary simplifications Final steps Greibach Normal Form Algorithm (Example) Summary Example: S → XA | S = A1 A1 → A2A3 | BB X = A2 A4A4 B → b | SB A = A3 A4 → b | A1A4 X→b B = A4 A2 → b A→a New New A3 → a Updated Updated CNF CNF Labels Labels CNF CNF Example: A1 → A2A3 | A Aii → → A AjjXXkk jj > > First Step A4A4 ii A4 → b | A1A4 Xk is a string of A2 → b zero A3 → a or more variables A4 → A1A4 Example: A Aii → → A AjjXXkk jj > > First Step ii A4 → A1A4 A1 → A2A3 | A4 → | A4A4A4 | b A4A4 A24A→3AbA 4 3A4 | A4A4A4 | b A4 → b | A1A4 A2 → b A3 → a Example: A1 → A2A3 | A4A4 Second A4 → bA3A4 | A4A4A4 | Step Eliminate Eliminate Left Left b Recursions Recursions A2 →b A3 →a A4 → A4A4A4 Example: Second Step Eliminate Eliminate Left Left Recursions Recursions A4 → bA3A4 | b| bA3A4Z | bZ A1 → A2A3 | A4A4 Z → A4A4| A4A4Z A4 → bA3A4 | A4A4A4 | b A2 →b A3 →a Example: A1 → A2A3 | A4A4 A4 → bA3A4 | b | bA3A4Z | bZ A A→→ αX αX Z → A4A4 | A4A4 Z A2 →b GN A3 →a F Example: A1 → A2A3 | A4A4 A4 → bA3A4 | b | bA3A4Z | bZ Z → A4A4 | A4A4 Z A2 → b A3 → a A1 → bA3| bA3A4A4 | bA4 | bA3A4ZA|4 bZA4 Z → bA3A4A4 | bA4 | bA3A4ZA4 | bZA4 | bA3A4A4 | bA4 | bA3A4ZA4 | bZA4 Example: A1 → bA3 | bA3A4A4 | bA4 | bA3A4ZA4 | bZA4 A4 → bA3A4 | b | bA3A4Z | bZ Z → bA3A4A4 | bA4 | bA3A4ZA4 | bZA4 | bA3A4A4 | bA4 | bA3A4ZA4 | bZA4 A2 → b A3 → a Grammar in Greibach Normal Form 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 1. Convert the following grammar to the Chomsky Normal Form. S→P P → aPb | ε 2. Is the following grammar context-free? S → aBSc | abc Ba → aB Bb → bb 3. Convert the following grammar to the Greibach Normal Form. S -> a | CD | CS A -> a | b | SS C -> a D -> AS

Use Quizgecko on...
Browser
Browser