Improved the K2Grammar generator. Generalized some duplicated code.