Transforming Context-Free Grammars to Chomsky Normal Form Roger L. Costello April 12, 2014 1 Objective This mini-tutorial will answer these questions: 1. What is Chomsky Normal Form? 2 Objective This mini-tutorial will answer these questions: 1. What is Chomsky Normal Form? 2. Why is Chomsky Normal Form useful/relevant?

3 Objective This mini-tutorial will answer these questions: 1. What is Chomsky Normal Form? 2. Why is Chomsky Normal Form useful/relevant? 3. How can arbitrary context-free grammars be converted to Chomsky Normal Form? 4 Objective This mini-tutorial will answer these questions: 1. What is Chomsky Normal Form? 2. Why is Chomsky Normal Form useful/relevant? 3. How can arbitrary context-free grammars be converted to Chomsky Normal Form? 4. Can we determine a priori how many steps it will take for a grammar to generate a string?

5 Objective This mini-tutorial will answer these questions: 1. What is Chomsky Normal Form? 2. Why is Chomsky Normal Form useful/relevant? 3. How can arbitrary context-free grammars be converted to Chomsky Normal Form? 4. Can we determine a priori how many steps it will take for a grammar to generate a string? 5. Is there a procedure for determining if a string is in the set of strings generated by a grammar? 6 But first, binary trees Before defining Chomsky Normal Form, lets talk a bit about binary trees. Each node in a binary tree has zero, one, or

two children. 7 Sample binary tree S A a B C D c d 8

Node with 2 children This node has two children S A a B C D c d 9 Node with 1 child S

A a B C D c d This node has one child 10 Node with 0 children S A

a B C c D d This node has no children 11 Well studied Binary trees have been well-studied. Lots is known about them. 12

Specialized binary trees There are specialized binary trees. One such specialized binary tree requires each node have either zero or two children (no nodes with one child). 13 Sample specialized binary tree S A B C D

Each node has either zero children or two children. 14 Full binary tree Definition: A full binary tree is a binary tree in which each node has exactly zero or two children. 15 Number of nodes a full binary tree Research has determined that the number of nodes, , in a full binary tree is twice the number of leaf nodes, , minus one: http://courses.cs.vt.edu/~cs3114/Fall09/wmcquain/Notes/T03a.BinaryTreeTheorems.pdf 16 Calculate number of nodes

in this full binary tree S A B C D There are 3 leaf nodes (A, C, D) so the number of nodes in the tree is: 17 Context-free grammar Here is a context-free grammar: S AaBb A aB Bb

Dont know what a context-free grammar is? Check out my tutorial: http://xfront.com/Context-free-grammars-are-a-subset-of-context-sensitive-grammars.pptx 18 Production tree A grammar generates strings. The below tree shows how the grammar generates this string: . The tree is called a production tree. S S AaBb A aB Bb A a a B

B b b grammar b 19 Number of child nodes S A a

a B This node has 4 child nodes B b b b 20 Number of child nodes S This node has 2 child nodes

a A a B B b b b 21 Number of child nodes

This node has 1 child node S A a a B B b b b 22

Number of child nodes S A a a B B b b This node has 0 child nodes b

23 Nodes have 0, 1, 2, or 4 child nodes S A a a B B b b b

24 Terminology: arity Arity is the maximum number of child nodes that a node in the tree may have. The arity of the tree on the previous slide is 4. Conversely, the arity of a binary tree is 2. 25 Not well-studied Whereas binary trees are well-studied, trees of arbitrary arity are not so well studied. For trees that have arbitrary arity it is hard to find nice, neat results. 26 Another context-free grammar

Here is a context-free grammar: S AB Aa Bb 27 Here is its production tree S S AB Aa Bb A B a

b The production tree is a binary tree. 28 Arbitrary context-free grammars versus restricted context-free grammars Arbitrary context-free grammars yield production trees that are not binary. Grammars with rules which are restricted to no more than 2 symbols on the right-hand side have production trees that are binary trees. 29 Benefit of restricted grammar rules There are benefits to grammars that are

