simplifying nonterminal loops in grammar