Refactor for grammar generator