restricted to no more than 2 symbols on the right-hand side of each rule: Their production trees are binary trees, which are well-studied and lots of useful research results can be applied to such trees. 30 Lets recap what weve learned Binary trees consist of nodes that have 0, 1, or 2 child nodes. 31 Lets recap what weve learned Binary trees consist of nodes that have 0, 1, or 2 child nodes. Binary trees are well-studied.

32 Lets recap what weve learned Binary trees consist of nodes that have 0, 1, or 2 child nodes. Binary trees are well-studied. Context-free grammars with rules that have at most 2 symbols on the right-hand side yield production trees that are binary trees. 33 Lets recap what weve learned Binary trees consist of nodes that have 0, 1, or 2 child nodes. Binary trees are well-studied. Context-free grammars with rules that have at most 2 symbols on the right-hand side yield production trees that are binary trees.

Arbitrary context-free grammars have production trees that are not binary trees. 34 Lets recap what weve learned Binary trees consist of nodes that have 0, 1, or 2 child nodes. Binary trees are well-studied. Context-free grammars with rules that have at most 2 symbols on the right-hand side yield production trees that are binary trees. Arbitrary context-free grammars have production trees that are not binary trees. Non-binary trees are not so well-studied. 35 Chomsky Normal Form A context-free grammar is in Chomsky Normal Form if each rule has one of these forms:

1. X a 2. X YZ That is, the right-hand side is either a single terminal or two non-terminals. Convention: uppercase letters denote non-terminal symbols and lowercase letters denote terminal symbols. 36 Objective This mini-tutorial will answer these questions: 1. What is Chomsky Normal Form? A context-free grammar is in Chomsky Normal Form if each rule has one of these forms: 1. 2. Xa

X YZ 2. Why is Chomsky Normal Form useful/relevant? The production trees for grammars in Chomsky Normal Form are binary trees. Binary trees are well-studied. The results from research on binary trees can be applied to grammars in Chomsky Normal Form. 37 -rules, -free A grammar rule that has an empty right-hand side, e.g., A is called an -rulerule. Read that rule as: A may be replaced by the empty string (which we denote by ). A grammar that contains no such rules is called -rulefree. 38

Transform any context-free grammar to Chomsky Normal Form To every -free context-free grammar one can find an equivalent grammar in Chomsky Normal Form. Context-free grammar transform Context-free grammar in Chomsky Normal Form 39 Example of a grammar that is transformed to Chomsky Normal Form S AaBb A aB

Bb transform S AX1 A A1 B Bb A1 a B1 b X1 A1X2 X2 Normal BB1Form Chomsky 40 3-step process The following slides shows a 3-step process for transforming any context-free grammar into an equivalent grammar in Chomsky Normal Form.

41 Step 1: replace terminals mixed in with non-terminals For every rule with a right-hand side that contains a mix of terminals and non-terminals, replace each terminal by and add a new rule Q aP Step 1 Q A1P A1 a 42 Example S AB

A aCa Aa B bB Bb CD Dd Step 1 S AB A A1CA1 Aa B B1B Bb CD Dd A1 a B1 b

Replace the right-hand side, aCa, by A1CA1 and then add a new rule A1 a Replace the right-hand side, bB, by B1B and then add a new rule B1 b 43 Step 2: convert sequence of non-terminals to pairs of non-terminals For every rule with a right-hand side that contains 3 or more non-terminals, replace all non-terminals but the first by Xi and then add a new rule where Xi has as its righthand side those non-terminals that were replaced by Xi Repeatedly apply Step 2 until there are no rules with more than two non-terminals on the right-hand side. Q ABCDE Step 2 Q AX1 X1 BX2 X2 CX3

X3 DE 44 Repeatedly apply step 2 Q ABCDE Step 2 Q AX1 X1 BCDE Step 2 Q AX1 X1 BX2 X2 CDE Step 2

Q AX1 X1 BX2 X2 CX3 X3 DE 45 Applying step 2 to a grammar S AB A A1CA1 Aa B B 1B Bb CD Dd A1 a B1 b Step 2

