Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Eliminating Useless Variables & Reaching Chomsky Normal Form in Context-Free Grammars, Slides of Theory of Automata

An in-depth exploration of various techniques for normalizing context-free grammars (cfgs), including eliminating useless variables, testing whether a variable derives some terminal string, and reaching the chomsky normal form. Concepts such as normal forms, nullable variables, and epsilon productions, and includes examples and proofs to illustrate the concepts.

Typology: Slides

2012/2013

Uploaded on 04/29/2013

shamir_69
shamir_69 🇮🇳

5

(4)

66 documents

1 / 36

Toggle sidebar

Partial preview of the text

Download Eliminating Useless Variables & Reaching Chomsky Normal Form in Context-Free Grammars and more Slides Theory of Automata in PDF only on Docsity! 1 Normal Forms for CFG’s Eliminating Useless Variables Removing Epsilon Removing Unit Productions Chomsky Normal Form Docsity.com 2 Variables That Derive Nothing Consider: S -> AB, A -> aA | a, B -> AB Although A derives all strings of a’s, B derives no terminal strings (can you prove this fact?). Thus, S derives nothing, and the language is empty. Docsity.com 5 Proof of Converse The proof is an induction on the height of the least-height parse tree by which a variable A derives a terminal string. Basis: Height = 1. Tree looks like: Then the basis of the algorithm tells us that A will be discovered. A a1 an. . . Docsity.com 6 Induction for Converse Assume IH for parse trees of height < h, and suppose A derives a terminal string via a parse tree of height h: By IH, those Xi’s that are variables are discovered. Thus, A will also be discovered, because it has a right side of terminals and/or discovered variables. A X1 Xn. . . w1 wn Docsity.com 7 Algorithm to Eliminate Variables That Derive Nothing 1. Discover all variables that derive terminal strings. 2. For all other variables, remove all productions in which they appear either on the left or the right. Docsity.com 10 Unreachable Symbols – (2) Easy inductions in both directions show that when we can discover no more symbols, then we have all and only the symbols that appear in derivations from S. Algorithm: Remove from the grammar all symbols not discovered reachable from S and all productions that involve these symbols. Docsity.com 11 Eliminating Useless Symbols  A symbol is useful if it appears in some derivation of some terminal string from the start symbol.  Otherwise, it is useless. Eliminate all useless symbols by: 1. Eliminate symbols that derive no terminal string. 2. Eliminate unreachable symbols. Docsity.com 12 Example: Useless Symbols – (2) S -> AB, A -> C, C -> c, B -> bB If we eliminated unreachable symbols first, we would find everything is reachable. A, C, and c would never get eliminated. Docsity.com 15 Nullable Symbols To eliminate ε-productions, we first need to discover the nullable variables = variables A such that A =>* ε. Basis: If there is a production A -> ε, then A is nullable. Induction: If there is a production A -> , and all symbols of  are nullable, then A is nullable. Docsity.com 16 Example: Nullable Symbols S -> AB, A -> aA | ε, B -> bB | A Basis: A is nullable because of A -> ε. Induction: B is nullable because of B -> A. Then, S is nullable because of S -> AB. Docsity.com 17 Proof of Nullable-Symbols Algorithm The proof that this algorithm finds all and only the nullable variables is very much like the proof that the algorithm for symbols that derive terminal strings works. Do you see the two directions of the proof? On what is each induction? Docsity.com 20 Why it Works  Prove that for all variables A: 1. If w  ε and A =>*old w, then A =>*new w. 2. If A =>*new w then w  ε and A =>*old w.  Then, letting A be the start symbol proves that L(new) = L(old) – {ε}.  (1) is an induction on the number of steps by which A derives w in the old grammar. Docsity.com 21 Proof of 1 – Basis If the old derivation is one step, then A -> w must be a production. Since w  ε, this production also appears in the new grammar. Thus, A =>new w. Docsity.com 22 Proof of 1 – Induction Let A =>*old w be an n-step derivation, and assume the IH for derivations of less than n steps. Let the first step be A =>old X1…Xn. Then w can be broken into w = w1…wn, where Xi =>*old wi, for all i, in fewer than n steps. Docsity.com 25 Unit Productions A unit production is one whose right side consists of exactly one variable. These productions can be eliminated. Key idea: If A =>* B by a series of unit productions, and B ->  is a non-unit- production, then add production A -> . Then, drop all unit productions. Docsity.com 26 Unit Productions – (2) Find all pairs (A, B) such that A =>* B by a sequence of unit productions only. Basis: Surely (A, A). Induction: If we have found (A, B), and B -> C is a unit production, then add (A, C). Docsity.com 27 Proof That We Find Exactly the Right Pairs By induction on the order in which pairs (A, B) are found, we can show A =>* B by unit productions. Conversely, by induction on the number of steps in the derivation by unit productions of A =>* B, we can show that the pair (A, B) is discovered. Docsity.com 30 Cleaning Up – (2)  Proof: Start with a CFG for L.  Perform the following steps in order: 1. Eliminate ε-productions. 2. Eliminate unit productions. 3. Eliminate variables that derive no terminal string. 4. Eliminate variables not reached from the start symbol. Must be first. Can create unit productions or useless variables. Docsity.com 31 Chomsky Normal Form  A CFG is said to be in Chomsky Normal Form if every production is of one of these two forms: 1. A -> BC (right side is two variables). 2. A -> a (right side is a single terminal).  Theorem: If L is a CFL, then L – {ε} has a CFG in CNF. Docsity.com 32 Proof of CNF Theorem Step 1: “Clean” the grammar, so every production right side is either a single terminal or of length at least 2. Step 2: For each right side  a single terminal, make the right side all variables.  For each terminal a create new variable Aa and production Aa -> a.  Replace a by Aa in right sides of length > 2. Docsity.com 35 Example of Step 3 – Continued Recall A -> BCDE is replaced by A -> BF, F -> CG, and G -> DE. In the new grammar, A => BF => BCG => BCDE. More importantly: Once we choose to replace A by BF, we must continue to BCG and BCDE.  Because F and G have only one production. Docsity.com 36 CNF Proof – Concluded We must prove that Steps 2 and 3 produce new grammars whose languages are the same as the previous grammar. Proofs are of a familiar type and involve inductions on the lengths of derivations. Docsity.com
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved