Add test for grammars and fix related bugs.