S AB A A1X1 Aa B B 1B Bb CD Dd A1 a B1 b X1 CA1 Replace the right-hand side, A1CA1, by A1X1 and then add a new rule X1 CA1 46 3 kinds of rules remain After performing steps 1 and 2, the resulting grammar has three kinds of rules: 1) X a

2) X Y 3) X YZ 47 Rules of the form: X a 1) X a 2) X Y 3) X YZ S AB A A1X1 Aa B B 1B Bb CD Dd A1 a B1 a

X1 CA1 48 Rules of the form: X Y 1) X a 2) X Y 3) X YZ S AB A A1X1 Aa B B 1B Bb CD Dd A1 a B1 a X1 CA1

49 Rules of the form: X YZ 1) X a 2) X Y 3) X YZ S AB A A1X1 Aa B B1B Bb CD Dd A1 a B1 a X1 CA1

50 Chain rules Rules with the form X Y are called chain rules. 51 Chain rules arent in Chomsky Normal Form Recall the definition of Chomsky Normal Form: A context-free grammar is in Chomsky Normal Form if each rule has one of these forms: 1. X a 2. X YZ Chain rules are of this form: X Y Clearly that is not Chomsky Normal Form. So we must transform chain rules into the

desired form. 52 Step 3: remove chain rules Consider this chain rule: XY From the previous few slides we know that the rule for Y must have one of these forms: 1. Y a 2. Y Z 3. Y YZ If there is a rule Y a then replace X Y by Xa If there is a rule Y YZ then replace X Y by X YZ If there is a rule Y Z then replace X Y by the result of replacing Z (recursive definition cool!) 53

Example S AB A AiXi Aa B BiB Bb CD Dd Ai a Bi a Xi CAi Step 3 S AB A AiXi Aa B BiB

Bb Cd Dd Ai a Bi a Xi CAi Chomsky Normal Form There is one chain rule: C D D is defined by this rule: D d So, replace the chain rule with: C d 54 Another example SA AB Bb Step 3

Sb Ab Bb This is a chain rule: S A A is defined by this chain rule: A B B is defined by this rule: B b So, replace the first chain rule with: S b And, replace the second chain rule with: A b 55 Multiple rules may be generated Consider this rule: XY The rule for Y may be an alternative: Y a | Z | AB So the rule for X must be replaced by:

Xa X AB plus the rule(s) generated by replacing Z 56 Recap Using the 3-step process we can transform any -free context-free grammar into an equivalent grammar in Chomsky Normal Form. Context-free grammar 3-rulestep transform Context-free grammar in Chomsky Normal Form 57

Grammars in Chomsky Normal Form produce binary trees Each production tree that is created from a grammar in Chomsky Normal Form is a binary tree. As weve discussed, lots is known about binary trees. 58 Objective This mini-tutorial will answer these questions: 1. What is Chomsky Normal Form? A context-free grammar is in Chomsky Normal Form if each rule has one of these forms: 1. 2. Xa

X YZ 2. Why is Chomsky Normal Form useful/relevant? The production trees for grammars in Chomsky Normal Form are binary trees. Binary trees are well-studied. The results from research on binary trees can be applied to grammars in Chomsky Normal Form. 3. How can arbitrary context-free grammars be converted to Chomsky Normal Form? Use the 3-step process described in the previous slides. 59 Grammars generate languages string-1 grammar generates string-2

string-n The set of strings is called a language The language generated by a grammar is denoted by: 60 This grammar generates a b n n S AX S AB Aa Bb X SB grammar

(in Chomsky Normal Form) ab generates aabb aabb Each string consists of as followed by an equal number of bs 61 Production tree for S S AX S AB Aa

Bb X SB A X generates a S B grammar (in Chomsky Normal Form) A

B a b b Notice that the production tree is a binary tree. 62 Chomsky Normal Form enables powerful results Interesting questions about grammars can be answered when the grammars are in Chomsky Normal Form. 63

Interesting Question: Is a string a member of the language? grammar G (in Chomsky Normal Form) string P Is P a member of the language generated by G? no yes 64 Is aabb a member of anbn?

