Download Converting Context-Free Grammars into Chomsky and Greibach Normal Forms and more Study Guides, Projects, Research Finance in PDF only on Docsity! Chomsky and Greibach Normal Forms Chomsky and Greibach Normal Forms โ p.1/24 Simplifying a CFG It is often convenient to simplify CFG One of the simplest and most useful simplified forms of CFG is called the Chomsky normal form Another normal form usually used in algebraic specifications is Greibach normal form Note the difference between grammar cleaning and simplification Chomsky and Greibach Normal Forms โ p.2/24 Simplifying a CFG It is often convenient to simplify CFG One of the simplest and most useful simplified forms of CFG is called the Chomsky normal form Another normal form usually used in algebraic specifications is Greibach normal form Note the difference between grammar cleaning and simplification Chomsky and Greibach Normal Forms โ p.2/24 Note Normal forms are useful when more advanced topics in computation theory are approached, as we shall see further Chomsky and Greibach Normal Forms โ p.3/24 Definition A context-free grammar is in Chomsky normal form if every rule is of the form: where is a terminal, are nonterminals, and may not be the start variable (the axiom) Chomsky and Greibach Normal Forms โ p.4/24 Theorem 2.9 Any context-free language is generated by a context-free grammar in Chomsky normal form. Proof idea: Show that any CFG can be converted into a CFG in Chomsky normal form Conversion procedure has several stages where the rules that violate Chomsky normal form conditions are replaced with equivalent rules that satisfy these conditions Order of transformations: (1) add a new start variable, (2) eliminate all -rules, (3) eliminate unit-rules, (4) convert other rules Check that the obtained CFG defines the same language Chomsky and Greibach Normal Forms โ p.6/24 Theorem 2.9 Any context-free language is generated by a context-free grammar in Chomsky normal form. Proof idea: Show that any CFG can be converted into a CFG in Chomsky normal form Conversion procedure has several stages where the rules that violate Chomsky normal form conditions are replaced with equivalent rules that satisfy these conditions Order of transformations: (1) add a new start variable, (2) eliminate all -rules, (3) eliminate unit-rules, (4) convert other rules Check that the obtained CFG defines the same language Chomsky and Greibach Normal Forms โ p.6/24 Theorem 2.9 Any context-free language is generated by a context-free grammar in Chomsky normal form. Proof idea: Show that any CFG can be converted into a CFG in Chomsky normal form Conversion procedure has several stages where the rules that violate Chomsky normal form conditions are replaced with equivalent rules that satisfy these conditions Order of transformations: (1) add a new start variable, (2) eliminate all -rules, (3) eliminate unit-rules, (4) convert other rules Check that the obtained CFG defines the same language Chomsky and Greibach Normal Forms โ p.6/24 Proof Let be the original CFG. Step 1: add a new start symbol to , and the rule to Note: this change guarantees that the start symbol of does not occur on the of any rule Chomsky and Greibach Normal Forms โ p.7/24 Proof Let be the original CFG. Step 1: add a new start symbol to , and the rule to Note: this change guarantees that the start symbol of does not occur on the of any rule Chomsky and Greibach Normal Forms โ p.7/24 Proof Let be the original CFG. Step 1: add a new start symbol to , and the rule to Note: this change guarantees that the start symbol of does not occur on the of any rule Chomsky and Greibach Normal Forms โ p.7/24 Convert all remaining rules Repeat 1. Replace a rule , , where each , , is a variable or a terminal, by: , , , where , , , are new variables 2. If replace any terminal with a new variable and add the rule until no rules of the form with remain Chomsky and Greibach Normal Forms โ p.10/24 Convert all remaining rules Repeat 1. Replace a rule , , where each , , is a variable or a terminal, by: , , , where , , , are new variables 2. If replace any terminal with a new variable and add the rule until no rules of the form with remain Chomsky and Greibach Normal Forms โ p.10/24 Convert all remaining rules Repeat 1. Replace a rule , , where each , , is a variable or a terminal, by: , , , where , , , are new variables 2. If replace any terminal with a new variable and add the rule until no rules of the form with remain Chomsky and Greibach Normal Forms โ p.10/24 Example CFG conversion Consider the grammar whose rules are: Notation: symbols removed are green and those added are red. After first step of transformation we get: Chomsky and Greibach Normal Forms โ p.11/24 Removing rules Removing : Removing : Chomsky and Greibach Normal Forms โ p.12/24 Removing rules Removing : Removing : Chomsky and Greibach Normal Forms โ p.12/24 Removing unit rule Removing : Removing : Chomsky and Greibach Normal Forms โ p.13/24 Removing unit rule Removing : Removing : Chomsky and Greibach Normal Forms โ p.13/24 More unit rules Removing : Removing : Chomsky and Greibach Normal Forms โ p.14/24 Converting remaining rules Chomsky and Greibach Normal Forms โ p.15/24 Converting remaining rules Chomsky and Greibach Normal Forms โ p.15/24 Converting remaining rules Chomsky and Greibach Normal Forms โ p.15/24 Note The conversion procedure produces several variables along with several rules . Since all these represent the same rule, we may simplify the result using a single variable and a single rule Chomsky and Greibach Normal Forms โ p.16/24 Greibach Normal Form A context-free grammar is in Greibach normal form if each rule has the property: , , and . Note: Greibach normal form provides a justifica- tion of operator prefix-notation usually employed in algebra. Chomsky and Greibach Normal Forms โ p.17/24 Greibach Normal Form A context-free grammar is in Greibach normal form if each rule has the property: , , and . Note: Greibach normal form provides a justifica- tion of operator prefix-notation usually employed in algebra. Chomsky and Greibach Normal Forms โ p.17/24 Greibach Theorem Every CFL where can be generated by a CFG in Greibach normal form. Proof idea: Let be a CFG generating . Assume that is in Chomsky normal form Let be an ordering of nonterminals. Construct the Greibach normal form from Chomsky normal form Chomsky and Greibach Normal Forms โ p.18/24 Greibach Theorem Every CFL where can be generated by a CFG in Greibach normal form. Proof idea: Let be a CFG generating . Assume that is in Chomsky normal form Let be an ordering of nonterminals. Construct the Greibach normal form from Chomsky normal form Chomsky and Greibach Normal Forms โ p.18/24 Construction 1. Modify the rules in so that if then 2. Starting with and proceeding to this is done as follows: (a) Assume that productions have been modified so that for , only if (b) If is a production with , generate a new set of productions substituting for the the rhs of each production (c) Repeating (b) at most times we obtain rules of the form , (d) Replace rules by removing left-recursive rules Chomsky and Greibach Normal Forms โ p.19/24 Removing left-recursion Left-recursion can be eliminated by the following scheme: If are all left recursive rules, and are all remaining -rules then chose a new nonterminal, say Add the new -rules , Replace the -rules by , This construction preserve the language . Chomsky and Greibach Normal Forms โ p.20/24 Removing left-recursion Left-recursion can be eliminated by the following scheme: If are all left recursive rules, and are all remaining -rules then chose a new nonterminal, say Add the new -rules , Replace the -rules by , This construction preserve the language . Chomsky and Greibach Normal Forms โ p.20/24 Removing left-recursion Left-recursion can be eliminated by the following scheme: If are all left recursive rules, and are all remaining -rules then chose a new nonterminal, say Add the new -rules , Replace the -rules by , This construction preserve the language . Chomsky and Greibach Normal Forms โ p.20/24 Example Convert the CFG where into Greibach normal form. Chomsky and Greibach Normal Forms โ p.23/24 Example Convert the CFG where into Greibach normal form. Chomsky and Greibach Normal Forms โ p.23/24 Example Convert the CFG where into Greibach normal form. Chomsky and Greibach Normal Forms โ p.23/24