# Theory of Computation: Question Set – 18

#### What is the Chomsky normal form for context-free grammars?

The Chomsky normal form is a standard form for context-free grammars that requires all production rules to be of the form A -> BC or A -> a, where A, B, and C are nonterminal symbols and a is a terminal symbol. This form is useful for simplifying some algorithmic processes, such as parsing.

#### Why is Chomsky Normal Form useful?

CNF is useful because it simplifies many operations on context-free grammars, such as parsing and proving properties of grammars. It also has some practical applications, such as in natural language processing.

#### How do you convert a context-free grammar to Chomsky Normal Form?

To convert a context-free grammar to Chomsky Normal Form, follow these steps:

- Eliminate all ε-productions from the grammar.
- Eliminate all unit productions (A → B) by replacing them with all the productions of the form A → w, where w is a string of terminals and nonterminals.
- Replace all the productions that have more than two symbols on the right-hand side with productions that have two symbols on the right-hand side by introducing new nonterminals.

#### What is the advantage of having all productions in Chomsky Normal Form be of the form A → BC or A → a?

The advantage of having all productions in Chomsky Normal Form be of the form A → BC or A → a is that it simplifies many operations on context-free grammars, such as parsing and proving properties of grammars. It also makes it easier to reason about the structure of the language generated by the grammar, since each production can be viewed as either introducing a nonterminal symbol or generating a terminal symbol.

#### Can every context-free grammar be converted to Chomsky Normal Form?

Yes, every context-free grammar can be converted to Chomsky Normal Form.

#### How can you test whether a string is in the language generated by a context-free grammar?

You can test whether a string is in the language generated by a context-free grammar by constructing a parse tree for the string, and checking whether the tree can be generated using the production rules of the grammar. If a parse tree can be constructed for the string, then the string is in the language; otherwise, it is not.

#### What is the difference between a left-recursive and a right-recursive context-free grammar?

A left-recursive context-free grammar has at least one production rule of the form A -> Aα, where A is a nonterminal symbol and α is a string of zero or more terminal and nonterminal symbols. A right-recursive context-free grammar has at least one production rule of the form A -> αA, where A is a nonterminal symbol and α is a string of zero or more terminal and nonterminal symbols.

#### What is the purpose of left factoring in a context-free grammar?

Left factoring is a technique used to simplify context-free grammars by removing common prefixes from multiple production rules. This can reduce the number of production rules needed to generate the same language, and can make the grammar easier to parse.

#### What is a parse table in the context of context-free grammars?

A parse table is a table that is used by parsing algorithms to determine how to parse a given string according to the production rules of a context-free grammar. The table is typically constructed from the grammar using an algorithm such as the LR parser or LL parser algorithms.