S AX S AB Aa Bb X SB aabb Is aabb a member of the language generated by G? yes no 65 Is abb a member of anbn?

S AX S AB Aa Bb X SB abb Is abb a member of the language generated by G? no yes 66 Another interesting question: Number of production steps needed?

grammar G (in Chomsky Normal Form) string P How many steps are needed to generate P? ?? steps 67 We will answer both questions But we will answer the latter question first: How many steps are needed to produce string P? 68

Number of production steps needed to generate ? S S AX S AB Aa Bb X SB 1 A X 2 generates 3

a S B 7 4 A B 5 a b

6 b 69 Number of production steps needed to generate ? S AX aX aSB aABB aaBB aabB aabb 1 2 3 4 5

6 7 7 steps needed to generate aabb 70 Calculate the number of steps based on string length The following slides show how to calculate the number of production steps needed to generate a string. The calculation will be based on the length of the string. 71

Notation for length of a string Let represent some arbitrary string. We will denote the length of by: Example: suppose is the string: Then 72 Generate 1 symbol takes 1 step S Sa generates 1 a

73 Generate 2 symbols takes 3 steps S S AB Aa Bb 1 generates A B 2 3

a b 74 One grammar S AB Aa Bb This grammar generates only two symbols. SX X AB Aa Bb How about this grammar? It also generates only two

symbols. True, but it is not in Chomsky Normal Form. Namely, the first rule is not in Chomsky Normal Form. 75 Generate 3 symbols takes 5 steps S S AX S AB Aa Bb X AB 1 generates A X

2 a 3 A 4 a B 5 b 76 Generate 4 symbols takes 7 steps

S S AX S AB Aa Bb X SB 1 A generates X 2 3 a

S B 7 4 A 5 B b 6 a

b 77 Every non-terminal has one of these forms A a Aa A B C A BC 78 Remove the terminal symbols S

S A X A a S B A B a

b X remove terminals S b A B B 79 The result is a full binary tree

S A X S A B B 80 Recall this: Number of nodes in a full binary tree In a full binary tree that has leaves, the total number of nodes in the tree

81 Number of nodes S A X S A B B so the total number of nodes in the tree 82 leaf nodes equals If a tree has leaf nodes, the total number of nodes in the tree

The sequence of leaf nodes is the string being generated; that is, | and are one and the same. 83 leaf nodes equals If the tree has leaves, the total number of nodes in the tree The sequence of leaf nodes is the string being generated; that is, and are one and the same. So the total number of nodes in the tree 84 leaf nodes equals If the tree has leaves, the total number of nodes in the tree The sequence of leaf nodes is the string being generated; that is, and are one

and the same. So the total number of nodes in the tree So the total number of s 85 Need steps to generate S A X a S B A

B a b b Number of steps needed to generate 86 Objective This mini-tutorial will answer these questions: 1. What is Chomsky Normal Form? A context-free grammar is in Chomsky Normal Form if each rule has one of these forms:

1. 2. Xa X YZ 2. Why is Chomsky Normal Form useful/relevant? The production trees for grammars in Chomsky Normal Form are binary trees. Binary trees are well-studied. The results from research on binary trees can be applied to grammars in Chomsky Normal Form. 3. How can arbitrary context-free grammars be converted to Chomsky Normal Form? Use the 3-step process described in the previous slides. 4. Can we determine a priori how many steps it will take for a grammar to generate a string? Generating a string P will require this number of steps: 87

Another interesting question: Is an element of ? Consider a grammar . Suppose we have a method for finding: all the strings that can generate in step all the strings that can generate in steps all the strings that can generate in steps and so forth. Here is a procedure for determining if is an element of : find the set of strings that can generate in steps. If is not in that set, then we know that is not an element of . 88 CF grammar, G Create a set of all the

strings that can be generated from G in 2|P| - 1 steps string P A procedure exists for deciding if a string P is an element of Gs language! set w P L(G) L(G) No Is P an element of w?

