rewrite counter and CKY algorithm