Implemented two of the three steps of the grammar optimization.