rewrite regular grammar