P L(G) L(G) 89 Method for finding all the strings that can generate in steps Queue AB S S substitute S S AB Aa Bb We can systematically generate all strings using a queue. 90

Objective This mini-tutorial will answer these questions: 1. What is Chomsky Normal Form? A context-free grammar is in Chomsky Normal Form if each rule has one of these forms: 1. 2. Xa X YZ 2. Why is Chomsky Normal Form useful/relevant? The production trees for grammars in Chomsky Normal Form are binary trees. Binary trees are wellstudied. The results from research on binary trees can be applied to grammars in Chomsky Normal Form. 3. How can arbitrary context-free grammars be converted to Chomsky Normal Form? Use the 3-step process described in the previous slides.

4. Can we determine a priori how many steps it will take for a grammar to generate a string? Generating a string P will require this number of steps: 5. Is there a procedure for determining if a string is in the set of strings generated by a grammar? Determine the number of steps that would be needed to generate the string. Generate the set of strings which require that number of steps. See if the string is an element of the set. 91 Case Study We are tasked to generate data for Books in a BookStore. The Genre of a Book is either fiction or non-fiction. The Publisher of a Book is either Springer, MIT Press, or Harvard Press. The Title of a Book is either The Wisdom of Crowds, Six Great Ideas, or Society of Mind.

Create a grammar that generates strings containing the title of a book, its genre, and its publisher. 92 BookStore Grammar Bookstore Bookstore Book Title Title Title Genre Genre Publisher Publisher Publisher

Book Bookstore Book Title Genre Publisher Wisdom of Crowds Six Great Ideas Society of Mind fiction non-fiction Springer

MIT Press Harvard Press 93 Not in Chomsky Normal Form chain rule too many nonterminals on right-hand side Bookstore Bookstore Book Title Title Title Genre Genre Publisher

Publisher Publisher Book Bookstore Book Title Genre Publisher Wisdom of Crowds

Six Great Ideas Society of Mind fiction non-fiction Springer MIT Press Harvard Press 94 Transform to Chomsky Normal Form Bookstore Bookstore Book Title Title Title Genre Genre

Publisher Publisher Publisher Book Bookstore Book Title Genre Publisher

Wisdom of Crowds Six Great Ideas Society of Mind fiction non-fiction Springer MIT Press Harvard Press transform Bookstore Bookstore Book Other Title Title Title Genre

Genre Publisher Publisher Publisher Book Bookstore

Title Other Title Other Genre Publisher Wisdom of Crowds Six Great Ideas Society of Mind fiction non-fiction Springer MIT Press Harvard Press Chomsky Normal Form 95 How many production steps needed to generate this data?

Wisdom of Crowds non-fiction Springer Society of Mind non-fiction Harvard Press 96 Determine the length of the data Wisdom of Crowds non-fiction Springer Society of Mind non-fiction Harvard Press 1 2 3 4 5 6 97

Calculate the answer Wisdom of Crowds non-fiction Springer Society of Mind non-fiction Harvard Press 1 2 3 4 5 6 Number of production steps needed = = =

98 Check the results Bookstore Bookstore Book Other Title Title Title Genre Genre Publisher Publisher Publisher

Book Bookstore Title Other Title Other Genre Publisher Wisdom of Crowds Six Great Ideas Society of Mind fiction

non-fiction Springer MIT Press Harvard Press Bookstore 1 Book Bookstore 2 Title 3 Wisdom of Crowds

7 Other Title 9 4 Genre 5 nonfiction Other Publisher 6 Springer

Society of Mind 8 Genre 10 nonfiction Publisher 11 Harvard Press 99 XML, XML Schema

If the Bookstore grammar is converted into an XML Schema, how many XML elements will be needed to markup this data: Wisdom of Crowds non-fiction Springer Society of Mind non-fiction Harvard Press 100 Number of XML elements = 1 2 3 Wisdom of Crowds 4 5 non-fiction 6 Springer 7

8

Society of Mind 9

10 non-fiction 11 Harvard Press 101 Bookstore XML Schema

102 Bookstore XML Schema

103