This spring I am taking a compilers course, and right now I'm trying to figure out Chomsky Normal Form and its relationship to the CYK algorithm.

As part of the course, which is run through the UW-Madison CS department, we use Piazza, a Q&A and discussion forum site. The typical experience is: you post a question, and then wait for instructors to answer. But exchanges like what I have below make me think that ChatGPT is going to quickly obsolete that function.

## # my interaction with ChatGPT this morning

Me:

Why does Chomsky Normal Form for a context-free grammar require nonterminal productions have exactly two nonterminals on the right hand side? Why not three? What's special about having exactly two?

ChatGPT:

Chomsky Normal Form (CNF) for a context-free grammar requires nonterminal productions to have exactly two nonterminals on the right-hand side because this property has several useful properties that make parsing and other operations on the grammar easier.

One important property of CNF is that it is "binary", meaning that every production has either a single terminal symbol on the right-hand side, or two nonterminal symbols. This allows us to use a simple "bottom-up" parsing algorithm, such as the CYK algorithm, to parse a string in the language generated by the grammar in time proportional to the length of the string.

Another important property of CNF is that it is "unambiguous", meaning that every string in the language generated by the grammar has exactly one parse tree. This property is useful for many applications, such as natural language processing, where ambiguity can be a significant problem.

Having exactly two nonterminals on the right-hand side of a production is special because it allows us to use these properties to efficiently manipulate and analyze the grammar. Additionally, every context-free grammar can be converted into an equivalent grammar in CNF, so any language that can be generated by a context-free grammar can be parsed efficiently using a binary CFG.

Me:

If you had three nonterminals on an RHS, could that be parsed by a variation of the CYK algorithm in which you split the string of tokens in two places, rather than just one?

ChatGPT:

Yes, if a production in a context-free grammar had three nonterminals on the right-hand side, it could be parsed using a variation of the CYK algorithm in which the string of tokens is split in two places, rather than just one.

This variation of the CYK algorithm is called the "Earley parser", which is a more general parsing algorithm that can handle arbitrary context-free grammars, including those that are not in Chomsky normal form. The Earley parser uses a chart-based parsing technique, which maintains a set of states that represent the possible positions of the parser in the input string. Each state contains information about the nonterminal that the parser is trying to expand, the position in the input string, and the position in the grammar that the parser has already matched.

The Earley parser works by repeatedly predicting and scanning symbols from the input string, and creating new states based on the predictions and scans. When a state is completed, it can be used to expand other states in the chart, until a complete parse tree for the input string is constructed.

While the Earley parser is more general than the CYK algorithm, it can be slower for some grammars and inputs, since it may need to maintain more states and perform more predictions and scans. However, for context-free grammars that cannot be converted into Chomsky normal form, the Earley parser is a powerful alternative that can handle a wider range of grammars and languages.

Me:

It sounds like the Earley parser algorithm is more general than the CYK algorithm, but it's also O(n^3); why, then, use CYK at all? Why not just Earley parsing?

ChatGPT:

You are correct that the Earley parser algorithm is more general than the CYK algorithm, but it is also O(n^3) in the worst-case, where n is the length of the input string. The CYK algorithm, on the other hand, has a worst-case time complexity of O(n^3log n) for any context-free grammar that is in Chomsky normal form.

The reason why the CYK algorithm is still used is that it is faster for many practical grammars, especially those that are in Chomsky normal form. The reason why the CYK algorithm is faster for these grammars is that it takes advantage of the structure of the grammar to reduce the number of possible parse trees for a given input string. Specifically, the CYK algorithm builds up a table of nonterminals that can derive each substring of the input string, and uses this information to eliminate parse trees that are not consistent with the grammar.

In contrast, the Earley parser has a more general algorithm that can handle arbitrary context-free grammars, but it does not take advantage of the structure of the grammar in the same way as the CYK algorithm. This means that the Earley parser may need to maintain more states and perform more predictions and scans, which can make it slower than the CYK algorithm for some grammars and inputs.

So, while the Earley parser is more powerful and can handle a wider range of grammars and languages, the CYK algorithm is still a useful and efficient algorithm for many practical purposes, especially for grammars that are in Chomsky normal form.

Me:

So, getting back to my original question, the requirement in Chomsky Normal Form that one have exactly two nonterminals on the RHS isn't because of some unavoidable theoretical constraint; it's motivated by matching up with the CYK algorithm, which in practical terms is pretty easy and fast for those situations. Is that right?

ChatGPT:

Yes, that is a good way to summarize it. The requirement in Chomsky normal form that productions have exactly two nonterminals on the right-hand side is motivated by the fact that the CYK algorithm can efficiently parse context-free grammars that are in this form. In other words, the requirement is a practical one that makes parsing easier and more efficient for certain types of grammars.

However, it's worth noting that the requirement for productions to have exactly two nonterminals on the right-hand side is not just an arbitrary restriction to match the CYK algorithm. Rather, it is a result of the fact that any context-free grammar can be converted into an equivalent grammar in Chomsky normal form, which means that this form is sufficient to generate any language that can be generated by a context-free grammar.

So, while the requirement for productions to have exactly two nonterminals on the right-hand side is motivated by practical considerations such as the efficiency of the CYK algorithm, it is also a theoretical result that highlights the power and flexibility of context-free grammars.

## # the future: integrate this into Piazza and similar sites

It seems pretty obvious to me that the above functionality is already shockingly good, and that it goes a long way towards replacing the instructors in my course -- at least the Q&A stuff.

The obvious next step here would be to integrate this into Piazza and similar sites. Have your chatbot answer the question, and do the kind of interaction we did above -- but then allow the course instructors to comment, downvote, or whatever, to bring in the context of the particular course.

I'm a former college math teacher, and if/when I go back to it, I think one thing I will do in my courses is teach students how to get help from chatbots like this.